1 14 15 package webEditor.core; 16 17 import java.io.*; 18 import java.util.*; 19 20 import org.w3c.dom.*; 21 import org.apache.xerces.dom.*; 22 import org.apache.xerces.parsers.DOMParser; 23 import org.apache.xml.serialize.*; 24 import org.apache.regexp.RE; 25 26 import webEditor.util.FileAccess; 27 28 33 public class article 34 { 35 38 private String wEd_root; 39 40 43 private String dataDir; 44 45 48 private String docRoot; 49 50 public article (configuration initParam) 51 { 52 String categoryName = "directories"; 53 54 this.wEd_root = initParam.readValue ("webEditor_Root","wEd_root"); 55 this.dataDir = initParam.readValue (categoryName,"dataDir"); 56 this.dataDir = this.wEd_root + "/" + this.dataDir; 57 58 this.docRoot = "/" + initParam.readValue (categoryName,"docRoot"); 59 this.docRoot = initParam.readValue (categoryName,"serverRoot") + this.docRoot; 60 } 61 62 63 67 public String newDocID () 68 { 69 String myDocID = null; 70 71 Date myDate = new Date(); 72 Calendar myCalendar = Calendar.getInstance(); 73 myDocID = "" + myCalendar.get(myCalendar.YEAR); 74 myDocID = myDocID + "/" + (myCalendar.get(myCalendar.MONTH) + 1); 75 myDocID = myDocID + "/" + myCalendar.get(myCalendar.DAY_OF_MONTH); 76 77 myDocID = myDocID + "/" + myDate.getTime() + ".xml"; 79 return (myDocID); 80 } 81 82 87 public String docDate(String docName) 88 { 89 return ("OK"); 90 } 91 92 99 public Document writeDoc(Hashtable dataStream, Document newsDoc, String docSection) 100 { 101 String rootTag = null; 102 String tagName = null; 103 String tagValue = null; 104 105 Element root = newsDoc.getDocumentElement(); 106 if (docSection.equals ("root")) { 107 rootTag = root.getTagName(); 108 } 109 else { 110 rootTag = docSection; 111 } 112 NodeList list = newsDoc.getElementsByTagName(rootTag).item(0).getChildNodes(); 113 114 for (Enumeration e = dataStream.keys() ; e.hasMoreElements() ;) { 117 tagName = (String ) e.nextElement(); 118 tagValue = (String ) dataStream.get (tagName); 119 for (int i=0; i < list.getLength(); i++) { 120 if (list.item(i).getNodeName().equals(tagName)) { 121 Node source = list.item(i); 122 Node myDocNode = source.cloneNode (true); 123 myDocNode.getLastChild().setNodeValue (tagValue); 124 Node parent = source.getParentNode(); 125 Node temp = parent.replaceChild (myDocNode, source); 126 } 127 } 128 } 129 130 return (newsDoc); 131 } 132 133 140 public Document writeImgDoc (Document imageDoc, Document newsDoc, String docSection) 141 { 142 143 Element selectedNode = (Element) newsDoc.getElementsByTagName 144 (docSection).item(0); 145 NodeList list = selectedNode.getChildNodes (); 146 for (int i=0; i < list.getLength(); i++) { 148 if (list.item(i).getNodeName().equals("image")) { 149 selectedNode.removeChild (list.item(i)); 150 } 151 } 152 153 Node newNode = selectedNode.cloneNode(true); 154 Node firstChild = newNode.getFirstChild(); 156 newNode.insertBefore (newsDoc.importNode 157 (imageDoc.getDocumentElement(), true), firstChild); 158 Node parent = selectedNode.getParentNode (); 159 parent.replaceChild (newNode, selectedNode); 160 161 return (newsDoc); 162 } 163 164 170 public Document deleteImgDoc (Document newsDoc, String docSection) 171 { 172 173 Element selectedNode = (Element) newsDoc.getElementsByTagName 174 (docSection).item(0); 175 NodeList list = selectedNode.getChildNodes (); 176 for (int i=0; i < list.getLength(); i++) { 178 if (list.item(i).getNodeName().equals("image")) { 179 selectedNode.removeChild (list.item(i)); 180 } 181 } 182 183 return (newsDoc); 184 } 185 186 187 192 public Document docRead(String docName) 193 { 194 Document doc = null; 195 try { 196 DOMParser parser = new DOMParser(); 197 docName = this.dataDir + "/news/" + docName; 198 docName = new File (docName).getAbsolutePath(); 199 docName = "file:" + docName; 200 201 parser.parse(docName); 202 doc = parser.getDocument(); 203 } 204 catch (Exception e) { 205 e.printStackTrace (); 206 } 207 return (doc); 208 209 } 210 211 217 public Document readSubtree (Document newsDoc, String root) 218 { 219 Element item; 220 Element docRoot = null; 221 String rootTag = null; 222 DocumentImpl docFragment = new DocumentImpl(); 223 224 if ( root.equals ("root") ) { 225 docRoot = newsDoc.getDocumentElement(); 226 } 227 else { 228 docRoot = (Element) newsDoc.getElementsByTagName(root).item(0); 229 } 230 231 if (docRoot == null) { 232 return (null); 234 } 235 236 docFragment.appendChild (docFragment.importNode 238 (docRoot, true)); 239 return (docFragment); 240 } 241 242 243 244 250 public void saveFile (String docName, Document doc) 251 { 252 try { 253 FileAccess fileHandler = new FileAccess (); 254 255 OutputFormat format = new OutputFormat (doc, "ISO-8859-1" ,true); 256 format.setIndent (2); 257 format.setLineWidth (80); 258 format.setPreserveSpace (true); 259 StringWriter stringOut = new StringWriter (); 260 XMLSerializer serial = new XMLSerializer (stringOut, format); 261 serial.asDOMSerializer (); 262 263 serial.serialize (doc.getDocumentElement()); 264 265 String filePath = this.dataDir + "/news/" + docName; 266 fileHandler.createDocDir (docName,this.dataDir + "/news/"); 268 File fd = new File (filePath); 270 if ( fd.exists() ) { 271 fd.delete(); 272 } 273 RandomAccessFile myFile = new RandomAccessFile (filePath, "rw"); 274 myFile.write (stringOut.toString().getBytes()); 275 myFile.close(); 276 } 277 catch (Exception e) { 278 e.printStackTrace(); 279 } 280 } 281 282 283 288 public String getHTMLname (String docID) 289 { 290 String docName = null; 291 try { 292 RE r = new RE("xml$"); 293 docName = r.subst (docID,"html"); 294 } 295 catch (Exception e) { 296 e.printStackTrace(); 297 } 298 return (docName); 299 } 300 301 302 308 public void saveHtmlFile (String docName, String content) 309 { 310 try { 311 FileAccess fileHandler = new FileAccess (); 312 313 docName = this.getHTMLname (docName); 316 317 String filePath = this.docRoot + "/news/" + docName; 318 fileHandler.createDocDir (docName,this.docRoot + "/news/"); 320 File fd = new File (filePath); 322 if ( fd.exists() ) { 323 fd.delete(); 324 } 325 RandomAccessFile myFile = new RandomAccessFile (filePath, "rw"); 326 myFile.write (content.getBytes()); 327 myFile.close(); 328 } 329 catch (Exception e) { 330 e.printStackTrace(); 331 } 332 } 333 334 } 335 | Popular Tags |