Example usage for javax.xml.transform OutputKeys STANDALONE

List of usage examples for javax.xml.transform OutputKeys STANDALONE

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys STANDALONE.

Prototype

String STANDALONE

To view the source code for javax.xml.transform OutputKeys STANDALONE.

Click Source Link

Document

standalone = "yes" | "no".

Usage

From source file:Main.java

public static void marshalToStream(Document doc, OutputStream ostream, boolean indent) throws Exception {
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    if (indent) {
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    }/*from   w  ww  . j a v  a 2  s .  c  o  m*/
    trans.setOutputProperty(OutputKeys.STANDALONE, "no");

    trans.transform(new DOMSource(doc), new StreamResult(ostream));
}

From source file:Main.java

public static void marshalToStream(Element elm, OutputStream ostream, boolean indent) throws Exception {
    TransformerFactory transFac = TransformerFactory.newInstance();
    Transformer trans = transFac.newTransformer();
    if (indent) {
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    }//from  w  ww. j a  v  a 2 s  .  c  o m
    trans.setOutputProperty(OutputKeys.STANDALONE, "no");

    trans.transform(new DOMSource(elm), new StreamResult(ostream));
}

From source file:Main.java

public static void flush(Document in, OutputStream out) throws RuntimeException {
    try {/* ww w  . ja va2s. c  o m*/
        // Write the content into XML file
        Transformer transformer = transformerFactory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "no");

        transformer.transform(new DOMSource(in), new StreamResult(out));
    } catch (TransformerException e) {
        throw new RuntimeException("Exception flush document", e);
    }
}

From source file:Main.java

public static Transformer getTransformer(boolean standalone, boolean indent, int indentNumber,
        boolean omitXmlDeclaration) throws TransformerException {
    TransformerFactory f = TransformerFactory.newInstance();
    f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
    if (indent) {
        f.setAttribute("indent-number", indentNumber);
    }//from  w ww .  ja  v  a  2 s. com

    Transformer t = f.newTransformer();
    if (standalone) {
        t.setOutputProperty(OutputKeys.STANDALONE, "yes");
    }
    if (indent) {
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty("{xml.apache.org/xslt}indent-amount", "" + indentNumber);
    }
    if (omitXmlDeclaration) {
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }

    return t;
}

From source file:Main.java

public static String toString(final Document document) {
    try {/*w ww.  j ava2s.  com*/
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        final TransformerFactory tf = TransformerFactory.newInstance();
        final Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
        //http://johnsonsolutions.blogspot.ca/2007/08/xml-transformer-indent-doesnt-work-with.html
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(document), new StreamResult(baos));

        final String result = baos.toString();
        return result;
    } catch (final TransformerException e) {
        throw new Error(e);
    }
}

From source file:Main.java

/**
 * Saves a WC3 DOM by writing it to an XML document.
 *
 * @param doc The WC3 DOM document object.
 * @param docPath The full path to the XML document.
 * @param encoding Encoding scheme to use for the XML document, e.g.,
 * "UTF-8."//from w  w w.j a v  a2  s . co m
 * @throws TransformerConfigurationException
 * @throws FileNotFoundException
 * @throws UnsupportedEncodingException
 * @throws TransformerException
 * @throws IOException
 */
public static void saveDocument(final Document doc, String encoding, String docPath)
        throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException,
        TransformerException, IOException {
    TransformerFactory xf = TransformerFactory.newInstance();
    xf.setAttribute("indent-number", 1); //NON-NLS
    Transformer xformer = xf.newTransformer();
    xformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NON-NLS
    xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS
    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    xformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //NON-NLS
    xformer.setOutputProperty(OutputKeys.VERSION, "1.0");
    File file = new File(docPath);
    try (FileOutputStream stream = new FileOutputStream(file)) {
        Result out = new StreamResult(new OutputStreamWriter(stream, encoding));
        xformer.transform(new DOMSource(doc), out);
        stream.flush();
    }
}

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);
    }//from   w w w . j  ava  2s. c  om
    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:org.opencastproject.remotetest.util.Utils.java

/**
 * Converts the node to a string representation.
 * /*  ww w .  jav a2  s .co  m*/
 * @param node
 *          the node
 * @return the string representation
 * @throws Exception
 */
public static String nodeToString(Node node) throws Exception {
    DOMSource domSource = new DOMSource(node);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(out);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(domSource, result);
    InputStream in = new ByteArrayInputStream(out.toByteArray());
    return IOUtils.toString(in, "UTF-8");
}

From source file:Main.java

private static void writeTo(Result result, Document document) throws TransformerConfigurationException,
        TransformerException, FileNotFoundException, UnsupportedEncodingException {

    // Use a Transformer for output
    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", new Integer(4));

    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(document);

    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
    transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N

    // indent the output to make it more legible... 
    try {/*from   ww  w  .  j a v a2  s .c o m*/
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
    } catch (IllegalArgumentException e) {
        // the JAXP implementation doesn't support indentation, no big deal
        //e.printStackTrace();
    }

    transformer.transform(source, result);
}

From source file:Main.java

/**
 * DOCUMENT ME!/*  w  ww .ja va  2s . c o  m*/
 *
 * @return DOCUMENT ME!
 *
 * @throws TransformerConfigurationException DOCUMENT ME!
 * @throws TransformerException DOCUMENT ME!
 * @throws Exception DOCUMENT ME!
 */
public static byte[] writeToBytes(Document document)
        throws TransformerConfigurationException, TransformerException, Exception {
    byte[] ret = null;

    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", new Integer(4));

    Transformer transformer = tFactory.newTransformer();
    DOMSource source = new DOMSource(document);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(bos);
    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
    transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); // NOI18N
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); // NOI18N

    // indent the output to make it more legible...
    try {
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N
    } catch (IllegalArgumentException e) {
        // the JAXP implementation doesn't support indentation, no big deal
        //e.printStackTrace();
    }

    try {
        transformer.transform(source, result);
        ret = bos.toByteArray();
    } finally {
        if (bos != null) {
            bos.flush();
            bos.close();
        }
    }

    return ret;
}