KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > wizard > DTDParser


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.xml.core.wizard;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.xml.sax.*;
26 import org.xml.sax.ext.*;
27 import org.xml.sax.helpers.*;
28
29 import org.openide.xml.*;
30 import org.openide.util.Lookup;
31
32 import org.netbeans.api.xml.services.UserCatalog;
33 import org.netbeans.api.xml.parsers.SAXEntityParser;
34
35 /**
36  * Silently produces Set<String> of roots from passed SAX declaration handler events.
37  *
38  * @author Petr Kuzel
39  */

40 final class DTDParser extends DefaultHandler implements DeclHandler {
41
42     static final String JavaDoc SAX_PROPERTY = "http://xml.org/sax/properties/"; // NOI18N
43
static final String JavaDoc DECL_HANDLER = "declaration-handler"; // NOI18N
44

45     private final Set roots = new TreeSet();
46             
47     /** Creates new DTDParser */
48     public DTDParser() {
49     }
50
51     /**
52      * @param in if filled only SID and PID the entity catalog "normalization" is used
53      */

54     public Set parse(InputSource in) {
55
56         Util.THIS.debug("DTDParser started.");
57                 
58         try {
59             // we do not want Crimson, it does not understand relative SYSTEM ids
60
XMLReader parser = XMLUtil.createXMLReader(true);
61             parser.setContentHandler(this);
62             parser.setErrorHandler(this);
63             parser.setProperty(SAX_PROPERTY + DECL_HANDLER, this);
64             
65             // provide fake entity resolver and source
66

67             UserCatalog catalog = UserCatalog.getDefault();
68             EntityResolver res = (catalog == null ? null : catalog.getEntityResolver());
69             
70             if (res != null) parser.setEntityResolver(res);
71             
72             SAXEntityParser dtdParser = new SAXEntityParser(parser, false);
73             dtdParser.parse(in);
74             
75             throw new IllegalStateException JavaDoc("How we can get here?"); // NOI18N
76
} catch (Stop stop) {
77             return roots; // expected
78
} catch (SAXException ex) {
79             Util.THIS.debug("Ignoring SAX ex. while parsing DTD:", ex); // NOI18N
80
if (ex.getException() instanceof RuntimeException JavaDoc) {
81                 Util.THIS.debug("Nested exception:", ex.getException()); // NOI18N
82
}
83             return roots; // better partial result than nothing
84
} catch (IOException ex) {
85             Util.THIS.debug("Ignoring I/O ex. while parsing DTD:", ex); // NOI18N
86
return roots; // better partial result than nothing
87
} finally {
88             Util.THIS.debug("DTDParser stopped."); // NOI18N
89
}
90     }
91             
92     public void elementDecl(String JavaDoc name, String JavaDoc model) throws SAXException {
93         Util.THIS.debug("\telementDecl(" + name + ",...)"); // NOI18N
94
roots.add(name);
95     }
96
97     public void externalEntityDecl(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException {
98     }
99
100     public void attributeDecl(String JavaDoc eName, String JavaDoc aName, String JavaDoc type, String JavaDoc valueDefault, String JavaDoc value) throws SAXException {
101     }
102
103     public void internalEntityDecl(String JavaDoc name, String JavaDoc value) throws SAXException {
104     }
105
106     public void notationDecl (String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException {
107     }
108
109     public void startElement (String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException {
110         Util.THIS.debug("\tstopping parser!"); // NOI18N
111
throw new Stop();
112     }
113
114     private class Stop extends SAXException {
115         
116         private static final long serialVersionUID = -64662796017444980L;
117         
118         Stop() {
119             super("STOP"); //NOI18N
120
}
121         
122         public Throwable JavaDoc fillInStackTrace() {
123             return this;
124         }
125     }
126 }
127
Popular Tags