Example usage for javax.xml.transform OutputKeys ENCODING

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

Introduction

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

Prototype

String ENCODING

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

Click Source Link

Document

encoding = string.

Usage

From source file:com.maxl.java.aips2sqlite.AllDown.java

private String prettyFormat(String input) {
    try {/*  w  ww .  j ava 2 s .  co  m*/
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:com.krawler.portal.tools.ServiceBuilder.java

public void writeToSourceCfgXml(String fileName) {
    try {//  w  ww. ja v a 2s .co m
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(PropsValues.CFG_SOURCE_FILE_PATH);
        Node hibernate_conf = doc.getChildNodes().item(1);
        //            .getChildNodes().item(1);
        Node SessionFac = hibernate_conf.getChildNodes().item(1);
        Element mapping = doc.createElement("mapping");
        mapping.setAttribute("resource", PropsValues.PACKAGE_FILE_PATH + fileName + ".hbm.xml");
        SessionFac.getChildNodes().getLength();
        SessionFac.appendChild(mapping);
        DOMSource ds = new DOMSource(doc);
        StreamResult sr = new StreamResult(PropsValues.CFG_SOURCE_FILE_PATH);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        trans.setOutputProperty(OutputKeys.VERSION, "1.0");
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
        trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Hibernate/Hibernate Configuration DTD 3.0//EN");
        trans.transform(ds, sr);
        //            writeToClassesCfgXml(fileName);
    } catch (TransformerException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (SAXException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (IOException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (ParserConfigurationException ex) {
        logger.warn(ex.getMessage(), ex);
    }
}

From source file:com.krawler.portal.tools.ServiceBuilder.java

public void writeToClassesCfgXml(String fileName) {
    try {/*from   www . j  a  v a 2 s  .c o  m*/
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        //            Document doc = docBuilder.parse(PropsValues.CFG_SOURCE_FILE_PATH);
        Document doc = docBuilder.parse(PropsValues.CFG_CLASSES_FILE_PATH);
        Node hibernate_conf = doc.getChildNodes().item(1);
        //            .getChildNodes().item(1);
        Node SessionFac = hibernate_conf.getChildNodes().item(1);
        Element mapping = doc.createElement("mapping");
        mapping.setAttribute("resource", PropsValues.PACKAGE_FILE_PATH + fileName + ".hbm.xml");
        SessionFac.getChildNodes().getLength();
        SessionFac.appendChild(mapping);
        DOMSource ds = new DOMSource(doc);
        //            StreamResult sr = new StreamResult(PropsValues.CFG_SOURCE_FILE_PATH);
        StreamResult sr = new StreamResult(PropsValues.CFG_CLASSES_FILE_PATH);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        trans.setOutputProperty(OutputKeys.VERSION, "1.0");
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
        trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Hibernate/Hibernate Configuration DTD 3.0//EN");
        trans.transform(ds, sr);

    } catch (TransformerException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (SAXException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (IOException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (ParserConfigurationException ex) {
        logger.warn(ex.getMessage(), ex);
    }

}

From source file:com.krawler.portal.tools.ServiceBuilder.java

public void deleteSourceEntryCfgXml(String fileName) {
    try {/*from   w  w  w.ja  v  a2  s  . co m*/
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(PropsValues.CFG_SOURCE_FILE_PATH);
        Node hibernate_conf = doc.getChildNodes().item(1);
        Node SessionFac = hibernate_conf.getChildNodes().item(1);
        Element sesFac = (Element) SessionFac;
        NodeList mapping_lists = sesFac.getElementsByTagName("mapping");
        Node toDelete = null;
        for (int num = 0; num < mapping_lists.getLength(); num++) {
            Element mapEle = (Element) mapping_lists.item(num);
            if (mapEle.getAttribute("resource").equals(PropsValues.PACKAGE_FILE_PATH + fileName + ".hbm.xml")) {
                toDelete = mapEle;
                break;
            }
        }
        sesFac.removeChild(toDelete);
        DOMSource ds = new DOMSource(doc);
        StreamResult sr = new StreamResult(PropsValues.CFG_SOURCE_FILE_PATH);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        trans.setOutputProperty(OutputKeys.VERSION, "1.0");
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
        trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Hibernate/Hibernate Configuration DTD 3.0//EN");
        trans.transform(ds, sr);
    } catch (TransformerException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (SAXException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (IOException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (ParserConfigurationException ex) {
        logger.warn(ex.getMessage(), ex);
    }
}

From source file:com.krawler.portal.tools.ServiceBuilder.java

public void deleteClassesEntryCfgXml(String fileName) {
    try {//from ww w .  ja va 2  s . c  om
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(PropsValues.CFG_CLASSES_FILE_PATH);
        Node hibernate_conf = doc.getChildNodes().item(1);
        Node SessionFac = hibernate_conf.getChildNodes().item(1);
        Element sesFac = (Element) SessionFac;
        NodeList mapping_lists = sesFac.getElementsByTagName("mapping");
        Node toDelete = null;
        for (int num = 0; num < mapping_lists.getLength(); num++) {
            Element mapEle = (Element) mapping_lists.item(num);
            if (mapEle.getAttribute("resource").equals(PropsValues.PACKAGE_FILE_PATH + fileName + ".hbm.xml")) {
                toDelete = mapEle;
                break;
            }
        }
        sesFac.removeChild(toDelete);
        DOMSource ds = new DOMSource(doc);
        StreamResult sr = new StreamResult(PropsValues.CFG_CLASSES_FILE_PATH);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        trans.setOutputProperty(OutputKeys.VERSION, "1.0");
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
        trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Hibernate/Hibernate Configuration DTD 3.0//EN");
        trans.transform(ds, sr);
    } catch (TransformerException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (SAXException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (IOException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (ParserConfigurationException ex) {
        logger.warn(ex.getMessage(), ex);
    }
}

From source file:com.zacwolf.commons.wbxcon.WBXCONorg.java

/**
 * This is a helper method that returns a String form of a DOM {@link org.w3c.dom.Node} Object
 * starting at the parent/child level specified by root node
 * @param rootnode   the DOM {@link org.w3c.dom.Node} to use as the root
 * @return an XML form of <code>rootnode</code> and any children
 * @throws IOException/*  w  w  w .  ja v  a 2  s  . co m*/
 * @throws TransformerException
 */
final static String documentToXMLstring(final Node rootnode) throws TransformerException {
    final StringWriter out = new StringWriter();
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.setOutputProperty(OutputKeys.ENCODING, org.apache.http.Consts.UTF_8.displayName());
    transformer.transform(new DOMSource(rootnode), new StreamResult(out));
    return out.toString();
}

From source file:org.openmrs.module.dhisconnector.api.impl.DHISConnectorServiceImpl.java

private String beautifyXML(String xml) {
    if (StringUtils.isNotBlank(xml)) {
        try {/*from  w  w w  .j  av a  2 s  .  c  om*/
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                    .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
            Transformer tf = TransformerFactory.newInstance().newTransformer();

            tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            tf.setOutputProperty(OutputKeys.INDENT, "yes");

            Writer out = new StringWriter();

            tf.transform(new DOMSource(document), new StreamResult(out));

            return out.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
    return xml;
}

From source file:com.zacwolf.commons.wbxcon.WBXCONorg.java

/**
 * This is a helper method that returns a String form of a {@link org.w3c.dom.Document}
 * object, fully indented into a "pretty print" format.
 * @param doc   {@link org.w3c.dom.Document} to be parsed
 * @param out   OutputStream to copy the text into
 * @throws IOException//w ww .  jav  a  2  s . c  o m
 * @throws TransformerException
 */
final static void documentPrettyPrint(final Document doc, final OutputStream out)
        throws IOException, TransformerException {
    final Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, org.apache.http.Consts.UTF_8.displayName());
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.transform(new DOMSource(doc), new StreamResult(out));
}

From source file:gov.va.ds4p.ds4pmobileportal.ui.eHealthExchange.java

private String converXmlDocToString(Document xmlDocument) {

    String xmlString = "";

    try {/*w  ww.j  av  a  2s .  c  om*/
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer));
        xmlString = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return xmlString;
}

From source file:com.maxl.java.aips2xml.Aips2Xml.java

static String prettyFormat(String input) {
    try {/*from ww w. ja v  a  2s .  com*/
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}