Java Code Examples for org.codehaus.stax2.XMLInputFactory2#createXMLStreamReader()

The following examples show how to use org.codehaus.stax2.XMLInputFactory2#createXMLStreamReader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlSource.java    From beam with Apache License 2.0 6 votes vote down vote up
private void setUpXMLParser(ReadableByteChannel channel, byte[] lookAhead) throws IOException {
  try {
    // We use Woodstox because the StAX implementation provided by OpenJDK reports
    // character locations incorrectly. Note that Woodstox still currently reports *byte*
    // locations incorrectly when parsing documents that contain multi-byte characters.
    XMLInputFactory2 xmlInputFactory = (XMLInputFactory2) XMLInputFactory.newInstance();
    this.parser =
        xmlInputFactory.createXMLStreamReader(
            new SequenceInputStream(
                new ByteArrayInputStream(lookAhead), Channels.newInputStream(channel)),
            getCurrentSource().configuration.getCharset());

    // Current offset should be the offset before reading the record element.
    while (true) {
      int event = parser.next();
      if (event == XMLStreamConstants.START_ELEMENT) {
        String localName = parser.getLocalName();
        if (localName.equals(getCurrentSource().configuration.getRecordElement())) {
          break;
        }
      }
    }
  } catch (FactoryConfigurationError | XMLStreamException e) {
    throw new IOException(e);
  }
}
 
Example 2
Source File: TestStreamResult.java    From woodstox with Apache License 2.0 6 votes vote down vote up
/**
 * This test is related to problem reported as [WSTX-182], inability
 * to use SystemId alone as source.
 */
public void testCreateUsingSystemId()
    throws IOException, XMLStreamException
{
    File tmpF = File.createTempFile("staxtest", ".xml");
    tmpF.deleteOnExit();

    XMLOutputFactory f = getOutputFactory();
    StreamResult dst = new StreamResult();
    dst.setSystemId(tmpF);
    XMLStreamWriter sw = f.createXMLStreamWriter(dst);

    sw.writeStartDocument();
    sw.writeEmptyElement("root");
    sw.writeEndDocument();
    sw.close();

    // plus let's read and check it
    XMLInputFactory2 inf = getInputFactory();
    XMLStreamReader sr = inf.createXMLStreamReader(tmpF);
    assertTokenType(START_ELEMENT, sr.next());
    assertTokenType(END_ELEMENT, sr.next());
    sr.close();
}
 
Example 3
Source File: TestCopyEventFromReader97.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testUTF8MsLinefeedCopyEvent() throws Exception
{
    final XMLInputFactory2 xmlIn = getInputFactory();
    final XMLOutputFactory2 xmlOut = getOutputFactory();
    InputStream in = getClass().getResource("issue97.xml").openStream();

    ByteArrayOutputStream bogus = new ByteArrayOutputStream();
    XMLStreamReader2 reader = (XMLStreamReader2) xmlIn.createXMLStreamReader(in);
    XMLStreamWriter2 writer = (XMLStreamWriter2) xmlOut.createXMLStreamWriter(bogus, "UTF-8");
    while (reader.hasNext()) {
       reader.next();
       writer.copyEventFromReader(reader, false);
    }

    in.close();
}
 
Example 4
Source File: TestWsdlValidation.java    From woodstox with Apache License 2.0 6 votes vote down vote up
public void testWsdlValidation() throws Exception {
	String runMe = System.getProperty("testWsdlValidation");
	if (runMe == null || "".equals(runMe)) {
		return;
	}
	XMLInputFactory2 factory = getInputFactory();
	XMLStreamReader2 reader = (XMLStreamReader2) factory.createXMLStreamReader(getClass().getResourceAsStream("test-message.xml"), "utf-8");
	QName msgQName = new QName("http://server.hw.demo/", "sayHi");
	while (true) {
		int what = reader.nextTag();
		if (what == XMLStreamConstants.START_ELEMENT) {
			if (reader.getName().equals(msgQName)) {
				reader.validateAgainst(schema);
			}
		} else if (what == XMLStreamConstants.END_ELEMENT) {
			if (reader.getName().equals(msgQName)) {
				reader.stopValidatingAgainst(schema);
			}
		} else if (what == XMLStreamConstants.END_DOCUMENT) {
			break;
		}
	}
}
 
Example 5
Source File: XmlDumpParser.java    From wikiforia with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor used by Multistream parser
 * @param header   parsed header
 * @param xmlInput parallel input stream
 */
public XmlDumpParser(Header header, InputStream xmlInput) {
    try {
        this.header = header;

        XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory2.newInstance();
        factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
        factory.setProperty(WstxInputProperties.P_INPUT_PARSING_MODE, WstxInputProperties.PARSING_MODE_FRAGMENT);

        xmlReader = (XMLStreamReader2)factory.createXMLStreamReader(xmlInput);

    } catch (XMLStreamException e) {
        throw new IOError(e);
    }
}
 
Example 6
Source File: XmlDumpParser.java    From wikiforia with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Standalone constructor
 * @param xmlInput the stream to read from
 */
public XmlDumpParser(InputStream xmlInput) {
    try {
        XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory2.newInstance();
        factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
        factory.setProperty(WstxInputProperties.P_INPUT_PARSING_MODE, WstxInputProperties.PARSING_MODE_FRAGMENT);

        xmlReader = (XMLStreamReader2) factory.createXMLStreamReader(xmlInput);
        this.header = readHeader(xmlReader);
    } catch (XMLStreamException e) {
        throw new IOError(e);
    }
}
 
Example 7
Source File: MultistreamBzip2XmlDumpParser.java    From wikiforia with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Header parsing code
 * @param xml the header xml
 * @return true if match
 * @throws javax.xml.stream.XMLStreamException
 */
public static Header parseHeader(String xml) throws XMLStreamException
{
    XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory2.newInstance();
    factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    BufferedReader buffreader = new BufferedReader(new StringReader(xml));

    XMLStreamReader2 xmlReader = (XMLStreamReader2)factory.createXMLStreamReader(buffreader);
    return XmlDumpParser.readHeader(xmlReader);
}