001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: PrefixResolverDefault.java,v 1.10 2005/01/23 00:52:41 mcnamara Exp $
018: */
019: package org.apache.xml.utils;
020:
021: import org.w3c.dom.NamedNodeMap;
022: import org.w3c.dom.Node;
023:
024: /**
025: * This class implements a generic PrefixResolver that
026: * can be used to perform prefix-to-namespace lookup
027: * for the XPath object.
028: * @xsl.usage general
029: */
030: public class PrefixResolverDefault implements PrefixResolver {
031:
032: /**
033: * The context to resolve the prefix from, if the context
034: * is not given.
035: */
036: Node m_context;
037:
038: /**
039: * Construct a PrefixResolverDefault object.
040: * @param xpathExpressionContext The context from
041: * which XPath expression prefixes will be resolved.
042: * Warning: This will not work correctly if xpathExpressionContext
043: * is an attribute node.
044: */
045: public PrefixResolverDefault(Node xpathExpressionContext) {
046: m_context = xpathExpressionContext;
047: }
048:
049: /**
050: * Given a namespace, get the corrisponding prefix. This assumes that
051: * the PrevixResolver hold's it's own namespace context, or is a namespace
052: * context itself.
053: * @param prefix Prefix to resolve.
054: * @return Namespace that prefix resolves to, or null if prefix
055: * is not bound.
056: */
057: public String getNamespaceForPrefix(String prefix) {
058: return getNamespaceForPrefix(prefix, m_context);
059: }
060:
061: /**
062: * Given a namespace, get the corrisponding prefix.
063: * Warning: This will not work correctly if namespaceContext
064: * is an attribute node.
065: * @param prefix Prefix to resolve.
066: * @param namespaceContext Node from which to start searching for a
067: * xmlns attribute that binds a prefix to a namespace.
068: * @return Namespace that prefix resolves to, or null if prefix
069: * is not bound.
070: */
071: public String getNamespaceForPrefix(String prefix,
072: org.w3c.dom.Node namespaceContext) {
073:
074: Node parent = namespaceContext;
075: String namespace = null;
076:
077: if (prefix.equals("xml")) {
078: namespace = Constants.S_XMLNAMESPACEURI;
079: } else {
080: int type;
081:
082: while ((null != parent)
083: && (null == namespace)
084: && (((type = parent.getNodeType()) == Node.ELEMENT_NODE) || (type == Node.ENTITY_REFERENCE_NODE))) {
085: if (type == Node.ELEMENT_NODE) {
086: if (parent.getNodeName().indexOf(prefix + ":") == 0)
087: return parent.getNamespaceURI();
088: NamedNodeMap nnm = parent.getAttributes();
089:
090: for (int i = 0; i < nnm.getLength(); i++) {
091: Node attr = nnm.item(i);
092: String aname = attr.getNodeName();
093: boolean isPrefix = aname.startsWith("xmlns:");
094:
095: if (isPrefix || aname.equals("xmlns")) {
096: int index = aname.indexOf(':');
097: String p = isPrefix ? aname
098: .substring(index + 1) : "";
099:
100: if (p.equals(prefix)) {
101: namespace = attr.getNodeValue();
102:
103: break;
104: }
105: }
106: }
107: }
108:
109: parent = parent.getParentNode();
110: }
111: }
112:
113: return namespace;
114: }
115:
116: /**
117: * Return the base identifier.
118: *
119: * @return null
120: */
121: public String getBaseIdentifier() {
122: return null;
123: }
124:
125: /**
126: * @see PrefixResolver#handlesNullPrefixes()
127: */
128: public boolean handlesNullPrefixes() {
129: return false;
130: }
131:
132: }
|