Example usage for javax.xml.transform Transformer setOutputProperties

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

Introduction

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

Prototype

public abstract void setOutputProperties(Properties oformat);

Source Link

Document

Set the output properties for the transformation.

Usage

From source file:Main.java

public static void addApp(String file, String name, Hashtable<String, String> attri) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    FileInputStream is = new FileInputStream(file);

    Document doc = dombuilder.parse(is);

    NodeList nodeList = doc.getElementsByTagName("app");
    if (nodeList != null && nodeList.getLength() >= 1) {
        Node deviceNode = nodeList.item(0);
        Element device = doc.createElement("aut");
        device.setTextContent(name);//w  w w  .  j av  a 2 s .c o  m
        for (Iterator itrName = attri.keySet().iterator(); itrName.hasNext();) {
            String attriKey = (String) itrName.next();
            String attriValue = (String) attri.get(attriKey);
            device.setAttribute(attriKey, attriValue);
        }
        deviceNode.appendChild(device);
    }

    //save
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    Properties props = t.getOutputProperties();
    props.setProperty(OutputKeys.ENCODING, "GB2312");
    t.setOutputProperties(props);
    DOMSource dom = new DOMSource(doc);
    StreamResult sr = new StreamResult(file);
    t.transform(dom, sr);
}

From source file:Main.java

/**
 * Converts a dom to a String/*from   w w w  . j a va  2s  .c om*/
 * 
 * @param dom
 *            dom to convert
 * @param outputProperties
 *            the properties for the String representation of the XML
 * @return the dom as a String
 */
public static String writeDomToString(Element dom, Properties outputProperties) {
    try {
        StringWriter ret = new StringWriter();
        // transFact.setAttribute("indent-number", 2);
        Transformer transformer = transFactory.newTransformer();
        if (outputProperties != null)
            transformer.setOutputProperties(outputProperties);
        DOMSource source = new DOMSource(dom);
        StreamResult result = new StreamResult(ret);
        transformer.transform(source, result);
        return ret.toString();
    } catch (Exception e) {
        throw new RuntimeException("Could not write dom to string!", e);
    }
}

From source file:Main.java

/**
 * Converts a dom to a String/*  w w  w .j ava 2  s  .co m*/
 * 
 * @param dom
 *            dom to convert
 * @param outputProperties
 *            the properties for the String representation of the XML
 * @return the dom as a String
 */
public static String writeDomToString(Document dom, Properties outputProperties) {
    try {
        StringWriter ret = new StringWriter();
        // transFact.setAttribute("indent-number", 2);
        Transformer transformer = transFactory.newTransformer();
        if (outputProperties != null)
            transformer.setOutputProperties(outputProperties);
        DOMSource source = new DOMSource(dom);
        StreamResult result = new StreamResult(ret);
        transformer.transform(source, result);
        return ret.toString();
    } catch (Exception e) {
        throw new RuntimeException("Could not write dom to string!", e);
    }
}

From source file:Main.java

/**
 * This method performs XSL Transformation. <br>
 * <b>Deprecated use XmlTransformer.transform</b>
 * //from   www  .ja v a  2 s  .  c o  m
 * @param source
 *            The input XML document
 * @param stylesheet
 *            The XSL stylesheet
 * @param params
 *            parameters to apply to the XSL Stylesheet
 * @param outputProperties
 *            properties to use for the xsl transform. Will overload the xsl output definition.
 * @return The output document transformed
 * @throws Exception
 *             The exception
 */
@Deprecated
public static String transform(Source source, Source stylesheet, Map<String, String> params,
        Properties outputProperties) throws Exception {
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(stylesheet);

        if (outputProperties != null) {
            transformer.setOutputProperties(outputProperties);
        }

        if (params != null) {
            transformer.clearParameters();

            for (Entry<String, String> entry : params.entrySet()) {
                String name = entry.getKey();
                String value = entry.getValue();
                transformer.setParameter(name, value);
            }
        }

        StringWriter sw = new StringWriter();
        Result result = new StreamResult(sw);
        transformer.transform(source, result);

        return sw.toString();
    } catch (TransformerConfigurationException e) {
        String strMessage = e.getMessage();

        if (e.getLocationAsString() != null) {
            strMessage += ("- location : " + e.getLocationAsString());
        }

        throw new Exception("Error transforming document XSLT : " + strMessage, e.getCause());
    } catch (TransformerFactoryConfigurationError e) {
        throw new Exception("Error transforming document XSLT : " + e.getMessage(), e);
    } catch (TransformerException e) {
        String strMessage = e.getMessage();

        if (e.getLocationAsString() != null) {
            strMessage += ("- location : " + e.getLocationAsString());
        }

        throw new Exception("Error transforming document XSLT : " + strMessage, e.getCause());
    } catch (Exception e) {
        throw new Exception("Error transforming document XSLT : " + e.getMessage(), e);
    }
}

From source file:Main.java

public static void output(Document doc, OutputStream outputStream) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    tFactory.setAttribute("indent-number", 4);

    Transformer transformer = tFactory.newTransformer();
    Properties outputProperties = new Properties();
    outputProperties.put(OutputKeys.INDENT, "yes");
    outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperties(outputProperties);

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new OutputStreamWriter(outputStream, "UTF-8"));
    transformer.transform(source, result);
}

From source file:Main.java

public static void save(String paramString, Document paramDocument) throws Exception {
    DOMSource localDOMSource = new DOMSource(paramDocument);
    File localFile1 = new File(paramString);
    File localFile2 = localFile1.getParentFile();
    localFile2.mkdirs();//from ww w . j  a  v  a  2  s . c om
    StreamResult localStreamResult = new StreamResult(localFile1);
    try {
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        Properties localProperties = localTransformer.getOutputProperties();
        localProperties.setProperty("encoding", "UTF-8");
        localProperties.setProperty("indent", "yes");
        localTransformer.setOutputProperties(localProperties);
        localTransformer.transform(localDOMSource, localStreamResult);
    } catch (TransformerConfigurationException localTransformerConfigurationException) {
        localTransformerConfigurationException.printStackTrace();
    } catch (TransformerException localTransformerException) {
        localTransformerException.printStackTrace();
    }
}

From source file:eu.scidipes.toolkits.palibrary.utils.XMLUtils.java

private static String nodeToString(final Node doc, final Properties outputProperties) {
    try {/*w  w w . j  a va2s  . co  m*/
        final Transformer transformer = transformerFactory.newTransformer();
        outputProperties.put(OutputKeys.METHOD, "xml");
        outputProperties.put(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperties(outputProperties);
        final StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (final TransformerException e) {
        LOG.error(e.getMessage(), e);
    }
    return "";
}

From source file:Main.java

public static String getXml(Document doc) {
    DOMSource doms = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    String xml = null;//from   w  w  w  .  ja v  a  2  s .  com
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        Properties properties = t.getOutputProperties();
        properties.setProperty(OutputKeys.ENCODING, "GB2312");
        properties.setProperty(OutputKeys.METHOD, "xml");//!
        properties.setProperty(OutputKeys.VERSION, "1.0");
        properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperties(properties);
        t.transform(doms, sr);
        String dtd = doc.getDoctype().getInternalSubset();
        if ((null != dtd) && (dtd.length() > 0)) {
            dtd = "\n<!DOCTYPE Catalog [\n" + dtd + "]>\n";
        }
        ;
        xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" + dtd;
        xml += sw.toString();
    } catch (TransformerConfigurationException tce) {
        //"Transformer Configuration Exception\n-----"
    } catch (TransformerException te) {
        //"Transformer Exception\n---------"
    }
    return xml;
}

From source file:DomUtil.java

/**
 * Work method for public save() methods.
 *///ww w  .ja v  a 2s  .com
private static void saveImpl(Document document, StreamResult output, Properties outputProperties)
        throws SAXException {
    try {
        TransformerFactory tFactory = getTransformerFactory();
        Transformer transformer = tFactory.newTransformer();
        if (outputProperties != null) {
            transformer.setOutputProperties(outputProperties);
        }
        DOMSource source = new DOMSource(document);

        transformer.transform(source, output);
    } catch (TransformerException ex) {
        throw new SAXException("Unable to write document to OutputStream.", ex);
    }
}

From source file:XMLUtils.java

public static String toString(Source source, Properties props) throws TransformerException, IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StreamResult sr = new StreamResult(bos);
    Transformer trans = newTransformer();
    if (props == null) {
        props = new Properties();
        props.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
    }// w ww .ja v  a 2  s . c o  m
    trans.setOutputProperties(props);
    trans.transform(source, sr);
    bos.close();
    return bos.toString();
}