01: package org.objectweb.celtix.tools.utils;
02:
03: import java.net.URL;
04: import java.util.logging.Logger;
05:
06: import javax.xml.stream.XMLInputFactory;
07: import javax.xml.stream.XMLStreamException;
08: import javax.xml.stream.XMLStreamReader;
09:
10: import org.xml.sax.InputSource;
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 LOG = LogUtils
18: .getL7dLogger(StAXUtil.class);
19: private static final XMLInputFactory XML_INPUT_FACTORY;
20: static {
21: XML_INPUT_FACTORY = XMLInputFactory.newInstance();
22: XML_INPUT_FACTORY.setProperty(
23: XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
24: }
25:
26: private StAXUtil() {
27: }
28:
29: public static void toStartTag(XMLStreamReader r)
30: throws XMLStreamException {
31: while (!r.isStartElement() && r.hasNext()) {
32: r.next();
33: }
34: }
35:
36: public static XMLStreamReader createFreshXMLStreamReader(
37: InputSource source) {
38: try {
39: if (source.getCharacterStream() != null) {
40: return XML_INPUT_FACTORY.createXMLStreamReader(source
41: .getSystemId(), source.getCharacterStream());
42: }
43: if (source.getByteStream() != null) {
44: return XML_INPUT_FACTORY.createXMLStreamReader(source
45: .getSystemId(), source.getByteStream());
46: }
47: return XML_INPUT_FACTORY.createXMLStreamReader(source
48: .getSystemId(), new URL(source.getSystemId())
49: .openStream());
50: } catch (Exception e) {
51: Message msg = new Message("FAIL_TO_CREATE_STAX", LOG);
52: throw new ToolException(msg, e);
53: }
54: }
55: }
|