2
* Licensed to the Apache Software Foundation (ASF) under one
3
* or more contributor license agreements. See the NOTICE file
4
* distributed with this work for additional information
5
* regarding copyright ownership. The ASF licenses this file
6
* to you under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
10
* http://www.apache.org/licenses/LICENSE-2.0
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
18
// $Id: JAXPPrefixResolver.java,v 1.2 2009/12/10 03:18:50 matthewoliver Exp $
20
package org.apache.xpath.jaxp;
22
import org.w3c.dom.Node;
23
import org.w3c.dom.NamedNodeMap;
24
import org.apache.xml.utils.PrefixResolver;
26
import javax.xml.namespace.NamespaceContext;
29
* <meta name="usage" content="general"/>
30
* This class implements a Default PrefixResolver which
31
* can be used to perform prefix-to-namespace lookup
32
* for the XPath object.
33
* This class delegates the resolution to the passed NamespaceContext
35
public class JAXPPrefixResolver implements PrefixResolver
38
private NamespaceContext namespaceContext;
41
public JAXPPrefixResolver ( NamespaceContext nsContext ) {
42
this.namespaceContext = nsContext;
46
public String getNamespaceForPrefix( String prefix ) {
47
return namespaceContext.getNamespaceURI( prefix );
51
* Return the base identifier.
55
public String getBaseIdentifier() {
60
* @see PrefixResolver#handlesNullPrefixes()
62
public boolean handlesNullPrefixes() {
68
* The URI for the XML namespace.
69
* (Duplicate of that found in org.apache.xpath.XPathContext).
72
public static final String S_XMLNAMESPACEURI =
73
"http://www.w3.org/XML/1998/namespace";
77
* Given a prefix and a Context Node, get the corresponding namespace.
78
* Warning: This will not work correctly if namespaceContext
79
* is an attribute node.
80
* @param prefix Prefix to resolve.
81
* @param namespaceContext Node from which to start searching for a
82
* xmlns attribute that binds a prefix to a namespace.
83
* @return Namespace that prefix resolves to, or null if prefix
86
public String getNamespaceForPrefix(String prefix,
87
org.w3c.dom.Node namespaceContext) {
88
Node parent = namespaceContext;
89
String namespace = null;
91
if (prefix.equals("xml")) {
92
namespace = S_XMLNAMESPACEURI;
96
while ((null != parent) && (null == namespace)
97
&& (((type = parent.getNodeType()) == Node.ELEMENT_NODE)
98
|| (type == Node.ENTITY_REFERENCE_NODE))) {
100
if (type == Node.ELEMENT_NODE) {
101
NamedNodeMap nnm = parent.getAttributes();
103
for (int i = 0; i < nnm.getLength(); i++) {
104
Node attr = nnm.item(i);
105
String aname = attr.getNodeName();
106
boolean isPrefix = aname.startsWith("xmlns:");
108
if (isPrefix || aname.equals("xmlns")) {
109
int index = aname.indexOf(':');
110
String p =isPrefix ?aname.substring(index + 1) :"";
112
if (p.equals(prefix)) {
113
namespace = attr.getNodeValue();
120
parent = parent.getParentNode();