org.codehaus.stax2.XMLStreamReader2 Java Examples
The following examples show how to use
org.codehaus.stax2.XMLStreamReader2.
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: TestCopyEventFromReader97.java From woodstox with Apache License 2.0 | 6 votes |
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 #2
Source File: Stax2ValidationUtils.java From cxf with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} * * @throws XMLStreamException */ public boolean setupValidation(XMLStreamReader reader, Endpoint endpoint, ServiceInfo serviceInfo) throws XMLStreamException { // Gosh, this is bad, but I don't know a better solution, unless we're willing // to require the stax2 API no matter what. XMLStreamReader effectiveReader = reader; if (effectiveReader instanceof DepthXMLStreamReader) { effectiveReader = ((DepthXMLStreamReader) reader).getReader(); } final XMLStreamReader2 reader2 = (XMLStreamReader2) effectiveReader; XMLValidationSchema vs = getValidator(endpoint, serviceInfo); if (vs == null) { return false; } reader2.setValidationProblemHandler(new ValidationProblemHandler() { public void reportProblem(XMLValidationProblem problem) throws XMLValidationException { throw new Fault(new Message("READ_VALIDATION_ERROR", LOG, problem.getMessage()), Fault.FAULT_CODE_CLIENT); } }); reader2.validateAgainst(vs); return true; }
Example #3
Source File: TestExtLocationInfo91.java From woodstox with Apache License 2.0 | 6 votes |
private XMLStreamReader2 getReader(String contents, String systemId, XMLResolver xmlResolver) throws XMLStreamException { XMLInputFactory f = getInputFactory(); if(xmlResolver != null){ f.setXMLResolver(xmlResolver); } setCoalescing(f, false); // shouldn't really matter setNamespaceAware(f, true); setSupportExternalEntities(f, true); setReplaceEntities(f, true); // No need to validate, just need entities setValidating(f, false); return (XMLStreamReader2) f.createXMLStreamReader(new StreamSource(new StringReader(contents), systemId)); }
Example #4
Source File: INRIALeMondeCorpusStaxHandler.java From grobid-ner with Apache License 2.0 | 6 votes |
/** * When the document is closed, add an empty line as document separator. */ @Override public void onEndElement(XMLStreamReader2 reader) { if (reader.getName().getLocalPart().equals("document")) { try { writer.write(sb.append("\n").toString()); } catch (IOException e) { throw new RuntimeException(); } sb = new StringBuilder(); } else if (reader.getName().getLocalPart().equals("sentence")) { inSentence = false; sb.append("\n"); } else if (reader.getName().getLocalPart().equals("ENAMEX")) { inNamedEntity = false; entityType = null; entitySubType = null; } }
Example #5
Source File: WstxInputFactory.java From woodstox with Apache License 2.0 | 6 votes |
@SuppressWarnings("resource") protected XMLStreamReader2 createSR(SystemId systemId, InputStream in, String enc, boolean forER, boolean autoCloseInput) throws XMLStreamException { // sanity check: if (in == null) { throw new IllegalArgumentException("Null InputStream is not a valid argument"); } ReaderConfig cfg = createPrivateConfig(); if (enc == null || enc.length() == 0) { return createSR(cfg, systemId, StreamBootstrapper.getInstance (null, systemId, in), forER, autoCloseInput); } /* !!! 17-Feb-2006, TSa: We don't yet know if it's xml 1.0 or 1.1; * so have to specify 1.0 (which is less restrictive WRT input * streams). Would be better to let bootstrapper deal with it * though: */ Reader r = DefaultInputResolver.constructOptimizedReader(cfg, in, false, enc); return createSR(cfg, systemId, ReaderBootstrapper.getInstance (null, systemId, r, enc), forER, autoCloseInput); }
Example #6
Source File: TestWsdlValidation.java From woodstox with Apache License 2.0 | 6 votes |
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 #7
Source File: TestDOMArrayReader.java From woodstox with Apache License 2.0 | 6 votes |
@Override protected XMLStreamReader2 getReader(String contents) throws XMLStreamException { try { XMLInputFactory f = getInputFactory(); setCoalescing(f, false); // shouldn't really matter setNamespaceAware(f, true); // First, need to parse using JAXP DOM: DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(new StringReader(contents))); return (XMLStreamReader2) f.createXMLStreamReader(new DOMSource(doc)); } catch (Exception e) { throw new XMLStreamException(e); } }
Example #8
Source File: INRIALeMondeCorpusStaxHandler.java From grobid-ner with Apache License 2.0 | 6 votes |
/** * How to use it * <p> * This class require a single parameter which is the input file containng the french * corpus from Le Monde manually annotated. * <p> * The class will output the cONLL 2013 format in a file having the same name as the input * suffixed with .output. */ public static void main(String[] args) throws IOException, XMLStreamException { if (args.length == 0) { System.out.println("Missing input file. First parameter."); System.exit(-1); } WstxInputFactory inputFactory = new WstxInputFactory(); Writer writer = new FileWriter(args[0] + ".output"); INRIALeMondeCorpusStaxHandler inriaLeMondeCorpusStaxHandler = new INRIALeMondeCorpusStaxHandler(writer); InputStream is = new FileInputStream(args[0]); XMLStreamReader2 reader = (XMLStreamReader2) inputFactory.createXMLStreamReader(is); StaxUtils.traverse(reader, inriaLeMondeCorpusStaxHandler); writer.close(); }
Example #9
Source File: TestXmlId.java From woodstox with Apache License 2.0 | 6 votes |
private XMLStreamReader2 getReader(String contents, boolean xmlidEnabled, boolean nsAware, boolean coal) throws XMLStreamException { XMLInputFactory2 f = getInputFactory(); setSupportDTD(f, true); setValidating(f, false); setCoalescing(f, coal); setNamespaceAware(f, nsAware); f.setProperty(XMLInputFactory2.XSP_SUPPORT_XMLID, xmlidEnabled ? XMLInputFactory2.XSP_V_XMLID_TYPING : XMLInputFactory2.XSP_V_XMLID_NONE); return constructStreamReader(f, contents); }
Example #10
Source File: TestDOMBinaryReader.java From woodstox with Apache License 2.0 | 6 votes |
@Override protected XMLStreamReader2 getReader(String contents) throws XMLStreamException { try { XMLInputFactory f = getInputFactory(); setCoalescing(f, false); // shouldn't really matter setNamespaceAware(f, true); // First, need to parse using JAXP DOM: DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(new StringReader(contents))); return (XMLStreamReader2) f.createXMLStreamReader(new DOMSource(doc)); } catch (Exception e) { throw new XMLStreamException(e); } }
Example #11
Source File: WstxInputFactory.java From woodstox with Apache License 2.0 | 6 votes |
@SuppressWarnings("resource") protected XMLStreamReader2 createSR(File f, boolean forER, boolean autoCloseInput) throws XMLStreamException { ReaderConfig cfg = createPrivateConfig(); try { /* 18-Nov-2008, TSa: If P_BASE_URL is set, and File reference is * relative, let's resolve against base... */ if (!f.isAbsolute()) { URL base = cfg.getBaseURL(); if (base != null) { URL src = new URL(base, f.getPath()); return createSR(cfg, SystemId.construct(src), URLUtil.inputStreamFromURL(src), forER, autoCloseInput); } } final SystemId systemId = SystemId.construct(URLUtil.toURL(f)); return createSR(cfg, systemId, new FileInputStream(f), forER, autoCloseInput); } catch (IOException ie) { throw new WstxIOException(ie); } }
Example #12
Source File: BaseStreamTest.java From woodstox with Apache License 2.0 | 5 votes |
protected static String getStreamingText(XMLStreamReader sr) throws IOException, XMLStreamException { StringWriter sw = new StringWriter(); ((XMLStreamReader2) sr).getText(sw, false); return sw.toString(); }
Example #13
Source File: TestExtLocationInfo91.java From woodstox with Apache License 2.0 | 5 votes |
private void assertOffset(XMLStreamReader2 sr, int startOffset, int endOffset, String systemId) throws XMLStreamException { LocationInfo li = sr.getLocationInfo(); Location startLoc = li.getStartLocation(); assertEquals("Incorrect starting systemID for event " + tokenTypeDesc(sr.getEventType()), systemId, startLoc.getSystemId()); Location endLoc = li.getEndLocation(); assertEquals("Incorrect ending systemID for event " + tokenTypeDesc(sr.getEventType()), systemId, endLoc.getSystemId()); assertOffset(sr, startOffset, endOffset); }
Example #14
Source File: TestStreaming.java From woodstox with Apache License 2.0 | 5 votes |
private XMLStreamReader2 getReader(String contents, boolean coalesce) throws XMLStreamException { WstxInputFactory f = getWstxInputFactory(); f.getConfig().doSupportNamespaces(true); f.getConfig().doCoalesceText(coalesce); f.getConfig().setInputBufferLength(16); f.getConfig().setShortestReportedTextSegment(4); return constructStreamReader(f, contents); }
Example #15
Source File: TestDTD.java From woodstox with Apache License 2.0 | 5 votes |
public void testFullValidationOk() throws XMLStreamException { String XML = "<root attr='123'><leaf /></root>"; XMLValidationSchema schema = parseDTDSchema(SIMPLE_DTD); XMLStreamReader2 sr = getReader(XML); sr.validateAgainst(schema); while (sr.next() != END_DOCUMENT) { } sr.close(); }
Example #16
Source File: TestDTD.java From woodstox with Apache License 2.0 | 5 votes |
public void testFullValidationIssue23() throws XMLStreamException { final String INPUT_DTD = "<!ELEMENT FreeFormText (#PCDATA) >\n" +"<!ATTLIST FreeFormText xml:lang CDATA #IMPLIED >\n" ; String XML = "<FreeFormText xml:lang='en-US'>foobar</FreeFormText>"; XMLInputFactory f = getInputFactory(); /* Resolver resolver = new XMLResolver() { @Override public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) { return new StringReader(DTD); } }; f.setXMLResolver(resolver); */ XMLValidationSchemaFactory schemaFactory = XMLValidationSchemaFactory.newInstance(XMLValidationSchema.SCHEMA_ID_DTD); XMLValidationSchema schema = schemaFactory.createSchema(new StringReader(INPUT_DTD)); XMLStreamReader2 sr = (XMLStreamReader2)f.createXMLStreamReader( new StringReader(XML)); sr.validateAgainst(schema); while (sr.next() != END_DOCUMENT) { /* System.err.println("Curr == "+sr.getEventType()); if (sr.getEventType() == CHARACTERS) { System.err.println(" text: "+sr.getText()); } */ } sr.close(); }
Example #17
Source File: TestDTD.java From woodstox with Apache License 2.0 | 5 votes |
/** * And then a test for validating starting when stream points * to START_ELEMENT */ public void testPartialValidationOk() throws XMLStreamException { String XML = "<root attr='123'><leaf /></root>"; XMLValidationSchema schema = parseDTDSchema(SIMPLE_DTD); XMLStreamReader2 sr = getReader(XML); assertTokenType(START_ELEMENT, sr.next()); sr.validateAgainst(schema); while (sr.next() != END_DOCUMENT) { } sr.close(); }
Example #18
Source File: TestDTD.java From woodstox with Apache License 2.0 | 5 votes |
private XMLStreamReader2 getReader(String contents) throws XMLStreamException { XMLInputFactory f = getInputFactory(); setValidating(f, false); return constructStreamReader(f, contents); }
Example #19
Source File: TestExtLocationInfo91.java From woodstox with Apache License 2.0 | 5 votes |
public void testLocationsWithExtEntity() throws XMLStreamException { XMLResolver resolver = new XMLResolver() { @Override public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException { if (INCL_URI.equals(systemID)){ StreamSource src = new StreamSource(new StringReader(TEST_EXT_ENT_INCL), systemID); return src; } fail("Unexpected systemID to resolve: " + systemID); return null; } }; XMLStreamReader2 sr = getReader(TEST_EXT_ENT, URI, resolver); assertOffset(sr, 0, 21); assertTokenType(DTD, sr.next()); assertTokenType(START_ELEMENT, sr.next()); assertEquals("main", sr.getLocalName()); assertOffset(sr, 77, 83, URI); assertTokenType(START_ELEMENT, sr.next()); assertEquals("include", sr.getLocalName()); assertOffset(sr, 0, 9, INCL_URI); assertTokenType(END_ELEMENT, sr.next()); assertOffset(sr, 9, 19, INCL_URI); assertTokenType(END_ELEMENT, sr.next()); assertOffset(sr, 89, 96, URI); sr.close(); }
Example #20
Source File: TestExtLocationInfo91.java From woodstox with Apache License 2.0 | 5 votes |
private void assertOffset(XMLStreamReader2 sr, int startOffset, int endOffset) throws XMLStreamException { LocationInfo li = sr.getLocationInfo(); Location startLoc = li.getStartLocation(); assertEquals("Incorrect starting offset for event "+tokenTypeDesc(sr.getEventType()), startOffset, startLoc.getCharacterOffset()); Location endLoc = li.getEndLocation(); assertEquals("Incorrect ending offset for event "+tokenTypeDesc(sr.getEventType()), endOffset, endLoc.getCharacterOffset()); }
Example #21
Source File: INRIALeMondeCorpusStaxHandler.java From grobid-ner with Apache License 2.0 | 5 votes |
/** * -DOCSTART- followed by the document id is appended to define the start of a new document */ @Override public void onStartElement(XMLStreamReader2 reader) { final String localName = reader.getName().getLocalPart(); if (localName.equals("document")) { inDocument = true; sb.append("-DOCSTART- ").append(reader.getAttributeValue("", "id")).append("\n"); } else if (localName.equals("sentence")) { inSentence = true; } else if (localName.equals("ENAMEX")) { inNamedEntity = true; readOtherAttributes(reader); } }
Example #22
Source File: BaseStax2ValidationTest.java From woodstox with Apache License 2.0 | 5 votes |
protected void verifyFailure(String xml, XMLValidationSchema schema, String failMsg, String failPhrase, boolean strict) throws XMLStreamException { XMLStreamReader2 sr = constructStreamReader(getInputFactory(), xml); sr.validateAgainst(schema); try { while (sr.hasNext()) { /* int type = */sr.next(); } fail("Expected validity exception for " + failMsg); } catch (XMLValidationException vex) { String origMsg = vex.getMessage(); String msg = (origMsg == null) ? "" : origMsg.toLowerCase(); if (msg.indexOf(failPhrase.toLowerCase()) < 0) { String actualMsg = "Expected validation exception for " + failMsg + ", containing phrase '" + failPhrase + "': got '" + origMsg + "'"; if (strict) { fail(actualMsg); } warn("suppressing failure due to MSV bug, failure: '" + actualMsg + "'"); } // should get this specific type; not basic stream exception } catch (XMLStreamException sex) { fail("Expected XMLValidationException for " + failMsg + "; instead got " + sex.getMessage()); } }
Example #23
Source File: TestXmlId.java From woodstox with Apache License 2.0 | 5 votes |
private XMLStreamReader2 getValidatingReader(String contents, boolean nsAware, boolean coal) throws XMLStreamException { XMLInputFactory2 f = getInputFactory(); setSupportDTD(f, true); setValidating(f, true); setCoalescing(f, coal); setNamespaceAware(f, nsAware); f.setProperty(XMLInputFactory2.XSP_SUPPORT_XMLID, XMLInputFactory2.XSP_V_XMLID_TYPING); return constructStreamReader(f, contents); }
Example #24
Source File: INRIALeMondeCorpusStaxHandler.java From grobid-ner with Apache License 2.0 | 5 votes |
private void readOtherAttributes(XMLStreamReader2 reader) { entityType = getAttributeFiltered(reader, "type"); if (isNotBlank(entityType)) { uri = getAttributeFiltered(reader, "uri"); entitySubType = getAttributeFiltered(reader, "sub_type"); disambiguatedName = getAttributeFiltered(reader, "name"); // if (StringUtils.equals(entityType, "Person")) { // gender = getAttributeFiltered(reader, "gender"); // } } }
Example #25
Source File: TestStreaming.java From woodstox with Apache License 2.0 | 5 votes |
public void testCDataStreaming() throws IOException, XMLStreamException { String CONTENT_INOUT = "Some content\nthat will be stored in a\n" +"CDATA <yes!> Block <[*]>\n" +" yet not be split in any way...." ; /* Let's also add trailing text, to ensure no coalescing is done * when not requested */ String XML = "<root><![CDATA[" + CONTENT_INOUT + "]]>some text!</root>"; XMLStreamReader2 sr = getReader(XML, false); assertTokenType(START_ELEMENT, sr.next()); assertTokenType(CDATA, sr.next()); StringWriter sw = new StringWriter(); sr.getText(sw, false); String act = sw.toString(); if (!act.equals(CONTENT_INOUT)) { if (CONTENT_INOUT.startsWith(act)) { fail("Streaming text accessors returned partial match; first " +act.length()+" chars of the expected " +CONTENT_INOUT.length()+" chars"); } fail("Content accessed using streaming text accessor (len " +act.length()+"; exp "+CONTENT_INOUT.length()+" chars) wrong: " +"expected ["+CONTENT_INOUT+"], got ["+act+"]"); } // And should get the following CHARACTERS then: assertTokenType(CHARACTERS, sr.next()); // and then closing element; let's not check text contents here assertTokenType(END_ELEMENT, sr.next()); }
Example #26
Source File: TestXmlId.java From woodstox with Apache License 2.0 | 5 votes |
private void doTestInvalidDisabled(boolean nsAware, boolean coal) throws XMLStreamException { /* In non-validating mode, shouldn't matter: but just to make sure, * let's also disable xml:id processing */ XMLStreamReader2 sr = getReader(XML_WITH_XMLID_INVALID, false, nsAware, coal); assertTokenType(DTD, sr.next()); assertTokenType(START_ELEMENT, sr.next()); }
Example #27
Source File: XmlDumpParser.java From wikiforia with GNU General Public License v2.0 | 5 votes |
/** * 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 #28
Source File: TestXmlId.java From woodstox with Apache License 2.0 | 5 votes |
private void doTestInvalid(boolean nsAware, boolean coal) throws XMLStreamException { XMLStreamReader2 sr = getValidatingReader(XML_WITH_XMLID_INVALID, nsAware, coal); try { assertTokenType(DTD, sr.next()); assertTokenType(START_ELEMENT, sr.next()); fail("Expected a validation exception for invalid Xml:id attribute declaration"); } catch (XMLValidationException vex) { //System.err.println("VLD exc -> "+vex); } }
Example #29
Source File: W3CSchemaWrite23Test.java From woodstox with Apache License 2.0 | 5 votes |
public void testSchemaValidatingCopy23() throws Exception { final String SCHEMA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<xs:schema elementFormDefault=\"unqualified\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" + " <xs:element name=\"Document\">\n" + " <xs:complexType>\n" + " <xs:sequence>\n" + " <xs:element name=\"Paragraph\" type=\"xs:string\"/>\n" + " </xs:sequence>\n" + " </xs:complexType>\n" + " </xs:element>\n" + "</xs:schema>"; final String CONTENT = "<Document>\n" + " <Paragraph>Hello world!</Paragraph>\n" + "</Document>"; final String DOC = "<?xml version='1.0' encoding='UTF-8'?>\n"+CONTENT; StringWriter strw = new StringWriter(); XMLStreamWriter2 xmlWriter = getSchemaValidatingWriter(strw, SCHEMA, false); XMLStreamReader2 xmlReader = constructNsStreamReader(DOC, false); // For this test we need validation, otherwise the reader returns characters events instead of white-space events. xmlReader.validateAgainst(parseW3CSchema(SCHEMA)); while (xmlReader.hasNext()) { /*int type =*/ xmlReader.next(); xmlWriter.copyEventFromReader(xmlReader, true); } xmlWriter.close(); xmlReader.close(); String xml = strw.toString(); if (!xml.contains(CONTENT)) { fail("Should contain ["+CONTENT+"], does not: ["+xml+"]"); } }
Example #30
Source File: TestFactories.java From woodstox with Apache License 2.0 | 5 votes |
public void testPropertiesStreamReader() throws XMLStreamException { XMLInputFactory f = getInputFactory(); XMLStreamReader2 r = (XMLStreamReader2) f.createXMLStreamReader(new StringReader("<root></root>")); // First, verify property is indeed unsupported assertFalse(r.isPropertySupported(NO_SUCH_PROPERTY)); /* Ok: as of Woodstox 4.0, behavior is such that no exception is thrown, * because javadocs do not indicate that it should be done (save for case * where property name is null). Whether this is right interpretation or not * is open to discussion; but for now we will verify that behavior does not * change from 4.0 without explicit decision. */ /* try { Object ob = r.getProperty(NO_SUCH_PROPERTY); fail("Expected exception, instead got result: "+ob); } catch (IllegalArgumentException e) { verifyException(e, NO_SUCH_PROPERTY); } */ Object ob = r.getProperty(NO_SUCH_PROPERTY); assertNull(ob); // And although setter is specified by Stax2, it too fails on unrecognized: try { r.setProperty(NO_SUCH_PROPERTY, "foobar"); fail("Expected exception"); } catch (IllegalArgumentException e) { verifyException(e, NO_SUCH_PROPERTY); } }