Example usage for javax.xml.transform TransformerFactory setAttribute

List of usage examples for javax.xml.transform TransformerFactory setAttribute

Introduction

In this page you can find the example usage for javax.xml.transform TransformerFactory setAttribute.

Prototype

public abstract void setAttribute(String name, Object value);

Source Link

Document

Allows the user to set specific attributes on the underlying implementation.

Usage

From source file:Main.java

public static Templates getTemplatesByName(File xslF) {

    Templates templates = null;/*from   w w w .  jav  a2s.c  o  m*/
    TransformerFactory tfactory = TransformerFactory.newInstance();
    tfactory.setAttribute("http://xml.apache.org/xalan/features/incremental", java.lang.Boolean.TRUE);

    InputStream is = null;
    try {
        StreamSource ss = new StreamSource(xslF);
        is = ss.getInputStream();
        templates = tfactory.newTemplates(ss);
        if (is != null) {
            is.close();
            is = null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
                is = null;
            }
        } catch (Throwable t) {
            System.out.println(t);
        }
    }

    return templates;
}

From source file:Main.java

/**
 * Print the indented document./*from www .jav  a2 s  . co  m*/
 * 
 * @param stream
 * @param document
 * 
 * @throws UnsupportedEncodingException 
 * @throws TransformerExceptions 
 */
public static void printXMLFormat(Document document) throws UnsupportedEncodingException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 4); //$NON-NLS-1$
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$

    StreamResult streamResult = new StreamResult(); //$NON-NLS-1$

    DOMSource source = new DOMSource(document);
    transformer.transform(source, streamResult);
}

From source file:Main.java

public static String prettyFormat(String input, int indent) {
    try {//from   w  w w.  ja  va  2s  .co  m
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:Main.java

/**
 * Formatte un XML//from www  .  ja v  a  2 s . c o  m
 * */
public static String prettyFormat(String input, int indent) {
    try {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Throwable e) {
        try {
            Source xmlInput = new StreamSource(new StringReader(input));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Throwable t) {
            return input;
        }
    }
}

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."/*w  w w. j  a  va 2  s  .c  o 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

public static String formatXmlAsString(Document document) {

    StringWriter writer = new StringWriter();
    TransformerFactory factory = TransformerFactory.newInstance();

    try {/*  ww w .j a v a  2s. c o m*/

        factory.setAttribute("indent-number", new Integer(2));
    } catch (IllegalArgumentException ex) {

    }

    try {

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

        transformer.transform(new DOMSource(document), new StreamResult(writer));
    } catch (TransformerException ex) {

        throw new RuntimeException("Error formatting xml as pretty-printed string", ex);
    }

    return writer.toString();
}

From source file:Main.java

/**
 * @param xsltFile The XSLT stylesheet file
 * @param parms XSL parameters to pass to the stylesheet.
 * @return the configured XSLT transformer
 * @throws TransformerException if there is a problem configuring the transformer
 */// w  w  w .  j a v a  2s . c  om
static Transformer createTransformer(final File xsltFile, final Map<String, String> parms)
        throws TransformerException {
    try {
        Source xslSource = new StreamSource(xsltFile);
        TransformerFactory transFact = TransformerFactory.newInstance();
        transFact.setAttribute("indent-number", 2);
        Transformer transformer = transFact.newTransformer(xslSource);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        for (String parm : parms.keySet()) {
            transformer.setParameter(parm, parms.get(parm));
        }
        return transformer;
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:Main.java

public static void prettyFormatXml(final InputStream xml, final OutputStream os, final int indent) {
    try {//from   w  w w  . jav  a 2 s .c  o m
        final Source xmlInput = new StreamSource(xml);
        final StreamResult xmlOutput = new StreamResult(os);
        final TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        final Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Pretty format a given XML document/*from  w w w  . j a v a  2 s .c om*/
 *
 * @param strInput
 *            Valid XML document (No validity check yet!)
 * @param nIndent
 *            Indent
 * @return Formatted XML document
 * @throws Exception
 *             in error case
 */
public static String prettyFormat(String strInput, int nIndent) throws Exception {
    try {
        Source xmlInput = new StreamSource(new StringReader(strInput));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", nIndent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(nIndent));
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Pretty formatting: " + e.getMessage(), LogLevel.Error);
        throw e;
    }
}

From source file:Main.java

public static String prettyXML(String xml, int indent) {
    try {//from www  .  j  av  a  2s. com
        // Turn xml string into a document
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

        // Remove whitespaces outside tags
        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
                XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            node.getParentNode().removeChild(node);
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter stringWriter = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
        return stringWriter.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}