Java Code Examples for javax.xml.stream.XMLInputFactory#createFilteredReader()
The following examples show how to use
javax.xml.stream.XMLInputFactory#createFilteredReader() .
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: StaEDIXMLStreamReaderTest.java From staedi with Apache License 2.0 | 5 votes |
@Test void testReadXml_WithOptionalInterchangeServiceRequests_TransactionOnly() throws Exception { EDIInputFactory ediFactory = EDIInputFactory.newFactory(); XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); InputStream stream = getClass().getResourceAsStream("/x12/optionalInterchangeServices.edi"); ediFactory.setProperty(EDIInputFactory.XML_DECLARE_TRANSACTION_XMLNS, Boolean.TRUE); EDIStreamReader reader = ediFactory.createEDIStreamReader(stream); EDIStreamReader filtered = ediFactory.createFilteredReader(reader, r -> true); XMLStreamReader xmlReader = ediFactory.createXMLStreamReader(filtered); xmlReader.next(); // Per StAXSource JavaDoc, put in START_DOCUMENT state XMLStreamReader xmlCursor = xmlFactory.createFilteredReader(xmlReader, r -> { boolean startTx = (r.getEventType() == XMLStreamConstants.START_ELEMENT && r.getName().getLocalPart().equals("TRANSACTION")); if (!startTx) { Logger.getGlobal().info("Skipping event: " + r.getEventType() + "; " + (r.getEventType() == XMLStreamConstants.START_ELEMENT || r.getEventType() == XMLStreamConstants.END_ELEMENT ? r.getName() : "")); } return startTx; }); xmlCursor.hasNext(); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter result = new StringWriter(); transformer.transform(new StAXSource(xmlReader), new StreamResult(result)); String resultString = result.toString(); System.out.println(resultString); Diff d = DiffBuilder.compare(Input.fromFile("src/test/resources/x12/optionalInterchangeServices_transactionOnly.xml")) .withTest(resultString).build(); assertTrue(!d.hasDifferences(), () -> "XML unexpectedly different:\n" + d.toString(new DefaultComparisonFormatter())); }
Example 2
Source File: PresetGeometries.java From lams with GNU General Public License v2.0 | 5 votes |
@SuppressWarnings("unused") public void init(InputStream is) throws XMLStreamException, JAXBException { // StAX: EventFilter startElementFilter = new EventFilter() { @Override public boolean accept(XMLEvent event) { return event.isStartElement(); } }; XMLInputFactory staxFactory = StaxHelper.newXMLInputFactory(); XMLEventReader staxReader = staxFactory.createXMLEventReader(is); XMLEventReader staxFiltRd = staxFactory.createFilteredReader(staxReader, startElementFilter); // ignore StartElement: /* XMLEvent evDoc = */ staxFiltRd.nextEvent(); // JAXB: JAXBContext jaxbContext = JAXBContext.newInstance(BINDING_PACKAGE); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); long cntElem = 0; while (staxFiltRd.peek() != null) { StartElement evRoot = (StartElement)staxFiltRd.peek(); String name = evRoot.getName().getLocalPart(); JAXBElement<CTCustomGeometry2D> el = unmarshaller.unmarshal(staxReader, CTCustomGeometry2D.class); CTCustomGeometry2D cus = el.getValue(); cntElem++; if(containsKey(name)) { LOG.log(POILogger.WARN, "Duplicate definition of " + name); } put(name, new CustomGeometry(cus)); } }
Example 3
Source File: AttributeLocalNameTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test public void testOne() { try { XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createFilteredReader(factory.createXMLStreamReader(new StringReader(XML)), new Filter()); reader.next(); reader.hasNext(); // force filter to cache Assert.assertTrue(reader.getAttributeLocalName(0) != null); } catch (Exception e) { e.printStackTrace(); Assert.fail("Unexpected Exception: " + e.getMessage()); } }
Example 4
Source File: HasNextTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private XMLStreamReader createStreamReader(HasNextTypeFilter f) { try { XMLInputFactory factory = XMLInputFactory.newInstance(); factory = XMLInputFactory.newInstance(); return factory.createFilteredReader(factory.createXMLStreamReader(this.getClass().getResourceAsStream(INPUT_FILE)), (StreamFilter) f); } catch (Exception e) { e.printStackTrace(); Assert.fail("Unexpected Exception: " + e.getMessage()); return null; } }
Example 5
Source File: Bug6976938Test.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void eventReaderTest(XMLInputFactory xif) { XMLEventReader eventReader = null; try { eventReader = xif.createXMLEventReader(this.getClass().getResourceAsStream(INPUT_FILE)); XMLEventReader filteredEventReader = xif.createFilteredReader(eventReader, new EventFilter() { public boolean accept(XMLEvent event) { if (!event.isStartElement()) { return false; } QName elementQName = event.asStartElement().getName(); if ((elementQName.getLocalPart().equals(ATTACHMENT_NAME.getLocalPart()) || elementQName.getLocalPart().equals("Attachment")) && elementQName.getNamespaceURI().equals(VF_GENERIC_TT_NAMESPACE)) { return true; } return false; } }); if (filteredEventReader.hasNext()) { System.out.println("containsAttachments() returns true"); } } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } finally { if (eventReader != null) { try { eventReader.close(); } catch (XMLStreamException xse) { // Ignored by intention } } } }
Example 6
Source File: EventFilterSupportTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private static XMLEventReader createXmlReader(int max) throws XMLStreamException { TestInputStream ts = new TestInputStream(max); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLEventReader reader = xif.createXMLEventReader(ts); return xif.createFilteredReader(reader, new TagFilter(max)); }
Example 7
Source File: JAXBEncoderDecoderTest.java From cxf with Apache License 2.0 | 5 votes |
@Test public void testUnmarshallFromStaxStreamReader() throws Exception { QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName()); MessagePartInfo part = new MessagePartInfo(elName, null); InputStream is = getClass().getResourceAsStream("resources/GreetMeDocLiteralReq.xml"); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createXMLStreamReader(is); QName[] tags = {SOAP_ENV, SOAP_BODY}; StaxStreamFilter filter = new StaxStreamFilter(tags); FixNamespacesXMLStreamReader filteredReader = new FixNamespacesXMLStreamReader( factory.createFilteredReader(reader, filter)); assertNull(filteredReader.getUnmarshaller()); //Remove START_DOCUMENT & START_ELEMENT pertaining to Envelope and Body Tags. part.setTypeClass(GreetMe.class); Unmarshaller um = context.createUnmarshaller(); Object val = JAXBEncoderDecoder.unmarshall(um, filteredReader, part, true); assertEquals(um, filteredReader.getUnmarshaller()); assertNotNull(val); assertTrue(val instanceof GreetMe); assertEquals("TestSOAPInputPMessage", ((GreetMe)val).getRequestType()); is.close(); }
Example 8
Source File: StaxUtils.java From cxf with Apache License 2.0 | 5 votes |
public static XMLStreamReader createFilteredReader(XMLStreamReader reader, StreamFilter filter) { XMLInputFactory factory = getXMLInputFactory(); try { return factory.createFilteredReader(reader, filter); } catch (XMLStreamException e) { throw new RuntimeException("Cant' create XMLStreamReader", e); } finally { returnXMLInputFactory(factory); } }
Example 9
Source File: DrawSimpleShape.java From lams with GNU General Public License v2.0 | 4 votes |
protected static CustomGeometry getCustomGeometry(String name, Graphics2D graphics) { @SuppressWarnings("unchecked") Map<String, CustomGeometry> presets = (graphics == null) ? null : (Map<String, CustomGeometry>)graphics.getRenderingHint(Drawable.PRESET_GEOMETRY_CACHE); if (presets == null) { presets = new HashMap<String,CustomGeometry>(); if (graphics != null) { graphics.setRenderingHint(Drawable.PRESET_GEOMETRY_CACHE, presets); } String packageName = "org.apache.poi.sl.draw.binding"; InputStream presetIS = Drawable.class.getResourceAsStream("presetShapeDefinitions.xml"); // StAX: EventFilter startElementFilter = new EventFilter() { @Override public boolean accept(XMLEvent event) { return event.isStartElement(); } }; try { XMLInputFactory staxFactory = StaxHelper.newXMLInputFactory(); XMLEventReader staxReader = staxFactory.createXMLEventReader(presetIS); XMLEventReader staxFiltRd = staxFactory.createFilteredReader(staxReader, startElementFilter); // Ignore StartElement: staxFiltRd.nextEvent(); // JAXB: JAXBContext jaxbContext = JAXBContext.newInstance(packageName); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); while (staxFiltRd.peek() != null) { StartElement evRoot = (StartElement)staxFiltRd.peek(); String cusName = evRoot.getName().getLocalPart(); // XMLEvent ev = staxReader.nextEvent(); JAXBElement<org.apache.poi.sl.draw.binding.CTCustomGeometry2D> el = unmarshaller.unmarshal(staxReader, CTCustomGeometry2D.class); CTCustomGeometry2D cusGeom = el.getValue(); presets.put(cusName, new CustomGeometry(cusGeom)); } staxFiltRd.close(); staxReader.close(); } catch (Exception e) { throw new RuntimeException("Unable to load preset geometries.", e); } finally { IOUtils.closeQuietly(presetIS); } } return presets.get(name); }