001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.databinding.utils.reader;
020:
021: import org.apache.axis2.util.ArrayStack;
022:
023: import javax.xml.namespace.NamespaceContext;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026:
027: public class ADBNamespaceContext implements NamespaceContext {
028:
029: private NamespaceContext parentNsContext;
030:
031: public NamespaceContext getParentNsContext() {
032: return parentNsContext;
033: }
034:
035: public void setParentNsContext(NamespaceContext parentNsContext) {
036: this .parentNsContext = parentNsContext;
037: }
038:
039: //Keep two arraylists for the prefixes and namespaces. They should be in sync
040: //since the index of the entry will be used to relate them
041: //use the minimum initial capacity to let things handle memory better
042:
043: private ArrayStack prefixStack = new ArrayStack();
044: private ArrayStack uriStack = new ArrayStack();
045:
046: /**
047: * Register a namespace in this context
048: *
049: * @param prefix
050: * @param uri
051: */
052: public void pushNamespace(String prefix, String uri) {
053: prefixStack.push(prefix);
054: uriStack.push(uri);
055:
056: }
057:
058: /** Pop a namespace */
059: public void popNamespace() {
060: prefixStack.pop();
061: uriStack.pop();
062: }
063:
064: public String getNamespaceURI(String prefix) {
065: //do the corrections as per the javadoc
066: if (prefixStack.contains(prefix)) {
067: int index = prefixStack.indexOf(prefix);
068: return (String) uriStack.get(index);
069: }
070: if (parentNsContext != null) {
071: return parentNsContext.getPrefix(prefix);
072: }
073: return null;
074: }
075:
076: public String getPrefix(String uri) {
077: //do the corrections as per the javadoc
078: int index = uriStack.indexOf(uri);
079: if (index != -1) {
080: return (String) prefixStack.get(index);
081: }
082:
083: if (parentNsContext != null) {
084: return parentNsContext.getPrefix(uri);
085: }
086: return null;
087: }
088:
089: public Iterator getPrefixes(String uri) {
090: //create an arraylist that contains the relevant prefixes
091: String[] uris = (String[]) uriStack.toArray(new String[uriStack
092: .size()]);
093: ArrayList tempList = new ArrayList();
094: for (int i = 0; i < uris.length; i++) {
095: if (uris[i].equals(uri)) {
096: tempList.add(prefixStack.get(i));
097: //we assume that array conversion preserves the order
098: }
099: }
100: //by now all the relevant prefixes are collected
101: //make a new iterator and provide a wrapper iterator to
102: //obey the contract on the API
103: return new WrappingIterator(tempList.iterator());
104: }
105:
106: private class WrappingIterator implements Iterator {
107:
108: private Iterator containedIterator = null;
109:
110: public WrappingIterator(Iterator containedIterator) {
111: this .containedIterator = containedIterator;
112: }
113:
114: public Iterator getContainedIterator() {
115: return containedIterator;
116: }
117:
118: public void setContainedIterator(Iterator containedIterator) {
119: this .containedIterator = containedIterator;
120: }
121:
122: /**
123: * As per the contract on the API of Namespace context the returned iterator should be
124: * immutable
125: */
126: public void remove() {
127: throw new UnsupportedOperationException();
128: }
129:
130: public boolean hasNext() {
131: return containedIterator.hasNext();
132: }
133:
134: public Object next() {
135: return containedIterator.next();
136: }
137: }
138: }
|