Merge from mainline
[official-gcc.git] / libjava / classpath / gnu / xml / transform / Bindings.java
blob4ee08322317a12967666c70485caf5c66f92b007
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 java.util.Collection;
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.HashSet;
44 import java.util.Iterator;
45 import java.util.LinkedList;
46 import java.util.Map;
47 import javax.xml.namespace.QName;
48 import javax.xml.xpath.XPathVariableResolver;
49 import org.w3c.dom.Node;
51 /**
52 * The set of variable bindings in effect for a stylesheet.
54 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
56 public class Bindings
57 implements XPathVariableResolver, Cloneable
60 static final int VARIABLE = 0;
61 static final int PARAM = 1;
62 static final int WITH_PARAM = 2;
64 final Stylesheet stylesheet;
66 /**
67 * Global variables.
69 final LinkedList variables;
71 /**
72 * Parameter value stack.
74 final LinkedList parameters;
76 /**
77 * Argument (with-param) value stack.
79 final LinkedList withParameters;
81 /**
82 * Only search globals.
84 boolean global;
86 Bindings(Stylesheet stylesheet)
88 this.stylesheet = stylesheet;
89 variables = new LinkedList();
90 parameters = new LinkedList();
91 withParameters = new LinkedList();
92 for (int i = 0; i < 3; i++)
94 push(i);
98 public Object clone()
102 return (Bindings) super.clone();
104 catch (CloneNotSupportedException e)
106 throw new Error(e.getMessage());
110 void push(int type)
112 switch (type)
114 case VARIABLE:
115 variables.addFirst(new HashMap());
116 break;
117 case PARAM:
118 parameters.addFirst(new HashMap());
119 break;
120 case WITH_PARAM:
121 withParameters.addFirst(new HashMap());
122 break;
126 void pop(int type)
128 switch (type)
130 case VARIABLE:
131 variables.removeFirst();
132 break;
133 case PARAM:
134 parameters.removeFirst();
135 break;
136 case WITH_PARAM:
137 withParameters.removeFirst();
138 break;
142 public boolean containsKey(QName name, int type)
144 if (global)
146 Map ctx1 = (Map) variables.getLast();
147 Map ctx2 = (Map) parameters.getLast();
148 return (ctx1.containsKey(name) || ctx2.containsKey(name));
150 Iterator i = null;
151 switch (type)
153 case VARIABLE:
154 i = variables.iterator();
155 break;
156 case PARAM:
157 i = parameters.iterator();
158 break;
159 case WITH_PARAM:
160 Map ctx = (Map) withParameters.getFirst();
161 return ctx.containsKey(name);
163 if (i != null)
165 while (i.hasNext())
167 Map ctx = (Map) i.next();
168 if (ctx.containsKey(name))
170 return true;
174 return false;
177 public Object get(QName name, Node context, int pos, int len)
179 if (global)
181 Map ctx = (Map) variables.getLast();
182 Object ret = ctx.get(name);
183 if (ret == null)
185 ctx = (Map) parameters.getLast();
186 ret = ctx.get(name);
188 return ret;
190 //System.err.println("bindings.get: "+name);
191 //System.err.println("\t"+toString());
192 Object ret = null;
193 //if (parameters.size() > 1 && containsKey(name, PARAM))
194 // check that template defines parameter
196 Map cwp = (Map) withParameters.getFirst();
197 ret = cwp.get(name);
198 //System.err.println("\twith-param: ret="+ret);
200 if (ret == null)
202 for (Iterator i = variables.iterator(); i.hasNext() && ret == null; )
204 Map vctx = (Map) i.next();
205 ret = vctx.get(name);
207 //System.err.println("\tvariable: ret="+ret);
209 if (ret == null)
211 for (Iterator i = parameters.iterator(); i.hasNext() && ret == null; )
213 Map pctx = (Map) i.next();
214 ret = pctx.get(name);
216 //System.err.println("\tparam: ret="+ret);
218 /*if (ret instanceof Expr && context != null)
220 Expr expr = (Expr) ret;
221 ret = expr.evaluate(context, 1, 1);
223 if (ret instanceof Node)
225 ret = Collections.singleton(ret);
227 if (ret == null)
229 ret = "";
231 //System.err.println("\tret="+ret);
232 return ret;
235 void set(QName name, Object value, int type)
237 switch (type)
239 case VARIABLE:
240 Map vctx = (Map) variables.getFirst();
241 vctx.put(name, value);
242 break;
243 case PARAM:
244 Map pctx = (Map) parameters.getFirst();
245 pctx.put(name, value);
246 break;
247 case WITH_PARAM:
248 Map wctx = (Map) withParameters.getFirst();
249 wctx.put(name, value);
250 break;
252 //System.err.println("Set "+name+"="+value);
255 public Object resolveVariable(QName qName)
257 return get(qName, null, 1, 1);
260 public String toString()
262 StringBuffer buf = new StringBuffer();
263 boolean next = false;
264 Collection seen = new HashSet();
265 Map wctx = (Map) withParameters.getFirst();
266 buf.append('(');
267 for (Iterator i = wctx.entrySet().iterator(); i.hasNext(); )
269 if (next)
271 buf.append(',');
273 else
275 next = true;
277 Map.Entry entry = (Map.Entry) i.next();
278 Object key = entry.getKey();
279 if (!seen.contains(key))
281 buf.append(key);
282 buf.append('=');
283 buf.append(entry.getValue());
284 seen.add(key);
287 buf.append(')');
288 next = false;
289 seen.clear();
290 buf.append('{');
291 for (Iterator i = variables.iterator(); i.hasNext(); )
293 Map ctx = (Map) i.next();
294 for (Iterator j = ctx.entrySet().iterator(); j.hasNext(); )
296 if (next)
298 buf.append(',');
300 else
302 next = true;
304 Map.Entry entry = (Map.Entry) j.next();
305 Object key = entry.getKey();
306 if (!seen.contains(key))
308 buf.append(key);
309 buf.append('=');
310 buf.append(entry.getValue());
311 seen.add(key);
315 buf.append('}');
316 next = false;
317 seen.clear();
318 buf.append('[');
319 for (Iterator i = parameters.iterator(); i.hasNext(); )
321 Map ctx = (Map) i.next();
322 for (Iterator j = ctx.entrySet().iterator(); j.hasNext(); )
324 if (next)
326 buf.append(',');
328 else
330 next = true;
332 Map.Entry entry = (Map.Entry) j.next();
333 Object key = entry.getKey();
334 if (!seen.contains(key))
336 buf.append(key);
337 buf.append('=');
338 buf.append(entry.getValue());
339 seen.add(key);
343 buf.append(']');
344 return buf.toString();