KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xml > binding > parser > sax > SaxJBossXBParser


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.xml.binding.parser.sax;
8
9 import org.jboss.xml.binding.parser.JBossXBParser;
10 import org.jboss.xml.binding.Unmarshaller;
11 import org.jboss.xml.binding.JBossXBException;
12 import org.jboss.util.xml.JBossEntityResolver;
13 import org.jboss.logging.Logger;
14 import org.xml.sax.XMLReader JavaDoc;
15 import org.xml.sax.ErrorHandler JavaDoc;
16 import org.xml.sax.SAXParseException JavaDoc;
17 import org.xml.sax.SAXException JavaDoc;
18 import org.xml.sax.Locator JavaDoc;
19 import org.xml.sax.Attributes JavaDoc;
20 import org.xml.sax.InputSource JavaDoc;
21 import org.xml.sax.EntityResolver JavaDoc;
22
23 import javax.xml.parsers.SAXParserFactory JavaDoc;
24 import javax.xml.parsers.SAXParser JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 /**
29  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
30  * @version <tt>$Revision: 1.3.2.2 $</tt>
31  */

32 public class SaxJBossXBParser
33    implements JBossXBParser
34 {
35    private static final Logger log = Logger.getLogger(SaxJBossXBParser.class);
36
37    private final SAXParser JavaDoc parser;
38    private JBossXBParser.ContentHandler contentHandler;
39
40    public SaxJBossXBParser()
41       throws JBossXBException
42    {
43       SAXParserFactory JavaDoc saxFactory = SAXParserFactory.newInstance();
44       saxFactory.setValidating(true);
45       saxFactory.setNamespaceAware(true);
46
47       try
48       {
49          parser = saxFactory.newSAXParser();
50       }
51       catch(Exception JavaDoc e)
52       {
53          throw new JBossXBException("Failed to create a new SAX parser", e);
54       }
55
56       XMLReader JavaDoc reader = null;
57       try
58       {
59          reader = parser.getXMLReader();
60       }
61       catch(SAXException JavaDoc e)
62       {
63          throw new JBossXBException("Failed to get parser's XMLReader", e);
64       }
65
66       reader.setContentHandler(new DelegatingContentHandler());
67       reader.setErrorHandler(new MetaDataErrorHandler());
68       reader.setEntityResolver(new JBossEntityResolver());
69
70       setFeature(Unmarshaller.VALIDATION, true);
71       setFeature(Unmarshaller.SCHEMA_VALIDATION, true);
72       setFeature(Unmarshaller.SCHEMA_FULL_CHECKING, true);
73       setFeature(Unmarshaller.DYNAMIC_VALIDATION, true);
74       setFeature(Unmarshaller.NAMESPACES, true);
75    }
76
77    // JBossXBParser implementation
78

79    public void setEntityResolver(EntityResolver entityResolver)
80       throws JBossXBException
81    {
82       try
83       {
84          parser.getXMLReader().setEntityResolver(entityResolver);
85       }
86       catch(SAXException JavaDoc e)
87       {
88          throw new JBossXBException("Failed to set EntityResolver", e);
89       }
90    }
91
92    public void setProperty(String JavaDoc name, Object JavaDoc value) throws JBossXBException
93    {
94       try
95       {
96          parser.getXMLReader().setProperty(name, value);
97       }
98       catch(SAXException JavaDoc e)
99       {
100          throw new JBossXBException("Failed to get parser's XMLReader", e);
101       }
102    }
103
104    public void setFeature(String JavaDoc name, boolean value) throws JBossXBException
105    {
106       try
107       {
108          parser.getXMLReader().setFeature(name, value);
109       }
110       catch(SAXException JavaDoc e)
111       {
112          throw new JBossXBException("Failed to get parser's XMLReader", e);
113       }
114    }
115
116    public void parse(String JavaDoc systemId, ContentHandler handler) throws JBossXBException
117    {
118       this.contentHandler = handler;
119       try
120       {
121          parser.getXMLReader().parse(systemId);
122       }
123       catch(Exception JavaDoc e)
124       {
125          throw new JBossXBException("Failed to parse source.", e);
126       }
127    }
128
129    public void parse(InputStream JavaDoc is, ContentHandler handler) throws JBossXBException
130    {
131       this.contentHandler = handler;
132       try
133       {
134          parser.getXMLReader().parse(new InputSource JavaDoc(is));
135       }
136       catch(Exception JavaDoc e)
137       {
138          throw new JBossXBException("Failed to parse source.", e);
139       }
140    }
141
142    public void parse(Reader JavaDoc reader, ContentHandler handler) throws JBossXBException
143    {
144       this.contentHandler = handler;
145       try
146       {
147          parser.getXMLReader().parse(new InputSource JavaDoc(reader));
148       }
149       catch(Exception JavaDoc e)
150       {
151          throw new JBossXBException("Failed to parse source.", e);
152       }
153    }
154
155    // Inner
156

157    private final class DelegatingContentHandler
158       implements org.xml.sax.ContentHandler JavaDoc
159    {
160       public void endDocument()
161       {
162       }
163
164       public void startDocument()
165       {
166       }
167
168       public void characters(char ch[], int start, int length)
169       {
170          // todo look at this later
171
// do not notify content handler if these are just whitespaces
172
int i = start;
173          while(i < start + length)
174          {
175             if(!Character.isWhitespace(ch[i++]))
176             {
177                contentHandler.characters(ch, start, length);
178                break;
179             }
180          }
181       }
182
183       public void ignorableWhitespace(char ch[], int start, int length)
184       {
185       }
186
187       public void endPrefixMapping(String JavaDoc prefix)
188       {
189          contentHandler.endPrefixMapping(prefix);
190       }
191
192       public void skippedEntity(String JavaDoc name)
193       {
194       }
195
196       public void setDocumentLocator(Locator JavaDoc locator)
197       {
198       }
199
200       public void processingInstruction(String JavaDoc target, String JavaDoc data)
201       {
202       }
203
204       public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
205       {
206          contentHandler.startPrefixMapping(prefix, uri);
207       }
208
209       public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
210       {
211          contentHandler.endElement(namespaceURI, localName, qName);
212       }
213
214       public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
215       {
216          contentHandler.startElement(namespaceURI, localName, qName, atts, null);
217       }
218    }
219
220    private static final class MetaDataErrorHandler
221       implements ErrorHandler JavaDoc
222    {
223       public void warning(SAXParseException JavaDoc exception)
224       {
225          log.warn(formatMessage(exception));
226       }
227
228       public void error(SAXParseException JavaDoc exception)
229          throws SAXException JavaDoc
230       {
231          throw new SAXException JavaDoc(formatMessage(exception));
232       }
233
234       public void fatalError(SAXParseException JavaDoc exception)
235          throws SAXException JavaDoc
236       {
237          throw new SAXException JavaDoc(formatMessage(exception));
238       }
239
240       public String JavaDoc formatMessage(SAXParseException JavaDoc exception)
241       {
242          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
243          buffer.append(exception.getMessage()).append(" @ ");
244          String JavaDoc location = exception.getPublicId();
245          if(location != null)
246          {
247             buffer.append(location);
248          }
249          else
250          {
251             location = exception.getSystemId();
252             if(location != null)
253             {
254                buffer.append(location);
255             }
256             else
257             {
258                buffer.append("*unknown*");
259             }
260          }
261          buffer.append('[');
262          buffer.append(exception.getLineNumber()).append(',');
263          buffer.append(exception.getColumnNumber()).append(']');
264          return buffer.toString();
265       }
266    }
267 }
268
Popular Tags