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);
}
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));
}
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();
}
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);
}
}
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());
}
Aggregations