/*- * ============LICENSE_START======================================================= * ONAP CLAMP * ================================================================================ * Copyright (C) 2019 Nokia. All rights * reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END============================================ * =================================================================== * */ package org.onap.clamp.clds.util; import java.io.StringWriter; import javax.xml.XMLConstants; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.apache.batik.anim.dom.SVGDOMImplementation; import org.apache.batik.dom.GenericDOMImplementation; import org.apache.batik.util.SVGConstants; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; public class XmlTools { /** * Private constructor to avoid creating instances of util class. */ private XmlTools(){ } /** * Transforms document to XML string. * * @param doc XML document * @return XML string */ public static String exportXmlDocumentAsString(Document doc) { try { TransformerFactory tf = TransformerFactory.newInstance(); tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.getBuffer().toString(); } catch (TransformerException e) { throw new RuntimeException(e); } } /** * Creates empty svg document. * * @return Document */ public static Document createEmptySvgDocument() { DOMImplementation domImplementation = GenericDOMImplementation.getDOMImplementation(); String svgNs = SVGDOMImplementation.SVG_NAMESPACE_URI; return domImplementation.createDocument(svgNs, SVGConstants.SVG_SVG_TAG, null); } }