List of usage examples for org.w3c.dom DOMImplementation getFeature
public Object getFeature(String feature, String version);
From source file:Main.java
static public String getInnerXmlText(Node xmlNode) { StringBuilder result = new StringBuilder(); Document xmlDocument = xmlNode.getOwnerDocument(); DOMImplementation xmlDocumentImpl = xmlDocument.getImplementation(); DOMImplementationLS lsImpl = (DOMImplementationLS) xmlDocumentImpl.getFeature("LS", "3.0"); LSSerializer lsSerializer = lsImpl.createLSSerializer(); NodeList childNodes = xmlNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { String childText = lsSerializer.writeToString(childNodes.item(i)); int pos = childText.indexOf("?>"); if (pos > -1) { childText = childText.substring(pos + 2); }/*from w w w .j a v a2s . c o m*/ result.append(childText); } return result.toString(); }
From source file:Main.java
public static String lsSerializePretty(Document doc) { DOMImplementation domImplementation = doc.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(doc, lsOutput); return stringWriter.toString(); } else {//from w w w .ja v a 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: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 {/*w w w. j a v a 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: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); }/*from www .java2 s . co m*/ lsSerializer.write(document, lsOutput); } } }
From source file:edu.kit.dama.mdm.content.util.DublinCoreHelper.java
/** * Create the Dublin Core document.// w w w .java 2s . c o m * * @param theObject The object to create the DC information for. * @param pCreator A custom creator stored as author/publisher in Dublin * Core. If not provided, the object's uploader is used if available. * @param out The output stream to which the DC document is written. * * @throws ParserConfigurationException If creating the Dublin Core document * failed. */ public static void writeDublinCoreDocument(DigitalObject theObject, UserData pCreator, OutputStream out) throws ParserConfigurationException { Document doc = createDublinCoreDocument(theObject, pCreator); DOMImplementation impl = doc.getImplementation(); DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSOutput lso = implLS.createLSOutput(); lso.setByteStream(out); LSSerializer writer = implLS.createLSSerializer(); writer.write(doc, lso); }
From source file:Main.java
/** * Get the DOM Level 3 Load/Save {@link DOMImplementationLS} for the given node. * //from w w w .j a v a 2 s .c om * @param node the node to evaluate * @return the DOMImplementationLS for the given node */ public static DOMImplementationLS getLSDOMImpl(Node node) { DOMImplementation domImpl; if (node instanceof Document) { domImpl = ((Document) node).getImplementation(); } else { domImpl = node.getOwnerDocument().getImplementation(); } DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0"); return domImplLS; }
From source file:Main.java
/** * Converts a dom element to a String/*w w w .ja va 2s. co 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: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 {/*www .j av a 2 s. c o m*/ 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:eu.semaine.util.XMLTool.java
/** * Document type to String format conversion * @param document/*from w ww . j ava 2s . 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(); }
From source file:edu.ur.ir.ir_export.service.DefaultContributorTypeExportService.java
/** * Create the xml file for the set of collections. * /*from www . j a va 2s . c o m*/ * @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); } }