KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > content > XMLRootHandler


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.content;
12
13 import java.io.IOException JavaDoc;
14 import java.io.StringReader JavaDoc;
15 import javax.xml.parsers.*;
16 import org.xml.sax.*;
17 import org.xml.sax.ext.LexicalHandler JavaDoc;
18 import org.xml.sax.helpers.DefaultHandler JavaDoc;
19
20 /**
21  * A content describer for detecting the name of the top-level element of the
22  * DTD system identifier in an XML file. This supports two parameters:
23  * <code>DTD_TO_FIND</code> and <code>ELEMENT_TO_FIND</code>. This is done
24  * using the <code>IExecutableExtension</code> mechanism. If the
25  * <code>":-"</code> method is used, then the value is treated as the
26  * <code>ELEMENT_TO_FIND</code>.
27  *
28  * @since 3.0
29  */

30 public final class XMLRootHandler extends DefaultHandler JavaDoc implements LexicalHandler JavaDoc {
31     /**
32      * An exception indicating that the parsing should stop. This is usually
33      * triggered when the top-level element has been found.
34      *
35      * @since 3.0
36      */

37     private class StopParsingException extends SAXException {
38         /**
39          * All serializable objects should have a stable serialVersionUID
40          */

41         private static final long serialVersionUID = 1L;
42
43         /**
44          * Constructs an instance of <code>StopParsingException</code> with a
45          * <code>null</code> detail message.
46          */

47         public StopParsingException() {
48             super((String JavaDoc) null);
49         }
50     }
51
52     /**
53      * Should we check the root element?
54      */

55     private boolean checkRoot;
56     /**
57      * The system identifier for the DTD that was found while parsing the XML.
58      * This member variable is <code>null</code> unless the file has been
59      * parsed successful to the point of finding the DTD's system identifier.
60      */

61     private String JavaDoc dtdFound = null;
62     /**
63      * This is the name of the top-level element found in the XML file. This
64      * member variable is <code>null</code> unless the file has been parsed
65      * successful to the point of finding the top-level element.
66      */

67     private String JavaDoc elementFound = null;
68
69     public XMLRootHandler(boolean checkRoot) {
70         this.checkRoot = checkRoot;
71     }
72
73     /*
74      * (non-Javadoc)
75      *
76      * @see org.xml.sax.ext.LexicalHandler#comment(char[], int, int)
77      */

78     public final void comment(final char[] ch, final int start, final int length) {
79         // Not interested.
80
}
81
82     /**
83      * Creates a new SAX parser for use within this instance.
84      *
85      * @return The newly created parser.
86      *
87      * @throws ParserConfigurationException
88      * If a parser of the given configuration cannot be created.
89      * @throws SAXException
90      * If something in general goes wrong when creating the parser.
91      * @throws SAXNotRecognizedException
92      * If the <code>XMLReader</code> does not recognize the
93      * lexical handler configuration option.
94      * @throws SAXNotSupportedException
95      * If the <code>XMLReader</code> does not support the lexical
96      * handler configuration option.
97      */

98     private final SAXParser createParser(SAXParserFactory parserFactory) throws ParserConfigurationException, SAXException, SAXNotRecognizedException, SAXNotSupportedException {
99         // Initialize the parser.
100
final SAXParser parser = parserFactory.newSAXParser();
101         final XMLReader reader = parser.getXMLReader();
102         reader.setProperty("http://xml.org/sax/properties/lexical-handler", this); //$NON-NLS-1$
103
// disable DTD validation (bug 63625)
104
try {
105             // be sure validation is "off" or the feature to ignore DTD's will not apply
106
reader.setFeature("http://xml.org/sax/features/validation", false); //$NON-NLS-1$
107
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$
108
} catch (SAXNotRecognizedException e) {
109             // not a big deal if the parser does not recognize the features
110
} catch (SAXNotSupportedException e) {
111             // not a big deal if the parser does not support the features
112
}
113         return parser;
114     }
115
116     /*
117      * (non-Javadoc)
118      *
119      * @see org.xml.sax.ext.LexicalHandler#endCDATA()
120      */

121     public final void endCDATA() {
122         // Not interested.
123
}
124
125     /*
126      * (non-Javadoc)
127      *
128      * @see org.xml.sax.ext.LexicalHandler#endDTD()
129      */

130     public final void endDTD() {
131         // Not interested.
132
}
133
134     /*
135      * (non-Javadoc)
136      *
137      * @see org.xml.sax.ext.LexicalHandler#endEntity(java.lang.String)
138      */

139     public final void endEntity(final String JavaDoc name) {
140         // Not interested.
141
}
142
143     public String JavaDoc getDTD() {
144         return dtdFound;
145     }
146
147     public String JavaDoc getRootName() {
148         return elementFound;
149     }
150
151     public boolean parseContents(InputSource contents) throws IOException JavaDoc, ParserConfigurationException, SAXException {
152         // Parse the file into we have what we need (or an error occurs).
153
try {
154             SAXParserFactory factory = Activator.getDefault().getFactory();
155             if (factory == null)
156                 return false;
157             final SAXParser parser = createParser(factory);
158             // to support external entities specified as relative URIs (see bug 63298)
159
contents.setSystemId("/"); //$NON-NLS-1$
160
parser.parse(contents, this);
161         } catch (StopParsingException e) {
162             // Abort the parsing normally. Fall through...
163
}
164         return true;
165     }
166
167     /*
168      * Resolve external entity definitions to an empty string. This is to speed
169      * up processing of files with external DTDs. Not resolving the contents
170      * of the DTD is ok, as only the System ID of the DTD declaration is used.
171      * @see org.xml.sax.helpers.DefaultHandler#resolveEntity(java.lang.String, java.lang.String)
172      */

173     public InputSource resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException {
174         return new InputSource(new StringReader JavaDoc("")); //$NON-NLS-1$
175
}
176
177     /*
178      * (non-Javadoc)
179      *
180      * @see org.xml.sax.ext.LexicalHandler#startCDATA()
181      */

182     public final void startCDATA() {
183         // Not interested.
184
}
185
186     /*
187      * (non-Javadoc)
188      *
189      * @see org.xml.sax.ext.LexicalHandler#startDTD(java.lang.String,
190      * java.lang.String, java.lang.String)
191      */

192     public final void startDTD(final String JavaDoc name, final String JavaDoc publicId, final String JavaDoc systemId) throws SAXException {
193         dtdFound = systemId;
194         // If we don't care about the top-level element, we can stop here.
195
if (!checkRoot)
196             throw new StopParsingException();
197     }
198
199     /*
200      * (non-Javadoc)
201      *
202      * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
203      * java.lang.String, java.lang.String, org.xml.sax.Attributes)
204      */

205     public final void startElement(final String JavaDoc uri, final String JavaDoc elementName, final String JavaDoc qualifiedName, final Attributes attributes) throws SAXException {
206         elementFound = elementName;
207         throw new StopParsingException();
208     }
209
210     /*
211      * (non-Javadoc)
212      *
213      * @see org.xml.sax.ext.LexicalHandler#startEntity(java.lang.String)
214      */

215     public final void startEntity(final String JavaDoc name) {
216         // Not interested.
217
}
218 }
219
Popular Tags