Example usage for javax.xml.transform Transformer getOutputProperties

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

Introduction

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

Prototype

public abstract Properties getOutputProperties();

Source Link

Document

<p>Get a copy of the output properties for the transformation.</p> <p>The properties returned should contain properties set by the user, and properties set by the stylesheet, and these properties are "defaulted" by default properties specified by <a href="http://www.w3.org/TR/xslt#output">section 16 of the XSL Transformations (XSLT) W3C Recommendation</a>.

Usage

From source file:Main.java

public static String transferXmlToString(Document doc) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {/*from   www . j a va  2 s  .c  o  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        Properties p = t.getOutputProperties();
        p.setProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperties(p);
        t.transform(new DOMSource(doc), new StreamResult(bos));
    } catch (NullPointerException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    String xmlStr = "";
    try {
        xmlStr = bos.toString("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return xmlStr;
}

From source file:Main.java

private static boolean writeTo(Document doc, String fileName) throws Exception {
    boolean isOver = false;
    DOMSource doms = new DOMSource(doc);
    File f = new File(fileName);
    StreamResult sr = new StreamResult(f);
    try {/*  ww  w  .  ja  v  a 2  s  . c  o  m*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        Properties properties = t.getOutputProperties();
        properties.setProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperties(properties);
        t.transform(doms, sr);
        isOver = true;
    } catch (TransformerConfigurationException tce) {
        tce.printStackTrace();
    } catch (TransformerException te) {
        te.printStackTrace();
    }
    return isOver;
}

From source file:Main.java

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

    Document doc = dombuilder.parse(is);
    NodeList devices = doc.getElementsByTagName("devices");
    NodeList nodeList = doc.getElementsByTagName("device");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node deviceNode = nodeList.item(i);
        if (deviceNode.getTextContent().equals(name)) {
            devices.item(0).removeChild(deviceNode);
        }//  w  w  w. ja va 2s.  c  o m
    }

    //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

public static String getXml(Document doc) {
    DOMSource doms = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    String xml = null;/*from  ww w.j a  v a 2  s  . co m*/
    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:Main.java

public static void addHandset(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("devices");
    if (nodeList != null && nodeList.getLength() >= 1) {
        Node deviceNode = nodeList.item(0);
        Element device = doc.createElement("device");
        device.setTextContent(name);/*from  w  ww.jav a  2s. 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

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);//ww  w .ja  v  a2s  .  c  om
        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

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 av  a  2 s  .c  o m*/
    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:com.c4om.jschematronvalidator.JSchematronValidatorMain.java

/**
 * Prints a {@link org.w3c.dom.Document} to an {@link OutputStream}.
 * @param outputDocument The output document
 * @param outputStream The output stream
 * @param charset the charset.//w  w  w .  j a  va 2 s . c  om
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
private static void printW3CDocumentToOutputStream(Document outputDocument, OutputStream outputStream,
        String charset)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    LOGGER.info("Printing W3C Document to an output stream with encoding '" + charset + "'");
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, charset);
    LOGGER.debug("XML output properties: " + transformer.getOutputProperties().toString());

    DOMSource source = new DOMSource(outputDocument);
    Writer outputWriter = new XmlStreamWriter(outputStream, charset);

    StreamResult result = new StreamResult(outputWriter);

    transformer.transform(source, result);
    LOGGER.info("Document printed");
}

From source file:net.sf.joost.trax.TemplatesImpl.java

/**
 * Gets the static properties for stx:output.
 * @return Properties according to JAXP-Spec or null if an error
 *  is occured./*from w w w  .j a v a  2 s. c  o m*/
 */
public Properties getOutputProperties() {

    try {
        Transformer transformer = newTransformer();
        return transformer.getOutputProperties();
    } catch (TransformerConfigurationException tE) {
        try {
            factory.defaultErrorListener.fatalError(tE);
        } catch (TransformerConfigurationException e) {
        }
        return null;
    }
}

From source file:org.apache.ode.utils.xsl.XslTransformHandler.java

/**
 * Transforms a Source document to a result using the XSL stylesheet referenced
 * by the provided URI. The stylesheet MUST have been parsed previously.
 * @param uri referencing the stylesheet
 * @param source XML document//from   w ww .ja  v a  2  s  .c  o  m
 * @param parameters passed to the stylesheet
 * @param resolver used to resolve includes and imports
 * @return result of the transformation (XSL, HTML or text depending of the output method specified in stylesheet.
 */
public Object transform(QName processQName, URI uri, Source source, Map<QName, Object> parameters,
        URIResolver resolver) {
    Templates tm;
    synchronized (_templateCache) {
        tm = (Templates) _templateCache.get(processQName, uri);
    }
    if (tm == null)
        throw new XslTransformException("XSL sheet" + uri + " has not been parsed before transformation!");
    try {
        Transformer tf = tm.newTransformer();
        tf.setURIResolver(resolver);
        if (parameters != null) {
            for (Map.Entry<QName, Object> param : parameters.entrySet()) {
                tf.setParameter(param.getKey().getLocalPart(), param.getValue());
            }
        }
        String method = tf.getOutputProperties().getProperty("method");
        if (method == null || "xml".equals(method)) {
            DOMResult result = new DOMResult();
            tf.transform(source, result);
            Node node = result.getNode();
            if (node.getNodeType() == Node.DOCUMENT_NODE)
                node = ((Document) node).getDocumentElement();
            if (__log.isDebugEnabled())
                __log.debug("Returned node: type=" + node.getNodeType() + ", " + DOMUtils.domToString(node));
            return node;
        } else {
            // text and html outputs are handled the same way
            StringWriter writerResult = new StringWriter();
            StreamResult result = new StreamResult(writerResult);
            tf.transform(source, result);
            writerResult.flush();
            String output = writerResult.toString();
            if (__log.isDebugEnabled())
                __log.debug("Returned string: " + output);
            return output;
        }
    } catch (TransformerConfigurationException e) {
        throw new XslTransformException(e);
    } catch (TransformerException e) {
        throw new XslTransformException("XSL Transformation failed!", e);
    }
}