KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 1998-2007 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 NamespaceWriterContext extends NamespaceContextImpl
47 {
48   // map from URIs -> NamespaceBinding
49
private final HashMap JavaDoc<String JavaDoc,NamespaceBinding> _bindings
50     = new HashMap JavaDoc<String JavaDoc,NamespaceBinding>();
51
52   private int _uniqueId = 0;
53   private NamespaceBinding _nullBinding = new NamespaceBinding(null, null, 0);
54
55   private boolean _repair = false;
56
57   public NamespaceWriterContext()
58   {
59     this(false);
60   }
61
62   public NamespaceWriterContext(boolean repair)
63   {
64     super();
65
66     _repair = repair;
67   }
68
69   public void declare(String JavaDoc prefix, String JavaDoc uri)
70   {
71     declare(prefix, uri, false);
72   }
73
74   /**
75    * declares a new namespace prefix in the current context
76    */

77   public void declare(String JavaDoc prefix, String JavaDoc uri, boolean emit)
78   {
79     NamespaceBinding binding;
80
81     if (uri == null)
82       binding = _nullBinding;
83     else {
84       binding = _bindings.get(uri);
85
86       if (binding != null &&
87           binding.getPrefix() != null &&
88           binding.getPrefix().equals(prefix)) {
89         // for writing, ignore matching prefixes
90
binding.setEmit(emit);
91         return;
92       }
93       else if (binding == null) {
94         // set the URI to null so that addOldBinding registers that there
95
// was no previous binding
96
binding = new NamespaceBinding(prefix, null, _version);
97
98         _bindings.put(uri, binding);
99       }
100     }
101
102     ElementBinding eltBinding = _stack.get(_stack.size() - 1);
103
104     if (eltBinding == null) {
105       eltBinding = new ElementBinding();
106       
107       _stack.set(_stack.size() - 1, eltBinding);
108     }
109
110     eltBinding.addOldBinding(binding, prefix, binding.getUri(), uri);
111
112     _version++;
113     binding.setPrefix(prefix);
114     binding.setUri(uri);
115     binding.setVersion(_version);
116     binding.setEmit(emit);
117   }
118
119   /**
120    * declares a new namespace prefix in the current context; the
121    * auto-allocated prefix is returned
122    */

123   public String JavaDoc declare(String JavaDoc uri)
124   {
125     NamespaceBinding binding = _bindings.get(uri);
126
127     // without an explicit prefix, don't add a new prefix
128
if (binding != null)
129       return binding.getPrefix();
130
131     String JavaDoc prefix = "ns" + _uniqueId++;
132
133     declare(prefix, uri, _repair);
134     
135     return prefix;
136   }
137
138   /**
139    * looks up the uri, returns the prefix it corresponds to
140    */

141   public String JavaDoc getPrefix(String JavaDoc uri)
142   {
143     NamespaceBinding binding = _bindings.get(uri);
144
145     if (binding == null)
146       return null;
147     
148     return binding.getPrefix();
149   }
150
151   public void emitDeclarations(WriteStream ws)
152     throws IOException JavaDoc
153   {
154     for (NamespaceBinding binding : _bindings.values())
155       binding.emit(ws);
156   }
157 }
158
Popular Tags