List of usage examples for org.w3c.dom.ls LSOutput setEncoding
public void setEncoding(String encoding);
From source file:Main.java
/** * Writes the given document to a stream (pretty-printed) * /*from w w w . j a v a 2 s . c o m*/ * @param doc Document to serialize * @param out the Stream to write to * @param encoding The encoding to use */ public static void writeDocument(Document doc, OutputStream out, String encoding) { if (doc == null) return; LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); LSOutput lsOutput = impl.createLSOutput(); lsOutput.setByteStream(out); lsOutput.setEncoding(encoding); writer.write(doc, lsOutput); }
From source file:Main.java
public static String prettyPrint(Document document) { // Pretty-prints a DOM document to XML using DOM Load and Save's // LSSerializer. // Note that the "format-pretty-print" DOM configuration parameter can // only be set in JDK 1.6+. DOMImplementationRegistry domImplementationRegistry; try {/*from ww w . j ava 2s. c om*/ domImplementationRegistry = DOMImplementationRegistry.newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | ClassCastException e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } DOMImplementation domImplementation = document.getImplementation(); if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) { /*DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation .getFeature("LS", "3.0");*/ DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry .getDOMImplementation("LS"); 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(document, lsOutput); return stringWriter.toString(); } else { 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:Main.java
public static <T extends OutputStream> T lsSerializeDom(Node doc, T byteStream, String encoding) throws Exception { if (doc == null) return byteStream; DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSOutput lsOutput = impl.createLSOutput(); lsOutput.setByteStream(byteStream);/*from w w w . j a va 2s . com*/ encoding = encoding == null ? "UTF-8" : encoding; lsOutput.setEncoding(encoding); impl.createLSSerializer().write(doc, lsOutput); return byteStream; }
From source file:Main.java
/** * Converts a dom element to a String/*from www. j ava 2s . c o m*/ * * @param node * @return the dom as a String */ public static String writeDomToString(Element node) { DOMImplementation domImplementation = node.getOwnerDocument().getImplementation(); if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) { DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0"); LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(node, lsOutput); return stringWriter.toString(); } else { throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported."); } }
From source file:eu.semaine.util.XMLTool.java
/** * Document type to String format conversion * @param document//from w w w. j a v a2 s. co m * @return * @throws Exception * @throws FileNotFoundException */ public static String document2String(Document document) throws SystemConfigurationException { LSSerializer serializer; DOMImplementationLS domImplLS; try { DOMImplementation implementation = DOMImplementationRegistry.newInstance() .getDOMImplementation("XML 3.0"); domImplLS = (DOMImplementationLS) implementation.getFeature("LS", "3.0"); serializer = domImplLS.createLSSerializer(); DOMConfiguration config = serializer.getDomConfig(); config.setParameter("format-pretty-print", Boolean.TRUE); //config.setParameter("canonical-form", Boolean.TRUE); } catch (Exception e) { throw new SystemConfigurationException("Problem instantiating XML serializer code", e); } LSOutput output = domImplLS.createLSOutput(); output.setEncoding("UTF-8"); StringWriter buf = new StringWriter(); output.setCharacterStream(buf); serializer.write(document, output); return buf.toString(); }
From source file:edu.wfu.inotado.helper.MarshalHelper.java
/** * Pretty-Prints the XML/*from w w w . j a v a 2s . co m*/ * * @param xml * @return */ @SuppressWarnings("unused") private String prettyPrintXml(String xml) { DocumentBuilder documentBuilder; StringWriter stringWriter = new StringWriter(); try { documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputStream inputStream = new ByteArrayInputStream(xml.getBytes("UTF8")); Document document = documentBuilder.parse(inputStream); DOMImplementation domImplementation = 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"); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(document, lsOutput); return stringWriter.toString(); } else { log.warn("DOMConfiguration 'format-pretty-print' parameter isn't settable."); } } else { log.warn("DOM 3.0 LS and/or DOM 2.0 Core not supported."); } } catch (ParserConfigurationException e) { log.error("Unable to parse xml: " + xml, e); } catch (UnsupportedEncodingException e) { log.error("Encoding unspported for xml: " + xml, e); } catch (SAXException e) { log.error("SAXException occurred: " + xml, e); } catch (IOException e) { log.error("IOException occurred:" + xml, e); } // return the input string if any error occurs return xml; }
From source file:com.xpn.xwiki.pdf.impl.PdfExportImpl.java
/** * Cleans up an HTML document, turning it into valid XHTML. * // w w w. j ava 2 s.c om * @param input the source HTML to process * @return the cleaned up source */ private String convertToStrictXHtml(String input) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Cleaning HTML: " + input); } try { // First step, Tidy the document StringWriter tidyOutput = new StringWriter(input.length()); this.tidy.parse(new StringReader(input), tidyOutput); // Tidy can't solve duplicate IDs, so it needs to be done manually DocumentBuilder docBuilder = dbFactory.newDocumentBuilder(); docBuilder.setEntityResolver(Utils.getComponent(EntityResolver.class)); String tidied = tidyOutput.toString().trim(); if (StringUtils.isEmpty(tidied)) { tidied = input.trim(); } Document doc = docBuilder.parse(new InputSource(new StringReader(tidied))); List<String> seenIDs = new ArrayList<String>(); this.cleanIDs(doc.getDocumentElement(), seenIDs); // Write back the fixed document to a String LSOutput output = lsImpl.createLSOutput(); StringWriter result = new StringWriter(); output.setCharacterStream(result); LSSerializer serializer = lsImpl.createLSSerializer(); serializer.setNewLine("\n"); output.setEncoding(doc.getXmlEncoding()); serializer.write(doc, output); return result.toString(); } catch (Exception ex) { LOGGER.warn("Failed to tidy document for export: " + ex.getMessage(), ex); return input; } }
From source file:de.escidoc.core.test.EscidocTestBase.java
/** * Serialize the given Dom Object to a String. * /*from www. j av a 2 s . com*/ * @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. */ public 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(true); format.setOmitXMLDeclaration(omitXMLDeclaration); format.setEncoding(DEFAULT_CHARSET); // 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(DEFAULT_CHARSET); ByteArrayOutputStream os = new ByteArrayOutputStream(); lsOutput.setByteStream(os); LSSerializer writer = impl.createLSSerializer(); // result = writer.writeToString(xml); writer.write(xml, lsOutput); result = ((ByteArrayOutputStream) lsOutput.getByteStream()).toString(DEFAULT_CHARSET); if ((omitXMLDeclaration) && (result.indexOf("?>") != -1)) { result = result.substring(result.indexOf("?>") + 2); } // result = toString(getDocument(writer.writeToString(xml)), // true); } return result; }
From source file:net.svcret.core.util.XMLUtils.java
public static void serialize(Document document, boolean prettyPrint, Writer theWriter) { DOMImplementationLS impl = getDOMImpl(); LSSerializer serializer = impl.createLSSerializer(); // document.normalizeDocument(); DOMConfiguration config = serializer.getDomConfig(); if (prettyPrint && config.canSetParameter("format-pretty-print", Boolean.TRUE)) { config.setParameter("format-pretty-print", true); }//from w ww . j a va 2s . c om config.setParameter("xml-declaration", true); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); output.setCharacterStream(theWriter); serializer.write(document, output); }
From source file:nl.imvertor.common.file.XmlFile.java
/** * Zet een Document weg als file. Transformeer middels het XSLT file. Als * XSLT file is "", identity transform.//from w ww. jav a 2 s . co m * * @param doc * @param xsltfile * @param parms * @throws Exception */ public void fromDocument(Document doc) throws Exception { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0"); if (impl == null) { System.out.println("No DOMImplementation found !"); System.exit(0); } LSSerializer serializer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); output.setByteStream(new FileOutputStream(this)); serializer.write(doc, output); }