List of usage examples for org.w3c.dom.ls DOMImplementationLS createLSOutput
public LSOutput createLSOutput();
LSOutput.characterStream
, LSOutput.byteStream
, LSOutput.systemId
, LSOutput.encoding
are null. From source file:Main.java
public static String prettyPrintWithDOM3LS(Document document) { 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"); StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(document, lsOutput); return stringWriter.toString(); } else {//from ww w . ja va2 s .co 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:Main.java
public static void writeDocumentToDisk(Document document, File directoryToWriteTo, String fileName) { 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"); try { lsOutput.setByteStream(new FileOutputStream(new File(directoryToWriteTo, fileName))); } catch (FileNotFoundException e) { throw new IllegalStateException(e); }// w w w .j a v a2 s . c o m lsSerializer.write(document, lsOutput); } } }
From source file:Main.java
public static void writeDocument(Document doc, Writer out) throws IOException { // happy to replace this code w/ the non-deprecated code, but I couldn't get the transformer // approach to work. // OutputFormat format = new OutputFormat(doc); // format.setIndenting(true); // format.setIndent(2); // XMLSerializer serializer = new XMLSerializer(out, format); // serializer.serialize(doc); DOMImplementationLS impl = (DOMImplementationLS) doc.getImplementation(); LSSerializer writer = impl.createLSSerializer(); DOMConfiguration config = writer.getDomConfig(); if (config.canSetParameter("format-pretty-print", Boolean.TRUE)) { config.setParameter("format-pretty-print", Boolean.TRUE); }/* ww w .ja v a 2s. c om*/ // what a crappy way to force the stream to be UTF-8. yuck! ByteArrayOutputStream baos = new ByteArrayOutputStream(); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); output.setByteStream(baos); writer.write(doc, output); out.write(baos.toString()); out.flush(); }
From source file:Main.java
private static void serializeXML(Element doc, Writer writer, boolean addXmlDeclaration) throws IOException { try {//from w w w . j a v a 2 s . c om 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
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 . j a v a 2s . c o 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:eu.europa.esig.dss.DSSXMLUtils.java
/** * Document Object Model (DOM) Level 3 Load and Save Specification See: http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/ * * @param xmlNode The node to be serialized. * @return/*from w w w. ja v a 2 s . c o m*/ */ public static byte[] serializeNode(final Node xmlNode) { try { final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final LSOutput output = impl.createLSOutput(); output.setByteStream(buffer); writer.write(xmlNode, output); final byte[] bytes = buffer.toByteArray(); return bytes; } catch (Exception e) { throw new DSSException(e); } }
From source file:XMLUtils.java
public static InputStream getInputStream(Document doc) throws Exception { DOMImplementationLS impl = null; DOMImplementation docImpl = doc.getImplementation(); // Try to get the DOMImplementation from doc first before // defaulting to the sun implementation. if (docImpl != null && docImpl.hasFeature("LS", "3.0")) { impl = (DOMImplementationLS) docImpl.getFeature("LS", "3.0"); } else {/*from w w w. ja v a2 s .c om*/ DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); if (impl == null) { System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); registry = DOMImplementationRegistry.newInstance(); impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); } } LSOutput output = impl.createLSOutput(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); output.setByteStream(byteArrayOutputStream); LSSerializer writer = impl.createLSSerializer(); writer.write(doc, output); byte[] buf = byteArrayOutputStream.toByteArray(); return new ByteArrayInputStream(buf); }
From source file:edu.ur.ir.ir_export.service.DefaultContributorTypeExportService.java
/** * Create the xml file for the set of collections. * // ww w .java2 s . com * @param xmlFile - file to write the xml to * @param contributor types - set of contributor types to export * * @throws IOException - if writing to the file fails. */ public void createXmlFile(File xmlFile, Collection<ContributorType> contributorTypes) throws IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; String path = FilenameUtils.getPath(xmlFile.getCanonicalPath()); if (!path.equals("")) { File pathOnly = new File(FilenameUtils.getFullPath(xmlFile.getCanonicalPath())); FileUtils.forceMkdir(pathOnly); } if (!xmlFile.exists()) { if (!xmlFile.createNewFile()) { throw new IllegalStateException("could not create file"); } } try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } DOMImplementation impl = builder.getDOMImplementation(); DOMImplementationLS domLs = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSSerializer serializer = domLs.createLSSerializer(); LSOutput lsOut = domLs.createLSOutput(); Document doc = impl.createDocument(null, "contributor_types", null); Element root = doc.getDocumentElement(); FileOutputStream fos; OutputStreamWriter outputStreamWriter; BufferedWriter writer; try { fos = new FileOutputStream(xmlFile); try { outputStreamWriter = new OutputStreamWriter(fos, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } writer = new BufferedWriter(outputStreamWriter); lsOut.setCharacterStream(writer); } catch (FileNotFoundException e) { throw new IllegalStateException(e); } // create XML for the child collections for (ContributorType ct : contributorTypes) { Element contributorType = doc.createElement("contributor_type"); this.addIdElement(contributorType, ct.getId().toString(), doc); this.addNameElement(contributorType, ct.getName(), doc); this.addDescription(contributorType, ct.getDescription(), doc); this.addSystemCode(contributorType, ct.getUniqueSystemCode(), doc); } serializer.write(root, lsOut); try { fos.close(); writer.close(); outputStreamWriter.close(); } catch (Exception e) { throw new IllegalStateException(e); } }
From source file:de.mpg.mpdl.inge.xmltransforming.TestBase.java
/** * Serialize the given Dom Object to a String. * /*from w w w.ja va 2 s . c om*/ * @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 { if (xml == null) { throw new IllegalArgumentException(TestBase.class.getSimpleName() + ":toString:xml is null"); } String result = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // serialize DOMImplementation implementation = DOMImplementationRegistry.newInstance().getDOMImplementation("XML 3.0"); DOMImplementationLS feature = (DOMImplementationLS) implementation.getFeature("LS", "3.0"); LSSerializer serial = feature.createLSSerializer(); LSOutput output = feature.createLSOutput(); output.setByteStream(outputStream); serial.write(xml, output); result = output.toString(); return result; }
From source file:eu.semaine.util.XMLTool.java
/** * Document type to String format conversion * @param document//from w ww .ja va2 s . c om * @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(); }