/** * Transforms the given {@code Source} to the {@code Result}. * @param source the source to transform from * @param result the result to transform to * @throws TransformerException in case of transformation errors */ protected void transform(Source source, Result result) throws TransformerException { this.transformerFactory.newTransformer().transform(source, result); }
/** * Create the {@link Transformer} instance used to prefer the XSLT transformation. * <p>The default implementation simply calls {@link Templates#newTransformer()}, and * configures the {@link Transformer} with the custom {@link URIResolver} if specified. * @param templates the XSLT Templates instance to create a Transformer for * @return the Transformer object * @throws TransformerConfigurationException in case of creation failure */ protected Transformer createTransformer(Templates templates) throws TransformerConfigurationException { Transformer transformer = templates.newTransformer(); if (this.uriResolver != null) { transformer.setURIResolver(this.uriResolver); } return transformer; }
/** * potentially unsafe XML transformation. * @param source The XML input to transform. * @param out The Result of transforming the <code>source</code>. */ private static void _transform(Source source, Result out) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); // this allows us to use UTF-8 for storing data, // plus it checks any well-formedness issue in the submitted data. Transformer t = factory.newTransformer(); t.transform(source, out); }
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
@Test public void readCorrect() throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); StAXSource source = new StAXSource(streamReader); StringWriter writer = new StringWriter(); transformer.transform(source, new StreamResult(writer)); Predicate<Node> nodeFilter = n -> n.getNodeType() != Node.DOCUMENT_TYPE_NODE && n.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE; assertThat(writer.toString(), isSimilarTo(XML).withNodeFilter(nodeFilter)); }
private Transformer getTransformer() throws TransformerException { final TransformerFactory xformFactory = TransformerFactory.newInstance(); final Transformer transformer = xformFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); return transformer; }
private void transformAndMarshal(Object graph, Result result) throws IOException { try { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); marshalOutputStream(graph, os); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); Transformer transformer = this.transformerFactory.newTransformer(); transformer.transform(new StreamSource(is), result); } catch (TransformerException ex) { throw new MarshallingFailureException( "Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex); } }
@Before public void setUp() throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformer = transformerFactory.newTransformer(); inputFactory = XMLOutputFactory.newInstance(); }
/** * Disable indenting for the supplied {@link javax.xml.transform.Transformer}. * @param transformer the target transformer * @see javax.xml.transform.OutputKeys#INDENT */ public static void disableIndenting(Transformer transformer) { Assert.notNull(transformer, "Transformer must not be null"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); }
/** * Initialize this XsltView's TransformerFactory. */ @Override protected void initApplicationContext() throws BeansException { this.transformerFactory = newTransformerFactory(this.transformerFactoryClass); this.transformerFactory.setErrorListener(this.errorListener); if (this.uriResolver != null) { this.transformerFactory.setURIResolver(this.uriResolver); } if (this.cacheTemplates) { this.cachedTemplates = loadTemplates(); } }
/** * Load the {@link Templates} instance for the stylesheet at the configured location. */ private Templates loadTemplates() throws ApplicationContextException { Source stylesheetSource = getStylesheetSource(); try { Templates templates = getTransformerFactory().newTemplates(stylesheetSource); return templates; } catch (TransformerConfigurationException ex) { throw new ApplicationContextException("Can't load stylesheet from '" + getUrl() + "'", ex); } finally { closeSourceIfNecessary(stylesheetSource); } }
@Controller @RequestMapping("/test") public class TestController { @RequestMapping(method = RequestMethod.POST, value = "math") @ResponseBody public Result math(@RequestBody final Request request) { final Result result = new Result(); result.setAddition(request.getLeft() + request.getRight()); result.setSubtraction(request.getLeft() - request.getRight()); result.setMultiplication(request.getLeft() * request.getRight()); return result; } }
TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); String str = buffer.toString();
private void transform(Source source, Result result) throws TransformerException { this.transformerFactory.newTransformer().transform(source, result); }
@Before public void setUp() throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformer = transformerFactory.newTransformer(); inputFactory = XMLInputFactory.newInstance(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilder = documentBuilderFactory.newDocumentBuilder(); }
/** * Copy the configured output {@link Properties}, if any, into the * {@link Transformer#setOutputProperty output property set} of the supplied * {@link Transformer}. * @param transformer the target transformer */ protected final void copyOutputProperties(Transformer transformer) { if (this.outputProperties != null) { Enumeration<?> en = this.outputProperties.propertyNames(); while (en.hasMoreElements()) { String name = (String) en.nextElement(); transformer.setOutputProperty(name, this.outputProperties.getProperty(name)); } } }
/** * Create the {@link Transformer} instance used to prefer the XSLT transformation. * <p>The default implementation simply calls {@link Templates#newTransformer()}, and * configures the {@link Transformer} with the custom {@link URIResolver} if specified. * @param templates the XSLT Templates instance to create a Transformer for * @return the Transformer object * @throws TransformerConfigurationException in case of creation failure */ protected Transformer createTransformer(Templates templates) throws TransformerConfigurationException { Transformer transformer = templates.newTransformer(); if (this.uriResolver != null) { transformer.setURIResolver(this.uriResolver); } return transformer; }
private static String toString(org.w3c.dom.Node node) { try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); DOMSource domSource = new DOMSource(node); StringWriter out = new StringWriter(); transformer.transform(domSource, new StreamResult(out)); return out.toString(); } catch (TransformerException e) { throw new JSONException("xml node to string error", e); } }
/** * Transforms the given {@code Source} to the {@code Result}. * @param source the source to transform from * @param result the result to transform to * @throws TransformerException in case of transformation errors */ protected void transform(Source source, Result result) throws TransformerException { this.transformerFactory.newTransformer().transform(source, result); }
private void transform(Source source, Result result) throws TransformerException { this.transformerFactory.newTransformer().transform(source, result); }