KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding.parser.sax;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27
28 import javax.xml.parsers.SAXParser JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
30 import org.jboss.logging.Logger;
31 import org.jboss.util.JBossStringBuilder;
32 import org.jboss.util.xml.JBossEntityResolver;
33 import org.jboss.xb.binding.JBossXBException;
34 import org.jboss.xb.binding.JBossXBRuntimeException;
35 import org.jboss.xb.binding.parser.JBossXBParser;
36 import org.xml.sax.Attributes JavaDoc;
37 import org.xml.sax.EntityResolver JavaDoc;
38 import org.xml.sax.ErrorHandler JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.Locator JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.SAXParseException JavaDoc;
43 import org.xml.sax.XMLReader JavaDoc;
44
45 /**
46  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
47  * @version <tt>$Revision: 2098 $</tt>
48  */

49 public class SaxJBossXBParser
50    implements JBossXBParser
51 {
52    private static final Logger log = Logger.getLogger(SaxJBossXBParser.class);
53
54    private static final SAXParserFactory JavaDoc saxFactory = SAXParserFactory.newInstance();
55    static
56    {
57       saxFactory.setValidating(true);
58       saxFactory.setNamespaceAware(true);
59       enableXInclude();
60    }
61
62    private final XMLReader JavaDoc reader;
63    private JBossXBParser.ContentHandler contentHandler;
64    private DelegatingContentHandler delegateHandler;
65    private boolean trace;
66
67    /**
68     * Enables XInclude if the saxFactory supports it.<p>
69     *
70     * NOTE: Checks the real factory class, not the JAXP interface.
71     */

72    private static void enableXInclude()
73    {
74       try
75       {
76          Class JavaDoc clazz = saxFactory.getClass();
77          Method JavaDoc method = clazz.getMethod("setXIncludeAware", new Class JavaDoc[] { Boolean.TYPE });
78          method.invoke(saxFactory, new Object JavaDoc[] { Boolean.TRUE });
79       }
80       catch (Exception JavaDoc e)
81       {
82          log.trace("Not setting XIncludeAware", e);
83       }
84    }
85    
86    public SaxJBossXBParser()
87       throws JBossXBException
88    {
89       SAXParser JavaDoc parser;
90       try
91       {
92          parser = saxFactory.newSAXParser();
93       }
94       catch(Exception JavaDoc e)
95       {
96          throw new JBossXBException("Failed to create a new SAX parser", e);
97       }
98
99       try
100       {
101          reader = parser.getXMLReader();
102       }
103       catch(SAXException JavaDoc e1)
104       {
105          throw new JBossXBRuntimeException("Failed to get parser's XMLReader", e1);
106       }
107
108       delegateHandler = new DelegatingContentHandler();
109       reader.setContentHandler(delegateHandler);
110       reader.setErrorHandler(MetaDataErrorHandler.INSTANCE);
111       reader.setEntityResolver(new JBossEntityResolver());
112
113 /*
114       setFeature(Unmarshaller.VALIDATION, true);
115       setFeature(Unmarshaller.SCHEMA_VALIDATION, true);
116       setFeature(Unmarshaller.SCHEMA_FULL_CHECKING, true);
117       setFeature(Unmarshaller.DYNAMIC_VALIDATION, true);
118       setFeature(Unmarshaller.NAMESPACES, true);
119 */

120    }
121
122    // JBossXBParser implementation
123

124    public void setEntityResolver(EntityResolver entityResolver)
125       throws JBossXBException
126    {
127       reader.setEntityResolver(entityResolver);
128    }
129
130    public void setProperty(String JavaDoc name, Object JavaDoc value)
131    {
132       try
133       {
134          reader.setProperty(name, value);
135       }
136       catch(SAXException JavaDoc e)
137       {
138          throw new JBossXBRuntimeException("Failed to set property on the XML reader", e);
139       }
140    }
141
142    public void setFeature(String JavaDoc name, boolean value)
143    {
144       try
145       {
146          reader.setFeature(name, value);
147       }
148       catch(SAXException JavaDoc e)
149       {
150          throw new JBossXBRuntimeException("Failed to set feature on the XMLReader", e);
151       }
152    }
153
154    public void parse(String JavaDoc systemId, ContentHandler handler) throws JBossXBException
155    {
156       this.contentHandler = handler;
157       trace = log.isTraceEnabled();
158       try
159       {
160          reader.parse(systemId);
161       }
162       catch(Exception JavaDoc e)
163       {
164          throw new JBossXBException("Failed to parse source: " + getLocationAsString(systemId), e);
165       }
166    }
167
168    public void parse(InputStream JavaDoc is, ContentHandler handler) throws JBossXBException
169    {
170       this.contentHandler = handler;
171       trace = log.isTraceEnabled();
172       try
173       {
174          reader.parse(new InputSource JavaDoc(is));
175       }
176       catch(Exception JavaDoc e)
177       {
178          throw new JBossXBException("Failed to parse source: " + e.getMessage(), e);
179       }
180    }
181
182    public void parse(Reader JavaDoc reader, ContentHandler handler) throws JBossXBException
183    {
184       this.contentHandler = handler;
185       trace = log.isTraceEnabled();
186       try
187       {
188          this.reader.parse(new InputSource JavaDoc(reader));
189       }
190       catch(Exception JavaDoc e)
191       {
192          throw new JBossXBException("Failed to parse source: " + e.getMessage(), e);
193       }
194    }
195
196    public String JavaDoc getLocationAsString(String JavaDoc fileName)
197    {
198       Locator JavaDoc locator = delegateHandler.getDocumentLocator();
199       if (locator == null)
200          return fileName;
201       else
202       {
203          JBossStringBuilder buffer = new JBossStringBuilder();
204          String JavaDoc id = locator.getSystemId();
205          if (id == null)
206             id = locator.getPublicId();
207          buffer.append(id).append('@');
208          buffer.append(locator.getLineNumber());
209          buffer.append(',');
210          buffer.append(locator.getColumnNumber());
211          return buffer.toString();
212       }
213    }
214
215    // Inner
216

217    private final class DelegatingContentHandler
218       implements org.xml.sax.ContentHandler JavaDoc
219    {
220       Locator JavaDoc locator;
221       
222       public void endDocument()
223       {
224       }
225
226       public void startDocument()
227       {
228       }
229
230       public void characters(char ch[], int start, int length)
231       {
232          // todo look at this later
233
// do not notify content handler if these are just whitespaces
234
int i = start;
235          while(i < start + length)
236          {
237             if(!Character.isWhitespace(ch[i++]))
238             {
239                contentHandler.characters(ch, start, length);
240                break;
241             }
242          }
243       }
244
245       public void ignorableWhitespace(char ch[], int start, int length)
246       {
247       }
248
249       public void endPrefixMapping(String JavaDoc prefix)
250       {
251          contentHandler.endPrefixMapping(prefix);
252       }
253
254       public void skippedEntity(String JavaDoc name)
255       {
256       }
257
258       public Locator JavaDoc getDocumentLocator()
259       {
260          return locator;
261       }
262       
263       public void setDocumentLocator(Locator JavaDoc locator)
264       {
265          this.locator = locator;
266       }
267
268       public void processingInstruction(String JavaDoc target, String JavaDoc data)
269       {
270          contentHandler.processingInstruction(target, data);
271       }
272
273       public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
274       {
275          contentHandler.startPrefixMapping(prefix, uri);
276       }
277
278       public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
279       {
280          String JavaDoc name = null;
281          if(trace)
282          {
283             if(localName.length() == 0)
284             {
285                name = qName;
286             }
287             else
288             {
289                name = namespaceURI + ':' + localName;
290             }
291             log.trace("Enter endElement " + name);
292          }
293          try
294          {
295             contentHandler.endElement(namespaceURI, localName, qName);
296          }
297          finally
298          {
299             if(trace)
300             {
301                log.trace("Exit endElement " + name);
302             }
303          }
304       }
305
306       public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
307       {
308          String JavaDoc name = null;
309          if(trace)
310          {
311             if(localName.length() == 0)
312             {
313                name = qName;
314             }
315             else
316             {
317                name = namespaceURI + ':' + localName;
318             }
319             log.trace("Enter startElement " + name);
320          }
321          try
322          {
323             contentHandler.startElement(namespaceURI, localName, qName, atts, null);
324          }
325          finally
326          {
327             if(trace)
328             {
329                log.trace("Exit startElement " + name);
330             }
331          }
332       }
333    }
334
335    private static final class MetaDataErrorHandler
336       implements ErrorHandler JavaDoc
337    {
338       public static final ErrorHandler JavaDoc INSTANCE = new MetaDataErrorHandler();
339
340       public void warning(SAXParseException JavaDoc exception)
341       {
342          log.warn(formatMessage(exception));
343       }
344
345       public void error(SAXParseException JavaDoc exception)
346          throws SAXException JavaDoc
347       {
348          throw new SAXException JavaDoc(formatMessage(exception));
349       }
350
351       public void fatalError(SAXParseException JavaDoc exception)
352          throws SAXException JavaDoc
353       {
354          throw new SAXException JavaDoc(formatMessage(exception));
355       }
356
357       public String JavaDoc formatMessage(SAXParseException JavaDoc exception)
358       {
359          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
360          buffer.append(exception.getMessage()).append(" @ ");
361          String JavaDoc location = exception.getPublicId();
362          if(location != null)
363          {
364             buffer.append(location);
365          }
366          else
367          {
368             location = exception.getSystemId();
369             if(location != null)
370             {
371                buffer.append(location);
372             }
373             else
374             {
375                buffer.append("*unknown*");
376             }
377          }
378          buffer.append('[');
379          buffer.append(exception.getLineNumber()).append(',');
380          buffer.append(exception.getColumnNumber()).append(']');
381          return buffer.toString();
382       }
383    }
384 }
385
Popular Tags