KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > libraries > LibraryDeclarationParser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.project.libraries;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Stack JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import org.openide.xml.XMLUtil;
29 import org.xml.sax.Attributes JavaDoc;
30 import org.xml.sax.ContentHandler JavaDoc;
31 import org.xml.sax.EntityResolver JavaDoc;
32 import org.xml.sax.ErrorHandler JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34 import org.xml.sax.Locator JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36 import org.xml.sax.SAXParseException JavaDoc;
37 import org.xml.sax.XMLReader JavaDoc;
38 import org.xml.sax.helpers.AttributesImpl JavaDoc;
39
40 /**
41  * The class reads XML documents according to specified DTD and
42  * translates all related events into LibraryDeclarationHandler events.
43  * <p>Usage sample:
44  * <pre>
45  * LibraryDeclarationParser parser = new LibraryDeclarationParser(...);
46  * parser.parse(new InputSource("..."));
47  * </pre>
48  * <p><b>Warning:</b> the class is machine generated. DO NOT MODIFY</p>
49  *
50  */

51 public class LibraryDeclarationParser implements ContentHandler JavaDoc, EntityResolver JavaDoc {
52     
53     private StringBuffer JavaDoc buffer;
54     
55     private LibraryDeclarationConvertor parslet;
56     
57     private LibraryDeclarationHandler handler;
58     
59     private Stack JavaDoc<Object JavaDoc[]> context;
60
61     
62     /**
63      * Creates a parser instance.
64      * @param handler handler interface implementation (never <code>null</code>
65      * It is recommended that it could be able to resolve at least the DTD.@param parslet convertors implementation (never <code>null</code>
66      *
67      */

68     public LibraryDeclarationParser(final LibraryDeclarationHandler handler, final LibraryDeclarationConvertor parslet) {
69         this.parslet = parslet;
70         this.handler = handler;
71         buffer = new StringBuffer JavaDoc(111);
72         context = new Stack JavaDoc<Object JavaDoc[]>();
73     }
74     
75     /**
76      * This SAX interface method is implemented by the parser.
77      *
78      */

79     public final void setDocumentLocator(Locator JavaDoc locator) {
80     }
81     
82     /**
83      * This SAX interface method is implemented by the parser.
84      *
85      */

86     public final void startDocument() throws SAXException JavaDoc {
87     }
88     
89     /**
90      * This SAX interface method is implemented by the parser.
91      *
92      */

93     public final void endDocument() throws SAXException JavaDoc {
94     }
95     
96     /**
97      * This SAX interface method is implemented by the parser.
98      *
99      */

100     public final void startElement(String JavaDoc ns, String JavaDoc name, String JavaDoc qname, Attributes JavaDoc attrs) throws SAXException JavaDoc {
101         dispatch(true);
102         context.push(new Object JavaDoc[] {qname, new AttributesImpl JavaDoc(attrs)});
103         if ("volume".equals(qname)) {
104             handler.start_volume(attrs);
105         } else if ("library".equals(qname)) {
106             handler.start_library(attrs);
107         }
108     }
109     
110     /**
111      * This SAX interface method is implemented by the parser.
112      *
113      */

114     public final void endElement(String JavaDoc ns, String JavaDoc name, String JavaDoc qname) throws SAXException JavaDoc {
115         dispatch(false);
116         context.pop();
117         if ("volume".equals(qname)) {
118             handler.end_volume();
119         } else if ("library".equals(qname)) {
120             handler.end_library();
121         }
122     }
123     
124     /**
125      * This SAX interface method is implemented by the parser.
126      *
127      */

128     public final void characters(char[] chars, int start, int len) throws SAXException JavaDoc {
129         buffer.append(chars, start, len);
130     }
131     
132     /**
133      * This SAX interface method is implemented by the parser.
134      *
135      */

136     public final void ignorableWhitespace(char[] chars, int start, int len) throws SAXException JavaDoc {
137     }
138     
139     /**
140      * This SAX interface method is implemented by the parser.
141      *
142      */

143     public final void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
144     }
145     
146     /**
147      * This SAX interface method is implemented by the parser.
148      *
149      */

150     public final void startPrefixMapping(final String JavaDoc prefix, final String JavaDoc uri) throws SAXException JavaDoc {
151     }
152     
153     /**
154      * This SAX interface method is implemented by the parser.
155      *
156      */

157     public final void endPrefixMapping(final String JavaDoc prefix) throws SAXException JavaDoc {
158     }
159     
160     /**
161      * This SAX interface method is implemented by the parser.
162      *
163      */

164     public final void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
165     }
166     
167     private void dispatch(final boolean fireOnlyIfMixed) throws SAXException JavaDoc {
168         if (fireOnlyIfMixed && buffer.length() == 0) return; //skip it
169

170         Object JavaDoc[] ctx = context.peek();
171         String JavaDoc here = (String JavaDoc) ctx[0];
172         Attributes JavaDoc attrs = (Attributes JavaDoc) ctx[1];
173         if ("description".equals(here)) {
174             if (fireOnlyIfMixed) throw new IllegalStateException JavaDoc("Unexpected characters() event! (Missing DTD?)");
175             handler.handle_description (buffer.length() == 0 ? null : buffer.toString(), attrs);
176         } else if ("type".equals(here)) {
177             if (fireOnlyIfMixed) throw new IllegalStateException JavaDoc("Unexpected characters() event! (Missing DTD?)");
178             handler.handle_type(buffer.length() == 0 ? null : buffer.toString(), attrs);
179         } else if ("resource".equals(here)) {
180             if (fireOnlyIfMixed) throw new IllegalStateException JavaDoc("Unexpected characters() event! (Missing DTD?)");
181             handler.handle_resource(parslet.parseResource(buffer.length() == 0 ? null : buffer.toString()), attrs);
182         } else if ("name".equals(here)) {
183             if (fireOnlyIfMixed) throw new IllegalStateException JavaDoc("Unexpected characters() event! (Missing DTD?)");
184             handler.handle_name(buffer.length() == 0 ? null : buffer.toString(), attrs);
185         } else if ("localizing-bundle".equals(here)) {
186             if (fireOnlyIfMixed) throw new IllegalStateException JavaDoc("Unexpected characters() event! (Missing DTD?)");
187             handler.handle_localizingBundle(buffer.length() == 0 ? null : buffer.toString(), attrs);
188         } else {
189             //do not care
190
}
191         buffer.delete(0, buffer.length());
192     }
193     
194     /**
195      * The recognizer entry method taking an InputSource.
196      * @param input InputSource to be parsed.
197      * @throws java.io.IOException on I/O error.
198      * @throws SAXException propagated exception thrown by a DocumentHandler.
199      * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
200      * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
201      *
202      */

203     public void parse(final InputSource JavaDoc input) throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
204         parse(input, this);
205     }
206     
207     /**
208      * The recognizer entry method taking a URL.
209      * @param url URL source to be parsed.
210      * @throws java.io.IOException on I/O error.
211      * @throws SAXException propagated exception thrown by a DocumentHandler.
212      * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
213      * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
214      *
215      */

216     public void parse(final URL JavaDoc url) throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
217         parse(new InputSource JavaDoc(url.toExternalForm()), this);
218     }
219     
220     /**
221      * The recognizer entry method taking an Inputsource.
222      * @param input InputSource to be parsed.
223      * @throws java.io.IOException on I/O error.
224      * @throws SAXException propagated exception thrown by a DocumentHandler.
225      * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
226      * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
227      *
228      */

229     public static void parse(final InputSource JavaDoc input, final LibraryDeclarationHandler handler, final LibraryDeclarationConvertor parslet) throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
230         parse(input, new LibraryDeclarationParser(handler, parslet));
231     }
232     
233     /**
234      * The recognizer entry method taking a URL.
235      * @param url URL source to be parsed.
236      * @throws java.io.IOException on I/O error.
237      * @throws SAXException propagated exception thrown by a DocumentHandler.
238      * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
239      * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
240      *
241      */

242     public static void parse(final URL JavaDoc url, final LibraryDeclarationHandler handler, final LibraryDeclarationConvertor parslet) throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
243         parse(new InputSource JavaDoc(url.toExternalForm()), handler, parslet);
244     }
245     
246     private static void parse(final InputSource JavaDoc input, final LibraryDeclarationParser recognizer) throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
247         try {
248             XMLReader JavaDoc parser = XMLUtil.createXMLReader(false, false);
249             parser.setContentHandler(recognizer);
250             parser.setErrorHandler(recognizer.getDefaultErrorHandler());
251             parser.setEntityResolver(recognizer);
252             parser.parse(input);
253         } finally {
254             //Recover recognizer internal state from exceptions to be reusable
255
if (!recognizer.context.empty()) {
256                 recognizer.context.clear();
257             }
258             if (recognizer.buffer.length() > 0) {
259                 recognizer.buffer.delete(0, recognizer.buffer.length());
260             }
261         }
262     }
263     
264     /**
265      * Creates default error handler used by this parser.
266      * @return org.xml.sax.ErrorHandler implementation
267      *
268      */

269     protected ErrorHandler JavaDoc getDefaultErrorHandler() {
270         return new ErrorHandler JavaDoc() {
271             public void error(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
272                 throw ex;
273             }
274             
275             public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
276                 throw ex;
277             }
278             
279             public void warning(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
280                 // ignore
281
}
282         };
283         
284     }
285     
286     /** Implementation of entity resolver. Points to the local DTD
287      * for our public ID */

288     public InputSource JavaDoc resolveEntity (String JavaDoc publicId, String JavaDoc systemId)
289     throws SAXException JavaDoc {
290         if ("-//NetBeans//DTD Library Declaration 1.0//EN".equals(publicId)) {
291             InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(new byte[0]);
292             return new InputSource JavaDoc(is);
293         }
294         return null; // i.e. follow advice of systemID
295
}
296 }
297
298
Popular Tags