001: package net.sf.saxon.xpath;
002:
003: import net.sf.saxon.om.NamespaceResolver;
004:
005: import javax.xml.namespace.NamespaceContext;
006: import java.util.ArrayList;
007: import java.util.Iterator;
008: import java.util.List;
009:
010: /**
011: * This class bridges between the JAXP 1.3 NamespaceContext interface and Saxon's
012: * equivalent NamespaceResolver interface. It allows any implementation of the Saxon
013: * NamespaceResolver to be wrapped as a JAXP NamespaceContext.
014: */
015:
016: public class NamespaceContextImpl implements NamespaceContext,
017: NamespaceResolver {
018:
019: NamespaceResolver resolver;
020:
021: /**
022: * Constructor: wrap a Saxon NamespaceResolver as a JAXP NamespaceContext
023: * @param resolver the Saxon NamespaceResolver
024: */
025:
026: public NamespaceContextImpl(NamespaceResolver resolver) {
027: this .resolver = resolver;
028: }
029:
030: /**
031: * Get the namespace URI corresponding to a given prefix. Return null
032: * if the prefix is not in scope.
033: * @param prefix the namespace prefix
034: * @param useDefault true if the default namespace is to be used when the
035: * prefix is ""
036: * @return the uri for the namespace, or null if the prefix is not in scope
037: */
038:
039: public String getURIForPrefix(String prefix, boolean useDefault) {
040: return resolver.getURIForPrefix(prefix, useDefault);
041: }
042:
043: /**
044: * Get an iterator over all the prefixes declared in this namespace context. This will include
045: * the default namespace (prefix="") and the XML namespace where appropriate
046: */
047:
048: public Iterator iteratePrefixes() {
049: return resolver.iteratePrefixes();
050: }
051:
052: /**
053: * Implement the JAXP getNamespaceURI() method in terms of the Saxon-specific methods
054: * @param prefix a namespace prefix
055: * @return the corresponding URI, if the prefix is bound, or "" otherwise
056: */
057:
058: public String getNamespaceURI(String prefix) {
059: if (prefix.equals("xmlns")) {
060: return "http://www.w3.org/2000/xmlns/";
061: }
062: return resolver.getURIForPrefix(prefix, true);
063: }
064:
065: /**
066: * Get the prefix bound to a particular namespace URI, if there is one, or null if not (JAXP method)
067: * @param uri the namespace URI
068: * @return the prefix bound to the URI if there is one, or null if not
069: */
070:
071: public String getPrefix(String uri) {
072: Iterator prefixes = iteratePrefixes();
073: while (prefixes.hasNext()) {
074: String p = (String) prefixes.next();
075: String u = resolver.getURIForPrefix(p, true);
076: if (u.equals(uri)) {
077: return p;
078: }
079: }
080: return null;
081: }
082:
083: /**
084: * Get all the prefixes mapped to a given namespace URI (JAXP method)
085: * @param uri the namespace URI
086: * @return an iterator over all the prefixes bound to this namespace URI
087: */
088: public Iterator getPrefixes(String uri) {
089: List list = new ArrayList(4);
090: Iterator prefixes = iteratePrefixes();
091: while (prefixes.hasNext()) {
092: String p = (String) prefixes.next();
093: String u = resolver.getURIForPrefix(p, true);
094: if (u.equals(uri)) {
095: list.add(p);
096: }
097: }
098: return list.iterator();
099: }
100: }
101:
102: //
103: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
104: // you may not use this file except in compliance with the License. You may obtain a copy of the
105: // License at http://www.mozilla.org/MPL/
106: //
107: // Software distributed under the License is distributed on an "AS IS" basis,
108: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
109: // See the License for the specific language governing rights and limitations under the License.
110: //
111: // The Original Code is: all this file.
112: //
113: // The Initial Developer of the Original Code is Michael H. Kay
114: //
115: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
116: //
117: // Contributor(s): none
118: //
|