KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > stream > NamespaceContextImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Adam Megacz
28  */

29
30 package com.caucho.xml.stream;
31
32 import com.caucho.vfs.WriteStream;
33
34 import javax.xml.XMLConstants JavaDoc;
35 import javax.xml.namespace.NamespaceContext JavaDoc;
36 import javax.xml.namespace.QName JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.Map JavaDoc;
42
43 /**
44  * Maintains a stack of namespace contexts
45  */

46 public abstract class NamespaceContextImpl implements NamespaceContext JavaDoc
47 {
48   // The stack of element bindings
49
protected final ArrayList JavaDoc<ElementBinding> _stack
50     = new ArrayList JavaDoc<ElementBinding>();
51
52   protected int _version = 0;
53
54   NamespaceContextImpl()
55   {
56     _stack.add(null);
57   }
58
59   /**
60    * Creates a new subcontext and enters it
61    */

62   public void push()
63   {
64     _stack.add(null);
65   }
66
67   /**
68    * deletes the current context and enters its parent
69    */

70   public void pop()
71   {
72     ElementBinding eltBinding = _stack.remove(_stack.size() - 1);
73
74     if (eltBinding != null) {
75       ArrayList JavaDoc<Decl> oldBinding = eltBinding.getOldBindingList();
76
77       for (int i = 0; oldBinding != null && i < oldBinding.size(); i++) {
78         Decl decl = oldBinding.get(i);
79         NamespaceBinding binding = decl.getBinding();
80
81         _version++;
82
83         binding.setUri(decl.getOldUri());
84         binding.setVersion(_version);
85       }
86
87       eltBinding.clear();
88     }
89   }
90   
91   public void setElementName(QName JavaDoc name)
92   {
93     ElementBinding eltBinding = _stack.get(_stack.size() - 1);
94
95     if (eltBinding == null) {
96       eltBinding = new ElementBinding();
97       
98       _stack.set(_stack.size() - 1, eltBinding);
99     }
100
101     eltBinding.setName(name);
102   }
103
104   public QName JavaDoc getElementName()
105   {
106     ElementBinding eltBinding = _stack.get(_stack.size() - 1);
107
108     if (eltBinding != null)
109       return eltBinding.getName();
110     else
111       return null;
112   }
113
114   /**
115    * declares a new namespace prefix in the current context
116    */

117   public abstract void declare(String JavaDoc prefix, String JavaDoc uri);
118
119   public String JavaDoc getNamespaceURI(String JavaDoc prefix)
120   {
121     throw new UnsupportedOperationException JavaDoc();
122   }
123
124   public String JavaDoc getPrefix(String JavaDoc uri)
125   {
126     throw new UnsupportedOperationException JavaDoc();
127   }
128
129   public Iterator JavaDoc getPrefixes(String JavaDoc uri)
130   {
131     throw new UnsupportedOperationException JavaDoc();
132   }
133
134   static class ElementBinding
135   {
136     private QName JavaDoc _name;
137     private ArrayList JavaDoc<Decl> _declList;
138
139     public void setName(QName JavaDoc name)
140     {
141       _name = name;
142     }
143
144     public void clear()
145     {
146       _declList = null;
147     }
148
149     public QName JavaDoc getName()
150     {
151       return _name;
152     }
153
154     public void addOldBinding(NamespaceBinding binding, String JavaDoc prefix,
155                               String JavaDoc oldUri, String JavaDoc newUri)
156     {
157       if (_declList == null)
158         _declList = new ArrayList JavaDoc<Decl>();
159
160       _declList.add(new Decl(binding, prefix, oldUri, newUri));
161     }
162
163     public ArrayList JavaDoc<Decl> getOldBindingList()
164     {
165       return _declList;
166     }
167   }
168
169   static class Decl {
170     private final NamespaceBinding _binding;
171     private final String JavaDoc _prefix;
172     private final String JavaDoc _oldUri;
173     private final String JavaDoc _newUri;
174
175     Decl(NamespaceBinding binding, String JavaDoc prefix,
176          String JavaDoc oldUri, String JavaDoc newUri)
177     {
178       _binding = binding;
179       _prefix = prefix;
180       _oldUri = oldUri;
181       _newUri = newUri;
182     }
183
184     NamespaceBinding getBinding()
185     {
186       return _binding;
187     }
188
189     String JavaDoc getPrefix()
190     {
191       return _prefix;
192     }
193
194     String JavaDoc getOldUri()
195     {
196       return _oldUri;
197     }
198
199     String JavaDoc getNewUri()
200     {
201       return _newUri;
202     }
203   }
204
205   static class PrefixIterator
206   {
207     private ElementBinding _eltBinding;
208     private int _index = 0;
209
210     PrefixIterator(NamespaceContextImpl context, ElementBinding eltBinding)
211     {
212       _eltBinding = eltBinding;
213     }
214
215     public void remove()
216     {
217       throw new RuntimeException JavaDoc("not supported");
218     }
219
220     public boolean hasNext()
221     {
222       if (_eltBinding == null)
223         return false;
224
225       return _index < _eltBinding.getOldBindingList().size();
226     }
227
228     public Object JavaDoc next()
229     {
230       if (_eltBinding == null)
231         return null;
232
233       ArrayList JavaDoc<Decl> oldBindingList = _eltBinding.getOldBindingList();
234
235       if (_index < oldBindingList.size())
236         return oldBindingList.get(_index++).getPrefix();
237
238       return null;
239     }
240   }
241 }
242
Popular Tags