1
2
3
4
5
6
7
8
9
10
11
12
13
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 }