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: package org.apache.commons.jxpath.ri;
017:
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: import org.apache.commons.jxpath.Pointer;
023: import org.apache.commons.jxpath.ri.model.NodeIterator;
024: import org.apache.commons.jxpath.ri.model.NodePointer;
025:
026: /**
027: * The reference implementation of JXPathContext.
028: *
029: * @author Dmitri Plotnikov
030: * @version $Revision: 1.2 $ $Date: 2004/06/29 22:57:20 $
031: */
032: public class NamespaceResolver implements Cloneable {
033:
034: protected HashMap namespaceMap = new HashMap();
035: protected HashMap reverseMap;
036: protected NodePointer pointer;
037: private boolean sealed;
038:
039: /**
040: * Registers a namespace prefix.
041: *
042: * @param prefix A namespace prefix
043: * @param namespaceURI A URI for that prefix
044: */
045: public void registerNamespace(String prefix, String namespaceURI) {
046: namespaceMap.put(prefix, namespaceURI);
047: reverseMap = null;
048: }
049:
050: /**
051: * Register a namespace for the expression context.
052: */
053: public void setNamespaceContextPointer(NodePointer pointer) {
054: this .pointer = pointer;
055: }
056:
057: public Pointer getNamespaceContextPointer() {
058: return pointer;
059: }
060:
061: /**
062: * Given a prefix, returns a registered namespace URI. If the requested
063: * prefix was not defined explicitly using the registerNamespace method,
064: * JXPathContext will then check the context node to see if the prefix is
065: * defined there. See
066: * {@link #setNamespaceContextPointer(Pointer) setNamespaceContextPointer}.
067: *
068: * @param prefix The namespace prefix to look up
069: * @return namespace URI or null if the prefix is undefined.
070: */
071: public String getNamespaceURI(String prefix) {
072: String uri = (String) namespaceMap.get(prefix);
073: if (uri == null && pointer != null) {
074: uri = pointer.getNamespaceURI(prefix);
075: }
076: // System.err.println("For prefix " + prefix + " URI=" + uri);
077: return uri;
078: }
079:
080: public String getPrefix(String namespaceURI) {
081: if (reverseMap == null) {
082: reverseMap = new HashMap();
083: NodeIterator ni = pointer.namespaceIterator();
084: if (ni != null) {
085: for (int position = 1; ni.setPosition(position); position++) {
086: NodePointer nsPointer = ni.getNodePointer();
087: QName qname = nsPointer.getName();
088: reverseMap.put(qname.getPrefix(), qname.getName());
089: }
090: }
091: Iterator it = namespaceMap.entrySet().iterator();
092: while (it.hasNext()) {
093: Map.Entry entry = (Map.Entry) it.next();
094: reverseMap.put(entry.getValue(), entry.getKey());
095: }
096: }
097: String prefix = (String) reverseMap.get(namespaceURI);
098: return prefix;
099: }
100:
101: public boolean isSealed() {
102: return sealed;
103: }
104:
105: public void seal() {
106: sealed = true;
107: }
108:
109: public Object clone() {
110: try {
111: return super .clone();
112: } catch (CloneNotSupportedException e) {
113: // Of course, it's supported.
114: e.printStackTrace();
115: return null;
116: }
117: }
118: }
|