Search in sources :

Example 56 with StAXSource

use of javax.xml.transform.stax.StAXSource in project spring-framework by spring-projects.

the class SourceHttpMessageConverterTests method readStAXSourceExternal.

@Test
public void readStAXSourceExternal() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
    converter.setSupportDtd(true);
    StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
    XMLStreamReader streamReader = result.getXMLStreamReader();
    assertThat(streamReader.hasNext()).isTrue();
    streamReader.next();
    streamReader.next();
    String s = streamReader.getLocalName();
    assertThat(s).isEqualTo("root");
    try {
        s = streamReader.getElementText();
        assertThat(s).isNotEqualTo("Foo Bar");
    } catch (XMLStreamException ex) {
    // Some parsers raise a parse exception
    }
    streamReader.close();
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.jupiter.api.Test)

Example 57 with StAXSource

use of javax.xml.transform.stax.StAXSource in project spring-framework by spring-projects.

the class AbstractUnmarshallerTests method unmarshalJaxp14StaxSourceXmlEventReader.

@Test
public void unmarshalJaxp14StaxSourceXmlEventReader() throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
    StAXSource source = new StAXSource(eventReader);
    Object flights = unmarshaller.unmarshal(source);
    testFlights(flights);
}
Also used : StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.jupiter.api.Test)

Example 58 with StAXSource

use of javax.xml.transform.stax.StAXSource in project spring-framework by spring-projects.

the class SourceHttpMessageConverter method readStAXSource.

private Source readStAXSource(InputStream body, HttpInputMessage inputMessage) {
    try {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, isSupportDtd());
        inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, isProcessExternalEntities());
        if (!isProcessExternalEntities()) {
            inputFactory.setXMLResolver(NO_OP_XML_RESOLVER);
        }
        XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body);
        return new StAXSource(streamReader);
    } catch (XMLStreamException ex) {
        throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex, inputMessage);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) XMLStreamException(javax.xml.stream.XMLStreamException) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 59 with StAXSource

use of javax.xml.transform.stax.StAXSource in project Activiti by Activiti.

the class BpmnXMLConverter method validateModel.

public void validateModel(XMLStreamReader xmlStreamReader) throws Exception {
    Schema schema = createSchema();
    Validator validator = schema.newValidator();
    validator.validate(new StAXSource(xmlStreamReader));
}
Also used : Schema(javax.xml.validation.Schema) StAXSource(javax.xml.transform.stax.StAXSource) Validator(javax.xml.validation.Validator)

Example 60 with StAXSource

use of javax.xml.transform.stax.StAXSource in project uPortal by Jasig.

the class ImportExportController method importEntity.

@RequestMapping(value = "/import", method = RequestMethod.POST)
public void importEntity(@RequestParam("file") MultipartFile entityFile, HttpServletRequest request, HttpServletResponse response) throws IOException, XMLStreamException {
    // Get a StAX reader for the source to determine info about the data to import
    final BufferedXMLEventReader bufferedXmlEventReader = createSourceXmlEventReader(entityFile);
    final PortalDataKey portalDataKey = getPortalDataKey(bufferedXmlEventReader);
    String target = determineTarget(portalDataKey);
    final IPerson person = personManager.getPerson(request);
    final EntityIdentifier ei = person.getEntityIdentifier();
    final IAuthorizationPrincipal ap = AuthorizationServiceFacade.instance().newPrincipal(ei.getKey(), ei.getType());
    if (!ap.hasPermission("UP_SYSTEM", "IMPORT_ENTITY", target)) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        return;
    }
    portalDataHandlerService.importData(new StAXSource(bufferedXmlEventReader));
    response.setStatus(HttpServletResponse.SC_OK);
}
Also used : BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) IPerson(org.apereo.portal.security.IPerson) IAuthorizationPrincipal(org.apereo.portal.security.IAuthorizationPrincipal) PortalDataKey(org.apereo.portal.io.xml.PortalDataKey) EntityIdentifier(org.apereo.portal.EntityIdentifier) StAXSource(javax.xml.transform.stax.StAXSource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

StAXSource (javax.xml.transform.stax.StAXSource)66 XMLStreamReader (javax.xml.stream.XMLStreamReader)27 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringReader (java.io.StringReader)19 XMLInputFactory (javax.xml.stream.XMLInputFactory)19 XMLEventReader (javax.xml.stream.XMLEventReader)16 StreamSource (javax.xml.transform.stream.StreamSource)15 DOMSource (javax.xml.transform.dom.DOMSource)14 Source (javax.xml.transform.Source)13 SAXSource (javax.xml.transform.sax.SAXSource)12 Test (org.junit.Test)12 StreamResult (javax.xml.transform.stream.StreamResult)10 InputStream (java.io.InputStream)9 TransformerException (javax.xml.transform.TransformerException)8 IOException (java.io.IOException)7 Transformer (javax.xml.transform.Transformer)7 Test (org.junit.jupiter.api.Test)7 Document (org.w3c.dom.Document)7 InputSource (org.xml.sax.InputSource)7 StringWriter (java.io.StringWriter)6