1 package org.objectweb.celtix.tools.utils; 2 3 import java.net.URL ; 4 import java.util.logging.Logger ; 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 ; 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.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 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 (source.getSystemId()).openStream()); 45 } catch (Exception e) { 46 Message msg = new Message("FAIL_TO_CREATE_STAX", LOG); 47 throw new ToolException(msg, e); 48 } 49 } 50 } 51 | Popular Tags |