Search in sources :

Example 66 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.

the class AbstractDom4jImporterExporterTest method testDom4jCommentFiltering.

@Test
public void testDom4jCommentFiltering() throws Exception {
    final TestDom4jImporter importer = new TestDom4jImporter();
    final TestDom4jExporter exporter = new TestDom4jExporter();
    exporter.setXmlUtilities(new XmlUtilitiesImpl());
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    final InputStream resource = this.getClass().getResourceAsStream("/org/apereo/portal/io/xml/crn/pilot-lo.fragment-layout.xml");
    final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(resource);
    final Tuple<String, Element> result = importer.unmarshal(new StAXSource(xmlEventReader));
    assertNotNull(result);
    final Element document = result.getSecond();
    final List<org.dom4j.Node> comments = document.selectNodes("//comment()");
    for (final org.dom4j.Node comment : comments) {
        comment.detach();
    }
    final StringWriter writer = new StringWriter();
    exporter.marshal(result, new StreamResult(writer));
    final String marshalResult = writer.toString();
    assertNotNull(marshalResult);
    XMLUnit.setIgnoreWhitespace(true);
    try {
        Diff d = new Diff(new InputStreamReader(this.getClass().getResourceAsStream("/org/apereo/portal/io/xml/crn/filtered-pilot-lo.fragment-layout.xml")), new StringReader(marshalResult));
        assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
    } catch (Exception e) {
        throw new XmlTestException("Failed to assert similar between marshall output and expected XML", marshalResult, e);
    } catch (Error e) {
        throw new XmlTestException("Failed to assert similar between marshall output and expected XML", marshalResult, e);
    }
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) InputStreamReader(java.io.InputStreamReader) Diff(org.custommonkey.xmlunit.Diff) InputStream(java.io.InputStream) Element(org.dom4j.Element) StAXSource(javax.xml.transform.stax.StAXSource) XmlTestException(org.apereo.portal.io.xml.XmlTestException) XmlUtilitiesImpl(org.apereo.portal.xml.XmlUtilitiesImpl) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XmlTestException(org.apereo.portal.io.xml.XmlTestException) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 67 with XMLEventReader

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

the class ContextUtils method getDataReader.

public static DataReader<XMLEventReader> getDataReader(CorbaMessage message) {
    Service service = ServiceModelUtil.getService(message.getExchange());
    DataReader<XMLEventReader> dataReader = service.getDataBinding().createReader(XMLEventReader.class);
    if (dataReader == null) {
    // throw a fault
    // throw new Fault(new org.apache.cxf.common.i18n.Message("NO_DATAREADER", BUNDLE, service
    // .getName()));
    }
    return dataReader;
}
Also used : Service(org.apache.cxf.service.Service) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 68 with XMLEventReader

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

the class JAXBEncoderDecoder method doUnmarshal.

private static Object doUnmarshal(final Unmarshaller u, final Object source, final QName elName, final Class<?> clazz, final boolean unwrap) throws Exception {
    Object obj = null;
    boolean unmarshalWithClass = true;
    if (clazz == null || (!clazz.isPrimitive() && !clazz.isArray() && !clazz.isEnum() && !clazz.equals(Calendar.class) && (Modifier.isAbstract(clazz.getModifiers()) || Modifier.isInterface(clazz.getModifiers())))) {
        unmarshalWithClass = false;
    }
    if (clazz != null && (clazz.getName().equals("javax.xml.datatype.XMLGregorianCalendar") || clazz.getName().equals("javax.xml.datatype.Duration"))) {
        // special treat two jaxb defined built-in abstract types
        unmarshalWithClass = true;
    }
    if (source instanceof Node) {
        obj = unmarshalWithClass ? u.unmarshal((Node) source, clazz) : u.unmarshal((Node) source);
    } else if (source instanceof DepthXMLStreamReader) {
        // JAXB optimizes a ton of stuff depending on the StreamReader impl. Thus,
        // we REALLY want to pass the original reader in.   This is OK with JAXB
        // as it doesn't read beyond the end so the DepthXMLStreamReader state
        // would be OK when it returns.   The main winner is FastInfoset where parsing
        // a testcase I have goes from about 300/sec to well over 1000.
        DepthXMLStreamReader dr = (DepthXMLStreamReader) source;
        XMLStreamReader reader = dr.getReader();
        // allows the XML Stream Reader to adjust it's behaviour based on the state of the unmarshaller
        if (reader instanceof UnmarshallerAwareXMLReader) {
            ((UnmarshallerAwareXMLReader) reader).setUnmarshaller(u);
        }
        if (u.getSchema() != null) {
            // validating, but we may need more namespaces
            reader = findExtraNamespaces(reader);
        }
        obj = unmarshalWithClass ? u.unmarshal(reader, clazz) : u.unmarshal(dr.getReader());
    } else if (source instanceof XMLStreamReader) {
        XMLStreamReader reader = (XMLStreamReader) source;
        // allows the XML Stream Reader to adjust it's behaviour based on the state of the unmarshaller
        if (reader instanceof UnmarshallerAwareXMLReader) {
            ((UnmarshallerAwareXMLReader) reader).setUnmarshaller(u);
        }
        if (u.getSchema() != null) {
            // validating, but we may need more namespaces
            reader = findExtraNamespaces(reader);
        }
        obj = unmarshalWithClass ? u.unmarshal(reader, clazz) : u.unmarshal(reader);
    } else if (source instanceof XMLEventReader) {
        // allows the XML Event Reader to adjust it's behaviour based on the state of the unmarshaller
        if (source instanceof UnmarshallerAwareXMLReader) {
            ((UnmarshallerAwareXMLReader) source).setUnmarshaller(u);
        }
        obj = unmarshalWithClass ? u.unmarshal((XMLEventReader) source, clazz) : u.unmarshal((XMLEventReader) source);
    } else if (source == null) {
        throw new Fault(new Message("UNKNOWN_SOURCE", LOG, "null"));
    } else {
        throw new Fault(new Message("UNKNOWN_SOURCE", LOG, source.getClass().getName()));
    }
    return unwrap ? getElementValue(obj) : obj;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) Message(org.apache.cxf.common.i18n.Message) Calendar(java.util.Calendar) Node(org.w3c.dom.Node) XMLEventReader(javax.xml.stream.XMLEventReader) Fault(org.apache.cxf.interceptor.Fault) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader)

Example 69 with XMLEventReader

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

the class JAXBEncoderDecoderTest method testMarshallIntoStaxStreamWriter.

@Test
public void testMarshallIntoStaxStreamWriter() throws Exception {
    GreetMe obj = new GreetMe();
    obj.setRequestType("Hello");
    QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
    MessagePartInfo part = new MessagePartInfo(elName, null);
    part.setElement(true);
    part.setElementQName(elName);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
    opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
    FixNamespacesXMLStreamWriter writer = new FixNamespacesXMLStreamWriter(opFactory.createXMLStreamWriter(baos));
    assertNull(writer.getMarshaller());
    Marshaller m = context.createMarshaller();
    JAXBEncoderDecoder.marshall(m, obj, part, writer);
    assertEquals(m, writer.getMarshaller());
    writer.flush();
    writer.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    XMLInputFactory ipFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = ipFactory.createXMLEventReader(bais);
    Unmarshaller um = context.createUnmarshaller();
    Object val = um.unmarshal(reader, GreetMe.class);
    assertTrue(val instanceof JAXBElement);
    val = ((JAXBElement<?>) val).getValue();
    assertTrue(val instanceof GreetMe);
    assertEquals(obj.getRequestType(), ((GreetMe) val).getRequestType());
}
Also used : GreetMe(org.apache.hello_world_soap_http.types.GreetMe) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Marshaller(javax.xml.bind.Marshaller) QName(javax.xml.namespace.QName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JAXBElement(javax.xml.bind.JAXBElement) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 70 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project Payara by payara.

the class GenericSniffer method readDeploymentConfig.

private String readDeploymentConfig(final InputStream is) throws IOException {
    String encoding = null;
    XMLEventReader rdr = null;
    try {
        is.mark(Integer.MAX_VALUE);
        rdr = xmlInputFactory.createXMLEventReader(new InputStreamReader(is));
        while (rdr.hasNext()) {
            final XMLEvent ev = rdr.nextEvent();
            if (ev.isStartDocument()) {
                final StartDocument sd = (StartDocument) ev;
                encoding = sd.getCharacterEncodingScheme();
                break;
            }
        }
    } catch (XMLStreamException e) {
        if (rdr != null) {
            try {
                rdr.close();
            } catch (XMLStreamException inner) {
                throw new IOException(e);
            }
        }
        throw new IOException(e);
    }
    if (encoding == null) {
        encoding = "UTF-8";
    }
    is.reset();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int bytesRead;
    final byte[] buffer = new byte[1024];
    while ((bytesRead = is.read(buffer)) != -1) {
        baos.write(buffer, 0, bytesRead);
    }
    try {
        rdr.close();
    } catch (XMLStreamException ex) {
        throw new IOException(ex);
    }
    is.close();
    return new String(baos.toByteArray(), encoding);
}
Also used : StartDocument(javax.xml.stream.events.StartDocument) InputStreamReader(java.io.InputStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)71 XMLInputFactory (javax.xml.stream.XMLInputFactory)36 XMLEvent (javax.xml.stream.events.XMLEvent)34 XMLStreamException (javax.xml.stream.XMLStreamException)23 StringReader (java.io.StringReader)18 Test (org.junit.Test)17 InputStream (java.io.InputStream)15 StAXSource (javax.xml.transform.stax.StAXSource)13 StartElement (javax.xml.stream.events.StartElement)12 IOException (java.io.IOException)10 Unmarshaller (javax.xml.bind.Unmarshaller)9 Attribute (javax.xml.stream.events.Attribute)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 StringWriter (java.io.StringWriter)6 ArrayList (java.util.ArrayList)6 QName (javax.xml.namespace.QName)6 Document (org.w3c.dom.Document)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 JAXBContext (javax.xml.bind.JAXBContext)5