summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/reader/DelegatingNamespaceContext.java
blob: 8f136df7dfa299df8d8608ce311bdd62e3fbcd20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.sca.common.xml.stax.reader;

import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.Iterator;
import java.util.List;

import javax.xml.namespace.NamespaceContext;

public class DelegatingNamespaceContext implements NamespaceContext {
    private static int count;

    private class WrappingIterator implements Iterator {

        private Iterator containedIterator;

        public WrappingIterator(Iterator containedIterator) {
            this.containedIterator = containedIterator;
        }

        public Iterator getContainedIterator() {
            return containedIterator;
        }

        public boolean hasNext() {
            return containedIterator.hasNext();
        }

        public Object next() {
            return containedIterator.next();
        }

        /**
         * As per the contract on the API of Namespace context the returned iterator should be immutable
         */
        public void remove() {
            throw new UnsupportedOperationException();
        }

        public void setContainedIterator(Iterator containedIterator) {
            this.containedIterator = containedIterator;
        }
    }

    private NamespaceContext parentNsContext;

    private FastStack<String> prefixStack = new FastStack<String>();

    // Keep two ArrayLists for the prefixes and namespaces. They should be in
    // sync
    // since the index of the entry will be used to relate them
    // use the minimum initial capacity to let things handle memory better

    private FastStack<String> uriStack = new FastStack<String>();

    /**
     * Generates a unique namespace prefix that is not in the scope of the NamespaceContext
     * 
     * @return string
     */
    public String generateUniquePrefix() {
        String prefix = "p" + count++;
        // null should be returned if the prefix is not bound!
        while (getNamespaceURI(prefix) != null) {
            prefix = "p" + count++;
        }

        return prefix;
    }

    public String getNamespaceURI(String prefix) {
        // do the corrections as per the Javadoc
        int index = prefixStack.search(prefix);
        if (index != -1) {
            return uriStack.get(index);
        }
        if (parentNsContext != null) {
            return parentNsContext.getPrefix(prefix);
        }
        return null;
    }

    public NamespaceContext getParentNsContext() {
        return parentNsContext;
    }

    public String getPrefix(String uri) {
        // do the corrections as per the Javadoc
        int index = uriStack.search(uri);
        if (index != -1) {
            return prefixStack.get(index);
        }

        if (parentNsContext != null) {
            return parentNsContext.getPrefix(uri);
        }
        return null;
    }

    public Iterator getPrefixes(String uri) {
        // create an ArrayList that contains the relevant prefixes
        String[] uris = uriStack.toArray(new String[uriStack.size()]);
        List<String> tempList = new ArrayList<String>();
        for (int i = uris.length - 1; i >= 0; i--) {
            if (uris[i].equals(uri)) {
                tempList.add(prefixStack.get(i));
                // we assume that array conversion preserves the order
            }
        }
        // by now all the relevant prefixes are collected
        // make a new iterator and provide a wrapper iterator to
        // obey the contract on the API
        return new WrappingIterator(tempList.iterator());
    }

    /**
     * Pop a namespace
     */
    public void popNamespace() {
        prefixStack.pop();
        uriStack.pop();
    }

    /**
     * Register a namespace in this context
     * 
     * @param prefix
     * @param uri
     */
    public void pushNamespace(String prefix, String uri) {
        prefixStack.push(prefix);
        uriStack.push(uri);

    }

    public void setParentNsContext(NamespaceContext parentNsContext) {
        this.parentNsContext = parentNsContext;
    }

    /**
     * An implementation of the {@link java.util.Stack} API that is based on an <code>ArrayList</code> instead of a
     * <code>Vector</code>, so it is not synchronized to protect against multi-threaded access. The implementation is
     * therefore operates faster in environments where you do not need to worry about multiple thread contention.
     * <p>
     * The removal order of an <code>ArrayStack</code> is based on insertion order: The most recently added element is
     * removed first. The iteration order is <i>not</i> the same as the removal order. The iterator returns elements
     * from the bottom up, whereas the {@link #remove()} method removes them from the top down.
     * <p>
     * Unlike <code>Stack</code>, <code>ArrayStack</code> accepts null entries.
     */
    public static class FastStack<T> extends ArrayList<T> {

        /** Ensure Serialization compatibility */
        private static final long serialVersionUID = 2130079159931574599L;

        /**
         * Constructs a new empty <code>ArrayStack</code>. The initial size is controlled by <code>ArrayList</code>
         * and is currently 10.
         */
        public FastStack() {
            super();
        }

        /**
         * Constructs a new empty <code>ArrayStack</code> with an initial size.
         * 
         * @param initialSize the initial size to use
         * @throws IllegalArgumentException if the specified initial size is negative
         */
        public FastStack(int initialSize) {
            super(initialSize);
        }

        /**
         * Return <code>true</code> if this stack is currently empty.
         * <p>
         * This method exists for compatibility with <code>java.util.Stack</code>. New users of this class should use
         * <code>isEmpty</code> instead.
         * 
         * @return true if the stack is currently empty
         */
        public boolean empty() {
            return isEmpty();
        }

        /**
         * Returns the top item off of this stack without removing it.
         * 
         * @return the top item on the stack
         * @throws EmptyStackException if the stack is empty
         */
        public T peek() throws EmptyStackException {
            int n = size();
            if (n <= 0) {
                throw new EmptyStackException();
            } else {
                return get(n - 1);
            }
        }

        /**
         * Returns the n'th item down (zero-relative) from the top of this stack without removing it.
         * 
         * @param n the number of items down to go
         * @return the n'th item on the stack, zero relative
         * @throws EmptyStackException if there are not enough items on the stack to satisfy this request
         */
        public T peek(int n) throws EmptyStackException {
            int m = (size() - n) - 1;
            if (m < 0) {
                throw new EmptyStackException();
            } else {
                return get(m);
            }
        }

        /**
         * Pops the top item off of this stack and return it.
         * 
         * @return the top item on the stack
         * @throws EmptyStackException if the stack is empty
         */
        public T pop() throws EmptyStackException {
            int n = size();
            if (n <= 0) {
                throw new EmptyStackException();
            } else {
                return remove(n - 1);
            }
        }

        /**
         * Pushes a new item onto the top of this stack. The pushed item is also returned. This is equivalent to calling
         * <code>add</code>.
         * 
         * @param item the item to be added
         * @return the item just pushed
         */
        public Object push(T item) {
            add(item);
            return item;
        }

        /**
         * Returns the top-most index for the object in the stack
         * 
         * @param object the object to be searched for
         * @return top-most index, or -1 if not found
         */
        public int search(T object) {
            int i = size() - 1; // Current index
            while (i >= 0) {
                T current = get(i);
                if ((object == null && current == null) || (object != null && object.equals(current))) {
                    return i;
                }
                i--;
            }
            return -1;
        }

        /**
         * Returns the element on the top of the stack.
         * 
         * @return the element on the top of the stack
         * @throws EmptyStackException if the stack is empty
         */
        public T get() {
            int size = size();
            if (size == 0) {
                throw new EmptyStackException();
            }
            return get(size - 1);
        }

        /**
         * Removes the element on the top of the stack.
         * 
         * @return the removed element
         * @throws EmptyStackException if the stack is empty
         */
        public T remove() {
            int size = size();
            if (size == 0) {
                throw new EmptyStackException();
            }
            return remove(size - 1);
        }

    }

}