Search in sources :

Example 41 with Source

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

the class XStreamMarshallerTests method assertXpathExists.

private static void assertXpathExists(String xPathExpression, String inXMLString) {
    Source source = Input.fromString(inXMLString).build();
    Iterable<Node> nodes = new JAXPXPathEngine().selectNodes(xPathExpression, source);
    assertTrue("Expecting to find matches for Xpath " + xPathExpression, count(nodes) > 0);
}
Also used : JAXPXPathEngine(org.xmlunit.xpath.JAXPXPathEngine) Node(org.w3c.dom.Node) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 42 with Source

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

the class XStreamMarshallerTests method assertXpathNotExists.

private static void assertXpathNotExists(String xPathExpression, String inXMLString) {
    Source source = Input.fromString(inXMLString).build();
    Iterable<Node> nodes = new JAXPXPathEngine().selectNodes(xPathExpression, source);
    assertEquals("Should be zero matches for Xpath " + xPathExpression, 0, count(nodes));
}
Also used : JAXPXPathEngine(org.xmlunit.xpath.JAXPXPathEngine) Node(org.w3c.dom.Node) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 43 with Source

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

the class FormHttpMessageConverterTests method writeMultipart.

@Test
public void writeMultipart() throws Exception {
    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
    parts.add("name 1", "value 1");
    parts.add("name 2", "value 2+1");
    parts.add("name 2", "value 2+2");
    parts.add("name 3", null);
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    parts.add("logo", logo);
    // SPR-12108
    Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {

        @Override
        public String getFilename() {
            return "Hallöle.jpg";
        }
    };
    parts.add("utf8", utf8);
    Source xml = new StreamSource(new StringReader("<root><child/></root>"));
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.setContentType(MediaType.TEXT_XML);
    HttpEntity<Source> entity = new HttpEntity<>(xml, entityHeaders);
    parts.add("xml", entity);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.converter.setMultipartCharset(StandardCharsets.UTF_8);
    this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);
    final MediaType contentType = outputMessage.getHeaders().getContentType();
    assertNotNull("No boundary found", contentType.getParameter("boundary"));
    // see if Commons FileUpload can read what we wrote
    FileItemFactory fileItemFactory = new DiskFileItemFactory();
    FileUpload fileUpload = new FileUpload(fileItemFactory);
    RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
    List<FileItem> items = fileUpload.parseRequest(requestContext);
    assertEquals(6, items.size());
    FileItem item = items.get(0);
    assertTrue(item.isFormField());
    assertEquals("name 1", item.getFieldName());
    assertEquals("value 1", item.getString());
    item = items.get(1);
    assertTrue(item.isFormField());
    assertEquals("name 2", item.getFieldName());
    assertEquals("value 2+1", item.getString());
    item = items.get(2);
    assertTrue(item.isFormField());
    assertEquals("name 2", item.getFieldName());
    assertEquals("value 2+2", item.getString());
    item = items.get(3);
    assertFalse(item.isFormField());
    assertEquals("logo", item.getFieldName());
    assertEquals("logo.jpg", item.getName());
    assertEquals("image/jpeg", item.getContentType());
    assertEquals(logo.getFile().length(), item.getSize());
    item = items.get(4);
    assertFalse(item.isFormField());
    assertEquals("utf8", item.getFieldName());
    assertEquals("Hallöle.jpg", item.getName());
    assertEquals("image/jpeg", item.getContentType());
    assertEquals(logo.getFile().length(), item.getSize());
    item = items.get(5);
    assertEquals("xml", item.getFieldName());
    assertEquals("text/xml", item.getContentType());
    verify(outputMessage.getBody(), never()).close();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) StreamSource(javax.xml.transform.stream.StreamSource) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) StringReader(java.io.StringReader) MediaType(org.springframework.http.MediaType) RequestContext(org.apache.commons.fileupload.RequestContext) FileUpload(org.apache.commons.fileupload.FileUpload) Test(org.junit.Test)

Example 44 with Source

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

the class XsltView method renderMergedOutputModel.

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Templates templates = this.cachedTemplates;
    if (templates == null) {
        templates = loadTemplates();
    }
    Transformer transformer = createTransformer(templates);
    configureTransformer(model, response, transformer);
    configureResponse(model, response, transformer);
    Source source = null;
    try {
        source = locateSource(model);
        if (source == null) {
            throw new IllegalArgumentException("Unable to locate Source object in model: " + model);
        }
        transformer.transform(source, createResult(response));
    } finally {
        closeSourceIfNecessary(source);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Templates(javax.xml.transform.Templates) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 45 with Source

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

the class XsltViewTests method testContentTypeCarriedFromTemplate.

@Test
public void testContentTypeCarriedFromTemplate() throws Exception {
    XsltView view = getXsltView(HTML_OUTPUT);
    Source source = new StreamSource(getProductDataResource().getInputStream());
    view.render(singletonMap("someKey", source), this.request, this.response);
    assertTrue(this.response.getContentType().startsWith("text/html"));
    assertEquals("UTF-8", this.response.getCharacterEncoding());
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.junit.Test)

Aggregations

Source (javax.xml.transform.Source)238 StreamSource (javax.xml.transform.stream.StreamSource)161 DOMSource (javax.xml.transform.dom.DOMSource)108 Transformer (javax.xml.transform.Transformer)76 StreamResult (javax.xml.transform.stream.StreamResult)74 InputSource (org.xml.sax.InputSource)67 SAXSource (javax.xml.transform.sax.SAXSource)56 StringReader (java.io.StringReader)52 IOException (java.io.IOException)46 TransformerException (javax.xml.transform.TransformerException)45 Result (javax.xml.transform.Result)42 TransformerFactory (javax.xml.transform.TransformerFactory)41 Test (org.junit.Test)39 StringWriter (java.io.StringWriter)35 InputStream (java.io.InputStream)32 SAXException (org.xml.sax.SAXException)32 Schema (javax.xml.validation.Schema)29 Validator (javax.xml.validation.Validator)29 SchemaFactory (javax.xml.validation.SchemaFactory)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26