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 25 import webEditor.util.FileAccess; 26 27 32 public class page 33 { 34 37 private configuration configParams; 38 39 42 private String wEd_root; 43 44 47 private String dataDir; 48 49 52 private String homePageFile; 53 54 57 private String docRoot; 58 59 60 public page (configuration initParam) 61 { 62 this.configParams = initParam; 63 String categoryName = "directories"; 64 65 this.wEd_root = initParam.readValue ("webEditor_Root","wEd_root"); 66 this.dataDir = initParam.readValue (categoryName,"dataDir"); 67 this.dataDir = this.wEd_root + "/" + this.dataDir; 68 69 this.homePageFile = initParam.readValue (categoryName,"homePage"); 70 71 this.docRoot = "/" + initParam.readValue (categoryName,"docRoot"); 72 this.docRoot = initParam.readValue (categoryName,"serverRoot") + this.docRoot; 73 74 } 75 76 77 81 public Document homePageList () 82 { 83 Element item; 84 DocumentImpl expandedDoc = null; 85 article myPage = new article (this.configParams); 86 87 DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile); 88 try { 90 Element root = doc.getDocumentElement(); 91 String rootTag = root.getTagName(); 92 NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes(); 93 94 DocumentType docType = doc.getDoctype(); 97 expandedDoc = new DocumentImpl (docType); 98 Element expandedRoot = expandedDoc.createElement (rootTag); 99 String size = root.getAttributes().getNamedItem("size").getNodeValue(); 100 expandedRoot.setAttribute ("size", size); 101 102 for (int i=0; i < list.getLength(); i++) { 104 Node myNode = list.item(i); 105 if (myNode.getNodeName().equals("document")) { 106 String order = myNode.getAttributes().getNamedItem("order").getNodeValue(); 107 String newsFile = myNode.getFirstChild().getNodeValue(); 108 109 Document newsDoc = myPage.docRead (newsFile); 111 item = expandedDoc.createElement ("docContent"); 112 item.setAttribute ("order", order); 113 114 item.appendChild (expandedDoc.importNode (newsDoc.getDocumentElement(), true)); 115 expandedRoot.appendChild (item); 116 } 117 } 118 expandedDoc.appendChild (expandedRoot); 119 } 120 catch (Exception e) { 121 e.printStackTrace (); 122 } 123 return (expandedDoc); 124 } 125 126 127 132 public void insertDoc (String docName) 133 { 134 Element item; 135 String intValue = null; 136 article myPage = new article (this.configParams); 137 138 DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile); 139 try { 141 Element root = doc.getDocumentElement(); 142 String rootTag = root.getTagName(); 143 item = doc.createElement ("document"); 145 item.setAttribute ("order", "1"); 146 item.appendChild (doc.createTextNode(docName)); 147 Node firstChild = doc.getElementsByTagName(rootTag).item(0).getFirstChild(); 149 root.insertBefore (item, firstChild); 150 151 NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes(); 152 153 int j = 1; 156 for (int i=0; i < list.getLength(); i++) { 157 Node myNode = list.item(i); 158 if (myNode.getNodeName().equals("document")) { 159 myNode.getAttributes().getNamedItem("order").setNodeValue(intValue.valueOf(j)); 160 161 String order = myNode.getAttributes().getNamedItem("order").getNodeValue(); 162 String newsFile = myNode.getFirstChild().getNodeValue(); 163 j++; 164 } 165 } 166 String size = root.getAttributes().getNamedItem("size").getNodeValue(); 168 Integer intSize = new Integer (size); 169 int mySize = intSize.intValue() + 1; 170 size = size.valueOf(mySize); 171 root.setAttribute ("size", size); 172 myPage.saveFile (this.homePageFile, doc); 173 174 } 175 catch (Exception e) { 176 e.printStackTrace (); 177 } 178 179 } 180 181 182 187 public void removeDoc (String docName) 188 { 189 Element item; 190 DocumentImpl resultDoc = null; 191 String intValue = null; 192 article myPage = new article (this.configParams); 193 194 DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile); 195 try { 197 Element root = doc.getDocumentElement(); 198 String rootTag = root.getTagName(); 199 NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes(); 200 201 DocumentType docType = doc.getDoctype(); 203 resultDoc = new DocumentImpl (docType); 204 Element resultRoot = resultDoc.createElement (rootTag); 205 206 int j = 1; 207 boolean find = false; 208 for (int i=0; i < list.getLength(); i++) { 210 Node myNode = list.item(i); 211 if (myNode.getNodeName().equals("document")) { 212 String newsFile = myNode.getFirstChild().getNodeValue(); 213 214 if ( newsFile.equals (docName) ) { 215 find = true; 217 continue; 218 } 219 else { 220 item = resultDoc.createElement ("document"); 221 item.setAttribute ("order", intValue.valueOf(j)); 222 item.appendChild (resultDoc.createTextNode(newsFile)); 223 resultRoot.appendChild (item); 224 j++; 225 } 226 } 227 } 228 if ( find ) { 230 String size = root.getAttributes().getNamedItem("size").getNodeValue(); 231 Integer intSize = new Integer (size); 232 int mySize = intSize.intValue() - 1; 233 size = size.valueOf(mySize); 234 resultRoot.setAttribute ("size", size); 235 236 resultDoc.appendChild (resultRoot); 237 myPage.saveFile (this.homePageFile, resultDoc); 238 } 239 } 240 catch (Exception e) { 241 e.printStackTrace (); 242 } 243 244 } 245 246 251 public void riseDoc (String docName) 252 { 253 Element item; 254 Element storedItem = null; 255 DocumentImpl resultDoc = null; 256 String intValue = null; 257 article myPage = new article (this.configParams); 258 259 DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile); 260 try { 262 Element root = doc.getDocumentElement(); 263 String rootTag = root.getTagName(); 264 NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes(); 265 266 DocumentType docType = doc.getDoctype(); 268 resultDoc = new DocumentImpl (docType); 269 Element resultRoot = resultDoc.createElement (rootTag); 270 String size = root.getAttributes().getNamedItem("size").getNodeValue(); 272 resultRoot.setAttribute ("size", size); 273 274 275 int j=1; 277 boolean find = false; 278 for (int i = 0; i < list.getLength(); i++) { 279 Node myNode = list.item(i); 280 if (myNode.getNodeName().equals("document")) { 281 String newsFile = myNode.getFirstChild().getNodeValue(); 282 String order = myNode.getAttributes().getNamedItem("order").getNodeValue(); 283 if ( !docName.equals (newsFile) ) { 284 item = resultDoc.createElement ("document"); 286 item.setAttribute ("order", order); 287 item.appendChild (resultDoc.createTextNode(newsFile)); 288 resultRoot.appendChild (item); 289 } 290 else { 291 if ( j == 1 ) { 292 break; 294 } 295 else { 296 item = resultDoc.createElement ("document"); 299 item.setAttribute ("order", intValue.valueOf(j-1)); 300 item.appendChild (resultDoc.createTextNode(newsFile)); 301 302 Node prevNode = resultRoot.getLastChild(); 303 resultRoot.insertBefore (item, prevNode); 304 305 String file = prevNode.getFirstChild().getNodeValue(); 307 item = resultDoc.createElement ("document"); 308 item.setAttribute ("order", intValue.valueOf(j)); 309 item.appendChild (resultDoc.createTextNode(file)); 310 311 resultRoot.replaceChild (item, prevNode); 312 find = true; 313 } 314 } 315 j++; 316 } 317 } 318 319 320 if ( find ) { 322 resultDoc.appendChild (resultRoot); 323 myPage.saveFile (this.homePageFile, resultDoc); 324 } 325 } 326 catch (Exception e) { 327 e.printStackTrace (); 328 } 329 330 } 331 332 337 public void lowerDoc (String docName) 338 { 339 Element item; 340 Element storedItem = null; 341 DocumentImpl resultDoc = null; 342 String intValue = null; 343 article myPage = new article (this.configParams); 344 345 DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile); 346 try { 348 Element root = doc.getDocumentElement(); 349 String rootTag = root.getTagName(); 350 NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes(); 351 352 DocumentType docType = doc.getDoctype(); 354 resultDoc = new DocumentImpl (docType); 355 Element resultRoot = resultDoc.createElement (rootTag); 356 String size = root.getAttributes().getNamedItem("size").getNodeValue(); 358 resultRoot.setAttribute ("size", size); 359 360 Integer IntSize = new Integer (size); 361 int intSize = IntSize.intValue(); 362 363 int j=1; 365 boolean find = false; 366 boolean write = false; 367 Element prevNode = null; 368 for (int i = 0; i < list.getLength(); i++) { 369 Node myNode = list.item(i); 370 if (myNode.getNodeName().equals("document")) { 371 String newsFile = myNode.getFirstChild().getNodeValue(); 372 String order = myNode.getAttributes().getNamedItem("order").getNodeValue(); 373 if ( !docName.equals (newsFile) ) { 374 if ( find ) { 375 order = intValue.valueOf(j-1); 376 } 377 item = resultDoc.createElement ("document"); 379 item.setAttribute ("order", order); 380 item.appendChild (resultDoc.createTextNode(newsFile)); 381 resultRoot.appendChild (item); 382 if ( find ) { 383 resultRoot.appendChild (prevNode); 385 find = false; 386 } 387 } 388 else { 389 if ( j == intSize ) { 390 break; 392 } 393 else { 394 prevNode = resultDoc.createElement ("document"); 396 prevNode.setAttribute ("order", intValue.valueOf(j+1)); 397 prevNode.appendChild (resultDoc.createTextNode(newsFile)); 398 399 write = true; 400 find = true; 401 } 402 } 403 j++; 404 } 405 } 406 407 408 if ( write ) { 410 resultDoc.appendChild (resultRoot); 411 myPage.saveFile (this.homePageFile, resultDoc); 412 } 413 } 414 catch (Exception e) { 415 e.printStackTrace (); 416 } 417 418 } 419 420 425 public void saveHtmlFile (String content) 426 { 427 try { 428 FileAccess fileHandler = new FileAccess (); 429 430 String filePath = this.docRoot + "/index.html"; 431 File fd = new File (filePath); 433 if ( fd.exists() ) { 434 fd.delete(); 435 } 436 RandomAccessFile myFile = new RandomAccessFile (filePath, "rw"); 437 myFile.write (content.getBytes()); 438 myFile.close(); 439 } 440 catch (Exception e) { 441 e.printStackTrace(); 442 } 443 } 444 445 } 446 | Popular Tags |