001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.libraries;
043:
044: import java.io.ByteArrayInputStream;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.net.URL;
048: import java.util.Stack;
049: import javax.xml.parsers.ParserConfigurationException;
050: import org.openide.xml.XMLUtil;
051: import org.xml.sax.Attributes;
052: import org.xml.sax.ContentHandler;
053: import org.xml.sax.EntityResolver;
054: import org.xml.sax.ErrorHandler;
055: import org.xml.sax.InputSource;
056: import org.xml.sax.Locator;
057: import org.xml.sax.SAXException;
058: import org.xml.sax.SAXParseException;
059: import org.xml.sax.XMLReader;
060: import org.xml.sax.helpers.AttributesImpl;
061:
062: /**
063: * The class reads XML documents according to specified DTD and
064: * translates all related events into LibraryDeclarationHandler events.
065: * <p>Usage sample:
066: * <pre>
067: * LibraryDeclarationParser parser = new LibraryDeclarationParser(...);
068: * parser.parse(new InputSource("..."));
069: * </pre>
070: * <p><b>Warning:</b> the class is machine generated. DO NOT MODIFY</p>
071: *
072: */
073: public class LibraryDeclarationParser implements ContentHandler,
074: EntityResolver {
075:
076: private StringBuffer buffer;
077:
078: private LibraryDeclarationConvertor parslet;
079:
080: private LibraryDeclarationHandler handler;
081:
082: private Stack<Object[]> context;
083:
084: /**
085: * Creates a parser instance.
086: * @param handler handler interface implementation (never <code>null</code>
087: * It is recommended that it could be able to resolve at least the DTD.@param parslet convertors implementation (never <code>null</code>
088: *
089: */
090: public LibraryDeclarationParser(
091: final LibraryDeclarationHandler handler,
092: final LibraryDeclarationConvertor parslet) {
093: this .parslet = parslet;
094: this .handler = handler;
095: buffer = new StringBuffer(111);
096: context = new Stack<Object[]>();
097: }
098:
099: /**
100: * This SAX interface method is implemented by the parser.
101: *
102: */
103: public final void setDocumentLocator(Locator locator) {
104: }
105:
106: /**
107: * This SAX interface method is implemented by the parser.
108: *
109: */
110: public final void startDocument() throws SAXException {
111: }
112:
113: /**
114: * This SAX interface method is implemented by the parser.
115: *
116: */
117: public final void endDocument() throws SAXException {
118: }
119:
120: /**
121: * This SAX interface method is implemented by the parser.
122: *
123: */
124: public final void startElement(String ns, String name,
125: String qname, Attributes attrs) throws SAXException {
126: dispatch(true);
127: context.push(new Object[] { qname, new AttributesImpl(attrs) });
128: if ("volume".equals(qname)) {
129: handler.start_volume(attrs);
130: } else if ("library".equals(qname)) {
131: handler.start_library(attrs);
132: }
133: }
134:
135: /**
136: * This SAX interface method is implemented by the parser.
137: *
138: */
139: public final void endElement(String ns, String name, String qname)
140: throws SAXException {
141: dispatch(false);
142: context.pop();
143: if ("volume".equals(qname)) {
144: handler.end_volume();
145: } else if ("library".equals(qname)) {
146: handler.end_library();
147: }
148: }
149:
150: /**
151: * This SAX interface method is implemented by the parser.
152: *
153: */
154: public final void characters(char[] chars, int start, int len)
155: throws SAXException {
156: buffer.append(chars, start, len);
157: }
158:
159: /**
160: * This SAX interface method is implemented by the parser.
161: *
162: */
163: public final void ignorableWhitespace(char[] chars, int start,
164: int len) throws SAXException {
165: }
166:
167: /**
168: * This SAX interface method is implemented by the parser.
169: *
170: */
171: public final void processingInstruction(String target, String data)
172: throws SAXException {
173: }
174:
175: /**
176: * This SAX interface method is implemented by the parser.
177: *
178: */
179: public final void startPrefixMapping(final String prefix,
180: final String uri) throws SAXException {
181: }
182:
183: /**
184: * This SAX interface method is implemented by the parser.
185: *
186: */
187: public final void endPrefixMapping(final String prefix)
188: throws SAXException {
189: }
190:
191: /**
192: * This SAX interface method is implemented by the parser.
193: *
194: */
195: public final void skippedEntity(String name) throws SAXException {
196: }
197:
198: private void dispatch(final boolean fireOnlyIfMixed)
199: throws SAXException {
200: if (fireOnlyIfMixed && buffer.length() == 0)
201: return; //skip it
202:
203: Object[] ctx = context.peek();
204: String here = (String) ctx[0];
205: Attributes attrs = (Attributes) ctx[1];
206: if ("description".equals(here)) {
207: if (fireOnlyIfMixed)
208: throw new IllegalStateException(
209: "Unexpected characters() event! (Missing DTD?)");
210: handler.handle_description(buffer.length() == 0 ? null
211: : buffer.toString(), attrs);
212: } else if ("type".equals(here)) {
213: if (fireOnlyIfMixed)
214: throw new IllegalStateException(
215: "Unexpected characters() event! (Missing DTD?)");
216: handler.handle_type(buffer.length() == 0 ? null : buffer
217: .toString(), attrs);
218: } else if ("resource".equals(here)) {
219: if (fireOnlyIfMixed)
220: throw new IllegalStateException(
221: "Unexpected characters() event! (Missing DTD?)");
222: handler.handle_resource(parslet.parseResource(buffer
223: .length() == 0 ? null : buffer.toString()), attrs);
224: } else if ("name".equals(here)) {
225: if (fireOnlyIfMixed)
226: throw new IllegalStateException(
227: "Unexpected characters() event! (Missing DTD?)");
228: handler.handle_name(buffer.length() == 0 ? null : buffer
229: .toString(), attrs);
230: } else if ("localizing-bundle".equals(here)) {
231: if (fireOnlyIfMixed)
232: throw new IllegalStateException(
233: "Unexpected characters() event! (Missing DTD?)");
234: handler.handle_localizingBundle(buffer.length() == 0 ? null
235: : buffer.toString(), attrs);
236: } else {
237: //do not care
238: }
239: buffer.delete(0, buffer.length());
240: }
241:
242: /**
243: * The recognizer entry method taking an InputSource.
244: * @param input InputSource to be parsed.
245: * @throws java.io.IOException on I/O error.
246: * @throws SAXException propagated exception thrown by a DocumentHandler.
247: * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
248: * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
249: *
250: */
251: public void parse(final InputSource input) throws SAXException,
252: ParserConfigurationException, IOException {
253: parse(input, this );
254: }
255:
256: /**
257: * The recognizer entry method taking a URL.
258: * @param url URL source to be parsed.
259: * @throws java.io.IOException on I/O error.
260: * @throws SAXException propagated exception thrown by a DocumentHandler.
261: * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
262: * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
263: *
264: */
265: public void parse(final URL url) throws SAXException,
266: ParserConfigurationException, IOException {
267: parse(new InputSource(url.toExternalForm()), this );
268: }
269:
270: /**
271: * The recognizer entry method taking an Inputsource.
272: * @param input InputSource to be parsed.
273: * @throws java.io.IOException on I/O error.
274: * @throws SAXException propagated exception thrown by a DocumentHandler.
275: * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
276: * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
277: *
278: */
279: public static void parse(final InputSource input,
280: final LibraryDeclarationHandler handler,
281: final LibraryDeclarationConvertor parslet)
282: throws SAXException, ParserConfigurationException,
283: IOException {
284: parse(input, new LibraryDeclarationParser(handler, parslet));
285: }
286:
287: /**
288: * The recognizer entry method taking a URL.
289: * @param url URL source to be parsed.
290: * @throws java.io.IOException on I/O error.
291: * @throws SAXException propagated exception thrown by a DocumentHandler.
292: * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
293: * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
294: *
295: */
296: public static void parse(final URL url,
297: final LibraryDeclarationHandler handler,
298: final LibraryDeclarationConvertor parslet)
299: throws SAXException, ParserConfigurationException,
300: IOException {
301: parse(new InputSource(url.toExternalForm()), handler, parslet);
302: }
303:
304: private static void parse(final InputSource input,
305: final LibraryDeclarationParser recognizer)
306: throws SAXException, ParserConfigurationException,
307: IOException {
308: try {
309: XMLReader parser = XMLUtil.createXMLReader(false, false);
310: parser.setContentHandler(recognizer);
311: parser.setErrorHandler(recognizer.getDefaultErrorHandler());
312: parser.setEntityResolver(recognizer);
313: parser.parse(input);
314: } finally {
315: //Recover recognizer internal state from exceptions to be reusable
316: if (!recognizer.context.empty()) {
317: recognizer.context.clear();
318: }
319: if (recognizer.buffer.length() > 0) {
320: recognizer.buffer.delete(0, recognizer.buffer.length());
321: }
322: }
323: }
324:
325: /**
326: * Creates default error handler used by this parser.
327: * @return org.xml.sax.ErrorHandler implementation
328: *
329: */
330: protected ErrorHandler getDefaultErrorHandler() {
331: return new ErrorHandler() {
332: public void error(SAXParseException ex) throws SAXException {
333: throw ex;
334: }
335:
336: public void fatalError(SAXParseException ex)
337: throws SAXException {
338: throw ex;
339: }
340:
341: public void warning(SAXParseException ex)
342: throws SAXException {
343: // ignore
344: }
345: };
346:
347: }
348:
349: /** Implementation of entity resolver. Points to the local DTD
350: * for our public ID */
351: public InputSource resolveEntity(String publicId, String systemId)
352: throws SAXException {
353: if ("-//NetBeans//DTD Library Declaration 1.0//EN"
354: .equals(publicId)) {
355: InputStream is = new ByteArrayInputStream(new byte[0]);
356: return new InputSource(is);
357: }
358: return null; // i.e. follow advice of systemID
359: }
360: }
|