1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: PrefixResolverDefault.java,v 1.9 2004/02/20 20:32:51 jycli Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. import org.w3c.dom.NamedNodeMap;
  21. import org.w3c.dom.Node;
  22. /**
  23. * This class implements a generic PrefixResolver that
  24. * can be used to perform prefix-to-namespace lookup
  25. * for the XPath object.
  26. * @xsl.usage general
  27. */
  28. public class PrefixResolverDefault implements PrefixResolver
  29. {
  30. /**
  31. * The context to resolve the prefix from, if the context
  32. * is not given.
  33. */
  34. Node m_context;
  35. /**
  36. * Construct a PrefixResolverDefault object.
  37. * @param xpathExpressionContext The context from
  38. * which XPath expression prefixes will be resolved.
  39. * Warning: This will not work correctly if xpathExpressionContext
  40. * is an attribute node.
  41. * @param xpathExpressionContext Node from which to start searching for a
  42. * xmlns attribute that binds a prefix to a namespace (when the namespace
  43. * context is not specified in the getNamespaceForPrefix call).
  44. */
  45. public PrefixResolverDefault(Node xpathExpressionContext)
  46. {
  47. m_context = xpathExpressionContext;
  48. }
  49. /**
  50. * Given a namespace, get the corrisponding prefix. This assumes that
  51. * the PrevixResolver hold's it's own namespace context, or is a namespace
  52. * context itself.
  53. * @param prefix Prefix to resolve.
  54. * @return Namespace that prefix resolves to, or null if prefix
  55. * is not bound.
  56. */
  57. public String getNamespaceForPrefix(String prefix)
  58. {
  59. return getNamespaceForPrefix(prefix, m_context);
  60. }
  61. /**
  62. * Given a namespace, get the corrisponding prefix.
  63. * Warning: This will not work correctly if namespaceContext
  64. * is an attribute node.
  65. * @param prefix Prefix to resolve.
  66. * @param namespaceContext Node from which to start searching for a
  67. * xmlns attribute that binds a prefix to a namespace.
  68. * @return Namespace that prefix resolves to, or null if prefix
  69. * is not bound.
  70. */
  71. public String getNamespaceForPrefix(String prefix,
  72. org.w3c.dom.Node namespaceContext)
  73. {
  74. Node parent = namespaceContext;
  75. String namespace = null;
  76. if (prefix.equals("xml"))
  77. {
  78. namespace = Constants.S_XMLNAMESPACEURI;
  79. }
  80. else
  81. {
  82. int type;
  83. while ((null != parent) && (null == namespace)
  84. && (((type = parent.getNodeType()) == Node.ELEMENT_NODE)
  85. || (type == Node.ENTITY_REFERENCE_NODE)))
  86. {
  87. if (type == Node.ELEMENT_NODE)
  88. {
  89. if (parent.getNodeName().indexOf(prefix+":") == 0)
  90. return parent.getNamespaceURI();
  91. NamedNodeMap nnm = parent.getAttributes();
  92. for (int i = 0; i < nnm.getLength(); i++)
  93. {
  94. Node attr = nnm.item(i);
  95. String aname = attr.getNodeName();
  96. boolean isPrefix = aname.startsWith("xmlns:");
  97. if (isPrefix || aname.equals("xmlns"))
  98. {
  99. int index = aname.indexOf(':');
  100. String p = isPrefix ? aname.substring(index + 1) : "";
  101. if (p.equals(prefix))
  102. {
  103. namespace = attr.getNodeValue();
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. parent = parent.getParentNode();
  110. }
  111. }
  112. return namespace;
  113. }
  114. /**
  115. * Return the base identifier.
  116. *
  117. * @return null
  118. */
  119. public String getBaseIdentifier()
  120. {
  121. return null;
  122. }
  123. /**
  124. * @see PrefixResolver#handlesNullPrefixes()
  125. */
  126. public boolean handlesNullPrefixes() {
  127. return false;
  128. }
  129. }