List of usage examples for org.w3c.dom.ls DOMImplementationLS createLSSerializer
public LSSerializer createLSSerializer();
LSSerializer
object. From source file:nz.co.senanque.messaging.GenericEndpoint.java
private String getStringFromDoc(org.w3c.dom.Document doc) { DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); return lsSerializer.writeToString(doc); }
From source file:edu.ur.ir.ir_export.service.DefaultContributorTypeExportService.java
/** * Create the xml file for the set of collections. * /*from w w w . j av a 2s . c om*/ * @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 www .j av a 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:edu.wfu.inotado.helper.MarshalHelper.java
/** * Pretty-Prints the XML// www . jav a 2 s. 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: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 .j av a2 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:at.gv.egiz.bku.slcommands.impl.CreateXMLSignatureCommandImpl.java
@Override public SLResult execute(SLCommandContext commandContext) { try {//from w ww .j a v a 2 s.c o m // get certificate in order to select appropriate algorithms for hashing // and signing log.info("Requesting signing certificate."); getSigningCertificate(commandContext); if (log.isDebugEnabled()) { log.debug("Got signing certificate. {}", signingCertificate); } else { log.info("Got signing certificate."); } // prepare the XMLSignature for signing log.info("Preparing XML signature."); prepareXMLSignature(commandContext); // sign the XMLSignature log.info("Signing XML signature."); signXMLSignature(commandContext); if (log.isDebugEnabled()) { DOMImplementationLS domImplLS = DOMUtils.getDOMImplementationLS(); LSSerializer serializer = domImplLS.createLSSerializer(); String debugString = serializer.writeToString(signature.getDocument()); log.debug(debugString); } else { log.info("XML signature signed."); } return new CreateXMLSignatureResultImpl(signature.getDocument()); } catch (SLException e) { return new ErrorResultImpl(e, commandContext.getLocale()); } }
From source file:no.difi.sdp.client.asice.signature.CreateSignatureTest.java
private String prettyPrint(final Signature signature) throws TransformerException, ClassNotFoundException, InstantiationException, IllegalAccessException { StreamSource xmlSource = new StreamSource(new ByteArrayInputStream(signature.getBytes())); Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMResult outputTarget = new DOMResult(); transformer.transform(xmlSource, outputTarget); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); writer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE); return writer.writeToString(outputTarget.getNode()); }
From source file:com.collabnet.ccf.core.recovery.HospitalArtifactReplayer.java
@SuppressWarnings("deprecation") private void loadBeanDefinitionsFromUrl(String url, GenericApplicationContext context) { BeanDefinitionReader reader = null;// w w w. ja va2 s. com if (url.endsWith(".xml")) { reader = new XmlBeanDefinitionReader(context); } else if (url.endsWith(".properties")) { reader = new PropertiesBeanDefinitionReader(context); } if (reader != null) { try { UrlResource urlResource = new UrlResource(url); InputStream is = urlResource.getInputStream(); Document document = builder.parse(is); Element routerElement = this.getRouterElement(document); this.stripOffProcessors(routerElement); this.addGAImportComponents(document, routerElement); DOMImplementationRegistry registry = null; try { registry = DOMImplementationRegistry.newInstance(); } catch (ClassCastException e) { log.error("error", e); throw new CCFRuntimeException("error", e); } catch (ClassNotFoundException e) { log.error("error", e); throw new CCFRuntimeException("error", e); } catch (InstantiationException e) { log.error("error", e); throw new CCFRuntimeException("error", e); } catch (IllegalAccessException e) { log.error("error", e); throw new CCFRuntimeException("error", e); } String originalConfigFileAbsolutePath = urlResource.getFile().getAbsolutePath(); String componentName = entry.getSourceComponent(); String configComponentIdentifier = "{" + originalConfigFileAbsolutePath + "}" + componentName; File outputFile = null; if (componentConfigFileMap.containsKey(configComponentIdentifier)) { outputFile = componentConfigFileMap.get(configComponentIdentifier); } else { outputFile = File.createTempFile(componentName, ".xml", replayWorkDir); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); FileOutputStream bas = new FileOutputStream(outputFile.getAbsolutePath()); output.setByteStream(bas); writer.write(document, output); bas.flush(); bas.close(); componentConfigFileMap.put(configComponentIdentifier, outputFile); } // FIXME Use of deprecated method UrlResource newUrlResource = new UrlResource(outputFile.toURL().toString()); ((XmlBeanDefinitionReader) reader).registerBeanDefinitions(document, newUrlResource); } catch (BeansException e) { log.error("error", e); throw new RuntimeException("BeansException : " + e.getMessage(), e); } catch (MalformedURLException e) { log.error("error", e); throw new RuntimeException("MalformedUrlException : " + e.getMessage(), e); } catch (IOException e) { log.error("error", e); throw new RuntimeException("IOExceptionException : " + e.getMessage(), e); } catch (SAXException e) { log.error("error", e); throw new RuntimeException("SAXException : " + e.getMessage(), e); } } else { throw new RuntimeException("No BeanDefinitionReader associated with " + url); } }
From source file:com.msopentech.odatajclient.engine.performance.BasicPerfTest.java
@Test public void writeAtomViaLowerlevelLibs() throws ParserConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document doc = builder.newDocument(); final Element entry = doc.createElement("entry"); entry.setAttribute("xmlns", "http://www.w3.org/2005/Atom"); entry.setAttribute("xmlns:m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"); entry.setAttribute("xmlns:d", "http://schemas.microsoft.com/ado/2007/08/dataservices"); entry.setAttribute("xmlns:gml", "http://www.opengis.net/gml"); entry.setAttribute("xmlns:georss", "http://www.georss.org/georss"); doc.appendChild(entry);//from ww w . j av a 2s.c o m final Element category = doc.createElement("category"); category.setAttribute("term", "Microsoft.Test.OData.Services.AstoriaDefaultService.Customer"); category.setAttribute("scheme", "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"); entry.appendChild(category); final Element properties = doc.createElement("m:properties"); entry.appendChild(properties); final Element name = doc.createElement("d:Name"); name.setAttribute("m:type", "Edm.String"); name.appendChild(doc.createTextNode("A name")); properties.appendChild(name); final Element customerId = doc.createElement("d:CustomerId"); customerId.setAttribute("m:type", "Edm.Int32"); customerId.appendChild(doc.createTextNode("0")); properties.appendChild(customerId); final Element bci = doc.createElement("d:BackupContactInfo"); bci.setAttribute("m:type", "Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails)"); properties.appendChild(bci); final Element topelement = doc.createElement("d:element"); topelement.setAttribute("m:type", "Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails"); bci.appendChild(topelement); final Element altNames = doc.createElement("d:AlternativeNames"); altNames.setAttribute("m:type", "Collection(Edm.String)"); topelement.appendChild(altNames); final Element element1 = doc.createElement("d:element"); element1.setAttribute("m:type", "Edm.String"); element1.appendChild(doc.createTextNode("myname")); altNames.appendChild(element1); final Element emailBag = doc.createElement("d:EmailBag"); emailBag.setAttribute("m:type", "Collection(Edm.String)"); topelement.appendChild(emailBag); final Element element2 = doc.createElement("d:element"); element2.setAttribute("m:type", "Edm.String"); element2.appendChild(doc.createTextNode("myname@mydomain.com")); emailBag.appendChild(element2); final Element contactAlias = doc.createElement("d:ContactAlias"); contactAlias.setAttribute("m:type", "Microsoft.Test.OData.Services.AstoriaDefaultService.Aliases"); topelement.appendChild(contactAlias); final Element altNames2 = doc.createElement("d:AlternativeNames"); altNames2.setAttribute("m:type", "Collection(Edm.String)"); contactAlias.appendChild(altNames2); final Element element3 = doc.createElement("d:element"); element3.setAttribute("m:type", "Edm.String"); element3.appendChild(doc.createTextNode("myAlternativeName")); altNames2.appendChild(element3); final StringWriter writer = new StringWriter(); final DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS"); final LSSerializer serializer = impl.createLSSerializer(); final LSOutput lso = impl.createLSOutput(); lso.setCharacterStream(writer); serializer.write(doc, lso); assertFalse(writer.toString().isEmpty()); }
From source file:com.redhat.plugin.eap6.AbstractEAP6Mojo.java
/** * method to convert Document to String//from ww w . j ava 2 s . c o m * * @param doc * @return */ protected String getStringFromDocument(final Document doc) { try { final StringWriter writer = new StringWriter(); final DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); final LSSerializer lsSerializer = domImplementation.createLSSerializer(); lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); final LSOutput lsOutput = domImplementation.createLSOutput(); lsOutput.setCharacterStream(writer); lsSerializer.write(doc, lsOutput); return writer.toString(); } catch (final LSException ex) { getLog().error(ex); return null; } }