List of usage examples for org.w3c.dom.ls LSSerializer getDomConfig
public DOMConfiguration getDomConfig();
DOMConfiguration
object used by the LSSerializer
when serializing a DOM node. From source file:Main.java
public static boolean serialize(Document doc, boolean setXmlDecl, File f) throws FileNotFoundException { DOMImplementationLS lsImpl = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0"); LSSerializer serializer = lsImpl.createLSSerializer(); serializer.getDomConfig().setParameter("xml-declaration", setXmlDecl); // set it to false to get LSOutput output = lsImpl.createLSOutput(); output.setByteStream(new FileOutputStream(f)); return serializer.write(doc, output); }
From source file:Main.java
public static String serialize(Document document, boolean prettyPrint) { 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 www . j av a2 s.c o m*/ config.setParameter("xml-declaration", true); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); Writer writer = new StringWriter(); output.setCharacterStream(writer); serializer.write(document, output); return writer.toString(); }
From source file:Main.java
public static String formatXml(String xml) { try {//from w ww . j a v a 2 s . co m final InputSource src = new InputSource(new StringReader(xml)); final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) .getDocumentElement(); final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml")); //May need this: System.setProperty(DOMImplementationRegistry. //PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); // Set this to true if the output needs to be beautified. writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the declaration is needed to be outputted. writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); return writer.writeToString(document); } catch (Exception e) { System.err.println("Exception thrown"); throw new RuntimeException(e); } }
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 w w w .ja v a2s . co m 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
private static void serializeXML(Element doc, Writer writer, boolean addXmlDeclaration) throws IOException { try {/*from w ww . ja v a 2s . com*/ DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer serializer = impl.createLSSerializer(); DOMConfiguration config = serializer.getDomConfig(); config.setParameter("xml-declaration", addXmlDeclaration); // config.setParameter("format-pretty-print", true); // config.setParameter("normalize-characters", true); LSOutput out = impl.createLSOutput(); out.setCharacterStream(writer); serializer.write(doc, out); } catch (Throwable e) { throw new IOException(e.getMessage()); } }
From source file:Main.java
/** * Obtain a the DOM, level 3, Load/Save serializer {@link LSSerializer} instance from the * given {@link DOMImplementationLS} instance. * //from w w w.j a v a 2 s. c o m * <p> * The serializer instance will be configured with the parameters passed as the <code>serializerParams</code> * argument. It will also be configured with an {@link LSSerializerFilter} that shows all nodes to the filter, * and accepts all nodes shown. * </p> * * @param domImplLS the DOM Level 3 Load/Save implementation to use * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer * instance, obtained via {@link LSSerializer#getDomConfig()}. May be null. * * @return a new LSSerializer instance */ public static LSSerializer getLSSerializer(DOMImplementationLS domImplLS, Map<String, Object> serializerParams) { LSSerializer serializer = domImplLS.createLSSerializer(); serializer.setFilter(new LSSerializerFilter() { public short acceptNode(Node arg0) { return FILTER_ACCEPT; } public int getWhatToShow() { return SHOW_ALL; } }); if (serializerParams != null) { DOMConfiguration serializerDOMConfig = serializer.getDomConfig(); for (String key : serializerParams.keySet()) { serializerDOMConfig.setParameter(key, serializerParams.get(key)); } } return serializer; }
From source file:com.prowidesoftware.swift.io.parser.MxParser.java
/** * Parse the business header from an XML Element node * @see #parseBusinessHeader()/*from w w w. j a va 2s . c om*/ * @since 7.8 */ public static BusinessHeader parseBusinessHeader(final org.w3c.dom.Element e) { org.w3c.dom.ls.DOMImplementationLS lsImpl = (org.w3c.dom.ls.DOMImplementationLS) e.getOwnerDocument() .getImplementation().getFeature("LS", "3.0"); org.w3c.dom.ls.LSSerializer serializer = lsImpl.createLSSerializer(); serializer.getDomConfig().setParameter("xml-declaration", false); String xml = serializer.writeToString(e); MxParser parser = new MxParser(xml); return parser.parseBusinessHeader(); }
From source file:eu.semaine.util.XMLTool.java
/** * Document type to String format conversion * @param document//www.jav a 2 s . c o 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:fr.fastconnect.factory.tibco.bw.codereview.pages.CodeReviewIndexMojo.java
protected String formatHtml(String html) throws MojoExecutionException { try {/*from w w w .j ava 2 s. c o m*/ InputSource src = new InputSource(new StringReader(html)); Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src) .getDocumentElement(); Boolean keepDeclaration = Boolean.valueOf(html.startsWith("<?xml")); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); return writer.writeToString(document); } catch (Exception e) { throw new MojoExecutionException(e.getMessage(), e); } }
From source file:edu.wfu.inotado.helper.MarshalHelper.java
/** * Pretty-Prints the XML//from w w w . j a v a 2s . c o 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; }