001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.main.saveload.xmlparser;
005    
006    import org.xml.sax.*;
007    
008    /**
009     * The class reads XML documents according to specified DTD and
010     * translates all related events into GraphmlHandler events.
011     * <p>Usage sample:
012     * <pre>
013     *    GraphmlParser parser = new GraphmlParser(...);
014     *    parser.parse(new InputSource("..."));
015     * </pre>
016     * <p><b>Warning:</b> the class is machine generated. DO NOT MODIFY</p>
017     */
018    public class GraphmlParser implements ContentHandler {
019    
020        private java.lang.StringBuffer buffer;
021    
022        private GraphmlHandler handler;
023    
024        private java.util.Stack context;
025    
026        private EntityResolver resolver;
027    
028        /**
029         * Creates a parser instance.
030         *
031         * @param handler  handler interface implementation (never <code>null</code>
032         * @param resolver SAX entity resolver implementation or <code>null</code>.
033         *                 It is recommended that it could be able to resolve at least the DTD.
034         */
035        public GraphmlParser(final GraphmlHandler handler, final EntityResolver resolver) {
036    
037            this.handler = handler;
038            this.resolver = resolver;
039            buffer = new StringBuffer(111);
040            context = new java.util.Stack();
041    
042        }
043    
044        /**
045         * This SAX interface method is implemented by the parser.
046         */
047        public final void setDocumentLocator(Locator locator) {
048        }
049    
050        /**
051         * This SAX interface method is implemented by the parser.
052         */
053        public final void startDocument() throws SAXException {
054        }
055    
056        /**
057         * This SAX interface method is implemented by the parser.
058         */
059        public final void endDocument() throws SAXException {
060        }
061    
062        /**
063         * This SAX interface method is implemented by the parser.
064         */
065        public final void startElement(java.lang.String ns, java.lang.String name, java.lang.String qname, Attributes attrs) throws SAXException {
066    
067            dispatch(true);
068            context.push(new Object[]{qname, new org.xml.sax.helpers.AttributesImpl(attrs)});
069            if ("edge".equals(qname)) {
070                handler.start_edge(attrs);
071            } else if ("locator".equals(qname)) {
072                handler.handle_locator(attrs);
073            } else if ("node".equals(qname)) {
074                handler.start_node(attrs);
075            } else if ("graph".equals(qname)) {
076                handler.start_graph(attrs);
077            } else if ("endpoint".equals(qname)) {
078                handler.start_endpoint(attrs);
079            } else if ("graphml".equals(qname)) {
080                handler.start_graphml(attrs);
081            } else if ("hyperedge".equals(qname)) {
082                handler.start_hyperedge(attrs);
083            } else if ("port".equals(qname)) {
084                handler.start_port(attrs);
085            }
086        }
087    
088        /**
089         * This SAX interface method is implemented by the parser.
090         */
091        public final void endElement(java.lang.String ns, java.lang.String name, java.lang.String qname) throws SAXException {
092    
093            dispatch(false);
094            context.pop();
095            if ("edge".equals(qname)) {
096                handler.end_edge();
097            } else if ("node".equals(qname)) {
098                handler.end_node();
099            } else if ("graph".equals(qname)) {
100                handler.end_graph();
101            } else if ("endpoint".equals(qname)) {
102                handler.end_endpoint();
103            } else if ("graphml".equals(qname)) {
104                handler.end_graphml();
105            } else if ("hyperedge".equals(qname)) {
106                handler.end_hyperedge();
107            } else if ("port".equals(qname)) {
108                handler.end_port();
109            }
110        }
111    
112        /**
113         * This SAX interface method is implemented by the parser.
114         */
115        public final void characters(char[] chars, int start, int len) throws SAXException {
116    
117            buffer.append(chars, start, len);
118        }
119    
120        /**
121         * This SAX interface method is implemented by the parser.
122         */
123        public final void ignorableWhitespace(char[] chars, int start, int len) throws SAXException {
124        }
125    
126        /**
127         * This SAX interface method is implemented by the parser.
128         */
129        public final void processingInstruction(java.lang.String target, java.lang.String data) throws SAXException {
130        }
131    
132        /**
133         * This SAX interface method is implemented by the parser.
134         */
135        public final void startPrefixMapping(final java.lang.String prefix, final java.lang.String uri) throws SAXException {
136        }
137    
138        /**
139         * This SAX interface method is implemented by the parser.
140         */
141        public final void endPrefixMapping(final java.lang.String prefix) throws SAXException {
142        }
143    
144        /**
145         * This SAX interface method is implemented by the parser.
146         */
147        public final void skippedEntity(java.lang.String name) throws SAXException {
148        }
149    
150        private void dispatch(final boolean fireOnlyIfMixed) throws SAXException {
151    
152            if (fireOnlyIfMixed && buffer.length() == 0) return; //skip it
153    
154            Object[] ctx = (Object[]) context.peek();
155            String here = (String) ctx[0];
156            Attributes attrs = (Attributes) ctx[1];
157            if ("key".equals(here)) {
158                if (fireOnlyIfMixed) throw new IllegalStateException("Unexpected characters() event! (Missing DTD?)");
159                handler.handle_key(buffer.length() == 0 ? null : buffer.toString(), attrs);
160            } else if ("data".equals(here)) {
161                if (fireOnlyIfMixed) throw new IllegalStateException("Unexpected characters() event! (Missing DTD?)");
162                handler.handle_data(buffer.length() == 0 ? null : buffer.toString(), attrs);
163            } else if ("desc".equals(here)) {
164                if (fireOnlyIfMixed) throw new IllegalStateException("Unexpected characters() event! (Missing DTD?)");
165                handler.handle_desc(buffer.length() == 0 ? null : buffer.toString(), attrs);
166            } else {
167                //do not care
168            }
169            buffer.delete(0, buffer.length());
170        }
171    
172        /**
173         * The recognizer entry method taking an InputSource.
174         *
175         * @param input InputSource to be parsed.
176         * @throws java.io.IOException on I/O error.
177         * @throws SAXException        propagated exception thrown by a DocumentHandler.
178         * @throws javax.xml.parsers.ParserConfigurationException
179         *                             a parser satisfining requested configuration can not be created.
180         * @throws javax.xml.parsers.FactoryConfigurationRrror
181         *                             if the implementation can not be instantiated.
182         */
183        public void parse(final InputSource input) throws SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException {
184    
185            parse(input, this);
186        }
187    
188        /**
189         * The recognizer entry method taking a URL.
190         *
191         * @param url URL source to be parsed.
192         * @throws java.io.IOException on I/O error.
193         * @throws SAXException        propagated exception thrown by a DocumentHandler.
194         * @throws javax.xml.parsers.ParserConfigurationException
195         *                             a parser satisfining requested configuration can not be created.
196         */
197        public void parse(final java.net.URL url) throws SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException {
198    
199            parse(new InputSource(url.toExternalForm()), this);
200        }
201    
202        /**
203         * The recognizer entry method taking an Inputsource.
204         *
205         * @param input InputSource to be parsed.
206         * @throws java.io.IOException on I/O error.
207         * @throws SAXException        propagated exception thrown by a DocumentHandler.
208         * @throws javax.xml.parsers.ParserConfigurationException
209         *                             a parser satisfining requested configuration can not be created.
210         * @throws javax.xml.parsers.FactoryConfigurationRrror
211         *                             if the implementation can not be instantiated.
212         */
213        public static void parse(final InputSource input, final GraphmlHandler handler) throws SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException {
214    
215            parse(input, new GraphmlParser(handler, null));
216        }
217    
218        /**
219         * The recognizer entry method taking a URL.
220         *
221         * @param url URL source to be parsed.
222         * @throws java.io.IOException on I/O error.
223         * @throws SAXException        propagated exception thrown by a DocumentHandler.
224         * @throws javax.xml.parsers.ParserConfigurationException
225         *                             a parser satisfining requested configuration can not be created.
226         * @throws javax.xml.parsers.FactoryConfigurationRrror
227         *                             if the implementation can not be instantiated.
228         */
229        public static void parse(final java.net.URL url, final GraphmlHandler handler) throws SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException {
230    
231            parse(new InputSource(url.toExternalForm()), handler);
232        }
233    
234        private static void parse(final InputSource input, final GraphmlParser recognizer) throws SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException {
235    
236            javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
237            factory.setValidating(false);  //the code was generated according DTD
238            factory.setNamespaceAware(false);  //the code was generated according DTD
239            XMLReader parser = factory.newSAXParser().getXMLReader();
240            parser.setContentHandler(recognizer);
241            parser.setErrorHandler(recognizer.getDefaultErrorHandler());
242            if (recognizer.resolver != null) parser.setEntityResolver(recognizer.resolver);
243            parser.parse(input);
244        }
245    
246        /**
247         * Creates default error handler used by this parser.
248         *
249         * @return org.xml.sax.ErrorHandler implementation
250         */
251        protected ErrorHandler getDefaultErrorHandler() {
252    
253            return new ErrorHandler() {
254                public void error(SAXParseException ex) throws SAXException {
255                    if (context.isEmpty()) System.err.println("Missing DOCTYPE.");
256                    throw ex;
257                }
258    
259                public void fatalError(SAXParseException ex) throws SAXException {
260                    throw ex;
261                }
262    
263                public void warning(SAXParseException ex) throws SAXException {
264                    // ignore
265                }
266            };
267    
268        }
269    }