View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  
16  package com.bea.xml.stream.util;
17  
18  import javax.xml.namespace.NamespaceContext;
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.Set;
22  import java.util.Iterator;
23  
24  public class NamespaceContextImpl 
25    implements NamespaceContext
26  {
27    SymbolTable prefixTable = new SymbolTable();
28    SymbolTable uriTable = new SymbolTable();
29    NamespaceContext rootContext;
30    public NamespaceContextImpl() {
31      init();
32    }
33    public NamespaceContextImpl(NamespaceContext rootContext) {
34      this.rootContext = null;
35      init();
36    }
37    public void init() {
38      bindNamespace("xml","http://www.w3.org/XML/1998/namespace");
39      bindNamespace("xmlns","http://www.w3.org/XML/1998/namespace");
40    }
41    public void openScope() {
42      prefixTable.openScope();
43      uriTable.openScope();
44    }
45    public void closeScope() {
46      prefixTable.closeScope();
47      uriTable.closeScope();
48    }
49  
50    public void bindNamespace(String prefix, String uri) {
51      prefixTable.put(prefix,uri);
52      uriTable.put(uri,prefix);
53    }
54  
55    public int getDepth() {
56      return prefixTable.getDepth();
57    }
58  
59    public String getNamespaceURI(String prefix) {
60      String value = prefixTable.get(prefix);
61      if (value == null && rootContext != null)
62        return rootContext.getNamespaceURI(prefix);
63      else
64        return value;
65    }
66  
67    public String getPrefix(String uri) {
68      String value = uriTable.get(uri);
69      if (value == null && rootContext != null)
70        return rootContext.getPrefix(uri);
71      else
72        return value;
73    }
74  
75    public void bindDefaultNameSpace(String uri) {
76      bindNamespace("",uri);
77    }
78    public void unbindDefaultNameSpace() {
79      bindNamespace("",null);
80    }
81  
82    public void unbindNamespace(String prefix, String uri) {
83      prefixTable.put(prefix,null);
84      prefixTable.put(uri,null);
85    }
86  
87    public String getDefaultNameSpace() {
88      return getNamespaceURI("");
89    }
90  
91    public Iterator getPrefixes(String uri) {
92      return (uriTable.getAll(uri)).iterator();
93    }
94  
95    public static void main(String args[]) throws Exception {
96      NamespaceContextImpl nci = new NamespaceContextImpl();
97      nci.openScope();
98      nci.bindNamespace("a","uri");
99      nci.bindNamespace("b","uri");
100     System.out.println("a="+nci.getNamespaceURI("a"));
101     System.out.println("uri="+nci.getPrefix("uri"));
102     
103     Iterator vals = nci.getPrefixes("uri");
104     while(vals.hasNext())
105       System.out.println("1 uri->"+vals.next());
106 
107     nci.openScope();
108     nci.bindNamespace("a","uri2");
109     vals = nci.getPrefixes("uri");
110     while(vals.hasNext())
111       System.out.println("2 uri->"+vals.next());
112     nci.closeScope();
113     nci.closeScope();
114   }
115 }