KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > util > NamespaceContextStack


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * NSSupportFilter.java
25  *
26  * Created on 12 novembre 2001, 21:53
27  */

28
29 package org.xquark.util;
30
31 import java.util.*;
32
33 /**
34  * Helper for XML prefix declaration management
35  */

36 public class NamespaceContextStack implements NamespaceContext
37 {
38     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40
41     /**
42      * Stack of NSContextInfo objects
43      */

44     private ArrayList contexts = new ArrayList();
45     /**
46      *
47      */

48     private NSContextInfo current = null;
49     /**
50      * Feature the XML depth (in fact the number of calls to pushContext()
51      */

52     private int level = 0;
53     /**
54      * The stack size (the number of times a real push() was performed after
55      * a pushContext() was called assumed one is performed at the biginning
56      * for xml).
57      */

58     private int count = 0;
59
60     public NamespaceContextStack()
61     {
62         reset();
63     }
64
65     public void reset()
66     {
67         level = 0;
68         count = 0;
69         current = push();
70         current.declarePrefix("xml", "http://www.w3.org/XML/1998/namespace");
71     }
72
73     public void declarePrefix(String JavaDoc prefix, String JavaDoc uri)
74     {
75         if (current.getLevel() < level)
76             current = push();
77         current.declarePrefix(prefix, uri);
78     }
79
80     public void pushContext()
81     {
82         level++;
83     }
84
85     public void popContext()
86     {
87         level--;
88         if (current.getLevel() > level)
89             current = pop();
90     }
91
92     ///////////////////////////////////////////////////////////////////////////
93
// PRIVATE
94
///////////////////////////////////////////////////////////////////////////
95
private NSContextInfo push()
96     {
97         NSContextInfo context = null;
98         if (count == contexts.size())
99         {
100             // allocate new context
101
context = new NSContextInfo();
102             contexts.add(context);
103         }
104         else
105         {
106             // reuse a previously allocated context
107
context = (NSContextInfo) contexts.get(count);
108         }
109         count++;
110         context.initialize(level, current);
111         return context;
112     }
113
114     private NSContextInfo pop()
115     {
116         // hack to avoid popping the last context
117
if (count > 1)
118             count--;
119         else
120             reset();
121         return (NSContextInfo) contexts.get(count - 1);
122     }
123
124     public NamespaceContext getNamespaceContext()
125     {
126         return current.getContext(true);
127     }
128
129     public String JavaDoc getNamespaceURI(String JavaDoc prefix)
130     {
131         return current.getContext().getNamespaceURI(prefix);
132     }
133     
134     public Collection getNamespaceURIs() {
135         return current.getContext().getNamespaceURIs();
136     }
137
138     public String JavaDoc getPrefix(String JavaDoc uri)
139     {
140         return current.getContext().getPrefix(uri);
141     }
142
143     public List getDeclaredPrefixes()
144     {
145         return current.getDeclaredPrefixes(level);
146     }
147
148     public Collection getPrefixes()
149     {
150         return current.getContext().getPrefixes();
151     }
152
153     public Collection getPrefixes(String JavaDoc uri)
154     {
155         return current.getContext().getPrefixes(uri);
156     }
157
158     ///////////////////////////////////////////////////////////////////////////
159
// PRIVATE
160
///////////////////////////////////////////////////////////////////////////
161
static class NSContextInfo
162     {
163         private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
164         private static final String JavaDoc RCSName = "$Name: $";
165         static private final List EMPTY_LIST = new ArrayList(1);
166
167         private NamespaceContextImpl context = new NamespaceContextImpl();
168         private int level = -1;
169         private boolean locked = false;
170
171         void initialize(int level, NSContextInfo parent)
172         {
173             this.level = level;
174             if (parent != null)
175             {
176                 if (locked)
177                     context = new NamespaceContextImpl();
178                 context.initialize(parent.getContext());
179             }
180             locked = false;
181         }
182
183         int getLevel()
184         {
185             return level;
186         }
187
188         NamespaceContextImpl getContext()
189         {
190             return getContext(false);
191         }
192
193         NamespaceContextImpl getContext(boolean lock)
194         {
195             if (!locked)
196                 locked = lock;
197             return context;
198         }
199
200         void declarePrefix(String JavaDoc prefix, String JavaDoc uri)
201         {
202             context.declarePrefix(prefix, uri);
203         }
204
205         public List getDeclaredPrefixes(int currentLevel)
206         {
207             if (currentLevel == level)
208                 return context.getDeclaredPrefixes();
209             else
210                 return EMPTY_LIST;
211         }
212     }
213
214     static class NamespaceContextImpl implements NamespaceContext
215     {
216         private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
217         private static final String JavaDoc RCSName = "$Name: $";
218         private HashMap prefixMap = new HashMap();
219         private HashMap namespaceMap = new HashMap();
220         private ArrayList declarations = new ArrayList();
221
222         void initialize(NamespaceContextImpl parent)
223         {
224             prefixMap.clear();
225             namespaceMap.clear();
226             declarations.clear();
227             prefixMap.putAll(parent.getPrefixMap());
228             namespaceMap.putAll(parent.getNamespaceMap());
229         }
230
231         HashMap getPrefixMap()
232         {
233             return prefixMap;
234         }
235
236         HashMap getNamespaceMap()
237         {
238             return namespaceMap;
239         }
240
241         void declarePrefix(String JavaDoc prefix, String JavaDoc uri)
242         {
243             prefixMap.put(prefix, uri);
244             namespaceMap.put(uri, prefix);
245             declarations.add(prefix);
246         }
247
248         public String JavaDoc getNamespaceURI(String JavaDoc prefix)
249         {
250             return (String JavaDoc) prefixMap.get(prefix);
251         }
252
253         public String JavaDoc getPrefix(String JavaDoc uri)
254         {
255             return (String JavaDoc) namespaceMap.get(uri);
256         }
257
258         public Collection getPrefixes()
259         {
260             return Collections.unmodifiableCollection(prefixMap.keySet());
261         }
262
263         public Collection getNamespaceURIs() {
264             return Collections.unmodifiableCollection(namespaceMap.keySet());
265         }
266
267         public List getDeclaredPrefixes() {
268             return Collections.unmodifiableList(declarations);
269         }
270         
271         public Collection getPrefixes(String JavaDoc uri)
272         {
273             ArrayList result = new ArrayList();
274             Iterator it = prefixMap.entrySet().iterator();
275             while (it.hasNext())
276             {
277                 Map.Entry entry = (Map.Entry) it.next();
278                 if (uri.equals(entry.getValue()))
279                     result.add(entry.getKey());
280             }
281             return result;
282         }
283     }
284
285 }
286
Popular Tags