libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / xml / transform / Bindings.java
blob009321315bbe0f816809d0fb0615cc29113c9016
1 /* Bindings.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.transform;
40 import gnu.java.lang.CPStringBuilder;
42 import java.util.Collection;
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.Iterator;
47 import java.util.LinkedList;
48 import java.util.Map;
49 import javax.xml.namespace.QName;
50 import javax.xml.xpath.XPathVariableResolver;
51 import org.w3c.dom.Node;
53 /**
54 * The set of variable bindings in effect for a stylesheet.
56 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
58 public class Bindings
59 implements XPathVariableResolver, Cloneable
62 static final int VARIABLE = 0;
63 static final int PARAM = 1;
64 static final int WITH_PARAM = 2;
66 final Stylesheet stylesheet;
68 /**
69 * Global variables.
71 final LinkedList variables;
73 /**
74 * Parameter value stack.
76 final LinkedList parameters;
78 /**
79 * Argument (with-param) value stack.
81 final LinkedList withParameters;
83 /**
84 * Only search globals.
86 boolean global;
88 Bindings(Stylesheet stylesheet)
90 this.stylesheet = stylesheet;
91 variables = new LinkedList();
92 parameters = new LinkedList();
93 withParameters = new LinkedList();
94 for (int i = 0; i < 3; i++)
96 push(i);
100 public Object clone()
104 return (Bindings) super.clone();
106 catch (CloneNotSupportedException e)
108 throw new Error(e.getMessage());
112 void push(int type)
114 switch (type)
116 case VARIABLE:
117 variables.addFirst(new HashMap());
118 break;
119 case PARAM:
120 parameters.addFirst(new HashMap());
121 break;
122 case WITH_PARAM:
123 withParameters.addFirst(new HashMap());
124 break;
128 void pop(int type)
130 switch (type)
132 case VARIABLE:
133 variables.removeFirst();
134 break;
135 case PARAM:
136 parameters.removeFirst();
137 break;
138 case WITH_PARAM:
139 withParameters.removeFirst();
140 break;
144 public boolean containsKey(QName name, int type)
146 if (global)
148 Map ctx1 = (Map) variables.getLast();
149 Map ctx2 = (Map) parameters.getLast();
150 return (ctx1.containsKey(name) || ctx2.containsKey(name));
152 Iterator i = null;
153 switch (type)
155 case VARIABLE:
156 i = variables.iterator();
157 break;
158 case PARAM:
159 i = parameters.iterator();
160 break;
161 case WITH_PARAM:
162 Map ctx = (Map) withParameters.getFirst();
163 return ctx.containsKey(name);
165 if (i != null)
167 while (i.hasNext())
169 Map ctx = (Map) i.next();
170 if (ctx.containsKey(name))
172 return true;
176 return false;
179 public Object get(QName name, Node context, int pos, int len)
181 if (global)
183 Map ctx = (Map) variables.getLast();
184 Object ret = ctx.get(name);
185 if (ret == null)
187 ctx = (Map) parameters.getLast();
188 ret = ctx.get(name);
190 return ret;
192 //System.err.println("bindings.get: "+name);
193 //System.err.println("\t"+toString());
194 Object ret = null;
195 //if (parameters.size() > 1 && containsKey(name, PARAM))
196 // check that template defines parameter
198 Map cwp = (Map) withParameters.getFirst();
199 ret = cwp.get(name);
200 //System.err.println("\twith-param: ret="+ret);
202 if (ret == null)
204 for (Iterator i = variables.iterator(); i.hasNext() && ret == null; )
206 Map vctx = (Map) i.next();
207 ret = vctx.get(name);
209 //System.err.println("\tvariable: ret="+ret);
211 if (ret == null)
213 for (Iterator i = parameters.iterator(); i.hasNext() && ret == null; )
215 Map pctx = (Map) i.next();
216 ret = pctx.get(name);
218 //System.err.println("\tparam: ret="+ret);
220 /*if (ret instanceof Expr && context != null)
222 Expr expr = (Expr) ret;
223 ret = expr.evaluate(context, 1, 1);
225 if (ret instanceof Node)
227 ret = Collections.singleton(ret);
229 if (ret == null)
231 ret = "";
233 //System.err.println("\tret="+ret);
234 return ret;
237 void set(QName name, Object value, int type)
239 switch (type)
241 case VARIABLE:
242 Map vctx = (Map) variables.getFirst();
243 vctx.put(name, value);
244 break;
245 case PARAM:
246 Map pctx = (Map) parameters.getFirst();
247 pctx.put(name, value);
248 break;
249 case WITH_PARAM:
250 Map wctx = (Map) withParameters.getFirst();
251 wctx.put(name, value);
252 break;
254 //System.err.println("Set "+name+"="+value);
257 public Object resolveVariable(QName qName)
259 return get(qName, null, 1, 1);
262 public String toString()
264 CPStringBuilder buf = new CPStringBuilder();
265 boolean next = false;
266 Collection seen = new HashSet();
267 Map wctx = (Map) withParameters.getFirst();
268 buf.append('(');
269 for (Iterator i = wctx.entrySet().iterator(); i.hasNext(); )
271 if (next)
273 buf.append(',');
275 else
277 next = true;
279 Map.Entry entry = (Map.Entry) i.next();
280 Object key = entry.getKey();
281 if (!seen.contains(key))
283 buf.append(key);
284 buf.append('=');
285 buf.append(entry.getValue());
286 seen.add(key);
289 buf.append(')');
290 next = false;
291 seen.clear();
292 buf.append('{');
293 for (Iterator i = variables.iterator(); i.hasNext(); )
295 Map ctx = (Map) i.next();
296 for (Iterator j = ctx.entrySet().iterator(); j.hasNext(); )
298 if (next)
300 buf.append(',');
302 else
304 next = true;
306 Map.Entry entry = (Map.Entry) j.next();
307 Object key = entry.getKey();
308 if (!seen.contains(key))
310 buf.append(key);
311 buf.append('=');
312 buf.append(entry.getValue());
313 seen.add(key);
317 buf.append('}');
318 next = false;
319 seen.clear();
320 buf.append('[');
321 for (Iterator i = parameters.iterator(); i.hasNext(); )
323 Map ctx = (Map) i.next();
324 for (Iterator j = ctx.entrySet().iterator(); j.hasNext(); )
326 if (next)
328 buf.append(',');
330 else
332 next = true;
334 Map.Entry entry = (Map.Entry) j.next();
335 Object key = entry.getKey();
336 if (!seen.contains(key))
338 buf.append(key);
339 buf.append('=');
340 buf.append(entry.getValue());
341 seen.add(key);
345 buf.append(']');
346 return buf.toString();