001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.util;
019:
020: import java.util.Enumeration;
021: import java.util.List;
022:
023: import javax.xml.XMLConstants;
024:
025: import org.apache.xerces.xni.NamespaceContext;
026:
027: /**
028: * <p>A read-only XNI wrapper around a JAXP NamespaceContext.</p>
029: *
030: * @author Michael Glavassevich, IBM
031: *
032: * @version $Id: JAXPNamespaceContextWrapper.java 447241 2006-09-18 05:12:57Z mrglavas $
033: */
034: public final class JAXPNamespaceContextWrapper implements
035: NamespaceContext {
036:
037: private javax.xml.namespace.NamespaceContext fNamespaceContext;
038: private SymbolTable fSymbolTable;
039: private List fPrefixes;
040:
041: public JAXPNamespaceContextWrapper(SymbolTable symbolTable) {
042: setSymbolTable(symbolTable);
043: }
044:
045: public void setNamespaceContext(
046: javax.xml.namespace.NamespaceContext context) {
047: fNamespaceContext = context;
048: }
049:
050: public javax.xml.namespace.NamespaceContext getNamespaceContext() {
051: return fNamespaceContext;
052: }
053:
054: public void setSymbolTable(SymbolTable symbolTable) {
055: fSymbolTable = symbolTable;
056: }
057:
058: public SymbolTable getSymbolTable() {
059: return fSymbolTable;
060: }
061:
062: public void setDeclaredPrefixes(List prefixes) {
063: fPrefixes = prefixes;
064: }
065:
066: public List getDeclaredPrefixes() {
067: return fPrefixes;
068: }
069:
070: /*
071: * NamespaceContext methods
072: */
073:
074: public String getURI(String prefix) {
075: if (fNamespaceContext != null) {
076: String uri = fNamespaceContext.getNamespaceURI(prefix);
077: if (uri != null && !XMLConstants.NULL_NS_URI.equals(uri)) {
078: return (fSymbolTable != null) ? fSymbolTable
079: .addSymbol(uri) : uri.intern();
080: }
081: }
082: return null;
083: }
084:
085: public String getPrefix(String uri) {
086: if (fNamespaceContext != null) {
087: if (uri == null) {
088: uri = XMLConstants.NULL_NS_URI;
089: }
090: String prefix = fNamespaceContext.getPrefix(uri);
091: if (prefix == null) {
092: prefix = XMLConstants.DEFAULT_NS_PREFIX;
093: }
094: return (fSymbolTable != null) ? fSymbolTable
095: .addSymbol(prefix) : prefix.intern();
096: }
097: return null;
098: }
099:
100: public Enumeration getAllPrefixes() {
101: // It's not possible to get the list of all prefixes from the NamespaceContext
102: // so the best we can do is return an empty enumeration.
103: return new Enumeration() {
104: public boolean hasMoreElements() {
105: return false;
106: }
107:
108: public Object nextElement() {
109: return null;
110: }
111: };
112: }
113:
114: public void pushContext() {
115: }
116:
117: public void popContext() {
118: }
119:
120: public boolean declarePrefix(String prefix, String uri) {
121: return true;
122: }
123:
124: public int getDeclaredPrefixCount() {
125: return (fPrefixes != null) ? fPrefixes.size() : 0;
126: }
127:
128: public String getDeclaredPrefixAt(int index) {
129: return (String) fPrefixes.get(index);
130: }
131:
132: public void reset() {
133: }
134:
135: } // JAXPNamespaceContextWrapper
|