KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > core > article


1 /* $Id: article.java,v 1.13 2001/04/03 21:20:15 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 import org.apache.regexp.RE;
25
26 import webEditor.util.FileAccess;
27
28 /**
29  * Encapsulate the access to the xml documents.
30  *
31  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
32  */

33 public class article
34 {
35    /**
36     * Editor root
37     */

38    private String JavaDoc wEd_root;
39
40    /**
41     * Path for the data configuration files
42     */

43    private String JavaDoc dataDir;
44    
45    /**
46     * Server path for the exported documents
47     */

48    private String JavaDoc docRoot;
49
50    public article (configuration initParam)
51    {
52     String JavaDoc 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    /**
64     * Returns a new Document identificator
65     * @return String New doc ID
66     */

67    public String JavaDoc newDocID ()
68    {
69     String JavaDoc 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     // In this version, we get the docID from the unix time.
78
myDocID = myDocID + "/" + myDate.getTime() + ".xml";
79     return (myDocID);
80    }
81    
82    /**
83     * Returns the date of the document file last modification, in a formatted string
84     * @param docName doc ID
85     * @return String Document formated date
86     */

87    public String JavaDoc docDate(String JavaDoc docName)
88    {
89     return ("OK");
90    }
91    
92    /**
93     * Modify the content of the document data file.
94     * @param dataStream Hash with the new values to write
95     * @param newsDoc DOM tree of the document
96     * @param docSection Section of the document
97     * @return Document New DOM tree
98     */

99    public Document writeDoc(Hashtable dataStream, Document newsDoc, String JavaDoc docSection)
100    {
101     String JavaDoc rootTag = null;
102     String JavaDoc tagName = null;
103     String JavaDoc 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     // Now, we look for document tags that appears in the hash, and
115
// we try to change its content
116
for (Enumeration e = dataStream.keys() ; e.hasMoreElements() ;) {
117         tagName = (String JavaDoc) e.nextElement();
118         tagValue = (String JavaDoc) 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   /**
134     * Inserts the image DOM structure into the given doc section
135     * @param imageDoc DOM tree with the content of the image
136     * @param newsDoc DOM tree of the document
137     * @param docSection Section of the document
138     * @return Document New DOM tree
139     */

140    public Document writeImgDoc (Document imageDoc, Document newsDoc, String JavaDoc docSection)
141    {
142
143     Element selectedNode = (Element) newsDoc.getElementsByTagName
144                     (docSection).item(0);
145     NodeList list = selectedNode.getChildNodes ();
146     // We remove all the previous image tags in the node
147
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     // And we create a new image tag (and in the first position)
155
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   /**
165     * Removes the image child from the given DOM node
166     * @param newsDoc DOM tree of the document
167     * @param docSection Section of the document
168     * @return Document New DOM tree
169     */

170    public Document deleteImgDoc (Document newsDoc, String JavaDoc docSection)
171    {
172
173     Element selectedNode = (Element) newsDoc.getElementsByTagName
174                     (docSection).item(0);
175     NodeList list = selectedNode.getChildNodes ();
176     // We remove all the "image" tags in the node
177
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    /**
188     * Returns the document content in a DOM tree structure.
189     * @param docName doc ID
190     * @return Document DOM tree of the document
191     */

192    public Document docRead(String JavaDoc 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 JavaDoc e) {
205     e.printStackTrace ();
206 }
207     return (doc);
208
209    }
210
211    /**
212     * Returns the subtree with the given root of the DOM document
213     * @param newsDoc DOM tree of the document
214     * @param root Sub tree root
215     * @return Document DOM sub tree
216     */

217    public Document readSubtree (Document newsDoc, String JavaDoc root)
218    {
219     Element item;
220     Element docRoot = null;
221     String JavaDoc 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         // The document doesn't have any tag with this name
233
return (null);
234     }
235
236     // So, we copy all the subtree into the new document
237
docFragment.appendChild (docFragment.importNode
238                 (docRoot, true));
239     return (docFragment);
240    }
241
242
243
244   /**
245     * Saves the content of a DOM tree into a file (in XML format, of course)
246     * @param docName doc ID (file to save)
247     * @param doc DOM tree of the document
248     * @return void
249     */

250    public void saveFile (String JavaDoc 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 JavaDoc filePath = this.dataDir + "/news/" + docName;
266     // Check if the directory structure exists...
267
fileHandler.createDocDir (docName,this.dataDir + "/news/");
268     // We need to delete the file before the write operation
269
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 JavaDoc e) {
278     e.printStackTrace();
279 }
280    }
281
282
283   /**
284     * Returns the associated HTML file name
285     * @param docID doc ID of the document
286     * @return void
287     */

288    public String JavaDoc getHTMLname (String JavaDoc docID)
289    {
290     String JavaDoc docName = null;
291 try {
292     RE r = new RE("xml$");
293     docName = r.subst (docID,"html");
294 }
295 catch (Exception JavaDoc e) {
296     e.printStackTrace();
297 }
298     return (docName);
299    }
300
301
302   /**
303     * Saves the string with the HTML content into a file
304     * @param docName doc ID (file to save)
305     * @param content String with the document
306     * @return void
307     */

308    public void saveHtmlFile (String JavaDoc docName, String JavaDoc content)
309    {
310 try {
311     FileAccess fileHandler = new FileAccess ();
312
313     // First at all, we expect to recive the XML name of the document,
314
// so we are going to change the file extension.
315
docName = this.getHTMLname (docName);
316
317     String JavaDoc filePath = this.docRoot + "/news/" + docName;
318     // Check if the directory structure exists...
319
fileHandler.createDocDir (docName,this.docRoot + "/news/");
320     // We need to delete the file before the write operation
321
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 JavaDoc e) {
330     e.printStackTrace();
331 }
332    }
333
334 }
335
Popular Tags