/** * ` * Serialize XML objects * * @param xmlObject : XACML or SAML objects to be serialized * @return serialized XACML or SAML objects * @throws EntitlementException */ private String marshall(XMLObject xmlObject) throws EntitlementException { try { doBootstrap(); System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory(); Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject); Element element = marshaller.marshall(xmlObject); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setByteStream(byteArrayOutputStream); writer.write(element, output); return byteArrayOutputStream.toString(); } catch (Exception e) { log.error("Error Serializing the SAML Response"); throw new EntitlementException("Error Serializing the SAML Response", e); } }
/** * Serializes the specified SAML 2.0 based XML content representation to its corresponding actual XML syntax * representation. * * @param xmlObject the SAML 2.0 based XML content object * @return a {@link String} representation of the actual XML representation of the SAML 2.0 based XML content * representation * @throws SSOException if an error occurs during the marshalling process */ public static String marshall(XMLObject xmlObject) throws SSOException { try { Marshaller marshaller = XMLObjectProviderRegistrySupport.getMarshallerFactory().getMarshaller(xmlObject); Element element = null; if (marshaller != null) { element = marshaller.marshall(xmlObject); } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS implementation = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = implementation.createLSSerializer(); LSOutput output = implementation.createLSOutput(); output.setByteStream(byteArrayOutputStream); writer.write(element, output); return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8); } catch (ClassNotFoundException | InstantiationException | MarshallingException | IllegalAccessException e) { throw new SSOException("Error in marshalling SAML 2.0 Assertion", e); } }
public static String toXml(Document domDoc) throws TransformerException { DOMImplementation domImplementation = domDoc.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("xml-declaration", Boolean.TRUE)) lsSerializer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE); 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(domDoc, lsOutput); return stringWriter.toString(); } } return toXml((Node)domDoc); }
/** * Serializes a node using a certain character encoding. * * @param node * DOM node to serialize * @param os * output stream, to which the node is serialized * @param omitXmlDeclaration * indicator whether to omit the XML declaration or not * @param encoding * character encoding, can be <code>null</code>, if * <code>null</code> then "UTF-8" is used * @throws Exception */ public static void transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration, String encoding) throws Exception { //NOPMD // previously we used javax.xml.transform.Transformer, however the JDK xalan implementation did not work correctly with a specified encoding // therefore we switched to DOMImplementationLS if (encoding == null) { encoding = "UTF-8"; } DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry.getDOMImplementation("LS"); LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding(encoding); lsOutput.setByteStream(os); LSSerializer lss = domImplementationLS.createLSSerializer(); lss.getDomConfig().setParameter("xml-declaration", !omitXmlDeclaration); lss.write(node, lsOutput); }
/** * Returns a textual representation of an XML Object. * * @param doc XML Dom Document * @return String containing a textual representation of the object */ public static String asString(Document doc) { try { DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); LSOutput lsOutput = domImplementation.createLSOutput(); lsOutput.setEncoding("UTF-8"); return lsSerializer.writeToString(doc); } catch (Exception e) { e.printStackTrace(); try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); } catch(Exception ex) { logger.error(ex); return StackTrace.asString(ex); } } }
/** * Marshall a SAML XML object into a W3C DOM and then into a String * * @param pXMLObject SAML Object to marshall * @return XML version of the SAML Object in string form */ private String marshall(XMLObject pXMLObject) { try { MarshallerFactory lMarshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory(); Marshaller lMarshaller = lMarshallerFactory.getMarshaller(pXMLObject); Element lElement = lMarshaller.marshall(pXMLObject); DOMImplementationLS lDOMImplementationLS = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS"); LSSerializer lSerializer = lDOMImplementationLS.createLSSerializer(); LSOutput lOutput = lDOMImplementationLS.createLSOutput(); lOutput.setEncoding("UTF-8"); Writer lStringWriter = new StringWriter(); lOutput.setCharacterStream(lStringWriter); lSerializer.write(lElement, lOutput); return lStringWriter.toString(); } catch (Exception e) { throw new ExInternal("Error Serializing the SAML Response", e); } }
private void format(Document document, Writer writer) { DOMImplementation implementation = document.getImplementation(); if(implementation.hasFeature(LS_FEATURE_KEY, LS_FEATURE_VERSION) && implementation.hasFeature(CORE_FEATURE_KEY, CORE_FEATURE_VERSION)) { DOMImplementationLS implementationLS = (DOMImplementationLS) implementation.getFeature(LS_FEATURE_KEY, LS_FEATURE_VERSION); LSSerializer serializer = implementationLS.createLSSerializer(); DOMConfiguration configuration = serializer.getDomConfig(); configuration.setParameter("format-pretty-print", Boolean.TRUE); configuration.setParameter("comments", preserveComments); LSOutput output = implementationLS.createLSOutput(); output.setEncoding("UTF-8"); output.setCharacterStream(writer); serializer.write(document, output); } }
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); } 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(); }
/** Utility methods */ private void writeXmlDocumentToFile(Node node, String filename) { try { // find file or create one and save all info File file = new File(filename); if(!file.exists()) { file.createNewFile(); } DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS implementationLS = (DOMImplementationLS)registry.getDOMImplementation("LS"); LSOutput output = implementationLS.createLSOutput(); output.setEncoding("UTF-8"); output.setCharacterStream(new FileWriter(file)); LSSerializer serializer = implementationLS.createLSSerializer(); serializer.getDomConfig().setParameter("format-pretty-print", true); serializer.write(node, output); } catch (Exception e1) { e1.printStackTrace(); } }
private String getXmlString(Node node) { try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS implementationLS = (DOMImplementationLS)registry.getDOMImplementation("LS"); LSOutput output = implementationLS.createLSOutput(); output.setEncoding(this.xmlEncoding); output.setCharacterStream(new StringWriter()); LSSerializer serializer = implementationLS.createLSSerializer(); serializer.write(node, output); return output.getCharacterStream().toString(); } catch (Exception e1) { e1.printStackTrace(); } return null; }
/** * Serializing a SAML2 object into a String * * @param xmlObject object that needs to serialized. * @return serialized object * @throws SAMLSSOException */ public static String marshall(XMLObject xmlObject) throws SAMLSSOException { try { System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration .getMarshallerFactory(); Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject); Element element = marshaller.marshall(xmlObject); ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream(); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setByteStream(byteArrayOutputStrm); writer.write(element, output); return byteArrayOutputStrm.toString(); } catch (Exception e) { log.error("Error Serializing the SAML Response"); throw new SAMLSSOException("Error Serializing the SAML Response", e); } }
/** * Serializing a SAML2 object into a String * * @param xmlObject object that needs to serialized. * @return serialized object * @throws SAML2SSOUIAuthenticatorException */ public static String marshall(XMLObject xmlObject) throws SAML2SSOUIAuthenticatorException { try { doBootstrap(); System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration .getMarshallerFactory(); Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject); Element element = marshaller.marshall(xmlObject); ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream(); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setByteStream(byteArrayOutputStrm); writer.write(element, output); return byteArrayOutputStrm.toString(); } catch (Exception e) { log.error("Error Serializing the SAML Response"); throw new SAML2SSOUIAuthenticatorException("Error Serializing the SAML Response", e); } }
private static String marshall(XMLObject xmlObject) throws org.wso2.carbon.identity.base.IdentityException { try { System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory(); Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject); Element element = marshaller.marshall(xmlObject); ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream(); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setByteStream(byteArrayOutputStrm); writer.write(element, output); return byteArrayOutputStrm.toString("UTF-8"); } catch (Exception e) { log.error("Error Serializing the SAML Response"); throw IdentityException.error("Error Serializing the SAML Response", e); } }
void write( Writer writer ) { // 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+. 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" ); lsOutput .setCharacterStream( writer ); lsSerializer.write( doc, lsOutput ); } 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."); } }
/** * Serializing a SAML2 object into a String * * @param xmlObject object that needs to serialized. * @return serialized object * @throws Exception */ public static String marshall(XMLObject xmlObject) throws Exception { try { doBootstrap(); System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); MarshallerFactory marshallerFactory = org.opensaml.xml.Configuration.getMarshallerFactory(); Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject); Element element = marshaller.marshall(xmlObject); ByteArrayOutputStream byteArrayOutputStrm = new ByteArrayOutputStream(); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setByteStream(byteArrayOutputStrm); writer.write(element, output); return byteArrayOutputStrm.toString(); } catch (Exception e) { throw new Exception("Error Serializing the SAML Response", e); } }
protected void serializeDOM_LS(Element elt, OutputStream out, boolean pretty) throws LSException { DOMImplementationLS impl = (DOMImplementationLS)XMLImplFinder.getDOMImplementation(); // init and configure serializer LSSerializer serializer = impl.createLSSerializer(); DOMConfiguration config = serializer.getDomConfig(); config.setParameter("format-pretty-print", pretty); // wrap output stream LSOutput output = impl.createLSOutput(); output.setByteStream(out); // launch serialization serializer.write(elt, output); }
public String export(ArrayList<Class> classes) { Document doc = exportDoc(classes); // Output the document to string. DOMImplementation impl = doc.getImplementation(); DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSSerializer lsSerializer = implLS.createLSSerializer(); LSOutput lsOutput = implLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); Writer stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(doc, lsOutput); String output = stringWriter.toString(); //Write to file file.setContent(output); file.write(); return file.getPath(); }
public static String dom2String2(Document document) { DOMImplementationRegistry registry; try { registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplLS = (DOMImplementationLS)registry.getDOMImplementation("LS"); LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer for the DOM LSOutput out = domImplLS.createLSOutput(); StringWriter stringOut = new StringWriter(); // Writer will be a String out.setCharacterStream(stringOut); ser.write(document, out); // Serialize the DOM System.out.println( "STRXML = " + stringOut.toString() ); // Spit out the DOM as a String return stringOut.toString(); } catch (Exception e) { System.err.println("Cannot create registry: "+e.getMessage()); } return null; }
private static String prettyFormat(Document document, boolean keepXmlDeclaration) { try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplementation = (DOMImplementationLS) registry.getDOMImplementation("LS"); Writer stringWriter = new StringWriter(); LSOutput formattedOutput = domImplementation.createLSOutput(); formattedOutput.setCharacterStream(stringWriter); LSSerializer domSerializer = domImplementation.createLSSerializer(); domSerializer.getDomConfig().setParameter("format-pretty-print", true); // Set this to true if the declaration is needed to be in the output. domSerializer.getDomConfig().setParameter("xml-declaration", keepXmlDeclaration); domSerializer.write(document, formattedOutput); return stringWriter.toString(); } catch (Exception e) { throw new RuntimeException(FORMAT_ERROR, e); } }
/*** * Helper method which converts XML Document into pretty formatted string * * @param doc to convert * @return converted XML as String */ public static String documentToString(Document doc) { String strMsg = ""; try { DOMImplementation domImpl = doc.getImplementation(); DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0"); LSSerializer lsSerializer = domImplLS.createLSSerializer(); lsSerializer.getDomConfig().setParameter("format-pretty-print", true); Writer stringWriter = new StringWriter(); LSOutput lsOutput = domImplLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(doc, lsOutput); strMsg = stringWriter.toString(); } catch (Exception e) { logger.warn("Error occurred when converting document to string", e); } return strMsg; }
private static String prettyPrintXmlDocument(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+. 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 { 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."); } }
/** * Writes a DOM Document to the given OutputStream using the "UTF-8" * encoding. The XML declaration is omitted. * * @param outStream * The destination OutputStream object. * @param doc * A Document node. */ void writeDocument(OutputStream outStream, Document doc) { DOMImplementationRegistry domRegistry = null; try { domRegistry = DOMImplementationRegistry.newInstance(); } catch (Exception e) { LOGR.warning(e.getMessage()); } DOMImplementationLS impl = (DOMImplementationLS) domRegistry .getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("xml-declaration", false); writer.getDomConfig().setParameter("format-pretty-print", true); LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); output.setByteStream(outStream); writer.write(doc, output); }
private static String elementToString(Element e) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DOMImplementationLS ls = (DOMImplementationLS) e.getOwnerDocument().getImplementation().getFeature("LS", "3.0"); // NOI18N LSOutput output = ls.createLSOutput(); output.setByteStream(baos); LSSerializer ser = ls.createLSSerializer(); ser.write(e, output); return baos.toString(); }
private void _dumpDocument( Document doc, String title ) { if( null == title || title.isEmpty() ) { title = NbBundle.getMessage(WebBrowserImpl.class, "Lbl_GenericDomDumpTitle"); } InputOutput io = IOProvider.getDefault().getIO( title, true ); io.select(); try { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation( "XML 3.0 LS 3.0" ); //NOI18N if( null == impl ) { io.getErr().println( NbBundle.getMessage(WebBrowserImpl.class, "Err_DOMImplNotFound") ); return; } LSSerializer serializer = impl.createLSSerializer(); if( serializer.getDomConfig().canSetParameter( "format-pretty-print", Boolean.TRUE ) ) { //NOI18N serializer.getDomConfig().setParameter( "format-pretty-print", Boolean.TRUE ); //NOI18N } LSOutput output = impl.createLSOutput(); output.setEncoding("UTF-8"); //NOI18N output.setCharacterStream( io.getOut() ); serializer.write(doc, output); io.getOut().println(); } catch( Exception ex ) { ex.printStackTrace( io.getErr() ); } finally { if( null != io ) { io.getOut().close(); io.getErr().close(); } } }
/** * Writes a Node out to a Writer using the DOM, level 3, Load/Save serializer. The written content is encoded using * the encoding specified in the writer configuration. * * @param node the node to write out * @param output the writer to write the XML to * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer * instance, obtained via {@link LSSerializer#getDomConfig()}. May be null. */ public static void writeNode(Node node, Writer output, Map<String, Object> serializerParams) { DOMImplementationLS domImplLS = getLSDOMImpl(node); LSSerializer serializer = getLSSerializer(domImplLS, serializerParams); LSOutput serializerOut = domImplLS.createLSOutput(); serializerOut.setCharacterStream(output); serializer.write(node, serializerOut); }
/** * Writes a Node out to an OutputStream using the DOM, level 3, Load/Save serializer. The written content * is encoded using the encoding specified in the output stream configuration. * * @param node the node to write out * @param output the output stream to write the XML to * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer * instance, obtained via {@link LSSerializer#getDomConfig()}. May be null. */ public static void writeNode(Node node, OutputStream output, Map<String, Object> serializerParams) { DOMImplementationLS domImplLS = getLSDOMImpl(node); LSSerializer serializer = getLSSerializer(domImplLS, serializerParams); LSOutput serializerOut = domImplLS.createLSOutput(); serializerOut.setByteStream(output); serializer.write(node, serializerOut); }
@SuppressWarnings("unchecked") private void getGroupings(Integer organisationId, HttpServletResponse response) throws IOException { Document doc = OrganisationGroupServlet.docBuilder.newDocument(); Element groupsElement = doc.createElement("groups"); doc.appendChild(groupsElement); List<OrganisationGrouping> groupings = userManagementService.findByProperty(OrganisationGrouping.class, "organisationId", organisationId); for (OrganisationGrouping grouping : groupings) { Element groupingElement = doc.createElement("grouping"); groupingElement.setAttribute("id", grouping.getGroupingId().toString()); groupingElement.setAttribute("name", StringEscapeUtils.escapeXml(grouping.getName())); groupsElement.appendChild(groupingElement); for (OrganisationGroup group : grouping.getGroups()) { Element groupElement = doc.createElement("group"); groupElement.setAttribute("id", group.getGroupId().toString()); groupElement.setAttribute("name", StringEscapeUtils.escapeXml(group.getName())); groupingElement.appendChild(groupElement); for (User user : group.getUsers()) { Element userElement = doc.createElement("user"); userElement.setAttribute("id", user.getUserId().toString()); userElement.setAttribute("firstname", StringEscapeUtils.escapeXml(user.getFirstName())); userElement.setAttribute("lastname", StringEscapeUtils.escapeXml(user.getLastName())); groupElement.appendChild(userElement); } } } response.setContentType("text/xml"); response.setCharacterEncoding("UTF-8"); DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImplementation.createLSSerializer(); LSOutput lsOutput = domImplementation.createLSOutput(); lsOutput.setEncoding("UTF-8"); lsOutput.setByteStream(response.getOutputStream()); lsSerializer.write(doc, lsOutput); }
public void write(Document doc) { File file = getOutputFile(testClass); try (FileOutputStream outputStream = new FileOutputStream(file)) { DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS"); LSSerializer serializer = impl.createLSSerializer(); LSOutput output = impl.createLSOutput(); output.setByteStream(outputStream); serializer.write(doc, output); } catch (Exception e) { throw new CucumberException(Constants.errorPrefix + "Failed to write document to disc", e); } }
public String prettyPrintDOMResult(DOMResult dr) throws ClassNotFoundException, InstantiationException, IllegalAccessException, ClassCastException { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplementationLS = (DOMImplementationLS)registry.getDOMImplementation("LS"); StringWriter writer = new StringWriter(); LSOutput formattedOutput = domImplementationLS.createLSOutput(); formattedOutput.setCharacterStream(writer); LSSerializer domSerializer = domImplementationLS.createLSSerializer(); domSerializer.getDomConfig().setParameter("format-pretty-print", true); domSerializer.getDomConfig().setParameter("xml-declaration", false); domSerializer.write(dr.getNode(), formattedOutput); return writer.toString(); }
private String serializerWrite(Node xml, boolean pretty) throws Exception { DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplementation = (DOMImplementationLS) registry.getDOMImplementation("LS"); StringWriter writer = new StringWriter(); LSOutput formattedOutput = domImplementation.createLSOutput(); formattedOutput.setCharacterStream(writer); LSSerializer domSerializer = domImplementation.createLSSerializer(); domSerializer.getDomConfig().setParameter(DOM_FORMAT_PRETTY_PRINT, pretty); domSerializer.getDomConfig().setParameter("xml-declaration", false); domSerializer.write(xml, formattedOutput); return writer.toString(); }
public static void write(Document document, OutputStream stream) { DOMImplementationLS impl = (DOMImplementationLS)REGISTRY.getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); LSOutput output=impl.createLSOutput(); output.setByteStream(stream); writer.write(document, output); }
/** Returns a <code>LSOutput</code> instance. * @param impl A <code>DOMImplementationLS</code> instance * @param os Optional <code>OutputStream</code> instance * @param encoding Optional character encoding, default is UTF-8 * @return A <code>LSOutput</code> instance * @see <a href="http://www.w3.org/TR/2004/REC-DOM-Level-3-LS-20040407/">DOM Level 3 Load and Save Specification</a> */ public static LSOutput createLSOutput(DOMImplementationLS impl, OutputStream os, String encoding) { LSOutput out = impl.createLSOutput(); if (os != null) { out.setByteStream(os); } if (encoding != null) { out.setEncoding(encoding); } return out; }
/** * Serializa un XML a ByteArrayOutputStream. * * @param doc documento que se va a serializar. * @param outputStreamXML documento de salida. * @throws Exception si se produce un error. */ public static void saveXML(Document doc, ByteArrayOutputStream outputStreamXML) throws Exception { System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMImplementationSourceImpl"); DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementation domImpl = registry.getDOMImplementation("LS 3.0"); DOMImplementationLS implLS = (DOMImplementationLS) domImpl; LSSerializer dom3Writer = implLS.createLSSerializer(); LSOutput output = implLS.createLSOutput(); output.setByteStream(outputStreamXML); output.setEncoding("UTF-8"); dom3Writer.write(doc, output); }
public boolean writeToURI(Node node, String uri) throws LSException { LSOutput output = new DomLSOutput(); output.setSystemId(uri); return write(node, output); }
public String writeToString(Node node) throws DOMException, LSException { Writer writer = new StringWriter(); LSOutput output = new DomLSOutput(); output.setCharacterStream(writer); write(node, output); return writer.toString(); }
/** * Pretty print W3C DOM represented XML document to a given output stream. * * @param node W3C DOM node to pretty print. * @param os Output stream to pretty print input XML to. */ public static void xmlSerializer(Node node, OutputStream os) { DOMImplementation domImpl = node.getNodeType() == Node.DOCUMENT_NODE ? ((Document) node).getImplementation() : node.getOwnerDocument().getImplementation(); DOMImplementationLS ls = (DOMImplementationLS) domImpl; LSOutput lso = ls.createLSOutput(); LSSerializer lss = ls.createLSSerializer(); lss.getDomConfig().setParameter("xml-declaration", true); lss.getDomConfig().setParameter("format-pretty-print", true); lso.setByteStream(os); lss.write(node, lso); }