Merge from the pain train
[official-gcc.git] / libjava / gnu / xml / transform / Bindings.java
blob75a8a165138b8e0f9bd4c10711a4ea941e21a249
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 final Stylesheet stylesheet;
62 /**
63 * Global variables.
65 final LinkedList variables;
67 /**
68 * Parameter value stack.
70 final LinkedList parameters;
72 Bindings(Stylesheet stylesheet)
74 this.stylesheet = stylesheet;
75 variables = new LinkedList();
76 parameters = new LinkedList();
77 push(true);
78 push(false);
81 public Object clone()
83 try
85 return (Bindings) super.clone();
87 catch (CloneNotSupportedException e)
89 throw new Error(e.getMessage());
93 void push(boolean global)
95 if (global)
97 variables.addFirst(new HashMap());
99 else
101 parameters.addFirst(new HashMap());
105 void pop(boolean global)
107 if (global)
109 variables.removeFirst();
111 else
113 parameters.removeFirst();
117 public boolean containsKey(String name, boolean global)
119 Iterator i = global ? variables.iterator() : parameters.iterator();
120 while (i.hasNext())
122 Map ctx = (Map) i.next();
123 if (ctx.containsKey(name))
125 return true;
128 return false;
131 public Object get(String name, Node context, int pos, int len)
133 //System.err.println("bindings.get: "+name);
134 //System.err.println("\t"+toString());
135 Object ret = null;
136 for (Iterator i = variables.iterator(); i.hasNext() && ret == null; )
138 Map vctx = (Map) i.next();
139 ret = vctx.get(name);
141 if (ret == null)
143 for (Iterator i = parameters.iterator(); i.hasNext() && ret == null; )
145 Map pctx = (Map) i.next();
146 ret = pctx.get(name);
149 /*if (ret instanceof Expr && context != null)
151 Expr expr = (Expr) ret;
152 ret = expr.evaluate(context, 1, 1);
154 if (ret instanceof Node)
156 ret = Collections.singleton(ret);
158 if (ret == null)
160 ret = "";
162 //System.err.println("\tret="+ret);
163 return ret;
166 void set(String name, Object value, boolean global)
168 if (global)
170 Map context = (Map) variables.getFirst();
171 context.put(name, value);
173 else
175 Map context = (Map) parameters.getFirst();
176 context.put(name, value);
180 public Object resolveVariable(QName qName)
182 return get(qName.toString(), null, 1, 1);
185 public String toString()
187 StringBuffer buf = new StringBuffer();
188 boolean next = false;
189 Collection seen = new HashSet();
190 buf.append('{');
191 for (Iterator i = variables.iterator(); i.hasNext(); )
193 Map ctx = (Map) i.next();
194 for (Iterator j = ctx.entrySet().iterator(); j.hasNext(); )
196 if (next)
198 buf.append(',');
200 else
202 next = true;
204 Map.Entry entry = (Map.Entry) j.next();
205 Object key = entry.getKey();
206 if (!seen.contains(key))
208 buf.append(key);
209 buf.append('=');
210 buf.append(entry.getValue());
211 seen.add(key);
215 for (Iterator i = parameters.iterator(); i.hasNext(); )
217 Map ctx = (Map) i.next();
218 for (Iterator j = ctx.entrySet().iterator(); j.hasNext(); )
220 if (next)
222 buf.append(',');
224 else
226 next = true;
228 Map.Entry entry = (Map.Entry) j.next();
229 Object key = entry.getKey();
230 if (!seen.contains(key))
232 buf.append(key);
233 buf.append('=');
234 buf.append(entry.getValue());
235 seen.add(key);
239 buf.append('}');
240 return buf.toString();