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: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common
009: * Development and Distribution License("CDDL") (collectively, the
010: * "License"). You may not use this file except in compliance with the
011: * License. You can obtain a copy of the License at
012: * http://www.netbeans.org/cddl-gplv2.html
013: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
014: * specific language governing permissions and limitations under the
015: * License. When distributing the software, include this License Header
016: * Notice in each file and include the License file at
017: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
018: * particular file as subject to the "Classpath" exception as provided
019: * by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the
021: * License Header, with the fields enclosed by brackets [] replaced by
022: * your own identifying information:
023: * "Portions Copyrighted [year] [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * The Original Software is NetBeans. The Initial Developer of the Original
028: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
029: * Microsystems, Inc. All Rights Reserved.
030: *
031: * If you wish your version of this file to be governed by only the CDDL
032: * or only the GPL Version 2, indicate your decision by adding
033: * "[Contributor] elects to include this software in this distribution
034: * under the [CDDL or GPL Version 2] license." If you do not indicate a
035: * single choice of license, a recipient has the option to distribute
036: * your version of this file under either the CDDL, the GPL Version 2 or
037: * to extend the choice of license to its licensees as provided above.
038: * However, if you add GPL Version 2 code and therefore, elected the GPL
039: * Version 2 license, then the option applies only if the new code is
040: * made subject to such option by the copyright holder.
041: */
042:
043: package org.netbeans.modules.j2me.cdc.project.ricoh.dalp;
044:
045: import org.xml.sax.*;
046:
047: /**
048: *
049: * The class reads XML documents according to specified DTD and
050: * translates all related events into DalpParserHandler events.
051: * <p>Usage sample:
052: * <pre>
053: * DalpParserParser parser = new DalpParserParser(...);
054: * parser.parse(new InputSource("..."));
055: * </pre>
056: * <p><b>Warning:</b> the class is machine generated. DO NOT MODIFY</p>
057: */
058: public class DalpParser implements ContentHandler {
059:
060: private java.lang.StringBuffer buffer;
061:
062: private DalpParserHandler handler;
063:
064: private java.util.Stack context;
065:
066: private EntityResolver resolver;
067:
068: /**
069: *
070: * Creates a parser instance.
071: * @param handler handler interface implementation (never <code>null</code>
072: * @param resolver SAX entity resolver implementation or <code>null</code>.
073: * It is recommended that it could be able to resolve at least the DTD.
074: */
075: public DalpParser(final DalpParserHandler handler,
076: final EntityResolver resolver) {
077: this .handler = handler;
078: this .resolver = resolver;
079: buffer = new StringBuffer(111);
080: context = new java.util.Stack();
081: }
082:
083: /**
084: *
085: * This SAX interface method is implemented by the parser.
086: */
087: public final void setDocumentLocator(Locator locator) {
088: }
089:
090: /**
091: *
092: * This SAX interface method is implemented by the parser.
093: */
094: public final void startDocument() throws SAXException {
095: }
096:
097: /**
098: *
099: * This SAX interface method is implemented by the parser.
100: */
101: public final void endDocument() throws SAXException {
102: }
103:
104: /**
105: *
106: * This SAX interface method is implemented by the parser.
107: */
108: public final void startElement(java.lang.String ns,
109: java.lang.String name, java.lang.String qname,
110: Attributes attrs) throws SAXException {
111: dispatch(true);
112: context.push(new Object[] { qname,
113: new org.xml.sax.helpers.AttributesImpl(attrs) });
114: if ("jar".equals(qname)) {
115: handler.handle_jar(attrs);
116: } else if ("dsdk".equals(qname)) {
117: handler.handle_dsdk(attrs);
118: } else if ("install".equals(qname)) {
119: handler.handle_install(attrs);
120: } else if ("dalp".equals(qname)) {
121: handler.start_dalp(attrs);
122: } else if ("resources".equals(qname)) {
123: handler.start_resources(attrs);
124: } else if ("offline-allowed".equals(qname)) {
125: handler.handle_offline_allowed(attrs);
126: } else if ("application-desc".equals(qname)) {
127: handler.handle_application_desc(attrs);
128: } else if ("all-permissions".equals(qname)) {
129: handler.handle_all_permissions(attrs);
130: } else if ("information".equals(qname)) {
131: handler.start_information(attrs);
132: } else if ("security".equals(qname)) {
133: handler.start_security(attrs);
134: }
135: }
136:
137: /**
138: *
139: * This SAX interface method is implemented by the parser.
140: */
141: public final void endElement(java.lang.String ns,
142: java.lang.String name, java.lang.String qname)
143: throws SAXException {
144: dispatch(false);
145: context.pop();
146: if ("dalp".equals(qname)) {
147: handler.end_dalp();
148: } else if ("resources".equals(qname)) {
149: handler.end_resources();
150: } else if ("information".equals(qname)) {
151: handler.end_information();
152: } else if ("security".equals(qname)) {
153: handler.end_security();
154: }
155: }
156:
157: /**
158: *
159: * This SAX interface method is implemented by the parser.
160: */
161: public final void characters(char[] chars, int start, int len)
162: throws SAXException {
163: buffer.append(chars, start, len);
164: }
165:
166: /**
167: *
168: * This SAX interface method is implemented by the parser.
169: */
170: public final void ignorableWhitespace(char[] chars, int start,
171: int len) throws SAXException {
172: }
173:
174: /**
175: *
176: * This SAX interface method is implemented by the parser.
177: */
178: public final void processingInstruction(java.lang.String target,
179: java.lang.String data) throws SAXException {
180: }
181:
182: /**
183: *
184: * This SAX interface method is implemented by the parser.
185: */
186: public final void startPrefixMapping(final java.lang.String prefix,
187: final java.lang.String uri) throws SAXException {
188: }
189:
190: /**
191: *
192: * This SAX interface method is implemented by the parser.
193: */
194: public final void endPrefixMapping(final java.lang.String prefix)
195: throws SAXException {
196: }
197:
198: /**
199: *
200: * This SAX interface method is implemented by the parser.
201: */
202: public final void skippedEntity(java.lang.String name)
203: throws SAXException {
204: }
205:
206: private void dispatch(final boolean fireOnlyIfMixed)
207: throws SAXException {
208: if (fireOnlyIfMixed && buffer.length() == 0)
209: return; //skip it
210:
211: Object[] ctx = (Object[]) context.peek();
212: String here = (String) ctx[0];
213: Attributes attrs = (Attributes) ctx[1];
214: if ("application-ver".equals(here)) {
215: if (fireOnlyIfMixed)
216: throw new IllegalStateException(
217: "Unexpected characters() event! (Missing DTD?)");
218: handler.handle_application_ver(buffer.length() == 0 ? null
219: : buffer.toString(), attrs);
220: } else if ("vendor".equals(here)) {
221: if (fireOnlyIfMixed)
222: throw new IllegalStateException(
223: "Unexpected characters() event! (Missing DTD?)");
224: handler.handle_vendor(buffer.length() == 0 ? null : buffer
225: .toString(), attrs);
226: } else if ("encode-file".equals(here)) {
227: if (fireOnlyIfMixed)
228: throw new IllegalStateException(
229: "Unexpected characters() event! (Missing DTD?)");
230: handler.handle_encode_file(buffer.length() == 0 ? null
231: : buffer.toString(), attrs);
232: } else if ("title".equals(here)) {
233: if (fireOnlyIfMixed)
234: throw new IllegalStateException(
235: "Unexpected characters() event! (Missing DTD?)");
236: handler.handle_title(buffer.length() == 0 ? null : buffer
237: .toString(), attrs);
238: } else if ("telephone".equals(here)) {
239: if (fireOnlyIfMixed)
240: throw new IllegalStateException(
241: "Unexpected characters() event! (Missing DTD?)");
242: handler.handle_telephone(buffer.length() == 0 ? null
243: : buffer.toString(), attrs);
244: } else if ("fax".equals(here)) {
245: if (fireOnlyIfMixed)
246: throw new IllegalStateException(
247: "Unexpected characters() event! (Missing DTD?)");
248: handler.handle_fax(buffer.length() == 0 ? null : buffer
249: .toString(), attrs);
250: } else if ("e-mail".equals(here)) {
251: if (fireOnlyIfMixed)
252: throw new IllegalStateException(
253: "Unexpected characters() event! (Missing DTD?)");
254: handler.handle_e_mail(buffer.length() == 0 ? null : buffer
255: .toString(), attrs);
256: } else if ("product-id".equals(here)) {
257: if (fireOnlyIfMixed)
258: throw new IllegalStateException(
259: "Unexpected characters() event! (Missing DTD?)");
260: handler.handle_product_id(buffer.length() == 0 ? null
261: : buffer.toString(), attrs);
262: } else if ("icon".equals(here)) {
263: if (fireOnlyIfMixed)
264: throw new IllegalStateException(
265: "Unexpected characters() event! (Missing DTD?)");
266: handler.handle_icon(buffer.length() == 0 ? null : buffer
267: .toString(), attrs);
268: } else if ("description".equals(here)) {
269: if (fireOnlyIfMixed)
270: throw new IllegalStateException(
271: "Unexpected characters() event! (Missing DTD?)");
272: handler.handle_description(buffer.length() == 0 ? null
273: : buffer.toString(), attrs);
274: } else if ("argument".equals(here)) {
275: if (fireOnlyIfMixed)
276: throw new IllegalStateException(
277: "Unexpected characters() event! (Missing DTD?)");
278: handler.handle_argument(buffer.length() == 0 ? null
279: : buffer.toString(), attrs);
280: } else {
281: //do not care
282: }
283: buffer.delete(0, buffer.length());
284: }
285:
286: /**
287: *
288: * The recognizer entry method taking an InputSource.
289: * @param input InputSource 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: public void parse(final InputSource input) throws SAXException,
296: javax.xml.parsers.ParserConfigurationException,
297: java.io.IOException {
298: parse(input, this );
299: }
300:
301: /**
302: *
303: * The recognizer entry method taking a URL.
304: * @param url URL source to be parsed.
305: * @throws java.io.IOException on I/O error.
306: * @throws SAXException propagated exception thrown by a DocumentHandler.
307: * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
308: * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
309: */
310: public void parse(final java.net.URL url) throws SAXException,
311: javax.xml.parsers.ParserConfigurationException,
312: java.io.IOException {
313: parse(new InputSource(url.toExternalForm()), this );
314: }
315:
316: /**
317: *
318: * The recognizer entry method taking an Inputsource.
319: * @param input InputSource to be parsed.
320: * @throws java.io.IOException on I/O error.
321: * @throws SAXException propagated exception thrown by a DocumentHandler.
322: * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
323: * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
324: */
325: public static void parse(final InputSource input,
326: final DalpParserHandler handler) throws SAXException,
327: javax.xml.parsers.ParserConfigurationException,
328: java.io.IOException {
329: parse(input, new DalpParser(handler, null));
330: }
331:
332: /**
333: *
334: * The recognizer entry method taking a URL.
335: * @param url URL source to be parsed.
336: * @throws java.io.IOException on I/O error.
337: * @throws SAXException propagated exception thrown by a DocumentHandler.
338: * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
339: * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
340: */
341: public static void parse(final java.net.URL url,
342: final DalpParserHandler handler) throws SAXException,
343: javax.xml.parsers.ParserConfigurationException,
344: java.io.IOException {
345: parse(new InputSource(url.toExternalForm()), handler);
346: }
347:
348: private static void parse(final InputSource input,
349: final DalpParser recognizer) throws SAXException,
350: javax.xml.parsers.ParserConfigurationException,
351: java.io.IOException {
352: javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory
353: .newInstance();
354: factory.setValidating(false); //the code was generated according DTD
355: factory.setNamespaceAware(false); //the code was generated according DTD
356: XMLReader parser = factory.newSAXParser().getXMLReader();
357: parser.setContentHandler(recognizer);
358: parser.setErrorHandler(recognizer.getDefaultErrorHandler());
359: if (recognizer.resolver != null)
360: parser.setEntityResolver(recognizer.resolver);
361: parser.parse(input);
362: }
363:
364: /**
365: *
366: * Creates default error handler used by this parser.
367: * @return org.xml.sax.ErrorHandler implementation
368: */
369: protected ErrorHandler getDefaultErrorHandler() {
370: return new ErrorHandler() {
371: public void error(SAXParseException ex) throws SAXException {
372: if (context.isEmpty())
373: System.err.println("Missing DOCTYPE.");
374: throw ex;
375: }
376:
377: public void fatalError(SAXParseException ex)
378: throws SAXException {
379: throw ex;
380: }
381:
382: public void warning(SAXParseException ex)
383: throws SAXException {
384: // ignore
385: }
386: };
387:
388: }
389: }
|