You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by dk...@apache.org on 2010/09/07 18:09:41 UTC
svn commit: r993413 - in /webservices/commons/trunk/modules/XmlSchema: ./
src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java
Author: dkulp
Date: Tue Sep 7 16:09:41 2010
New Revision: 993413
URL: http://svn.apache.org/viewvc?rev=993413&view=rev
Log:
Merged revisions 993403 via svnmerge from
https://svn.apache.org/repos/asf/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH
........
r993403 | dkulp | 2010-09-07 11:57:20 -0400 (Tue, 07 Sep 2010) | 2 lines
[WSCOMMONS-537, WSCOMMONS-484] When running with DOM level 3 (and recent JDK), use the namespace/prefix lookup methods on the Element instead of doing the NodeNamespaceContext stuff. That performs better and uses less memory. On level2 and lower, use the old method.
........
Modified:
webservices/commons/trunk/modules/XmlSchema/ (props changed)
webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java
Propchange: webservices/commons/trunk/modules/XmlSchema/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Sep 7 16:09:41 2010
@@ -1 +1 @@
-/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH:763303
+/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH:763303,993403
Propchange: webservices/commons/trunk/modules/XmlSchema/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Tue Sep 7 16:09:41 2010
@@ -1 +1 @@
-/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH:1-741314,763303
+/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH:1-741314,763303,993403
Modified: webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java?rev=993413&r1=993412&r2=993413&view=diff
==============================================================================
--- webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java (original)
+++ webservices/commons/trunk/modules/XmlSchema/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java Tue Sep 7 16:09:41 2010
@@ -19,7 +19,7 @@
package org.apache.ws.commons.schema.utils;
-import java.lang.reflect.Method;
+import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -28,39 +28,42 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.apache.ws.commons.schema.constants.Constants;
+
/**
* Implementation of {@link NamespaceContext}, which is based on a DOM node.
*/
-public final class NodeNamespaceContext implements NamespacePrefixList {
- static Method getUserData;
- static Method setUserData;
- private static final String NODE_NAMSPACE_CONTEXT = NamespacePrefixList.class.getName();
-
+public final class NodeNamespaceContext implements NamespacePrefixList, Serializable {
+ private static final boolean DOM_LEVEL_3;
+
private static final Collection<String> XML_NS_PREFIX_COLLECTION = Collections
.singletonList(Constants.XML_NS_PREFIX);
private static final Collection<String> XMLNS_ATTRIBUTE_COLLECTION = Collections
.singletonList(Constants.XMLNS_ATTRIBUTE);
- private final Map<String, String> declarations;
+
+ private final Map<String, String> declarations;
private String[] prefixes;
+
static {
+ boolean level3 = false;
try {
Class cls = Class.forName("org.w3c.dom.UserDataHandler", false, Node.class.getClassLoader());
- getUserData = Node.class.getMethod("getUserData", new Class[] {
+ Node.class.getMethod("getUserData", new Class[] {
String.class
});
- setUserData = Node.class.getMethod("setUserData", new Class[] {
+ Node.class.getMethod("setUserData", new Class[] {
String.class, Object.class, cls
});
+ level3 = true;
} catch (Throwable e) {
- getUserData = null;
- setUserData = null;
+ level3 = false;
}
+ DOM_LEVEL_3 = level3;
}
/**
@@ -70,51 +73,33 @@ public final class NodeNamespaceContext
declarations = decls;
}
- public static NodeNamespaceContext getNamespaceContext(Node pNode) {
- if (getUserData != null) {
- try {
- NodeNamespaceContext ctx = (NodeNamespaceContext)getUserData.invoke(pNode, new Object[] {
- NODE_NAMSPACE_CONTEXT
- });
- if (ctx == null) {
- Map<String, String> declarations = new HashMap<String, String>();
-
- Node parentNode = pNode.getParentNode();
- if (parentNode != null) {
- NodeNamespaceContext parent = (NodeNamespaceContext)
- getUserData.invoke(parentNode,
- new Object[] {NODE_NAMSPACE_CONTEXT });
- if (parent == null) {
- parent = getNamespaceContext(parentNode);
- }
- declarations.putAll(parent.declarations);
- }
-
- NamedNodeMap map = pNode.getAttributes();
- if (map != null) {
- for (int i = 0; i < map.getLength(); i++) {
- Node attr = map.item(i);
- final String uri = attr.getNamespaceURI();
- if (Constants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) {
- String localName = attr.getLocalName();
- String prefix = Constants.XMLNS_ATTRIBUTE.equals(localName)
- ? Constants.DEFAULT_NS_PREFIX : localName;
- declarations.put(prefix, attr.getNodeValue());
- }
- }
- }
- ctx = new NodeNamespaceContext(declarations);
- setUserData.invoke(pNode, new Object[] {
- NODE_NAMSPACE_CONTEXT, ctx, null
- });
- }
- return ctx;
- } catch (Throwable t) {
- // ignore. DOM level 2 implementation would not have the getUserData stuff.
- // Thus, fall back to the old, slower method.
- }
+
+ public static String getNamespacePrefix(Element el, String ns) {
+ if (DOM_LEVEL_3) {
+ return getNamespacePrefixDomLevel3(el, ns);
}
+ return getNamespaceContext(el).getPrefix(ns);
+ }
+ private static String getNamespacePrefixDomLevel3(Element el, String ns) {
+ return el.lookupPrefix(ns);
+ }
+
+
+ public static String getNamespaceURI(Element el, String pfx) {
+ if (DOM_LEVEL_3) {
+ return getNamespaceURIDomLevel3(el, pfx);
+ }
+ return getNamespaceContext(el).getNamespaceURI(pfx);
+ }
+ private static String getNamespaceURIDomLevel3(Element el, String pfx) {
+ if ("".equals(pfx)) {
+ pfx = null;
+ }
+ return el.lookupNamespaceURI(pfx);
+ }
+
+ public static NodeNamespaceContext getNamespaceContext(Node pNode) {
final Map<String, String> declarations = new HashMap<String, String>();
new PrefixCollector() {
protected void declare(String pPrefix, String pNamespaceURI) {