com.obs.services.internal.xml.OBSXMLBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of esdk-obs-java-bundle Show documentation
Show all versions of esdk-obs-java-bundle Show documentation
The HuaweiCloud OBS Bundle SDK for Java used for accessing Object Storage Service, this SDK bundle
includes third-party libraries and relocated to different namespaces
The newest version!
/**
* Copyright 2019 Huawei Technologies Co.,Ltd.
* 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.
*/
package com.obs.services.internal.xml;
import com.obs.log.ILogger;
import com.obs.log.LoggerBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
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 java.io.IOException;
import java.io.StringWriter;
import java.security.AccessController;
import java.security.PrivilegedAction;
public class OBSXMLBuilder {
private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";
private static String xmlDocumentBuilderFactoryClass =
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
private static String xmlTransformerFactoryClass =
"";
private static final ILogger log = LoggerBuilder.getLogger(OBSXMLBuilder.class);
private Document xmlDocument;
private Node xmlNode;
public static void setXmlDocumentBuilderFactoryClass(String className) {
if (null != className && !className.trim().equals("")) {
xmlDocumentBuilderFactoryClass = className;
}
}
public static void setXmlTransformerFactoryClass(String className) {
if (null != className && !className.trim().equals("")) {
xmlTransformerFactoryClass = className;
}
}
protected OBSXMLBuilder(Document xmlDocument) {
this.xmlDocument = xmlDocument;
this.xmlNode = xmlDocument.getDocumentElement();
}
protected OBSXMLBuilder(Node myNode, Node parentNode) {
this.xmlNode = myNode;
if (myNode instanceof Document) {
this.xmlDocument = (Document) myNode;
} else {
this.xmlDocument = myNode.getOwnerDocument();
}
if (parentNode != null) {
parentNode.appendChild(myNode);
}
}
private static DocumentBuilderFactory findDocumentBuilderFactory() {
if (xmlDocumentBuilderFactoryClass != null && xmlDocumentBuilderFactoryClass.startsWith(DEFAULT_PACKAGE)) {
return DocumentBuilderFactory.newInstance();
}
return newInstance(DocumentBuilderFactory.class, xmlDocumentBuilderFactoryClass, null, true, false);
}
private static TransformerFactory findTransformerFactory() {
if (xmlTransformerFactoryClass == null || xmlTransformerFactoryClass.equals("")) {
return TransformerFactory.newInstance();
} else {
return TransformerFactory.newInstance(xmlTransformerFactoryClass, null);
}
}
protected static Document createDocumentImpl(
String name, String namespaceURI, boolean isNamespaceAware)
throws ParserConfigurationException, FactoryConfigurationError {
DocumentBuilderFactory factory = OBSXMLBuilder.findDocumentBuilderFactory();
try {
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
} catch (Throwable e) {
log.debug("setFeature not supported, detail:", e);
}
factory.setNamespaceAware(isNamespaceAware);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element rootElement;
if (namespaceURI != null && namespaceURI.length() > 0) {
rootElement = document.createElementNS(namespaceURI, name);
} else {
rootElement = document.createElement(name);
}
document.appendChild(rootElement);
return document;
}
protected static Document parseDocumentImpl(
InputSource inputSource, boolean isNamespaceAware)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = OBSXMLBuilder.findDocumentBuilderFactory();
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
factory.setNamespaceAware(isNamespaceAware);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inputSource);
return document;
}
private static ClassLoader getContextClassLoader() throws SecurityException {
return (ClassLoader)
AccessController.doPrivileged(
(PrivilegedAction
© 2015 - 2024 Weber Informatics LLC | Privacy Policy