KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > traversers > SchemaContentHandler


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.xs.traversers;
18
19 import org.apache.xerces.impl.xs.opti.SchemaDOMParser;
20 import org.apache.xerces.util.NamespaceSupport;
21 import org.apache.xerces.util.SAXLocatorWrapper;
22 import org.apache.xerces.util.SymbolTable;
23 import org.apache.xerces.util.XMLAttributesImpl;
24 import org.apache.xerces.util.XMLSymbols;
25 import org.apache.xerces.xni.NamespaceContext;
26 import org.apache.xerces.xni.QName;
27 import org.apache.xerces.xni.XMLString;
28 import org.apache.xerces.xni.XNIException;
29 import org.apache.xerces.xni.parser.XMLParseException;
30 import org.w3c.dom.Document JavaDoc;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.ContentHandler JavaDoc;
33 import org.xml.sax.Locator JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.SAXParseException JavaDoc;
36 import org.xml.sax.helpers.LocatorImpl JavaDoc;
37
38 /**
39  * <p>SchemaContentHandler converts SAX events into XNI
40  * and passes them directly to the SchemaDOMParser.</p>
41  *
42  * @xerces.internal
43  *
44  * @author Michael Glavassevich, IBM
45  * @author Jack Z. Wang, IBM
46  *
47  * @version $Id: SchemaContentHandler.java,v 1.2 2005/05/30 03:20:40 mrglavas Exp $
48  */

49 final class SchemaContentHandler implements ContentHandler JavaDoc {
50     
51     /** Symbol table **/
52     private SymbolTable fSymbolTable;
53     
54     /** SchemaDOMParser, events will be delegated to SchemaDOMParser to pass */
55     private SchemaDOMParser fSchemaDOMParser;
56     
57     /** XML Locator wrapper for SAX. **/
58     private final SAXLocatorWrapper fSAXLocatorWrapper = new SAXLocatorWrapper();
59     
60     /** The namespace context of this document: stores namespaces in scope */
61     private NamespaceSupport fNamespaceContext = new NamespaceSupport();
62     
63     /** Indicate if push NamespaceContest is needed */
64     private boolean fNeedPushNSContext;
65     
66     /** Flag used to track whether namespace declarations are reported as attributes. */
67     private boolean fNamespacePrefixes = false;
68     
69     /** Flag used to track whether XML names and Namespace URIs have been internalized. */
70     private boolean fStringsInternalized = false;
71     
72     /** Fields for start element, end element and characters. */
73     private final QName fElementQName = new QName();
74     private final QName fAttributeQName = new QName();
75     private final XMLAttributesImpl fAttributes = new XMLAttributesImpl();
76     private final XMLString fTempString = new XMLString();
77     
78     /**
79      * <p>Constructs an SchemaContentHandler.</p>
80      */

81     public SchemaContentHandler() {}
82     
83     /*
84      * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
85      */

86     public Document JavaDoc getDocument() {
87         return fSchemaDOMParser.getDocument();
88     }
89     
90     /*
91      * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
92      */

93     public void setDocumentLocator(Locator JavaDoc locator) {
94         fSAXLocatorWrapper.setLocator(locator);
95     }
96     
97     /*
98      * @see org.xml.sax.ContentHandler#startDocument()
99      */

100     public void startDocument() throws SAXException JavaDoc {
101         fNeedPushNSContext = true;
102         try {
103             fSchemaDOMParser.startDocument(fSAXLocatorWrapper, null, fNamespaceContext, null);
104         }
105         catch (XMLParseException e) {
106             convertToSAXParseException(e);
107         }
108         catch (XNIException e) {
109             convertToSAXException(e);
110         }
111     }
112     
113     /*
114      * @see org.xml.sax.ContentHandler#endDocument()
115      */

116     public void endDocument() throws SAXException JavaDoc {
117         fSAXLocatorWrapper.setLocator(null);
118         try {
119             fSchemaDOMParser.endDocument(null);
120         }
121         catch (XMLParseException e) {
122             convertToSAXParseException(e);
123         }
124         catch (XNIException e) {
125             convertToSAXException(e);
126         }
127     }
128     
129     /*
130      * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
131      */

132     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
133         if (fNeedPushNSContext) {
134             fNeedPushNSContext = false;
135             fNamespaceContext.pushContext();
136         }
137         if (!fStringsInternalized) {
138             prefix = (prefix != null) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING;
139             uri = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null;
140         }
141         else {
142             if (prefix == null) {
143                 prefix = XMLSymbols.EMPTY_STRING;
144             }
145             if (uri != null && uri.length() == 0) {
146                 uri = null;
147             }
148         }
149         fNamespaceContext.declarePrefix(prefix, uri);
150     }
151     
152     /*
153      * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
154      */

155     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
156         // do nothing
157
}
158     
159     /*
160      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
161      */

162     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
163         if (fNeedPushNSContext) {
164             fNamespaceContext.pushContext();
165         }
166         fNeedPushNSContext = true;
167         
168         // Fill element QName and XMLAttributes
169
fillQName(fElementQName, uri, localName, qName);
170         fillXMLAttributes(atts);
171         
172         // Add namespace declarations if necessary
173
if (!fNamespacePrefixes) {
174             final int prefixCount = fNamespaceContext.getDeclaredPrefixCount();
175             if (prefixCount > 0) {
176                 addNamespaceDeclarations(prefixCount);
177             }
178         }
179         
180         try {
181             fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
182         }
183         catch (XMLParseException e) {
184             convertToSAXParseException(e);
185         }
186         catch (XNIException e) {
187             convertToSAXException(e);
188         }
189     }
190     
191     /*
192      * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
193      */

194     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
195         fillQName(fElementQName, uri, localName, qName);
196         try {
197             fSchemaDOMParser.endElement(fElementQName, null);
198         }
199         catch (XMLParseException e) {
200             convertToSAXParseException(e);
201         }
202         catch (XNIException e) {
203             convertToSAXException(e);
204         }
205         finally {
206             fNamespaceContext.popContext();
207         }
208     }
209     
210     /*
211      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
212      */

213     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
214         try {
215             fTempString.setValues(ch, start, length);
216             fSchemaDOMParser.characters(fTempString, null);
217         }
218         catch (XMLParseException e) {
219             convertToSAXParseException(e);
220         }
221         catch (XNIException e) {
222             convertToSAXException(e);
223         }
224     }
225     
226     /*
227      * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
228      */

229     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException JavaDoc {
230         try {
231             fTempString.setValues(ch, start, length);
232             fSchemaDOMParser.ignorableWhitespace(fTempString, null);
233         }
234         catch (XMLParseException e) {
235             convertToSAXParseException(e);
236         }
237         catch (XNIException e) {
238             convertToSAXException(e);
239         }
240     }
241     
242     /*
243      * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
244      */

245     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
246         try {
247             fTempString.setValues(data.toCharArray(), 0, data.length());
248             fSchemaDOMParser.processingInstruction(target, fTempString, null);
249         }
250         catch (XMLParseException e) {
251             convertToSAXParseException(e);
252         }
253         catch (XNIException e) {
254             convertToSAXException(e);
255         }
256     }
257     
258     /*
259      * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
260      */

261     public void skippedEntity(String JavaDoc arg) throws SAXException JavaDoc {
262         // do-nothing
263
}
264     
265     /*
266      * Other methods
267      */

268     
269     private void fillQName(QName toFill, String JavaDoc uri, String JavaDoc localpart, String JavaDoc rawname) {
270         if (!fStringsInternalized) {
271             uri = (uri != null && uri.length() > 0) ? fSymbolTable.addSymbol(uri) : null;
272             localpart = (localpart != null) ? fSymbolTable.addSymbol(localpart) : XMLSymbols.EMPTY_STRING;
273             rawname = (rawname != null) ? fSymbolTable.addSymbol(rawname) : XMLSymbols.EMPTY_STRING;
274         }
275         else {
276             if (uri != null && uri.length() == 0) {
277                 uri = null;
278             }
279             if (localpart == null) {
280                 localpart = XMLSymbols.EMPTY_STRING;
281             }
282             if (rawname == null) {
283                 rawname = XMLSymbols.EMPTY_STRING;
284             }
285         }
286         String JavaDoc prefix = XMLSymbols.EMPTY_STRING;
287         int prefixIdx = rawname.indexOf(':');
288         if (prefixIdx != -1) {
289             prefix = fSymbolTable.addSymbol(rawname.substring(0, prefixIdx));
290             // local part may be an empty string if this is a namespace declaration
291
if (localpart == XMLSymbols.EMPTY_STRING) {
292                 localpart = fSymbolTable.addSymbol(rawname.substring(prefixIdx + 1));
293             }
294         }
295         // local part may be an empty string if this is a namespace declaration
296
else if (localpart == XMLSymbols.EMPTY_STRING) {
297             localpart = rawname;
298         }
299         toFill.setValues(prefix, localpart, rawname, uri);
300     }
301     
302     private void fillXMLAttributes(Attributes JavaDoc atts) {
303         fAttributes.removeAllAttributes();
304         final int attrCount = atts.getLength();
305         for (int i = 0; i < attrCount; ++i) {
306             fillQName(fAttributeQName, atts.getURI(i), atts.getLocalName(i), atts.getQName(i));
307             String JavaDoc type = atts.getType(i);
308             fAttributes.addAttributeNS(fAttributeQName, (type != null) ? type : XMLSymbols.fCDATASymbol, atts.getValue(i));
309             fAttributes.setSpecified(i, true);
310         }
311     }
312     
313     private void addNamespaceDeclarations(final int prefixCount) {
314         String JavaDoc prefix = null;
315         String JavaDoc localpart = null;
316         String JavaDoc rawname = null;
317         String JavaDoc nsPrefix = null;
318         String JavaDoc nsURI = null;
319         for (int i = 0; i < prefixCount; ++i) {
320             nsPrefix = fNamespaceContext.getDeclaredPrefixAt(i);
321             nsURI = fNamespaceContext.getURI(nsPrefix);
322             if (nsPrefix.length() > 0) {
323                 prefix = XMLSymbols.PREFIX_XMLNS;
324                 localpart = nsPrefix;
325                 rawname = fSymbolTable.addSymbol(prefix + ":" + localpart);
326             }
327             else {
328                 prefix = XMLSymbols.EMPTY_STRING;
329                 localpart = XMLSymbols.PREFIX_XMLNS;
330                 rawname = XMLSymbols.PREFIX_XMLNS;
331             }
332             fAttributeQName.setValues(prefix, localpart, rawname, NamespaceContext.XMLNS_URI);
333             fAttributes.addAttribute(fAttributeQName, XMLSymbols.fCDATASymbol, nsURI);
334         }
335     }
336     
337     public void reset(SchemaDOMParser schemaDOMParser, SymbolTable symbolTable,
338             boolean namespacePrefixes, boolean stringsInternalized) {
339         fSchemaDOMParser = schemaDOMParser;
340         fSymbolTable = symbolTable;
341         fNamespacePrefixes = namespacePrefixes;
342         fStringsInternalized = stringsInternalized;
343     }
344     
345     /*
346      * Static methods
347      */

348     
349     static void convertToSAXParseException(XMLParseException e) throws SAXException JavaDoc {
350         Exception JavaDoc ex = e.getException();
351         if (ex == null) {
352             // must be a parser exception; mine it for locator info and throw
353
// a SAXParseException
354
LocatorImpl JavaDoc locatorImpl = new LocatorImpl JavaDoc();
355             locatorImpl.setPublicId(e.getPublicId());
356             locatorImpl.setSystemId(e.getExpandedSystemId());
357             locatorImpl.setLineNumber(e.getLineNumber());
358             locatorImpl.setColumnNumber(e.getColumnNumber());
359             throw new SAXParseException JavaDoc(e.getMessage(), locatorImpl);
360         }
361         if (ex instanceof SAXException JavaDoc) {
362             // why did we create an XMLParseException?
363
throw (SAXException JavaDoc) ex;
364         }
365         throw new SAXException JavaDoc(ex);
366     }
367     
368     static void convertToSAXException(XNIException e) throws SAXException JavaDoc {
369         Exception JavaDoc ex = e.getException();
370         if (ex == null) {
371             throw new SAXException JavaDoc(e.getMessage());
372         }
373         if (ex instanceof SAXException JavaDoc) {
374             throw (SAXException JavaDoc) ex;
375         }
376         throw new SAXException JavaDoc(ex);
377     }
378     
379 } // SchemaContentHandler
380
Popular Tags