Example usage for javax.xml.transform OutputKeys INDENT

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

Introduction

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

Prototype

String INDENT

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

Click Source Link

Document

indent = "yes" | "no".

Usage

From source file:IOUtils.java

public static ContentHandler getSerializer(File file) throws IOException, TransformerException {
    final FileWriter writer = new FileWriter(file);

    final TransformerHandler transformerHandler = FACTORY.newTransformerHandler();
    final Transformer transformer = transformerHandler.getTransformer();

    final Properties format = new Properties();
    format.put(OutputKeys.METHOD, "xml");
    format.put(OutputKeys.OMIT_XML_DECLARATION, "no");
    format.put(OutputKeys.ENCODING, "UTF-8");
    format.put(OutputKeys.INDENT, "yes");
    transformer.setOutputProperties(format);

    transformerHandler.setResult(new StreamResult(writer));

    try {//  w  w  w  .j  a  v a2s.co  m
        if (needsNamespacesAsAttributes(format)) {
            return new NamespaceAsAttributes(transformerHandler);
        }
    } catch (SAXException se) {
        throw new TransformerException("Unable to detect of namespace support for sax works properly.", se);
    }
    return transformerHandler;
}

From source file:com.github.woozoo73.ht.XmlUtils.java

public static String getContent(String path) {
    try {/*w w w .  j a  v a2 s.com*/
        URL url = XmlUtils.class.getClassLoader().getResource(path);
        File schemaLocation = new File(url.toURI());

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(schemaLocation);

        StringWriter writer = new StringWriter();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(new DOMSource(document), new StreamResult(writer));

        String content = writer.getBuffer().toString();

        return content;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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  w  w . j a  v  a 2  s . 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:Main.java

/**
 * Serialize an XML Element into a String.
 * @param df DocumentFragment to be serialized.
 * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration.
 * @return String representation of the XML document fragment.
 *//*from  w  w w  . ja  v  a2  s. co m*/
public static final String serialize(DocumentFragment df, boolean omitXMLDeclaration) {
    if (df != null) {
        try {
            DOMSource domSource = new DOMSource(df);
            Transformer serializer = TransformerFactory.newInstance().newTransformer();
            serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    ((omitXMLDeclaration) ? "yes" : "no"));
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING);
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            serializer.transform(domSource, new StreamResult(baos));
            baos.close();
            return new String(baos.toByteArray(), UTF8_ENCODING);
        } catch (Throwable t) {
        }
    }
    return null;
}

From source file:com.github.jjYBdx4IL.utils.fma.FMAConfig.java

public static String formatXml(String xml) {

    try {/*  ww w.j ava2 s  .c  o m*/
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();

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

        Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());

        serializer.transform(xmlSource, res);

        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());

    } catch (IllegalArgumentException | TransformerException e) {
        LOG.error("", e);
        return xml;
    }
}

From source file:org.openmrs.module.metadatasharing.converter.ConverterEngine.java

private static String toXML(Node doc) throws SerializationException {
    try {// ww  w  .  j  a va 2  s.c  o m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        transformer.transform(new DOMSource(doc), result);
        return result.getWriter().toString();
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}

From source file:Main.java

/**
 * Save the XML document to an output stream.
 * /*w w w  .j a  v  a2s . c o  m*/
 * @param doc
 *            The XML document to save.
 * @param outStream
 *            The stream to save the document to.
 * @param encoding
 *            The encoding to save the file as.
 * 
 * @throws TransformerException
 *             If there is an error while saving the XML.
 */
public static void save(Node doc, OutputStream outStream, String encoding) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    // initialize StreamResult with File object to save to file
    Result result = new StreamResult(outStream);
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
}

From source file:Main.java

private static String docToString(Document doc) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

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

    return result.getWriter().toString();
}

From source file:Main.java

public static void getStringFromXML(Node node, String dtdFilename, Writer outputWriter)
        throws TransformerException {
    File dtdFile = null;//from   w ww  . j a  va 2  s  .c om
    String workingPath = null;
    if (dtdFilename != null) {
        dtdFile = new File(dtdFilename);
        workingPath = System.setProperty("user.dir", dtdFile.getAbsoluteFile().getParent());
    }
    try {
        if (node instanceof Document)
            node = ((Document) node).getDocumentElement();

        TransformerFactory tranFact = TransformerFactory.newInstance();
        Transformer tf = tranFact.newTransformer();
        if (dtdFile != null) {
            tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdFile.getName());
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
        }

        Source src = new DOMSource(node);
        Result dest = new StreamResult(outputWriter);

        tf.transform(src, dest);
    } finally {
        if (workingPath != null)
            System.setProperty("user.dir", workingPath);
    }
}

From source file:com.jaeksoft.searchlib.util.XmlWriter.java

public XmlWriter(PrintWriter out, String encoding) throws TransformerConfigurationException, SAXException {
    StreamResult streamResult = new StreamResult(out);
    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    transformerHandler = tf.newTransformerHandler();
    Transformer serializer = transformerHandler.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, encoding);
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformerHandler.setResult(streamResult);
    startedElementStack = new Stack<String>();
    transformerHandler.startDocument();//from www  .ja va 2 s  .c  om
    elementAttributes = new AttributesImpl();
    Pattern p = Pattern.compile("\\p{Cntrl}");
    controlMatcher = p.matcher("");

}