KickJava   Java API By Example, From Geeks To Geeks.

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


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 Scott Ferguson
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 class NamespaceReaderContext extends NamespaceContextImpl
47 {
48   // The current prefix -> namespace bindings
49
private final HashMap JavaDoc<String JavaDoc,NamespaceBinding> _bindings
50     = new HashMap JavaDoc<String JavaDoc,NamespaceBinding>();
51
52   private NamespaceBinding _nullEltBinding
53     = new NamespaceBinding(null, null, 0);
54   private NamespaceBinding _nullAttrBinding
55     = new NamespaceBinding(null, null, 0);
56
57   /**
58    * declares a new namespace prefix in the current context
59    */

60   public void declare(String JavaDoc prefix, String JavaDoc uri)
61   {
62     NamespaceBinding binding = getElementNamespace(prefix);
63
64     ElementBinding eltBinding = _stack.get(_stack.size() - 1);
65
66     if (eltBinding == null) {
67       eltBinding = new ElementBinding();
68       
69       _stack.set(_stack.size() - 1, eltBinding);
70     }
71
72     eltBinding.addOldBinding(binding, prefix, binding.getUri(), uri);
73
74     _version++;
75     binding.setUri(uri);
76     binding.setVersion(_version);
77   }
78
79   /**
80    * looks up the prefix, returns the uri it corresponds to
81    */

82   public String JavaDoc getUri(String JavaDoc prefix)
83   {
84     NamespaceBinding binding = _bindings.get(prefix);
85
86     if (binding != null)
87       return binding.getUri();
88     else
89       return null;
90   }
91
92   // --> javax.xml.namespace.NamespaceContext
93

94   public String JavaDoc getNamespaceURI(String JavaDoc prefix)
95   {
96     NamespaceBinding binding = _bindings.get(prefix);
97
98     if (binding != null)
99       return binding.getUri();
100
101     String JavaDoc uri = null;
102     
103     if (XMLConstants.XML_NS_PREFIX.equals(prefix))
104       uri = XMLConstants.XML_NS_URI;
105     else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
106       uri = XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
107
108     return uri;
109   }
110
111   // <-- javax.xml.namespace.NamespaceContext
112

113   // --> used by StaxIntern
114
public int getDepth()
115   {
116     return _stack.size() - 1;
117   }
118
119   public int getNumDecls()
120   {
121     ElementBinding eltBinding = _stack.get(_stack.size() - 1);
122
123     if (eltBinding != null) {
124       ArrayList JavaDoc<Decl> oldBindingList = eltBinding.getOldBindingList();
125
126       if (oldBindingList != null)
127        return oldBindingList.size();
128       else
129        return 0;
130     }
131     else
132       return 0;
133   }
134
135   public String JavaDoc getUri(int i)
136   {
137     ElementBinding eltBinding = _stack.get(_stack.size() - 1);
138
139     if (eltBinding != null) {
140       return eltBinding.getOldBindingList().get(i).getNewUri();
141     }
142     else
143       return null;
144   }
145
146   public String JavaDoc getPrefix(int i)
147   {
148     ElementBinding eltBinding = _stack.get(_stack.size() - 1);
149
150     if (eltBinding != null) {
151       return eltBinding.getOldBindingList().get(i).getPrefix();
152     }
153
154     return null;
155   }
156   // <-- used by StaxIntern
157

158   NamespaceBinding getElementNamespace(String JavaDoc prefix)
159   {
160     NamespaceBinding binding;
161
162     // default namespace
163
if (prefix == null)
164       binding = _nullEltBinding;
165     else
166       binding = _bindings.get(prefix);
167
168     if (binding != null)
169       return binding;
170     else {
171       binding = new NamespaceBinding(prefix, null, _version);
172
173       _bindings.put(prefix, binding);
174
175       return binding;
176     }
177   }
178
179   NamespaceBinding getAttributeNamespace(String JavaDoc prefix)
180   {
181     NamespaceBinding binding;
182
183     // default namespace
184
if (prefix == null)
185       binding = _nullAttrBinding;
186     else
187       binding = _bindings.get(prefix);
188
189     if (binding != null)
190       return binding;
191     else {
192       binding = new NamespaceBinding(prefix, null, _version);
193
194       _bindings.put(prefix, binding);
195
196       return binding;
197     }
198   }
199 }
200
Popular Tags