KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > xml > parser > XercesParser


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: XercesParser.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.xml.parser;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.Reader JavaDoc;
25
26 import org.apache.lenya.xml.DOMWriter;
27 import org.apache.xerces.dom.CDATASectionImpl;
28 import org.apache.xerces.dom.CommentImpl;
29 import org.apache.xerces.dom.DocumentImpl;
30 import org.apache.xerces.dom.ElementImpl;
31 import org.apache.xerces.dom.TextImpl;
32 import org.apache.xerces.parsers.DOMParser;
33 import org.w3c.dom.CDATASection JavaDoc;
34 import org.w3c.dom.Comment JavaDoc;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Text JavaDoc;
38
39
40 /**
41  * Xerces Parser Implementation
42  */

43 public class XercesParser implements Parser {
44     /**
45      * DOCUMENT ME!
46      *
47      * @param args DOCUMENT ME!
48      */

49     public static void main(String JavaDoc[] args) {
50         Parser parser = new XercesParser();
51
52         if (args.length != 1) {
53             System.err.println("Usage: java " + parser.getClass().getName() + " example.xml");
54
55             return;
56         }
57
58         Document JavaDoc doc = null;
59
60         try {
61             doc = parser.getDocument(args[0]);
62         } catch (Exception JavaDoc e) {
63             System.err.println(e);
64         }
65
66         new DOMWriter(new PrintWriter JavaDoc(System.out)).print(doc);
67         System.out.println("");
68
69         Document JavaDoc document = parser.getDocument();
70         Element JavaDoc michi = parser.newElementNode(document, "Employee");
71         michi.setAttribute("Id", "michi");
72         michi.appendChild(parser.newTextNode(document, "Michi"));
73
74         Element JavaDoc employees = parser.newElementNode(document, "Employees");
75         employees.appendChild(parser.newTextNode(document, "\n"));
76         employees.appendChild(michi);
77         employees.appendChild(parser.newTextNode(document, "\n"));
78         document.appendChild(employees);
79         new DOMWriter(new PrintWriter JavaDoc(System.out)).print(document);
80         System.out.println("");
81     }
82
83     /**
84      * DOCUMENT ME!
85      *
86      * @param filename DOCUMENT ME!
87      *
88      * @return DOCUMENT ME!
89      *
90      * @throws Exception DOCUMENT ME!
91      */

92     public Document JavaDoc getDocument(String JavaDoc filename) throws Exception JavaDoc {
93         DOMParser parser = new DOMParser();
94
95         org.xml.sax.InputSource JavaDoc in = new org.xml.sax.InputSource JavaDoc(filename);
96         parser.parse(in);
97
98         return parser.getDocument();
99     }
100
101     /**
102      * DOCUMENT ME!
103      *
104      * @param is DOCUMENT ME!
105      *
106      * @return DOCUMENT ME!
107      *
108      * @throws Exception DOCUMENT ME!
109      */

110     public Document JavaDoc getDocument(InputStream JavaDoc is) throws Exception JavaDoc {
111         DOMParser parser = new DOMParser();
112         org.xml.sax.InputSource JavaDoc in = new org.xml.sax.InputSource JavaDoc(is);
113         parser.parse(in);
114
115         return parser.getDocument();
116     }
117
118     /**
119      * Creates a document from a reader.
120      *
121      * @param is DOCUMENT ME!
122      *
123      * @return DOCUMENT ME!
124      *
125      * @throws Exception DOCUMENT ME!
126      */

127     public Document JavaDoc getDocument(Reader JavaDoc reader) throws Exception JavaDoc {
128         DOMParser parser = new DOMParser();
129         org.xml.sax.InputSource JavaDoc in = new org.xml.sax.InputSource JavaDoc(reader);
130         parser.parse(in);
131
132         return parser.getDocument();
133     }
134
135     /**
136      * DOCUMENT ME!
137      *
138      * @return DOCUMENT ME!
139      */

140     public Document JavaDoc getDocument() {
141         return new DocumentImpl();
142     }
143
144     /**
145      * DOCUMENT ME!
146      *
147      * @param document DOCUMENT ME!
148      * @param name DOCUMENT ME!
149      *
150      * @return DOCUMENT ME!
151      */

152     public Element JavaDoc newElementNode(Document JavaDoc document, String JavaDoc name) {
153         return new ElementImpl((DocumentImpl) document, name);
154     }
155
156     /**
157      * Creates an element with namespace support.
158      *
159      * @param document The owner document.
160      * @param namespaceUri The namespace URI of the element.
161      * @param qualifiedName The qualified name of the element.
162      *
163      * @return An element.
164      */

165     public Element JavaDoc newElementNSNode(Document JavaDoc document, String JavaDoc namespaceUri, String JavaDoc qualifiedName) {
166         return document.createElementNS(namespaceUri, qualifiedName);
167     }
168
169     /**
170      * DOCUMENT ME!
171      *
172      * @param document DOCUMENT ME!
173      * @param data DOCUMENT ME!
174      *
175      * @return DOCUMENT ME!
176      */

177     public Text JavaDoc newTextNode(Document JavaDoc document, String JavaDoc data) {
178         return new TextImpl((DocumentImpl) document, data);
179     }
180
181     /**
182      * CDATA
183      *
184      * @param document DOM Document
185      * @param data Text
186      *
187      * @return CDATASection
188      */

189     public CDATASection JavaDoc newCDATASection(Document JavaDoc document, String JavaDoc data) {
190         return new CDATASectionImpl((DocumentImpl) document, data);
191     }
192
193     /**
194      * DOCUMENT ME!
195      *
196      * @param document DOCUMENT ME!
197      * @param data DOCUMENT ME!
198      *
199      * @return DOCUMENT ME!
200      */

201     public Comment JavaDoc newCommentNode(Document JavaDoc document, String JavaDoc data) {
202         return new CommentImpl((DocumentImpl) document, data);
203     }
204 }
205
Popular Tags