KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > generator > SAXBindingsParser


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 package org.netbeans.modules.xml.tools.generator;
20
21 import org.xml.sax.*;
22
23 /**
24  * The class reads XML documents according to specified DTD and
25  * translates all related events into SAXBindingsHandler events.
26  * <p>Usage sample:
27  * <pre>
28  * SAXBindingsParser parser = new SAXBindingsParser(...);
29  * parser.parse(new InputSource("..."));
30  * </pre>
31  * <p><b>Warning:</b> the class is machine generated. DO NOT MODIFY</p>
32  */

33 public class SAXBindingsParser extends org.xml.sax.helpers.DefaultHandler JavaDoc {
34
35     private java.lang.StringBuffer JavaDoc buffer;
36     
37     private SAXBindingsHandler handler;
38     
39     private java.util.Stack JavaDoc context;
40         
41     public SAXBindingsParser(final SAXBindingsHandler handler) {
42         this.handler = handler;
43         buffer = new StringBuffer JavaDoc(111);
44         context = new java.util.Stack JavaDoc();
45     }
46     
47     public void setDocumentLocator(Locator locator) {
48     }
49     
50     
51     public void startElement(String JavaDoc ns, String JavaDoc ln, String JavaDoc name, Attributes attrs) throws SAXException {
52         dispatch(true);
53         context.push(new Object JavaDoc[] {name, new org.xml.sax.helpers.AttributesImpl JavaDoc(attrs)});
54         if ("parslet".equals(name)) { // NOI18N
55
handler.handle_parslet(attrs);
56         } else if ("SAX-bindings".equals(name)) { // NOI18N
57
handler.start_SAX_bindings(attrs);
58         } else if ("attbind".equals(name)) { // NOI18N
59
handler.handle_attbind(attrs);
60         } else if ("bind".equals(name)) { // NOI18N
61
handler.start_bind(attrs);
62         }
63     }
64     
65     public void endElement(String JavaDoc ns, String JavaDoc ln, java.lang.String JavaDoc name) throws SAXException {
66         dispatch(false);
67         context.pop();
68         if ("SAX-bindings".equals(name)) { // NOI18N
69
handler.end_SAX_bindings();
70         } else if ("bind".equals(name)) { // NOI18N
71
handler.end_bind();
72         }
73     }
74     
75     public void characters(char[] chars, int start, int len) throws SAXException {
76         buffer.append(chars, start, len);
77     }
78     
79     public void ignorableWhitespace(char[] chars, int start, int len) throws SAXException {
80     }
81     
82     public void processingInstruction(java.lang.String JavaDoc target, java.lang.String JavaDoc data) throws SAXException {
83     }
84     
85     private void dispatch(final boolean fireOnlyIfMixed) throws SAXException {
86         if (fireOnlyIfMixed && buffer.length() == 0) return; //skip it
87

88         Object JavaDoc[] ctx = (Object JavaDoc[]) context.peek();
89         String JavaDoc here = (String JavaDoc) ctx[0];
90         Attributes attrs = (Attributes) ctx[1];
91         buffer.delete(0, buffer.length());
92     }
93     
94     /**
95      * The recognizer entry method taking an InputSource.
96      * @param input InputSource to be parsed.
97      * @throws java.io.IOException on I/O error.
98      * @throws SAXException propagated exception thrown by a DocumentHandler.
99      * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
100      * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
101      */

102     public void parse(final InputSource input) throws SAXException, javax.xml.parsers.ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
103         parse(input, this);
104     }
105     
106     /**
107      * The recognizer entry method taking a URL.
108      * @param url URL source to be parsed.
109      * @throws java.io.IOException on I/O error.
110      * @throws SAXException propagated exception thrown by a DocumentHandler.
111      * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
112      * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
113      */

114     public void parse(final java.net.URL JavaDoc url) throws SAXException, javax.xml.parsers.ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
115         parse(new InputSource(url.toExternalForm()), this);
116     }
117     
118     /**
119      * The recognizer entry method taking an Inputsource.
120      * @param input InputSource to be parsed.
121      * @throws java.io.IOException on I/O error.
122      * @throws SAXException propagated exception thrown by a DocumentHandler.
123      * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
124      * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
125      */

126     public static void parse(final InputSource input, final SAXBindingsHandler handler) throws SAXException, javax.xml.parsers.ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
127         parse(input, new SAXBindingsParser(handler));
128     }
129     
130     /**
131      * The recognizer entry method taking a URL.
132      * @param url URL source to be parsed.
133      * @throws java.io.IOException on I/O error.
134      * @throws SAXException propagated exception thrown by a DocumentHandler.
135      * @throws javax.xml.parsers.ParserConfigurationException a parser satisfining requested configuration can not be created.
136      * @throws javax.xml.parsers.FactoryConfigurationRrror if the implementation can not be instantiated.
137      */

138     public static void parse(final java.net.URL JavaDoc url, final SAXBindingsHandler handler) throws SAXException, javax.xml.parsers.ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
139         parse(new InputSource(url.toExternalForm()), handler);
140     }
141     
142     private static void parse(final InputSource input, final SAXBindingsParser recognizer) throws SAXException, javax.xml.parsers.ParserConfigurationException JavaDoc, java.io.IOException JavaDoc {
143         javax.xml.parsers.SAXParserFactory JavaDoc factory = javax.xml.parsers.SAXParserFactory.newInstance();
144         factory.setValidating(true); //the code was generated according DTD
145
factory.setNamespaceAware(false); //the code was generated according DTD
146
XMLReader parser = factory.newSAXParser().getXMLReader();
147         parser.setContentHandler(recognizer);
148         parser.setErrorHandler(recognizer.getDefaultErrorHandler());
149         parser.parse(input);
150     }
151     
152     private ErrorHandler getDefaultErrorHandler() {
153         return new ErrorHandler() {
154             public void error(SAXParseException ex) throws SAXException {
155                 if (context.isEmpty()) {
156                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Missing DOCTYPE."); // NOI18N
157
}
158                 throw ex;
159             }
160             
161             public void fatalError(SAXParseException ex) throws SAXException {
162                 throw ex;
163             }
164             
165             public void warning(SAXParseException ex) throws SAXException {
166                 // ignore
167
}
168         };
169         
170     }
171     
172 }
173
Popular Tags