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 17:57:21 UTC
svn commit: r993403 - in
/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema:
SchemaBuilder.java utils/NodeNamespaceContext.java
Author: dkulp
Date: Tue Sep 7 15:57:20 2010
New Revision: 993403
URL: http://svn.apache.org/viewvc?rev=993403&view=rev
Log:
[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/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java
Modified: webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java?rev=993403&r1=993402&r2=993403&view=diff
==============================================================================
--- webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java (original)
+++ webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java Tue Sep 7 15:57:20 2010
@@ -191,8 +191,8 @@ public class SchemaBuilder {
+ "Please update the schema to the \""
+ XmlSchema.SCHEMA_NS + "\" namespace");
}
- for (; el != null; el = XDOMUtil.getNextSiblingElementNS(el,
- XmlSchema.SCHEMA_NS)) {
+ for (; el != null;
+ el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
// String elPrefix = el.getPrefix() == null ? "" : el.getPrefix();
//if(elPrefix.equals(schema.schema_ns_prefix)) {
@@ -257,7 +257,7 @@ public class SchemaBuilder {
//add the extesibility components
processExtensibilityComponents(schema, schemaEl);
-
+
return schema;
}
@@ -433,9 +433,8 @@ public class SchemaBuilder {
restrictionEl, XmlSchema.SCHEMA_NS, "simpleType");
if (restrictionEl.hasAttribute("base")) {
- NamespaceContext ctx = NodeNamespaceContext.getNamespaceContext(restrictionEl);
restriction.baseTypeName = getRefQName(restrictionEl
- .getAttribute("base"), ctx);
+ .getAttribute("base"), restrictionEl);
} else if (inlineSimpleType != null) {
restriction.baseType = handleSimpleType(schema,
@@ -562,17 +561,13 @@ public class SchemaBuilder {
return simpleType;
}
- private QName getRefQName(String pName, Node pNode) {
- return getRefQName(pName, NodeNamespaceContext.getNamespaceContext(pNode));
- }
-
- private QName getRefQName(String pName, NamespaceContext pContext) {
+ private QName getRefQName(String pName, Element pNode) {
final int offset = pName.indexOf(':');
String uri;
final String localName;
final String prefix;
if (offset == -1) {
- uri = pContext.getNamespaceURI(Constants.DEFAULT_NS_PREFIX);
+ uri = NodeNamespaceContext.getNamespaceURI(pNode, Constants.DEFAULT_NS_PREFIX);
if (Constants.NULL_NS_URI.equals(uri)) {
return new QName(Constants.NULL_NS_URI, pName);
}
@@ -580,7 +575,7 @@ public class SchemaBuilder {
prefix = Constants.DEFAULT_NS_PREFIX;
} else {
prefix = pName.substring(0, offset);
- uri = pContext.getNamespaceURI(prefix);
+ uri = NodeNamespaceContext.getNamespaceURI(pNode, prefix);
if (uri == null || Constants.NULL_NS_URI.equals(uri)) {
if (schema.parent != null
&& schema.parent.getNamespaceContext() != null) {
@@ -1297,7 +1292,6 @@ public class SchemaBuilder {
NamedNodeMap attrNodes = attrEl.getAttributes();
Vector attrs = new Vector();
- NodeNamespaceContext ctx = null;
for (int i = 0; i < attrNodes.getLength(); i++) {
Attr att = (Attr) attrNodes.item(i);
String attName = att.getName();
@@ -1312,10 +1306,7 @@ public class SchemaBuilder {
if (value.indexOf(":") > -1) {
// there is a possiblily of some namespace mapping
String prefix = value.substring(0, value.indexOf(":"));
- if (ctx == null) {
- ctx = NodeNamespaceContext.getNamespaceContext(attrEl);
- }
- String namespace = ctx.getNamespaceURI(prefix);
+ String namespace = NodeNamespaceContext.getNamespaceURI(attrEl, prefix);
if (!Constants.NULL_NS_URI.equals(namespace)) {
Attr nsAttr = attrEl.getOwnerDocument()
.createAttribute("xmlns:" + prefix);
Modified: webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java
URL: http://svn.apache.org/viewvc/webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java?rev=993403&r1=993402&r2=993403&view=diff
==============================================================================
--- webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java (original)
+++ webservices/commons/branches/modules/XmlSchema/1_4_X_BRANCH/src/main/java/org/apache/ws/commons/schema/utils/NodeNamespaceContext.java Tue Sep 7 15:57:20 2010
@@ -21,33 +21,37 @@ package org.apache.ws.commons.schema.uti
import org.apache.ws.commons.schema.constants.Constants;
+import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import javax.xml.namespace.NamespaceContext;
+import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
/**
* Implementation of {@link NamespaceContext}, which is based on a DOM node.
*/
-public class NodeNamespaceContext implements NamespacePrefixList {
- private static final String NODE_NAMSPACE_CONTEXT = NamespacePrefixList.class.getName();
+public class NodeNamespaceContext implements NamespacePrefixList, Serializable {
private static final Collection XML_NS_PREFIX_COLLECTION = Collections.singletonList(Constants.XML_NS_PREFIX);
private static final Collection XMLNS_ATTRIBUTE_COLLECTION = Collections.singletonList(Constants.XMLNS_ATTRIBUTE);
- static Method getUserData;
- static Method setUserData;
+ static final boolean domLevel3;
+
static {
+ boolean level3 = false;
try {
Class cls = Class.forName("org.w3c.dom.UserDataHandler", false, Node.class.getClassLoader());
- getUserData = Node.class.getMethod("getUserData", new Class[]{String.class});
- setUserData = Node.class.getMethod("setUserData", new Class[]{String.class, Object.class, cls});
+ Node.class.getMethod("getUserData", new Class[]{String.class});
+ Node.class.getMethod("setUserData", new Class[]{String.class, Object.class, cls});
+ level3 = true;
} catch (Throwable e) {
- getUserData = null;
- setUserData = null;
+ level3 = false;
}
+ domLevel3 = level3;
}
@@ -62,45 +66,31 @@ public class NodeNamespaceContext implem
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 declarations = new HashMap();
-
- 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 (domLevel3) {
+ 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 (domLevel3) {
+ 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 declarations = new HashMap();
new PrefixCollector(){
protected void declare(String pPrefix, String pNamespaceURI) {