xmlutil.java
来自「ejb3 java session bean」· Java 代码 · 共 925 行 · 第 1/3 页
JAVA
925 行
/* * JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. * * This is free software; you can redistribute it and/or modify it * under the terms of the JBPM BPEL PUBLIC LICENSE AGREEMENT as * published by JBoss Inc.; either version 1.0 of the License, or * (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */package org.jbpm.bpel.xml.util;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.StringReader;import java.net.URL;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import javax.xml.namespace.QName;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.Templates;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.sax.SAXSource;import javax.xml.transform.stream.StreamResult;import org.apache.commons.collections.IteratorUtils;import org.apache.commons.collections.Predicate;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import com.ibm.wsdl.util.xml.DOMUtils;import org.jbpm.bpel.endpointref.EndpointReference;import org.jbpm.bpel.graph.exe.BpelFaultException;import org.jbpm.bpel.xml.BpelConstants;import org.jbpm.util.ClassLoaderUtil;/** * Utility methods for dealing with JAXP objects. * @author Alejandro Guizar * @version $Revision: 1.26 $ $Date: 2007/11/25 13:03:14 $ */public class XmlUtil { static final String QUALIFIED_VALUE_PREFIX = "valueNS"; private static final Log log = LogFactory.getLog(XmlUtil.class); private static final boolean traceEnabled = log.isTraceEnabled(); private static final String[] schemaSources = createSchemaSources(); private static ThreadLocal documentBuilderLocal = new ThreadLocal() { protected Object initialValue() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); factory.setCoalescing(true); factory.setIgnoringElementContentWhitespace(true); factory.setIgnoringComments(true); try { // set xml schema as the schema language factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", BpelConstants.NS_XML_SCHEMA); // set schema sources factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSources); } catch (IllegalArgumentException e) { log.warn("JAXP implementation does not support XML Schema, " + "XML documents will not be checked for grammar errors", e); } try { // validate the document only if a grammar is specified factory.setAttribute("http://apache.org/xml/features/validation/dynamic", Boolean.TRUE); } catch (IllegalArgumentException e) { log.warn("JAXP implementation is not Xerces, cannot enable dynamic validation, " + "XML documents without schema location will not parse", e); } try { DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(LocalEntityResolver.INSTANCE); return builder; } catch (ParserConfigurationException e) { // should not happen throw new AssertionError(e); } } }; private static ThreadLocal transformerFactoryLocal = new ThreadLocal() { protected Object initialValue() { return TransformerFactory.newInstance(); } }; // suppress default constructor, ensuring non-instantiability private XmlUtil() { } private static String[] createSchemaSources() { final String[] schemaResources = { "xml.xsd", "wsdl.xsd", "bpel_2_0.xsd", "plnktype_2_0.xsd", "varprop.xsd", "serviceref.xsd", "bpel_1_1.xsd", "plnktype_1_1.xsd", "bpel_definition_1_1.xsd", "bpel_deployment_1_1.xsd", "addressing.xsd" }; String[] schemaSources = new String[schemaResources.length]; for (int i = 0; i < schemaResources.length; i++) schemaSources[i] = XmlUtil.class.getResource(schemaResources[i]).toExternalForm(); return schemaSources; } /** * Gets the first child element of the given node having the specified local name and a * <code>null</code> or empty namespace URI. * @param parent the parent node to examine * @param localName the local name of the desired child element * @return the corresponding child element, or <code>null</code> if there is no match */ public static Element getElement(Node parent, String localName) { return getElement(parent, null, localName); } /** * Gets the first child element of the given node having the specified namespace URI and local * name. * @param parent the parent node to examine * @param namespaceURI the namespace URI of the desired child element; if <code>null</code> or * empty, only elements with a <code>null</code> or empty namespace URI will be considered * @param localName the local name of the desired child element * @return the corresponding child element, or <code>null</code> if there is no match */ public static Element getElement(Node parent, String namespaceURI, String localName) { for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (nodeNameEquals(child, namespaceURI, localName)) return (Element) child; } return null; } /** * Gets the first child element of the given node. * @param parent the parent node to examine * @return the corresponding child element, or <code>null</code> if there is no match */ public static Element getElement(Node parent) { for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) return (Element) child; } return null; } /** * Gets an iterator over the child elements of the given node with the specified namespace URI and * any local name. * @param parent the parent node to iterate * @param namespaceURI the namespace URI of the desired child elements; if <code>null</code>, * only elements with a <code>null</code> or empty namespace URI will be iterated * @return an {@link Element} iterator, empty if there is no match */ public static Iterator getElements(Node parent, String namespaceURI) { return IteratorUtils.filteredIterator(new NodeIterator(parent), new NamespaceElementPredicate( namespaceURI)); } /** * Gets an iterator over the child elements of the given node with the specified namespace URI and * local name. * @param parent the parent node to iterate * @param namespaceURI the namespace URI of the desired child elements; if <code>null</code>, * only elements with a <code>null</code> or empty namespace URI will be iterated * @param localName the local name of the desired child elements * @return an {@link Element} iterator, empty if there is no match */ public static Iterator getElements(Node parent, String namespaceURI, String localName) { return IteratorUtils.filteredIterator(new NodeIterator(parent), new QualifiedNameElementPredicate(namespaceURI, localName)); } /** * Gets an iterator over the child elements of the given node. * @param parent the parent node to iterate * @return an {@link Element} iterator, empty if there is no match */ public static Iterator getElements(Node parent) { return IteratorUtils.filteredIterator(new NodeIterator(parent), ElementPredicate.INSTANCE); } /** * Counts the child elements of the given node with the specified namespace URI and local name. * @param parent the parent node to iterate * @param namespaceURI the namespace URI of the desired child elements; if <code>null</code>, * only elements with a <code>null</code> or empty namespace URI will be iterated * @param localName the local name of the desired child elements * @return the element count, non-negative */ public static int countElements(Node parent, String namespaceURI, String localName) { int count = 0; for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) { if (nodeNameEquals(child, namespaceURI, localName)) count++; } return count; } /** * Gets an attribute value of the given element. * @param ownerElem the element that owns the attribute * @param attrName the name of the attribute to retrieve * @return the attribute value as a string, or <code>null</code> if that attribute does not have * a specified value */ public static String getAttribute(Element ownerElem, String attrName) { Attr attribute = ownerElem.getAttributeNode(attrName); return attribute != null ? attribute.getValue() : null; } /** * Parses the string argument as a {@linkplain QName qualified name} in the context of the given * node. * @param prefixedName a name that may contain a colon character * @param contextNode the node to search for namespace declarations * @return a qualified name constructed with the following arguments: * <ul> * <li><code>localPart</code> substring of <code>prefixedName</code> after the first colon</li> * <li><code>prefix</code> substring of <code>prefixedName</code> before the first colon</li> * <li><code>namespaceURI</code> namespace associated with <code>prefix</code> in the * <code>contextNode</code></li> * </ul> */ public static QName parseQName(String prefixedName, Node contextNode) { int index = prefixedName.indexOf(':'); // no colon? if (index == -1) return new QName(prefixedName); // local name String prefix = prefixedName.substring(0, index); return new QName(getNamespaceURI(prefix, contextNode), prefixedName.substring(index + 1), prefix); } /** * Parses the text content of the given element as a {@linkplain QName qualified name}. * @param elem the element whose text content will be parsed * @return the element text content as a {@link QName} */ public static QName getQNameValue(Element elem) { return parseQName(DatatypeUtil.toString(elem), elem); } /** * Parses the value of the given attribute as a {@linkplain QName qualified name}. * @param attr the attribute whose value will be parsed * @return the attribute value as a {@link QName} */ public static QName getQNameValue(Attr attr) { return parseQName(attr.getValue(), attr.getOwnerElement()); } /** * Tells whether the name of the specified node matches the given qualified name. * @param node the node to examine * @param name the qualified name to match * @return <code>true</code> if the qualified name matches, <code>false</code> otherwise */ public static boolean nodeNameEquals(Node node, QName name) { return nodeNameEquals(node, name.getNamespaceURI(), name.getLocalPart()); } /** * Tells whether the name of the specified node matches the given namespace URI and local name. * @param node the node to examine * @param namespaceURI the namespace URI to match * @param localName the local name to match * @return <code>true</code> if the namespace URI and local name match, <code>false</code> * otherwise */ public static boolean nodeNameEquals(Node node, String namespaceURI, String localName) { return nodeNamespaceURIEquals(node, namespaceURI) && localName.equals(node.getLocalName()); } /** * Tells whether the namespace URI of the specified node matches the given namespace URI. * @param node the node to examine * @param namespaceURI the namespace URI to match
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?