KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > xml > DOMParserFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: DOMParserFactory.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.xml;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import org.apache.lenya.xml.parser.Parser;
29 import org.apache.log4j.Category;
30 import org.w3c.dom.CDATASection JavaDoc;
31 import org.w3c.dom.Comment JavaDoc;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.NamedNodeMap JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37 import org.w3c.dom.Text JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39
40
41 /**
42  * Utility class for creating DOM documents
43  * @deprecated replaced by DocumentHelper
44  */

45 public class DOMParserFactory {
46     static Category log = Category.getInstance(DOMParserFactory.class);
47     public Parser parser = null;
48
49     /**
50      * Reads the properties and gets the parser
51      */

52     public DOMParserFactory() {
53         Properties JavaDoc properties = new Properties JavaDoc();
54         String JavaDoc propertiesFileName = "conf.properties";
55
56         try {
57             properties.load(DOMParserFactory.class.getResourceAsStream(propertiesFileName));
58         } catch (Exception JavaDoc e) {
59             log.fatal(": Failed to load properties from resource: " + propertiesFileName);
60         }
61
62         String JavaDoc parserName = properties.getProperty("Parser");
63
64         if (parserName == null) {
65             log.fatal(": No Parser specified in " + propertiesFileName);
66         }
67
68         try {
69             Class JavaDoc parserClass = Class.forName(parserName);
70             parser = (Parser) parserClass.newInstance();
71         } catch (Exception JavaDoc e) {
72             log.fatal(": " + e);
73         }
74     }
75
76     /**
77      * DOCUMENT ME!
78      *
79      * @param args DOCUMENT ME!
80      */

81     public static void main(String JavaDoc[] args) {
82         DOMParserFactory dpf = new DOMParserFactory();
83
84         if (args.length != 1) {
85             System.out.println("Usage: java " + dpf.getClass().getName() + " example.xml");
86
87             return;
88         }
89
90         Document JavaDoc doc = null;
91
92         try {
93             doc = dpf.getDocument(args[0]);
94         } catch (FileNotFoundException JavaDoc e) {
95             System.err.println("No such file or directory: " + e.getMessage());
96
97             return;
98         } catch (SAXException JavaDoc e) {
99             System.err.println(e);
100
101             return;
102         } catch (Exception JavaDoc e) {
103             System.err.println(e.getMessage());
104
105             return;
106         }
107     }
108
109     /**
110      * DOCUMENT ME!
111      *
112      * @param filename DOCUMENT ME!
113      *
114      * @return DOCUMENT ME!
115      *
116      * @throws FileNotFoundException DOCUMENT ME!
117      * @throws Exception DOCUMENT ME!
118      */

119     public Document JavaDoc getDocument(String JavaDoc filename) throws FileNotFoundException JavaDoc, Exception JavaDoc {
120         File JavaDoc file = new File JavaDoc(filename);
121
122         if (!file.exists()) {
123             log.error("No such file or directory: " + filename);
124             throw new FileNotFoundException JavaDoc(filename);
125         }
126
127         return parser.getDocument(filename);
128     }
129
130     /**
131      * DOCUMENT ME!
132      *
133      * @param inputStream DOCUMENT ME!
134      *
135      * @return DOCUMENT ME!
136      *
137      * @throws Exception DOCUMENT ME!
138      */

139     public Document JavaDoc getDocument(InputStream JavaDoc inputStream)
140         throws Exception JavaDoc {
141         return parser.getDocument(inputStream);
142     }
143
144     /**
145      * Create a document from a reader.
146      *
147      * @param inputStream DOCUMENT ME!
148      * @return DOCUMENT ME!
149      * @throws Exception DOCUMENT ME!
150      */

151     public Document JavaDoc getDocument(Reader JavaDoc reader) throws Exception JavaDoc {
152         return parser.getDocument(reader);
153     }
154
155     /**
156      * DOCUMENT ME!
157      *
158      * @return DOCUMENT ME!
159      */

160     public Document JavaDoc getDocument() {
161         return parser.getDocument();
162     }
163
164     /**
165      * DOCUMENT ME!
166      *
167      * @param document DOCUMENT ME!
168      * @param name DOCUMENT ME!
169      *
170      * @return DOCUMENT ME!
171      */

172     public Element JavaDoc newElementNode(Document JavaDoc document, String JavaDoc name) {
173         return parser.newElementNode(document, name);
174     }
175
176     /**
177      * Creates an element with namespace support.
178      *
179      * @param document The owner document.
180      * @param namespaceUri The namespace URI of the element.
181      * @param qualifiedName The qualified name of the element.
182      *
183      * @return An element.
184      */

185     public Element JavaDoc newElementNSNode(Document JavaDoc document, String JavaDoc namespaceUri, String JavaDoc qualifiedName) {
186         return parser.newElementNSNode(document, namespaceUri, qualifiedName);
187     }
188         
189     /**
190      * DOCUMENT ME!
191      *
192      * @param document DOCUMENT ME!
193      * @param data DOCUMENT ME!
194      *
195      * @return DOCUMENT ME!
196      */

197     public Text JavaDoc newTextNode(Document JavaDoc document, String JavaDoc data) {
198         return parser.newTextNode(document, data);
199     }
200
201     /**
202      * CDATA
203      *
204      * @param document DOM document
205      * @param data Text
206      *
207      * @return CDATASection
208      */

209     public CDATASection JavaDoc newCDATASection(Document JavaDoc document, String JavaDoc data) {
210         return parser.newCDATASection(document, data);
211     }
212
213     /**
214      * DOCUMENT ME!
215      *
216      * @param document DOCUMENT ME!
217      * @param data DOCUMENT ME!
218      *
219      * @return DOCUMENT ME!
220      */

221     public Comment JavaDoc newCommentNode(Document JavaDoc document, String JavaDoc data) {
222         return parser.newCommentNode(document, data);
223     }
224
225     /**
226      * Clone node, which means copy a node into another document
227      *
228      * @param document New document where original nodes shall be attached to
229      * @param original Original node from original document
230      * @param deep true means clone also all children
231      *
232      * @return New node, which is clone of original node
233      */

234     public Node JavaDoc cloneNode(Document JavaDoc document, Node JavaDoc original, boolean deep) {
235         Node JavaDoc node = null;
236         short nodeType = original.getNodeType();
237
238         switch (nodeType) {
239         case Node.ELEMENT_NODE: {
240             Element JavaDoc element = newElementNSNode(document, original.getNamespaceURI(), original.getNodeName());
241             log.debug(".cloneNode(): Clone element: " + original.getNodeName());
242             NamedNodeMap JavaDoc attributes = original.getAttributes();
243
244             for (int i = 0; i < attributes.getLength(); i++) {
245                 Node JavaDoc attribute = attributes.item(i);
246                 log.debug(".cloneNode(): LocalName: " + attribute.getLocalName() + ", Prefix: " + attribute.getPrefix() + ", NamespaceURI: " + attribute.getNamespaceURI());
247                 element.setAttributeNS(attribute.getNamespaceURI(), attribute.getNodeName(), attribute.getNodeValue());
248             }
249
250             node = element;
251
252             break;
253         }
254
255         case Node.TEXT_NODE: {
256             Text JavaDoc text = newTextNode(document, original.getNodeValue());
257
258             node = text;
259
260             break;
261         }
262
263         case Node.CDATA_SECTION_NODE: {
264             CDATASection JavaDoc cdata = newCDATASection(document, original.getNodeValue());
265
266             node = cdata;
267
268             break;
269         }
270
271         case Node.COMMENT_NODE: {
272             Comment JavaDoc comment = newCommentNode(document, original.getNodeValue());
273
274             node = comment;
275
276             break;
277         }
278
279         default:
280             log.warn(".cloneNode(): Node type not implemented: " + nodeType);
281             break;
282         }
283
284         if (deep && original.hasChildNodes()) {
285             NodeList JavaDoc nl = original.getChildNodes();
286
287             for (int i = 0; i < nl.getLength(); i++) {
288                 node.appendChild(cloneNode(document, nl.item(i), deep));
289             }
290         }
291
292         return node;
293     }
294
295     /**
296      * DOCUMENT ME!
297      *
298      * @param document DOCUMENT ME!
299      * @param element DOCUMENT ME!
300      * @param value DOCUMENT ME!
301      */

302     public void setElementValue(Document JavaDoc document, Element JavaDoc element, String JavaDoc value) {
303         // remove all child nodes
304
NodeList JavaDoc nl = element.getChildNodes();
305
306         for (int i = 0; i < nl.getLength(); i++) {
307             try {
308                 element.removeChild(nl.item(i));
309             } catch (Exception JavaDoc e) {
310                 System.err.println("EXCEPTION: " + this.getClass().getName() +
311                     ".setElementValue(): " + e);
312             }
313         }
314
315         // add a new TextNode for storing the new value
316
element.appendChild(newTextNode(document, value));
317     }
318 }
319
Popular Tags