Search in sources :

Example 21 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project spring-framework by spring-projects.

the class AbstractUnmarshallerTests method unmarshalPartialStaxSourceXmlStreamReader.

@Test
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
    // skip to flights
    streamReader.nextTag();
    assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flights"), streamReader.getName());
    // skip to flight
    streamReader.nextTag();
    assertEquals("Invalid element", new QName("http://samples.springframework.org/flight", "flight"), streamReader.getName());
    Source source = StaxUtils.createStaxSource(streamReader);
    Object flight = unmarshaller.unmarshal(source);
    testFlight(flight);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StringReader(java.io.StringReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.Test)

Example 22 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project spring-framework by spring-projects.

the class Jaxb2CollectionHttpMessageConverter method createXmlInputFactory.

/**
	 * Create a {@code XMLInputFactory} that this converter will use to create {@link
	 * javax.xml.stream.XMLStreamReader} and {@link javax.xml.stream.XMLEventReader} objects.
	 * <p>Can be overridden in subclasses, adding further initialization of the factory.
	 * The resulting factory is cached, so this method will only be called once.
	 */
protected XMLInputFactory createXmlInputFactory() {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    inputFactory.setXMLResolver(NO_OP_XML_RESOLVER);
    return inputFactory;
}
Also used : XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 23 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project spring-framework by spring-projects.

the class Jaxb2CollectionHttpMessageConverterTests method readXmlRootElementExternalEntityDisabled.

@Test
@SuppressWarnings("unchecked")
public void readXmlRootElementExternalEntityDisabled() throws Exception {
    Resource external = new ClassPathResource("external.txt", getClass());
    String content = "<!DOCTYPE root [" + "  <!ELEMENT external ANY >\n" + "  <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + "  <list><rootElement><type s=\"1\"/><external>&ext;</external></rootElement></list>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    converter = new Jaxb2CollectionHttpMessageConverter<Collection<Object>>() {

        @Override
        protected XMLInputFactory createXmlInputFactory() {
            XMLInputFactory inputFactory = super.createXmlInputFactory();
            inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, true);
            return inputFactory;
        }
    };
    try {
        Collection<RootElement> result = converter.read(rootElementListType, null, inputMessage);
        assertEquals(1, result.size());
        assertEquals("", result.iterator().next().external);
    } catch (HttpMessageNotReadableException ex) {
    // Some parsers raise an exception
    }
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Collection(java.util.Collection) ClassPathResource(org.springframework.core.io.ClassPathResource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 24 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project spring-framework by spring-projects.

the class Jaxb2CollectionHttpMessageConverterTests method readXmlRootElementExternalEntityEnabled.

@Test
@SuppressWarnings("unchecked")
public void readXmlRootElementExternalEntityEnabled() throws Exception {
    Resource external = new ClassPathResource("external.txt", getClass());
    String content = "<!DOCTYPE root [" + "  <!ELEMENT external ANY >\n" + "  <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + "  <list><rootElement><type s=\"1\"/><external>&ext;</external></rootElement></list>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    Jaxb2CollectionHttpMessageConverter<?> c = new Jaxb2CollectionHttpMessageConverter<Collection<Object>>() {

        @Override
        protected XMLInputFactory createXmlInputFactory() {
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
            return inputFactory;
        }
    };
    Collection<RootElement> result = c.read(rootElementListType, null, inputMessage);
    assertEquals(1, result.size());
    assertEquals("Foo Bar", result.iterator().next().external);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 25 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project camel by apache.

the class ScrHelper method getScrProperties.

public static Map<String, String> getScrProperties(String xmlLocation, String componentName) throws Exception {
    Map<String, String> result = new HashMap<>();
    XMLInputFactory inputFactory = XMLInputFactory.newFactory();
    inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
    XMLEventReader eventReader = inputFactory.createXMLEventReader(new FileReader(xmlLocation));
    boolean collect = false;
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.getEventType() == XMLStreamConstants.START_ELEMENT && event.asStartElement().getName().toString().equals("scr:component") && event.asStartElement().getAttributeByName(QName.valueOf("name")).getValue().equals(componentName)) {
            collect = true;
        } else if (collect && event.getEventType() == XMLStreamConstants.START_ELEMENT && event.asStartElement().getName().toString().equals("property")) {
            result.put(event.asStartElement().getAttributeByName(QName.valueOf("name")).getValue(), event.asStartElement().getAttributeByName(QName.valueOf("value")).getValue());
        } else if (collect && event.getEventType() == XMLStreamConstants.END_ELEMENT && event.asEndElement().getName().toString().equals("scr:component")) {
            break;
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FileReader(java.io.FileReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

XMLInputFactory (javax.xml.stream.XMLInputFactory)154 XMLStreamReader (javax.xml.stream.XMLStreamReader)98 XMLStreamException (javax.xml.stream.XMLStreamException)63 StringReader (java.io.StringReader)43 InputStream (java.io.InputStream)41 IOException (java.io.IOException)33 XMLEventReader (javax.xml.stream.XMLEventReader)30 Test (org.junit.Test)22 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStreamReader (java.io.InputStreamReader)14 JAXBException (javax.xml.bind.JAXBException)14 StAXSource (javax.xml.transform.stax.StAXSource)14 StreamSource (javax.xml.transform.stream.StreamSource)14 Unmarshaller (javax.xml.bind.Unmarshaller)13 ArrayList (java.util.ArrayList)12 XMLEvent (javax.xml.stream.events.XMLEvent)12 DOMSource (javax.xml.transform.dom.DOMSource)11 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)10 JAXBContext (javax.xml.bind.JAXBContext)9 HashMap (java.util.HashMap)8