KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > core > page


1 /* $Id: page.java,v 1.3 2001/04/20 21:09:09 agarcia3 Exp $
2     webEditor. The new way in content management
3     Copyright (C) 2001 Alfredo Garcia
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13 */

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 /**
28  * Home Page content operations
29  *
30  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
31  */

32 public class page
33 {
34    /**
35     * Config Parameters
36     */

37    private configuration configParams;
38
39    /**
40     * Editor root
41     */

42    private String JavaDoc wEd_root;
43
44    /**
45     * Path for the data configuration files
46     */

47    private String JavaDoc dataDir;
48    
49    /**
50     * Home Page file
51     */

52    private String JavaDoc homePageFile;
53
54   /**
55     * Server path for the exported documents
56     */

57    private String JavaDoc docRoot;
58
59
60    public page (configuration initParam)
61    {
62     this.configParams = initParam;
63     String JavaDoc 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   /**
78     * Expands the content of the home page into a master document
79     * @return Document Expanded Home Page
80     */

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     // Once we have the document DOM tree, whe look for the file content
89
try {
90     Element root = doc.getDocumentElement();
91     String JavaDoc rootTag = root.getTagName();
92     NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();
93
94     // We are going to store in expandedDoc the content of all the news in
95
// the homepage. For this, we create a document of the same type
96
DocumentType docType = doc.getDoctype();
97     expandedDoc = new DocumentImpl (docType);
98     Element expandedRoot = expandedDoc.createElement (rootTag);
99     String JavaDoc size = root.getAttributes().getNamedItem("size").getNodeValue();
100     expandedRoot.setAttribute ("size", size);
101
102     // For every child add the codes and the words
103
for (int i=0; i < list.getLength(); i++) {
104         Node myNode = list.item(i);
105         if (myNode.getNodeName().equals("document")) {
106             String JavaDoc order = myNode.getAttributes().getNamedItem("order").getNodeValue();
107             String JavaDoc newsFile = myNode.getFirstChild().getNodeValue();
108             
109             // For every document in the home page, we read the file content
110
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 JavaDoc e) {
121     e.printStackTrace ();
122 }
123     return (expandedDoc);
124    }
125
126
127    /**
128     * Insert the given document on the top of the Home Page
129     * @param docName doc ID to insert
130     * @return void
131     */

132    public void insertDoc (String JavaDoc docName)
133    {
134     Element item;
135     String JavaDoc intValue = null;
136     article myPage = new article (this.configParams);
137
138     DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile);
139     // Once we have the document DOM tree, whe look for the file content
140
try {
141     Element root = doc.getDocumentElement();
142     String JavaDoc rootTag = root.getTagName();
143     // We create a new element with the "docName" value and order 1
144
item = doc.createElement ("document");
145     item.setAttribute ("order", "1");
146     item.appendChild (doc.createTextNode(docName));
147     // And then, we try to insert this element as the first child of the root
148
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     // Ok, now that we have the new node in the first place, we just need to
154
// change the order attribute on the rest of the nodes
155
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 JavaDoc order = myNode.getAttributes().getNamedItem("order").getNodeValue();
162             String JavaDoc newsFile = myNode.getFirstChild().getNodeValue();
163             j++;
164         }
165     }
166     // Now that we have the new index master document, we write it to disk
167
String JavaDoc size = root.getAttributes().getNamedItem("size").getNodeValue();
168     Integer JavaDoc intSize = new Integer JavaDoc (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 JavaDoc e) {
176     e.printStackTrace ();
177 }
178
179   }
180
181
182   /**
183     * Delete the given document from the Home Page
184     * @param docName doc ID to delete
185     * @return void
186     */

187    public void removeDoc (String JavaDoc docName)
188    {
189     Element item;
190     DocumentImpl resultDoc = null;
191     String JavaDoc intValue = null;
192     article myPage = new article (this.configParams);
193
194     DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile);
195     // Once we have the document DOM tree, whe look for the file content
196
try {
197     Element root = doc.getDocumentElement();
198     String JavaDoc rootTag = root.getTagName();
199     NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();
200
201     // We are going to store in resultDoc all the docs, but the given doc.
202
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     // We copy the old structure until we find the given doc.
209
for (int i=0; i < list.getLength(); i++) {
210         Node myNode = list.item(i);
211         if (myNode.getNodeName().equals("document")) {
212             String JavaDoc newsFile = myNode.getFirstChild().getNodeValue();
213             
214             if ( newsFile.equals (docName) ) {
215                 // So we simply go to the next iteration
216
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     // Now that we have the new index master document, we write it to disk
229
if ( find ) {
230         String JavaDoc size = root.getAttributes().getNamedItem("size").getNodeValue();
231         Integer JavaDoc intSize = new Integer JavaDoc (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 JavaDoc e) {
241     e.printStackTrace ();
242 }
243
244   }
245
246   /**
247     * Promotes the given document one position into the Home Page
248     * @param docName doc ID to promote
249     * @return void
250     */

251    public void riseDoc (String JavaDoc docName)
252    {
253     Element item;
254     Element storedItem = null;
255     DocumentImpl resultDoc = null;
256     String JavaDoc intValue = null;
257     article myPage = new article (this.configParams);
258
259     DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile);
260     // Once we have the document DOM tree, whe look for the file content
261
try {
262     Element root = doc.getDocumentElement();
263     String JavaDoc rootTag = root.getTagName();
264     NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();
265
266     // We are going to store in resultDoc the new document order.
267
DocumentType docType = doc.getDoctype();
268     resultDoc = new DocumentImpl (docType);
269     Element resultRoot = resultDoc.createElement (rootTag);
270     // Oh, yes, don't forget the "size" attribute
271
String JavaDoc size = root.getAttributes().getNamedItem("size").getNodeValue();
272     resultRoot.setAttribute ("size", size);
273
274
275     // Main loop
276
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 JavaDoc newsFile = myNode.getFirstChild().getNodeValue();
282             String JavaDoc order = myNode.getAttributes().getNamedItem("order").getNodeValue();
283             if ( !docName.equals (newsFile) ) {
284                // We copy this node
285
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                 // Nothing to do (we are in the first doc)
293
break;
294                }
295                else {
296                 // So we have found the given doc, and it's not the
297
// first... Great! at last real work ;-)
298
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                 // And then, we replace the previous node with the new order
306
String JavaDoc 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     // Now that we have the new index master document, we write it to disk
321
if ( find ) {
322         resultDoc.appendChild (resultRoot);
323         myPage.saveFile (this.homePageFile, resultDoc);
324     }
325 }
326 catch (Exception JavaDoc e) {
327     e.printStackTrace ();
328 }
329
330   }
331
332   /**
333     * Lowers the given document one position into the Home Page
334     * @param docName doc ID to lower
335     * @return void
336     */

337    public void lowerDoc (String JavaDoc docName)
338    {
339     Element item;
340     Element storedItem = null;
341     DocumentImpl resultDoc = null;
342     String JavaDoc intValue = null;
343     article myPage = new article (this.configParams);
344
345     DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile);
346     // Once we have the document DOM tree, whe look for the file content
347
try {
348     Element root = doc.getDocumentElement();
349     String JavaDoc rootTag = root.getTagName();
350     NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();
351
352     // We are going to store in resultDoc the new document order.
353
DocumentType docType = doc.getDoctype();
354     resultDoc = new DocumentImpl (docType);
355     Element resultRoot = resultDoc.createElement (rootTag);
356     // Oh, yes, don't forget the "size" attribute
357
String JavaDoc size = root.getAttributes().getNamedItem("size").getNodeValue();
358     resultRoot.setAttribute ("size", size);
359
360     Integer JavaDoc IntSize = new Integer JavaDoc (size);
361     int intSize = IntSize.intValue();
362     
363     // Main loop
364
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 JavaDoc newsFile = myNode.getFirstChild().getNodeValue();
372             String JavaDoc order = myNode.getAttributes().getNamedItem("order").getNodeValue();
373             if ( !docName.equals (newsFile) ) {
374                if ( find ) {
375                 order = intValue.valueOf(j-1);
376                }
377                // We copy this node
378
item = resultDoc.createElement ("document");
379                item.setAttribute ("order", order);
380                item.appendChild (resultDoc.createTextNode(newsFile));
381                resultRoot.appendChild (item);
382                if ( find ) {
383                 // We do this, because we want to preserve the order secuence
384
resultRoot.appendChild (prevNode);
385                 find = false;
386                }
387             }
388             else {
389                if ( j == intSize ) {
390                   // Ok, we are in the last doc, so we have nothing to do
391
break;
392                }
393                else {
394                 // We have found the given doc
395
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     // Now that we have the new index master document, we write it to disk
409
if ( write ) {
410         resultDoc.appendChild (resultRoot);
411         myPage.saveFile (this.homePageFile, resultDoc);
412     }
413 }
414 catch (Exception JavaDoc e) {
415     e.printStackTrace ();
416 }
417
418   }
419
420   /**
421     * Saves the string with the HTML content into a file
422     * @param content String with the HTML home page
423     * @return void
424     */

425    public void saveHtmlFile (String JavaDoc content)
426    {
427 try {
428     FileAccess fileHandler = new FileAccess ();
429
430     String JavaDoc filePath = this.docRoot + "/index.html";
431     // We need to delete the file before the write operation
432
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 JavaDoc e) {
441     e.printStackTrace();
442 }
443    }
444
445 }
446
Popular Tags