1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.exolab.castor.xml;
15
16 import java.util.ArrayList;
17 import java.util.Enumeration;
18 import java.util.List;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.xml.sax.helpers.AttributeListImpl;
23
24
25
26
27
28
29
30
31
32 @SuppressWarnings("deprecation")
33 public class NamespacesStack {
34
35 private final Log logger = LogFactory.getLog(this.getClass());
36
37
38
39
40 private static final String DEFAULT_NAMESPACE = "";
41
42
43
44
45 private List<Namespaces> namespaceStack = new ArrayList<Namespaces>();
46
47
48
49
50
51
52
53 public void addNamespace(String namespacePrefix, String namespaceUri) {
54 Namespaces namespaces = getCurrentNamespaceScope();
55 if (namespaces != null) {
56 namespaces.addNamespace(namespacePrefix, namespaceUri);
57 }
58 }
59
60
61
62
63
64
65 public void addDefaultNamespace(String namespaceUri) {
66 addNamespace(DEFAULT_NAMESPACE, namespaceUri);
67 }
68
69
70
71
72
73
74 public void removeNamespace(String namespacePrefix) {
75 getCurrentNamespaceScope().removeNamespace(namespacePrefix);
76 }
77
78
79
80
81
82
83
84
85
86 public String getNamespaceURI(String namespacePrefix) {
87 String namespaceUri = null;
88 for (int ind = namespaceStack.size() - 1; ind >= 0 && namespaceUri == null; ind--) {
89 namespaceUri = namespaceStack.get(ind).getNamespaceURI(namespacePrefix);
90 }
91 return namespaceUri;
92 }
93
94
95
96
97
98
99
100
101 @SuppressWarnings("deprecation")
102 public void declareAsAttributes(AttributeListImpl attributeList, boolean localOnly) {
103 getCurrentNamespaceScope().declareAsAttributes(attributeList);
104 if (!localOnly) {
105 for (int ind = namespaceStack.size() - 1; ind >= 0; ind--) {
106
107 namespaceStack.get(ind).declareAsAttributes(attributeList);
108 }
109 }
110 }
111
112
113
114
115
116
117 public String getDefaultNamespaceURI() {
118 return getNamespaceURI(DEFAULT_NAMESPACE);
119 }
120
121
122
123
124
125
126
127
128 public String getNamespacePrefix(String namespaceUri) {
129 String namespacePrefix = null;
130 for (int ind = namespaceStack.size() - 1; ind >= 0 && namespacePrefix == null; ind--) {
131 namespacePrefix = namespaceStack.get(ind).getNamespacePrefix(namespaceUri);
132 }
133 return namespacePrefix;
134 }
135
136
137
138
139
140
141
142
143 public String getNonDefaultNamespacePrefix(String namespaceUri) {
144 String namespacePrefix = null;
145 for (int ind = namespaceStack.size() - 1; ind >= 0 && namespacePrefix == null; ind--) {
146 namespacePrefix = namespaceStack.get(ind).getNonDefaultNamespacePrefix(namespaceUri);
147 }
148 return namespacePrefix;
149 }
150
151
152
153
154
155
156 public Enumeration<String> getLocalNamespacePrefixes() {
157 Namespaces namespaces = getCurrentNamespaceScope();
158 return namespaces != null ? namespaces.getLocalNamespacePrefixes() : null;
159 }
160
161
162
163
164 public void addNewNamespaceScope() {
165 namespaceStack.add(new Namespaces());
166 }
167
168
169
170
171 public void removeNamespaceScope() {
172
173 if (!namespaceStack.isEmpty()) {
174 namespaceStack.remove(namespaceStack.size() - 1);
175 } else {
176 this.logger.error("Trying to remove a namespaces scope from an empty stack of Namespaces");
177 }
178 }
179
180
181
182
183
184
185 public Namespaces getCurrentNamespaceScope() {
186 if (namespaceStack.isEmpty()) {
187 addNewNamespaceScope();
188 }
189 return namespaceStack.get(namespaceStack.size() - 1);
190 }
191 }