001: package net.sf.saxon.om;
002:
003: import net.sf.saxon.type.Type;
004:
005: import java.util.ArrayList;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: /**
010: * A NamespaceResolver that resolves namespace prefixes by reference to a node in a document for which
011: * those namespaces are in-scope.
012: */
013: public class InscopeNamespaceResolver implements NamespaceResolver {
014:
015: private NodeInfo node;
016:
017: public InscopeNamespaceResolver(NodeInfo node) {
018: if (node.getNodeKind() == Type.ELEMENT) {
019: this .node = node;
020: } else {
021: this .node = node.getParent();
022: }
023: }
024:
025: /**
026: * Get the namespace URI corresponding to a given prefix. Return null
027: * if the prefix is not in scope.
028: *
029: * @param prefix the namespace prefix
030: * @param useDefault true if the default namespace is to be used when the
031: * prefix is ""
032: * @return the uri for the namespace, or null if the prefix is not in scope
033: * Return "" for the no-namespace.
034: */
035:
036: public String getURIForPrefix(String prefix, boolean useDefault) {
037: if ("".equals(prefix) && !useDefault) {
038: return "";
039: }
040: AxisIterator iter = node.iterateAxis(Axis.NAMESPACE);
041: while (true) {
042: NodeInfo node = (NodeInfo) iter.next();
043: if (node == null) {
044: break;
045: }
046: if (node.getLocalPart().equals(prefix)) {
047: return node.getStringValue();
048: }
049: }
050: if ("".equals(prefix)) {
051: return "";
052: } else {
053: return null;
054: }
055: }
056:
057: /**
058: * Get an iterator over all the prefixes declared in this namespace context. This will include
059: * the default namespace (prefix="") and the XML namespace where appropriate
060: */
061:
062: public Iterator iteratePrefixes() {
063: List list = new ArrayList(16);
064: AxisIterator iter = node.iterateAxis(Axis.NAMESPACE);
065: while (true) {
066: NodeInfo node = (NodeInfo) iter.next();
067: if (node == null) {
068: break;
069: }
070: list.add(node.getLocalPart());
071: }
072: return list.iterator();
073: }
074:
075: /**
076: * Get the node on which this namespace resolver is based
077: */
078:
079: public NodeInfo getNode() {
080: return node;
081: }
082: }
083:
084: //
085: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
086: // you may not use this file except in compliance with the License. You may obtain a copy of the
087: // License at http://www.mozilla.org/MPL/
088: //
089: // Software distributed under the License is distributed on an "AS IS" basis,
090: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
091: // See the License for the specific language governing rights and limitations under the License.
092: //
093: // The Original Code is: all this file.
094: //
095: // The Initial Developer of the Original Code is Michael H. Kay
096: //
097: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
098: //
099: // Contributor(s): none
100: //
|