KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > utils > StAXUtil


1 package org.objectweb.celtix.tools.utils;
2
3 import java.net.URL JavaDoc;
4 import java.util.logging.Logger JavaDoc;
5
6 import javax.xml.stream.XMLInputFactory;
7 import javax.xml.stream.XMLStreamException;
8 import javax.xml.stream.XMLStreamReader;
9
10 import org.xml.sax.InputSource JavaDoc;
11
12 import org.objectweb.celtix.common.i18n.Message;
13 import org.objectweb.celtix.common.logging.LogUtils;
14 import org.objectweb.celtix.tools.common.ToolException;
15
16 public final class StAXUtil {
17     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(StAXUtil.class);
18     private static final XMLInputFactory XML_INPUT_FACTORY;
19     static {
20         XML_INPUT_FACTORY = XMLInputFactory.newInstance();
21         XML_INPUT_FACTORY.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
22     }
23
24     private StAXUtil() {
25     }
26
27     public static void toStartTag(XMLStreamReader r) throws XMLStreamException {
28         while (!r.isStartElement() && r.hasNext()) {
29             r.next();
30         }
31     }
32
33     public static XMLStreamReader createFreshXMLStreamReader(InputSource JavaDoc source) {
34         try {
35             if (source.getCharacterStream() != null) {
36                 return XML_INPUT_FACTORY.createXMLStreamReader(source.getSystemId(),
37                                                              source.getCharacterStream());
38             }
39             if (source.getByteStream() != null) {
40                 return XML_INPUT_FACTORY.createXMLStreamReader(source.getSystemId(),
41                                                              source.getByteStream());
42             }
43             return XML_INPUT_FACTORY.createXMLStreamReader(source.getSystemId(),
44                                                          new URL JavaDoc(source.getSystemId()).openStream());
45         } catch (Exception JavaDoc e) {
46             Message msg = new Message("FAIL_TO_CREATE_STAX", LOG);
47             throw new ToolException(msg, e);
48         }
49     }
50 }
51
Popular Tags