org.apache.ctakes.jdl.data.xml.DomUtil Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.ctakes.jdl.data.xml;
import org.apache.ctakes.core.resource.FileLocator;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
/**
* Utility to manage DOM.
*
* @author mas
*/
public final class DomUtil {
private DomUtil() {
}
/**
* @return the emptyDocument
* @throws ParserConfigurationException
* exception
*/
public static Document getEmptyDocument() throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
// factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.newDocument();
}
/**
* @param node
* the node to convert
* @return the document
*/
public static Document nodeToDocument(final Node node) {
try {
Document document = getEmptyDocument();
document.appendChild(document.importNode(node, true));
return document;
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* @param strXml
* the strXml to convert
* @return the documnet
*/
public static Document strToDocument(final String strXml) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
// factory.setValidating(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(strXml)));
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* @param srcXml
* the srcXml to convert
* @return the document
*/
public static Document srcToDocument(final String srcXml) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
// factory.setValidating(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
// return builder.parse(new InputSource(srcXml));
// return builder.parse(srcXml);
return builder.parse( FileLocator.getStreamQuiet( srcXml ) );
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* @param node
* the node to convert
* @return the node
*/
public static String nodeToStr(final Node node) {
final String YES = "yes";
StringWriter sw = new StringWriter();
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, YES);
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, YES);
transformer.transform(new DOMSource(node), new StreamResult(sw));
return sw.toString().trim();
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy