xmlutil.java
来自「ejb3 java session bean」· Java 代码 · 共 925 行 · 第 1/3 页
JAVA
925 行
* @return <code>true</code> if the namespace URI matches, <code>false</code> otherwise */ public static boolean nodeNamespaceURIEquals(Node node, String namespaceURI) { String nodeNamespaceURI = node.getNamespaceURI(); return nodeNamespaceURI == null || nodeNamespaceURI.length() == 0 ? namespaceURI == null || namespaceURI.length() == 0 : nodeNamespaceURI.equals(namespaceURI); } public static void setObjectValue(Node node, Object value) { switch (node.getNodeType()) { case Node.ELEMENT_NODE: setObjectValue((Element) node, value); break; case Node.DOCUMENT_NODE: setObjectValue(((Document) node).getDocumentElement(), value); break; default: // BPEL-243 throw selectionFailure if the source is an EII with xsi:nil=true if (value instanceof Element) { String nil = ((Element) value).getAttributeNS(BpelConstants.NS_XML_SCHEMA_INSTANCE, BpelConstants.ATTR_NIL); if (DatatypeUtil.parseBoolean(nil) == Boolean.TRUE) throw new BpelFaultException(BpelConstants.FAULT_SELECTION_FAILURE); } // replace content node.setNodeValue(DatatypeUtil.toString(value)); } } public static void setObjectValue(Element elem, Object value) { if (value instanceof Node) { switch (((Node) value).getNodeType()) { case Node.ELEMENT_NODE: // replace element copy(elem, (Element) value); break; case Node.DOCUMENT_NODE: // replace element copy(elem, ((Document) value).getDocumentElement()); break; default: // replace content setStringValue(elem, ((Node) value).getNodeValue()); } } else if (value instanceof EndpointReference) { // replace element ((EndpointReference) value).writeServiceRef(elem); } else { // replace content setStringValue(elem, DatatypeUtil.toString(value)); } } public static void setStringValue(Element elem, String value) { // remove jbpm:initialized elem.removeAttributeNS(BpelConstants.NS_VENDOR, BpelConstants.ATTR_INITIALIZED); Node firstChild = elem.getFirstChild(); // if first child is a text node, reuse it if (firstChild instanceof org.w3c.dom.Text) firstChild.setNodeValue(value); // otherwise, just create a new text node else firstChild = elem.getOwnerDocument().createTextNode(value); // remove all children removeChildNodes(elem); // append text elem.appendChild(firstChild); } public static void setQNameValue(Element elem, QName value) { setStringValue(elem, formatQName(value, elem)); } public static void setQNameValue(Attr attr, QName value) { attr.setValue(formatQName(value, attr.getOwnerElement())); } private static String formatQName(QName value, Element elem) { String namespace = value.getNamespaceURI(); String localName = value.getLocalPart(); // easy way out: no namespace if (namespace.length() == 0) return localName; String prefix = getPrefix(namespace, elem); if (prefix == null) { String givenPrefix = value.getPrefix(); prefix = generatePrefix(givenPrefix.length() > 0 ? givenPrefix : QUALIFIED_VALUE_PREFIX, elem); addNamespaceDeclaration(elem, namespace, prefix); } return prefix + ':' + localName; } public static String toTraceString(Element elem) { String namespace = elem.getNamespaceURI(); String localName = elem.getLocalName(); // easy way out: no namespace if (namespace == null || namespace.length() == 0) return localName; StringBuffer traceBuffer = new StringBuffer(namespace.length() + localName.length()); traceBuffer.append('{').append(namespace).append('}'); String prefix = elem.getPrefix(); if (prefix != null && prefix.length() > 0) traceBuffer.append(prefix).append(':'); return traceBuffer.append(localName).toString(); } public static void copy(Element target, Element source) { if (traceEnabled) log.trace("copying from: " + toTraceString(source)); // attributes removeAttributes(target); copyAttributes(target, source); // all namespaces copyVisibleNamespaces(target, source); ensureOwnNamespaceDeclared(target); // child nodes removeChildNodes(target); copyChildNodes(target, source); if (traceEnabled) log.trace("copied to: " + toTraceString(target)); } public static void copyVisibleNamespaces(final Element target, Element source) { // copy namespaces declared at source element copyNamespaces(target, source); // go up the element hierarchy for (Node parent = source.getParentNode(); parent instanceof Element; parent = parent.getParentNode()) copyNamespaces(target, (Element) parent); } public static void copyNamespaces(final Element target, Element source) { // easy way out: no attributes if (!source.hasAttributes()) return; // traverse attributes to discover namespace declarations NamedNodeMap attributes = source.getAttributes(); for (int i = 0, n = attributes.getLength(); i < n; i++) { Node attribute = attributes.item(i); // is attribute a namespace declaration? if (!BpelConstants.NS_XMLNS.equals(attribute.getNamespaceURI())) continue; // namespace declaration format // xmlns:prefix="namespaceURI" | xmlns="defaultNamespaceURI" String namespaceURI = attribute.getNodeValue(); String prefix = attribute.getLocalName(); // default namespace declaration? if ("xmlns".equals(prefix)) { // BPEL-195: prevent addition matching visible declaration at target if ("".equals(getPrefix(namespaceURI, target))) continue; addNamespaceDeclaration(target, namespaceURI); if (traceEnabled) log.trace("added default namespace declaration: " + namespaceURI); } else { // BPEL-195: prevent addition matching visible declaration at target if (prefix.equals(getPrefix(namespaceURI, target))) continue; addNamespaceDeclaration(target, namespaceURI, prefix); if (traceEnabled) log.trace("added namespace declaration: " + prefix + "->" + namespaceURI); } } } public static void copyAttributes(Element target, Element source) { // easy way out: no attributes if (!source.hasAttributes()) return; // traverse attributes NamedNodeMap attributes = source.getAttributes(); for (int i = 0, n = attributes.getLength(); i < n; i++) { Node sourceAttr = attributes.item(i); String namespaceURI = sourceAttr.getNamespaceURI(); String name = sourceAttr.getNodeName(); // namespace declaration? if (BpelConstants.NS_XMLNS.equals(namespaceURI)) continue; // unqualified? if (namespaceURI == null || namespaceURI.length() == 0) { target.setAttribute(name, sourceAttr.getNodeValue()); if (traceEnabled) log.trace("set attribute: " + name); } // qualified else { Attr targetAttr = target.getOwnerDocument().createAttributeNS(namespaceURI, name); targetAttr.setValue(sourceAttr.getNodeValue()); target.setAttributeNodeNS(targetAttr); ensureNamespaceDeclared(targetAttr, namespaceURI, sourceAttr.getPrefix()); if (traceEnabled) log.trace("set attribute: {" + namespaceURI + '}' + name); } } } public static void copyChildNodes(Element target, Element source) { Document targetDoc = target.getOwnerDocument(); for (Node child = source.getFirstChild(); child != null; child = child.getNextSibling()) { switch (child.getNodeType()) { case Node.ELEMENT_NODE: copyChildElement(target, (Element) child); break; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: target.appendChild(targetDoc.createTextNode(child.getNodeValue())); if (traceEnabled) log.trace("appended text: " + child.getNodeValue()); break; default: log.debug("discarding child: " + child); } } } public static void copyChildElement(Element parent, Element source) { Element target = (Element) parent.getOwnerDocument().importNode(source, true); parent.appendChild(target); // ensure namespace declared after appending in order to consider all visible declarations ensureOwnNamespaceDeclared(target); if (traceEnabled) log.trace("appended element: " + toTraceString(target)); } /** * Ensures the given namespace URI is associated to the given prefix in the scope of the given * attribute. If the namespace URI is <em>not</em> associated to the prefix, a namespace * declaration is {@linkplain #addNamespaceDeclaration(Element, String, String) added} to the * attribute's owner element. Otherwise, this call has no effect. * @param attribute the attribute to verify * @param namespaceURI the namespace URI to ensure; cannot be <code>null</code> or empty * @param prefix the prefix to ensure; cannot be <code>null</code> or empty */ public static void ensureNamespaceDeclared(Attr attribute, String namespaceURI, String prefix) { if (prefix == null || prefix.length() == 0) throw new IllegalArgumentException("prefix cannot be empty"); if (namespaceURI == null || namespaceURI.length() == 0) throw new IllegalArgumentException("namespaceURI cannot be empty"); Element element = attribute.getOwnerElement(); if (!prefix.equals(getPrefix(namespaceURI, element))) { // prefix is associated with other/no URI, declare locally addNamespaceDeclaration(element, namespaceURI, prefix); } } /** * Ensures the given namespace URI is associated to the given prefix in the scope of the given * element. If the namespace URI is <em>not</em> associated to the prefix, a namespace * declaration is {@linkplain #addNamespaceDeclaration(Element, String, String) added} to the * element. Otherwise, this call has no effect. * @param element the element to verify * @param namespaceURI the namespace URI to ensure; cannot be <code>null</code> or empty unless * <code>prefix</code> is <code>null</code> or empty as well * @param prefix the prefix to ensure; if <code>null</code> or empty, the given namespace URI is * ensured to be the default * @throws IllegalArgumentException if <code>namespaceURI</code> is <code>null</code> or * empty, <em>unless</em> <code>prefix</code> is empty as well */ public static void ensureNamespaceDeclared(Element element, String namespaceURI, String prefix) { if (prefix == null || prefix.length() == 0) { if (namespaceURI == null) namespaceURI = ""; // verify the given URI is the default namespace if (!"".equals(getPrefix(namespaceURI, element))) { // the given URI is not the default namespace, declare locally addNamespaceDeclaration(element, namespaceURI); } } else { if (namespaceURI == null || namespaceURI.length() == 0) throw new IllegalArgumentException("namespaceURI cannot be empty unless prefix is empty"); // verify given prefix is associated to given URI if (!prefix.equals(getPrefix(namespaceURI, element))) { // prefix is associated with other/no URI, declare locally addNamespaceDeclaration(element, namespaceURI, prefix); } } } public static void ensureOwnNamespaceDeclared(Element elem) { ensureNamespaceDeclared(elem, elem.getNamespaceURI(), elem.getPrefix()); } public static Node appendForeignChild(Node node, Node foreignChild) { return node.appendChild(node.getOwnerDocument().importNode(foreignChild, true)); } public static void addNamespaceDeclaration(Element elem, String namespaceURI) { elem.setAttributeNS(BpelConstants.NS_XMLNS, "xmlns", namespaceURI); } public static void addNamespaceDeclaration(Element elem, String namespaceURI, String prefix) { elem.setAttributeNS(BpelConstants.NS_XMLNS, "xmlns:" + prefix, namespaceURI); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?