xmlutils.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 586 行 · 第 1/2 页
JAVA
586 行
/*
* 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.axis2.util;
import com.ibm.wsdl.Constants;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.util.Base64;
import org.apache.axiom.om.util.StAXUtils;
import org.w3c.dom.Attr;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Stack;
public class XMLUtils {
public static final String charEncoding = "ISO-8859-1";
private static final String saxParserFactoryProperty =
"javax.xml.parsers.SAXParserFactory";
private static DocumentBuilderFactory dbf = getDOMFactory();
private static SAXParserFactory saxFactory;
private static Stack saxParsers = new Stack();
private static String empty = "";
private static ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
static {
// Initialize SAX Parser factory defaults
initSAXFactory(null, true, false);
}
/**
* Initializes the SAX parser factory.
*
* @param factoryClassName The (optional) class name of the desired
* SAXParserFactory implementation. Will be
* assigned to the system property
* <b>javax.xml.parsers.SAXParserFactory</b>
* unless this property is already set.
* If <code>null</code>, leaves current setting
* alone.
* @param namespaceAware true if we want a namespace-aware parser
* @param validating true if we want a validating parser
*/
public static void initSAXFactory(String factoryClassName,
boolean namespaceAware,
boolean validating) {
if (factoryClassName != null) {
try {
saxFactory = (SAXParserFactory) Loader.loadClass(factoryClassName).
newInstance();
/*
* Set the system property only if it is not already set to
* avoid corrupting environments in which Axis is embedded.
*/
if (System.getProperty(saxParserFactoryProperty) == null) {
System.setProperty(saxParserFactoryProperty,
factoryClassName);
}
} catch (Exception e) {
//log.error(Messages.getMessage("exception00"), e);
saxFactory = null;
}
} else {
saxFactory = SAXParserFactory.newInstance();
}
saxFactory.setNamespaceAware(namespaceAware);
saxFactory.setValidating(validating);
// Discard existing parsers
saxParsers.clear();
}
private static DocumentBuilderFactory getDOMFactory() {
DocumentBuilderFactory dbf;
try {
dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
}
catch (Exception e) {
//log.error(Messages.getMessage("exception00"), e );
dbf = null;
}
return (dbf);
}
private static boolean tryReset = true;
/**
* Returns a SAX parser for reuse.
*
* @param parser A SAX parser that is available for reuse
*/
public static void releaseSAXParser(SAXParser parser) {
if (!tryReset) {
return;
}
//Free up possible ref. held by past contenthandler.
try {
XMLReader xmlReader = parser.getXMLReader();
if (null != xmlReader) {
synchronized (XMLUtils.class) {
saxParsers.push(parser);
}
} else {
tryReset = false;
}
} catch (org.xml.sax.SAXException e) {
tryReset = false;
}
}
/**
* Gets an empty new Document.
*
* @return Returns Document.
* @throws ParserConfigurationException if construction problems occur
*/
public static Document newDocument()
throws ParserConfigurationException {
synchronized (dbf) {
return dbf.newDocumentBuilder().newDocument();
}
}
/**
* Gets a new Document read from the input source.
*
* @return Returns Document.
* @throws ParserConfigurationException if construction problems occur
* @throws SAXException if the document has xml sax problems
* @throws IOException if i/o exceptions occur
*/
public static Document newDocument(InputSource inp)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilder db;
synchronized (dbf) {
try {
db = dbf.newDocumentBuilder();
} catch (Exception e){
// Under some peculiar conditions (classloader issues), just scrap the old dbf, create a new one and try again.
dbf = getDOMFactory();
db = dbf.newDocumentBuilder();
}
}
db.setEntityResolver(new DefaultEntityResolver());
db.setErrorHandler(new ParserErrorHandler());
return (db.parse(inp));
}
/**
* Gets a new Document read from the input stream
*
* @return Returns Document.
* @throws ParserConfigurationException if construction problems occur
* @throws SAXException if the document has xml sax problems
* @throws IOException if i/o exceptions occur
*/
public static Document newDocument(InputStream inp)
throws ParserConfigurationException, SAXException, IOException {
return XMLUtils.newDocument(new InputSource(inp));
}
/**
* Gets a new Document read from the indicated uri
*
* @return Returns Document.
* @throws ParserConfigurationException if construction problems occur
* @throws SAXException if the document has xml sax problems
* @throws IOException if i/o exceptions occur
*/
public static Document newDocument(String uri)
throws ParserConfigurationException, SAXException, IOException {
// call the authenticated version as there might be
// username/password info embeded in the uri.
return XMLUtils.newDocument(uri, null, null);
}
/**
* Creates a new document from the given URI. Uses the username and password
* if the URI requires authentication.
*
* @param uri the resource to get
* @param username basic auth username
* @param password basic auth password
* @throws ParserConfigurationException if construction problems occur
* @throws SAXException if the document has xml sax problems
* @throws IOException if i/o exceptions occur
*/
public static Document newDocument(String uri, String username, String password)
throws ParserConfigurationException, SAXException, IOException {
InputSource ins = XMLUtils.getInputSourceFromURI(uri, username, password);
Document doc = XMLUtils.newDocument(ins);
// Close the Stream
if (ins.getByteStream() != null) {
ins.getByteStream().close();
} else if (ins.getCharacterStream() != null) {
ins.getCharacterStream().close();
}
return doc;
}
public static String getPrefix(String uri, Node e) {
while (e != null && (e.getNodeType() == Element.ELEMENT_NODE)) {
NamedNodeMap attrs = e.getAttributes();
for (int n = 0; n < attrs.getLength(); n++) {
Attr a = (Attr) attrs.item(n);
String name;
if ((name = a.getName()).startsWith("xmlns:") &&
a.getNodeValue().equals(uri)) {
return name.substring(6);
}
}
e = e.getParentNode();
}
return null;
}
public static String getNamespace(String prefix, Node e) {
while (e != null && (e.getNodeType() == Node.ELEMENT_NODE)) {
Attr attr =
((Element) e).getAttributeNodeNS(Constants.NS_URI_XMLNS, prefix);
if (attr != null) {
return attr.getValue();
}
e = e.getParentNode();
}
return null;
}
/**
* Returns a QName when passed a string like "foo:bar" by mapping
* the "foo" prefix to a namespace in the context of the given Node.
*
* @return Returns a QName generated from the given string representation.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?