List of usage examples for javax.xml.transform.dom DOMResult DOMResult
public DOMResult(Node node)
From source file:com.twinsoft.convertigo.engine.print.PrintHTML.java
@Override public String print(String location) throws IOException, EngineException, SAXException, TransformerFactoryConfigurationError, TransformerException, ParserConfigurationException { super.print(location); out.close();// w ww. j a va 2s .c o m updateStatus("Create Ressources Directory", 70); //get the dom Document fopResult = XMLUtils.parseDOM(outputFile); Element root = fopResult.getDocumentElement(); String templateFileName = Engine.TEMPLATES_PATH + "/doc/doc.html.xsl"; File htmlFile = new File(templateFileName); Source xsltSrc = new StreamSource(new FileInputStream(htmlFile), localizedDir); //create the ressources repository String ressourcesFolder = outputFolder + "/ressources"; File repository = new File(ressourcesFolder); if (!repository.exists()) { repository.mkdir(); } //export images NodeList images = fopResult.getElementsByTagName("image"); Node image; String attrImg, attrImgName; InputStream imagesIn; OutputStream imagesOut; for (int i = 0; i < images.getLength(); i++) { image = images.item(i); attrImg = image.getAttributes().getNamedItem("url").getTextContent(); attrImgName = attrImg.replaceAll("(.*)/", ""); image.getAttributes().getNamedItem("url").setTextContent(attrImgName); imagesIn = new FileInputStream(attrImg); imagesOut = new FileOutputStream(ressourcesFolder + "/" + attrImgName); org.apache.commons.io.IOUtils.copy(imagesIn, imagesOut); imagesIn.close(); imagesOut.close(); } //export css FileInputStream cssIn = new FileInputStream(Engine.TEMPLATES_PATH + "/doc/style.css"); FileOutputStream cssOut = new FileOutputStream(ressourcesFolder + "/style.css"); org.apache.commons.io.IOUtils.copy(cssIn, cssOut); cssIn.close(); cssOut.close(); updateStatus("HTML Transformation", 85); // transformation of the dom Transformer xslt = TransformerFactory.newInstance().newTransformer(xsltSrc); Element xsl = fopResult.createElement("xsl"); xslt.transform(new DOMSource(fopResult), new DOMResult(xsl)); fopResult.removeChild(root); fopResult.appendChild(xsl.getFirstChild()); //write the dom String newOutputFileName = outputFolder + "/" + projectName + ".html"; outputFile = new File(newOutputFileName); out = new FileOutputStream(outputFile); out = new java.io.BufferedOutputStream(out); OutputStreamWriter output = new OutputStreamWriter(out); output.write(XMLUtils.prettyPrintDOM(fopResult)); output.close(); //remove the temp file new File(outputFileName).delete(); updateStatus("Printing finished", 100); return newOutputFileName; }
From source file:Main.java
public static Document format(Source xslSource, Document xml, URIResolver resolver) throws TransformerFactoryConfigurationError, TransformerConfigurationException, Exception, TransformerException {//from w w w . j av a2 s. c om TransformerFactory factory = TransformerFactory.newInstance(); DOMSource xmlSource = new DOMSource(xml); Templates t = factory.newTemplates(xslSource); Transformer transformer = t.newTransformer(); transformer.setURIResolver(resolver); Document resultDoc = newDocument(); DOMResult result = new DOMResult(resultDoc); transformer.transform(xmlSource, result); return resultDoc; }
From source file:ca.nines.ise.util.XMLDriver.java
/** * Parse an input stream and add location information to the document and the * nodes in the document. {@code source} is the source of the XML. Useful for * reading XML documents inside the bundled up JAR. * * <pre><code>/*from w ww . j a v a 2s.c om*/ * String loc = "/resources/schemas/default.xml"; * InputStream in = Schema.class.getResourceAsStream(loc); * XMLDriver driver = new XMLDriver(); * Document doc = driver.drive(in); * </code></pre> * * @param source the source of the XML * @param in the input stream * @return a W3C document * * @throws TransformerException * @throws IOException */ public Document drive(String source, InputStream in) throws TransformerException, IOException { Document doc = docBuilder.newDocument(); DOMResult domResult = new DOMResult(doc); LocationAnnotator locationAnnotator = new LocationAnnotator(source, xmlReader, doc); InputSource inputSource = new InputSource(in); SAXSource saxSource = new SAXSource(locationAnnotator, inputSource); nullTransformer.transform(saxSource, domResult); return doc; }
From source file:org.n52.smartsensoreditor.dao.SOSWebServiceIT.java
/** * Extracts SensorML from the Soap message *//*ww w.j a v a 2 s . c o m*/ private Document transform(Document response) { //For Transformation Document sensorML = DOMUtil.newDocument(true); Source source = new DOMSource(response); Result result = new DOMResult(sensorML); String transformerFilePath = transformerFiles.get("sos"); xsltTransformer.setRulesetSystemID(transformerFilePath); // transform xsltTransformer.transform(source, result); return sensorML; }
From source file:com.gargoylesoftware.htmlunit.javascript.host.XSLTProcessor.java
/** * @return {@link Node} or {@link String} *//* ww w . ja v a 2 s . c om*/ private Object transform(final Node source) { try { Source xmlSource = new DOMSource(source.getDomNodeOrDie()); final Source xsltSource = new DOMSource(style_.getDomNodeOrDie()); final org.w3c.dom.Document containerDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder() .newDocument(); final org.w3c.dom.Element containerElement = containerDocument.createElement("container"); containerDocument.appendChild(containerElement); final DOMResult result = new DOMResult(containerElement); final Transformer transformer = TransformerFactory.newInstance().newTransformer(xsltSource); for (final Map.Entry<String, Object> entry : parameters_.entrySet()) { transformer.setParameter(entry.getKey(), entry.getValue()); } transformer.transform(xmlSource, result); final org.w3c.dom.Node transformedNode = result.getNode(); if (transformedNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) { return transformedNode; } //output is not DOM (text) xmlSource = new DOMSource(source.getDomNodeOrDie()); final StringWriter writer = new StringWriter(); final Result streamResult = new StreamResult(writer); transformer.transform(xmlSource, streamResult); return writer.toString(); } catch (final Exception e) { throw Context.reportRuntimeError("Exception: " + e); } }
From source file:ca.nines.ise.util.XMLDriver.java
/** * Parse a string for XML. Mostly useful for testing. * * @param in/* www .j a v a 2 s . com*/ * @return Document * @throws TransformerException */ public Document drive(String in) throws TransformerException { Document doc = docBuilder.newDocument(); DOMResult domResult = new DOMResult(doc); LocationAnnotator locationAnnotator = new LocationAnnotator(xmlReader, doc); InputSource inputSource = new InputSource(IOUtils.toInputStream(in)); SAXSource saxSource = new SAXSource(locationAnnotator, inputSource); nullTransformer.transform(saxSource, domResult); return doc; }
From source file:it.cnr.icar.eric.common.cms.AbstractService.java
protected SOAPElement getSOAPElementFromBindingObject(Object obj) throws Exception { SOAPElement soapElem = null;/* w ww . ja va2 s . c om*/ SOAPElement parent = SOAPFactory.newInstance().createElement("dummy"); Marshaller marshaller = getJAXBContext().createMarshaller(); marshaller.marshal(obj, System.err); marshaller.marshal(obj, new DOMResult(parent)); soapElem = (SOAPElement) parent.getChildElements().next(); return soapElem; }
From source file:de.betterform.agent.web.servlet.XSLTServlet.java
/** * This method is only called when non-scripted mode is used to update the UI. This basically exists to support * the PFG (POST/FORWARD/GET) pattern that allows to use the browser back button without POSTDATA warning from the browser * and to re-initialize the preceding form (if any). * <p/>//from w w w .jav a 2s . c om * To make sure that an update is requested from the XFormsSession and not by the user clicking the reload button the * XFormsSession holds a property XFormsSession.UPDATE_REQUEST when coming from XFormsSession. If this property exists, * the UI is refreshed otherwise the form is re-inited with a GET. * * @param request servlet request * @param response servlet response * @throws javax.servlet.ServletException * @throws java.io.IOException */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext servletContext = getServletContext(); String stylePath = null; try { stylePath = WebFactory.getRealPath(editorHome, servletContext); } catch (XFormsConfigException e) { throw new ServletException(e); } File styleFile = new File(stylePath, xslFile); if (styleFile == null) { throw new ServletException("XSL stylesheet cannot be found: " + styleFile); } String contentType = null; Document doc = null; String xsl = null; contentType = (String) request.getAttribute("contenttype"); if (contentType == null) contentType = defContentType; // The servlet returns HTML. response.setContentType(contentType); if (cache == null) cache = new HashMap(); Transformer t = null; // Get the XML input document and the stylesheet. // Source xmlSource = new DOMSource(doc); String inputFile = request.getPathTranslated(); File input = new File(inputFile); if (input == null) { throw new ServletException("XML document cannot be found: " + inputFile); } Source xmlSource = new StreamSource(new FileInputStream(input)); // Perform the transformation, sending the output to the response. // XSL processing can be time consuming, but this is mainly due to the overhead // of compiling the XSL sheet. // By caching the compiled sheets, the process is speeded up dramatically. // Time saved on subsequent requests can be 99% or more (hundreds of milliseconds). try { // check if the XSL sheet was found in cache, and use that if available // if (cache.containsKey(xsl)) t = (Transformer)cache.get(xsl); // else // { // otherwise, load the XSL sheet from disk, compile it and store the compiled // sheet in the cache TransformerFactory tFactory = TransformerFactory.newInstance(); Source xslSource = new StreamSource(new FileInputStream(styleFile)); t = tFactory.newTransformer(xslSource); String contextName = request.getContextPath(); t.setParameter("APP_CONTEXT", contextName); t.setParameter("EDITOR_HOME", stylePath.substring(stylePath.indexOf("/betterform/")) + "/"); t.setParameter("filename", "file://" + inputFile); // cache.put(xsl, t); // } // perform the XSL transformation of the XML Document into the servlet outputstream // t.transform(xmlSource, new StreamResult(out)); Document resultDoc = DOMUtil.newDocument(true, false); t.transform(xmlSource, new DOMResult(resultDoc)); request.setAttribute(WebFactory.XFORMS_NODE, resultDoc); } catch (TransformerConfigurationException e) { e.printStackTrace(); throw new ServletException(e); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); throw new ServletException(e); } catch (TransformerException e) { e.printStackTrace(); throw new ServletException(e); } }
From source file:org.javelin.sws.ext.bind.SweJaxbMarshaller.java
@Override public void marshal(Object jaxbElement, Node node) throws JAXBException { this.marshal(jaxbElement, new DOMResult(node)); }
From source file:com.gargoylesoftware.htmlunit.activex.javascript.msxml.XSLProcessor.java
/** * @return {@link XMLDOMNode} or {@link String} *///from w w w . j av a 2 s. c o m private Object transform(final XMLDOMNode source) { try { Source xmlSource = new DOMSource(source.getDomNodeOrDie()); final Source xsltSource = new DOMSource(style_.getDomNodeOrDie()); final org.w3c.dom.Document containerDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder() .newDocument(); final org.w3c.dom.Element containerElement = containerDocument.createElement("container"); containerDocument.appendChild(containerElement); final DOMResult result = new DOMResult(containerElement); final Transformer transformer = TransformerFactory.newInstance().newTransformer(xsltSource); for (final Map.Entry<String, Object> entry : parameters_.entrySet()) { transformer.setParameter(entry.getKey(), entry.getValue()); } transformer.transform(xmlSource, result); final org.w3c.dom.Node transformedNode = result.getNode(); if (transformedNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) { return transformedNode; } //output is not DOM (text) xmlSource = new DOMSource(source.getDomNodeOrDie()); final StringWriter writer = new StringWriter(); final Result streamResult = new StreamResult(writer); transformer.transform(xmlSource, streamResult); return writer.toString(); } catch (final Exception e) { throw Context.reportRuntimeError("Exception: " + e); } }