List of usage examples for org.w3c.dom.ls LSOutput setEncoding
public void setEncoding(String encoding);
From source file:org.intellij.xquery.runner.rt.vendor.exist.ExistRunnerApp.java
private String prettyPrint(Element node, DOMImplementation domImplementation) throws InstantiationException, IllegalAccessException, ClassNotFoundException { DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry .getDOMImplementation("LS"); LSOutput lsOutput = domImplementationLS.createLSOutput(); LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); lsOutput.setEncoding("UTF-8"); Writer stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(node, lsOutput);/* ww w .j av a2 s . c om*/ String result = stringWriter.toString(); return result; }
From source file:org.ms123.common.importing.XmlImporter.java
private String prettyPrint(Element e) { DOMImplementation domImplementation = m_document.getImplementation(); if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) { DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0"); LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); DOMConfiguration domConfiguration = lsSerializer.getDomConfig(); if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) { lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(e, lsOutput); return stringWriter.toString(); } else {//from w w w. j a va 2 s . c o m throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable."); } } else { throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported."); } }
From source file:org.sakaiproject.util.StorageUtils.java
/** * Write a DOM Document to an xml file./*from ww w.java 2 s. c o m*/ * * @param doc * The DOM Document to write. * @param fileName * The complete file name path. */ public static void writeDocument(Document doc, String fileName) { OutputStream out = null; try { out = new FileOutputStream(fileName); // get an instance of the DOMImplementation registry DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); DOMImplementationLS feature = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSSerializer serializer = feature.createLSSerializer(); LSOutput output = feature.createLSOutput(); output.setByteStream(out); output.setEncoding("UTF-8"); serializer.write(doc, output); out.close(); } catch (Exception any) { M_log.warn("writeDocument: " + any.toString()); } finally { if (out != null) { try { out.close(); } catch (IOException e) { } } } }
From source file:org.sakaiproject.util.StorageUtils.java
/** * Write a DOM Document to an output stream. * //from ww w. j av a2 s . c o m * @param doc * The DOM Document to write. * @param out * The output stream. */ public static String writeDocumentToString(Document doc) { try { StringWriter sw = new StringWriter(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); DOMImplementation impl = builder.getDOMImplementation(); DOMImplementationLS feature = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSSerializer serializer = feature.createLSSerializer(); LSOutput output = feature.createLSOutput(); output.setCharacterStream(sw); output.setEncoding("UTF-8"); serializer.write(doc, output); sw.flush(); return sw.toString(); } catch (Exception any) { M_log.warn("writeDocumentToString: " + any.toString()); return null; } }
From source file:org.wso2.carbon.mediation.configadmin.util.XMLPrettyPrinter.java
/** * XML Pretty Print method with XML Comments support. * * @return XML formatted String//w w w . j a v a 2 s.c o m */ public String xmlFormatWithComments() { String xmlOutput = null; Document doc; LSSerializer lsSerializer; try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); doc = documentBuilderFactory.newDocumentBuilder().parse(in); DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); lsSerializer = domImplementation.createLSSerializer(); lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); LSOutput lsOutput = domImplementation.createLSOutput(); lsOutput.setEncoding(encoding); Writer stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(doc, lsOutput); xmlOutput = stringWriter.toString(); } catch (IOException e) { log.error("XML Pretty Printer Error reading data from given InputStream to XML Document ", e); } catch (SAXException e) { log.error("XML Pretty Printer Error parsing the given InputStream to XML Document", e); } catch (Exception e) { log.error("XML Pretty Printer failed. ", e); } return xmlOutput; }
From source file:test.framework.TestBase.java
/** * Serialize the given Dom Object to a String. * /*from ww w. j av a 2 s . c o m*/ * @param xml The Xml Node to serialize. * @param omitXMLDeclaration Indicates if XML declaration will be omitted. * @return The String representation of the Xml Node. * @throws Exception If anything fails. */ protected static String toString(final Node xml, final boolean omitXMLDeclaration) throws Exception { String result = new String(); if (xml instanceof AttrImpl) { result = xml.getTextContent(); } else if (xml instanceof Document) { StringWriter stringOut = new StringWriter(); // format OutputFormat format = new OutputFormat((Document) xml); format.setIndenting(true); format.setPreserveSpace(false); format.setOmitXMLDeclaration(omitXMLDeclaration); format.setEncoding("UTF-8"); // serialize XMLSerializer serial = new XMLSerializer(stringOut, format); serial.asDOMSerializer(); serial.serialize((Document) xml); result = stringOut.toString(); } else { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSOutput lsOutput = impl.createLSOutput(); lsOutput.setEncoding("UTF-8"); ByteArrayOutputStream os = new ByteArrayOutputStream(); lsOutput.setByteStream(os); LSSerializer writer = impl.createLSSerializer(); // result = writer.writeToString(xml); writer.write(xml, lsOutput); result = ((ByteArrayOutputStream) lsOutput.getByteStream()).toString(); if ((omitXMLDeclaration) && (result.indexOf("?>") != -1)) { result = result.substring(result.indexOf("?>") + 2); } // result = toString(getDocument(writer.writeToString(xml)), // true); } return result; }