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. // $Id: JAXPPrefixResolver.java,v 1.2 2004/07/10 03:27:20 rameshm Exp $
  17. package com.sun.org.apache.xpath.internal.jaxp;
  18. import org.w3c.dom.Node;
  19. import org.w3c.dom.NamedNodeMap;
  20. import com.sun.org.apache.xml.internal.utils.PrefixResolver;
  21. import javax.xml.namespace.NamespaceContext;
  22. /**
  23. * <meta name="usage" content="general"/>
  24. * This class implements a Default PrefixResolver which
  25. * can be used to perform prefix-to-namespace lookup
  26. * for the XPath object.
  27. * This class delegates the resolution to the passed NamespaceContext
  28. */
  29. public class JAXPPrefixResolver implements PrefixResolver
  30. {
  31. private NamespaceContext namespaceContext;
  32. public JAXPPrefixResolver ( NamespaceContext nsContext ) {
  33. this.namespaceContext = nsContext;
  34. }
  35. public String getNamespaceForPrefix( String prefix ) {
  36. return namespaceContext.getNamespaceURI( prefix );
  37. }
  38. /**
  39. * Return the base identifier.
  40. *
  41. * @return null
  42. */
  43. public String getBaseIdentifier() {
  44. return null;
  45. }
  46. /**
  47. * @see PrefixResolver#handlesNullPrefixes()
  48. */
  49. public boolean handlesNullPrefixes() {
  50. return false;
  51. }
  52. /**
  53. * The URI for the XML namespace.
  54. * (Duplicate of that found in com.sun.org.apache.xpath.internal.XPathContext).
  55. */
  56. public static final String S_XMLNAMESPACEURI =
  57. "http://www.w3.org/XML/1998/namespace";
  58. /**
  59. * Given a prefix and a Context Node, get the corresponding namespace.
  60. * Warning: This will not work correctly if namespaceContext
  61. * is an attribute node.
  62. * @param prefix Prefix to resolve.
  63. * @param namespaceContext Node from which to start searching for a
  64. * xmlns attribute that binds a prefix to a namespace.
  65. * @return Namespace that prefix resolves to, or null if prefix
  66. * is not bound.
  67. */
  68. public String getNamespaceForPrefix(String prefix,
  69. org.w3c.dom.Node namespaceContext) {
  70. Node parent = namespaceContext;
  71. String namespace = null;
  72. if (prefix.equals("xml")) {
  73. namespace = S_XMLNAMESPACEURI;
  74. } else {
  75. int type;
  76. while ((null != parent) && (null == namespace)
  77. && (((type = parent.getNodeType()) == Node.ELEMENT_NODE)
  78. || (type == Node.ENTITY_REFERENCE_NODE))) {
  79. if (type == Node.ELEMENT_NODE) {
  80. NamedNodeMap nnm = parent.getAttributes();
  81. for (int i = 0; i < nnm.getLength(); i++) {
  82. Node attr = nnm.item(i);
  83. String aname = attr.getNodeName();
  84. boolean isPrefix = aname.startsWith("xmlns:");
  85. if (isPrefix || aname.equals("xmlns")) {
  86. int index = aname.indexOf(':');
  87. String p =isPrefix ?aname.substring(index + 1) :"";
  88. if (p.equals(prefix)) {
  89. namespace = attr.getNodeValue();
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. parent = parent.getParentNode();
  96. }
  97. }
  98. return namespace;
  99. }
  100. }