Search in sources :

Example 61 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project poi by apache.

the class PresetGeometries method init.

@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 = XMLInputFactory.newFactory();
    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));
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) CTCustomGeometry2D(org.apache.poi.sl.draw.binding.CTCustomGeometry2D) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) EventFilter(javax.xml.stream.EventFilter) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 62 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project tomee by apache.

the class StaxCompare method compare.

public static void compare(final String a, final String b) throws Exception {
    final StringBuilder message = new StringBuilder();
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    final XMLEventReader rA = factory.createXMLEventReader(new StringReader(a));
    final XMLEventReader rB = factory.createXMLEventReader(new StringReader(b));
    if (!compare(rA, rB, message)) {
        throw new Exception(message.toString());
    }
}
Also used : StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)62 XMLInputFactory (javax.xml.stream.XMLInputFactory)30 XMLEvent (javax.xml.stream.events.XMLEvent)30 XMLStreamException (javax.xml.stream.XMLStreamException)21 StringReader (java.io.StringReader)18 InputStream (java.io.InputStream)15 Test (org.junit.Test)14 StAXSource (javax.xml.transform.stax.StAXSource)13 StartElement (javax.xml.stream.events.StartElement)10 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)6 Unmarshaller (javax.xml.bind.Unmarshaller)6 Attribute (javax.xml.stream.events.Attribute)6 Document (org.w3c.dom.Document)6 ArrayList (java.util.ArrayList)5 JAXBContext (javax.xml.bind.JAXBContext)5 Diff (org.custommonkey.xmlunit.Diff)5 InputSource (org.xml.sax.InputSource)5 JAXBException (javax.xml.bind.JAXBException)4 DOMSource (javax.xml.transform.dom.DOMSource)4