Example usage for javax.xml.transform Transformer setOutputProperty

List of usage examples for javax.xml.transform Transformer setOutputProperty

Introduction

In this page you can find the example usage for javax.xml.transform Transformer setOutputProperty.

Prototype

public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;

Source Link

Document

Set an output property that will be in effect for the transformation.

Usage

From source file:curam.molsa.test.customfunctions.MOLSADOMReader.java

/**
 * This method is to transform the XML node to a string.
 * // ww  w . j a va 2 s .c  om
 * @param node
 * @return buffer
 */
public static String toString(final Node node) {

    try {
        final TransformerFactory transFactory = TransformerFactory.newInstance();
        final Transformer transformer = transFactory.newTransformer();
        final StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        return buffer.toString();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:XmlUtils.java

public static void styleDocument(Document document, String stylesheet, boolean xslInPath,
        Map<String, Object> parameters, OutputStream out) throws Exception {

    Transformer transformer = XmlUtils.getTransformer(stylesheet, xslInPath);
    if (parameters != null) {
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            transformer.setParameter(entry.getKey(), entry.getValue());
        }//from w  ww  . ja v  a  2 s.co m
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    DocumentSource source = new DocumentSource(document);
    // now lets style the given document

    StreamResult sresult = new StreamResult(out);
    transformer.transform(source, sresult);

}

From source file:com.bluexml.xforms.controller.FluxFacade.java

public static void prettyPrintDOM(Node node, OutputStream stream) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.transform(new DOMSource(node), new StreamResult(stream));
}

From source file:com.isa.utiles.Utiles.java

public static String printDocument(Document doc) {
    try {//w  ww  .j  a v a 2  s  . c om
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;

    } catch (TransformerConfigurationException ex) {
        return "";
    } catch (TransformerException ex) {
        return "";
    }
}

From source file:Main.java

private static Transformer getThreadedTransformer(final boolean omitXmlDeclaration, final boolean standalone,
        final Map threadMap, final String xslURL) throws TransformerConfigurationException {
    final Thread currentThread = Thread.currentThread();
    Transformer transformer = null;
    if (threadMap != null) {
        transformer = (Transformer) threadMap.get(currentThread);
    }/*w  ww .  j  av  a 2  s.c o m*/
    if (transformer == null) {
        if (xslURL == null) {
            transformer = tFactory.newTransformer(); // "never null"
        } else {
            transformer = tFactory.newTransformer(new StreamSource(xslURL));
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        if (threadMap != null) {
            threadMap.put(currentThread, transformer);
        }
    }
    transformer.setOutputProperty(OutputKeys.STANDALONE, standalone ? "yes" : "no");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    return transformer;
}

From source file:com.valco.utility.FacturasUtility.java

public static OutputStream getCadenaOriginal(String cadenaOriginalDir, String xml) throws Exception {
    StreamSource sourceXSL = new StreamSource(new File(cadenaOriginalDir));
    StringReader reader = new StringReader(xml);
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    transformer = tFactory.newTransformer(sourceXSL);
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    OutputStream output = new ByteArrayOutputStream();
    transformer.transform(new StreamSource(reader), new StreamResult(output));

    return output;
}

From source file:es.gob.afirma.signers.ooxml.be.fedict.eid.applet.service.signer.AbstractXmlSignatureService.java

protected static void writeDocumentNoClosing(final Document document, final OutputStream documentOutputStream,
        final boolean omitXmlDeclaration) throws TransformerException {
    final NoCloseOutputStream outputStream = new NoCloseOutputStream(documentOutputStream);
    final Result result = new StreamResult(outputStream);
    final Transformer xformer = TransformerFactory.newInstance().newTransformer();
    if (omitXmlDeclaration) {
        xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //$NON-NLS-1$
    }/*from  w  w  w .  j a va2s. c om*/
    final Source source = new DOMSource(document);
    xformer.transform(source, result);
}

From source file:com.eucalyptus.imaging.manifest.DownloadManifestFactory.java

private static final String nodeToString(Node node, boolean addDeclaration) throws Exception {
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    if (!addDeclaration)
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    Writer out = new StringWriter();
    tf.transform(new DOMSource(node), new StreamResult(out));
    return out.toString();
}

From source file:com.hpe.application.automation.tools.octane.executor.TestExecutionJobCreatorService.java

private static String prepareMtbxData(List<TestExecutionInfo> tests) throws IOException {
    /*<Mtbx>//  w w  w  .j av  a  2s. c o  m
    <Test name="test1" path="${WORKSPACE}\${CHECKOUT_SUBDIR}\APITest1">
     <Parameter name="A" value="abc" type="string"/>
     <DataTable path="${WORKSPACE}\aa\bbb.xslx"/>
      .
     </Test>
     <Test name="test2" path="${WORKSPACE}\${CHECKOUT_SUBDIR}\test2">
    <Parameter name="p1" value="123" type="int"/>
    <Parameter name="p4" value="123.4" type="float"/>
     .
     </Test>
    </Mtbx>*/

    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("Mtbx");
        doc.appendChild(rootElement);

        for (TestExecutionInfo test : tests) {
            Element testElement = doc.createElement("Test");
            String packageAndTestName = (StringUtils.isNotEmpty(test.getPackageName())
                    ? test.getPackageName() + "\\"
                    : "") + test.getTestName();
            testElement.setAttribute("name", packageAndTestName);
            String path = "${WORKSPACE}\\${CHECKOUT_SUBDIR}"
                    + (StringUtils.isEmpty(test.getPackageName()) ? ""
                            : OctaneConstants.General.WINDOWS_PATH_SPLITTER + test.getPackageName())
                    + OctaneConstants.General.WINDOWS_PATH_SPLITTER + test.getTestName();
            testElement.setAttribute("path", path);

            if (StringUtils.isNotEmpty(test.getDataTable())) {
                Element dataTableElement = doc.createElement("DataTable");
                dataTableElement.setAttribute("path", "${WORKSPACE}\\${CHECKOUT_SUBDIR}"
                        + OctaneConstants.General.WINDOWS_PATH_SPLITTER + test.getDataTable());
                testElement.appendChild(dataTableElement);
            }

            rootElement.appendChild(testElement);
        }

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));

        return writer.toString();
    } catch (Exception e) {
        throw new IOException("Failed to build MTBX content : " + e.getMessage());
    }

}

From source file:com.ToResultSet.java

public static String getDocumentAsXml(Document doc)
        throws TransformerConfigurationException, TransformerException {
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    // we want to pretty format the XML output
    // note : this is broken in jdk1.5 beta!
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    ///* ww  w.  j  a v  a 2s  . com*/
    java.io.StringWriter sw = new java.io.StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    return sw.toString();
}