KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > impl > XSContentHandlerImpl


1 /*
2  * Copyright 2003, 2004 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.ws.jaxme.xs.impl;
18
19 import java.io.IOException JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
22
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24
25 import org.apache.ws.jaxme.xs.XSContentHandler;
26 import org.apache.ws.jaxme.xs.XSSchema;
27 import org.apache.ws.jaxme.xs.parser.XSContext;
28 import org.apache.ws.jaxme.xs.parser.XsSAXParser;
29 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
30 import org.apache.ws.jaxme.xs.xml.XsESchema;
31 import org.apache.ws.jaxme.xs.xml.XsObjectFactory;
32 import org.xml.sax.Attributes JavaDoc;
33 import org.xml.sax.Locator JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35
36
37 /**
38  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
39  */

40 class XSContentHandlerImpl implements XSContentHandler {
41   private final XSLogicalParser parser;
42   private final XsSAXParser xsSAXParser;
43   private boolean finished;
44   private int level = 0;
45   private final String JavaDoc systemId;
46
47   XSContentHandlerImpl(XSLogicalParser pParser, String JavaDoc pSystemId) throws SAXException {
48     parser = pParser;
49     systemId = pSystemId;
50     XSContext data = parser.getData();
51     data.setXSLogicalParser(parser);
52     parser.clearSyntaxSchemas();
53     XsObjectFactory factory = data.getXsObjectFactory();
54     XsESchema mySchema = factory.newXsESchema();
55     parser.setSchema(data.getXSObjectFactory().newXSSchema(data, mySchema));
56     xsSAXParser = factory.newXsSAXParser(mySchema);
57     parser.addSyntaxSchema(mySchema);
58     data.setCurrentContentHandler(xsSAXParser);
59   }
60
61   public XSSchema getXSSchema() {
62     if (!finished) {
63       throw new IllegalStateException JavaDoc("The endDocument() method has not yet been invoked.");
64     }
65     return parser.getSchema();
66   }
67
68   public void startDocument() throws SAXException {
69     xsSAXParser.startDocument();
70   }
71
72   public void endDocument() throws SAXException {
73     xsSAXParser.endDocument();
74     XsESchema syntaxSchema = (XsESchema) xsSAXParser.getBean();
75     parser.removeSyntaxSchema();
76     XSContext data = parser.getData();
77     data.setCurrentContentHandler(null);
78
79     try {
80       parser.parse(syntaxSchema, systemId);
81       XSSchema mySchema = parser.getSchema();
82       parser.createSubstitutionGroups(mySchema);
83       mySchema.validate();
84       finished = true;
85     } catch (IOException JavaDoc e) {
86       throw new SAXException(e);
87     } catch (ParserConfigurationException JavaDoc e) {
88       throw new SAXException(e);
89     } catch (RuntimeException JavaDoc e) {
90       Exception JavaDoc ex = e;
91       for (;;) {
92         UndeclaredThrowableException JavaDoc te = null;
93         Throwable JavaDoc t;
94         if (ex instanceof UndeclaredThrowableException JavaDoc) {
95           te = ((UndeclaredThrowableException JavaDoc) ex);
96           t = te.getUndeclaredThrowable();
97         } else if (ex instanceof InvocationTargetException JavaDoc) {
98           t = ((InvocationTargetException JavaDoc) ex).getTargetException();
99         } else {
100           break;
101         }
102         if (t instanceof Exception JavaDoc) {
103           ex = (Exception JavaDoc) t;
104         } else {
105           if (te == null) {
106             te = new UndeclaredThrowableException JavaDoc(t);
107           }
108           t.printStackTrace();
109           throw te;
110         }
111       }
112       throw new LocSAXException(ex.getClass().getName() + ": " + ex.getMessage(),
113                                  parser.getData().getLocator(), ex);
114     }
115   }
116
117   public void characters(char[] pChars, int pStart, int pLen) throws SAXException {
118     xsSAXParser.characters(pChars, pStart, pLen);
119   }
120
121   public void ignorableWhitespace(char[] pChars, int pStart, int pLen) throws SAXException {
122     xsSAXParser.ignorableWhitespace(pChars, pStart, pLen);
123   }
124
125   public void endPrefixMapping(String JavaDoc pPrefix) throws SAXException {
126     xsSAXParser.endPrefixMapping(pPrefix);
127   }
128
129   public void skippedEntity(String JavaDoc pEntity) throws SAXException {
130     xsSAXParser.skippedEntity(pEntity);
131   }
132
133   public void setDocumentLocator(Locator JavaDoc pLocator) {
134     xsSAXParser.setDocumentLocator(pLocator);
135   }
136
137   public void processingInstruction(String JavaDoc pTarget, String JavaDoc pData) throws SAXException {
138     xsSAXParser.processingInstruction(pTarget, pData);
139   }
140
141   public void startPrefixMapping(String JavaDoc pPrefix, String JavaDoc pURI) throws SAXException {
142     xsSAXParser.startPrefixMapping(pPrefix, pURI);
143   }
144
145   public void endElement(String JavaDoc pNamespaceURI, String JavaDoc pLocalName, String JavaDoc pQName) throws SAXException {
146     xsSAXParser.endElement(pNamespaceURI, pLocalName, pQName);
147     --level;
148   }
149
150   public void startElement(String JavaDoc pNamespaceURI, String JavaDoc pLocalName, String JavaDoc pQName, Attributes JavaDoc pAttrs)
151       throws SAXException {
152     xsSAXParser.startElement(pNamespaceURI, pLocalName, pQName, pAttrs);
153   }
154 }
Popular Tags