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)
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
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
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
;
47 import javax
.xml
.namespace
.QName
;
48 import javax
.xml
.xpath
.XPathVariableResolver
;
49 import org
.w3c
.dom
.Node
;
52 * The set of variable bindings in effect for a stylesheet.
54 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
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
;
69 final LinkedList variables
;
72 * Parameter value stack.
74 final LinkedList parameters
;
77 * Argument (with-param) value stack.
79 final LinkedList withParameters
;
82 * Only search globals.
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
++)
102 return (Bindings
) super.clone();
104 catch (CloneNotSupportedException e
)
106 throw new Error(e
.getMessage());
115 variables
.addFirst(new HashMap());
118 parameters
.addFirst(new HashMap());
121 withParameters
.addFirst(new HashMap());
131 variables
.removeFirst();
134 parameters
.removeFirst();
137 withParameters
.removeFirst();
142 public boolean containsKey(QName name
, int type
)
146 Map ctx1
= (Map
) variables
.getLast();
147 Map ctx2
= (Map
) parameters
.getLast();
148 return (ctx1
.containsKey(name
) || ctx2
.containsKey(name
));
154 i
= variables
.iterator();
157 i
= parameters
.iterator();
160 Map ctx
= (Map
) withParameters
.getFirst();
161 return ctx
.containsKey(name
);
167 Map ctx
= (Map
) i
.next();
168 if (ctx
.containsKey(name
))
177 public Object
get(QName name
, Node context
, int pos
, int len
)
181 Map ctx
= (Map
) variables
.getLast();
182 Object ret
= ctx
.get(name
);
185 ctx
= (Map
) parameters
.getLast();
190 //System.err.println("bindings.get: "+name);
191 //System.err.println("\t"+toString());
193 //if (parameters.size() > 1 && containsKey(name, PARAM))
194 // check that template defines parameter
196 Map cwp
= (Map
) withParameters
.getFirst();
198 //System.err.println("\twith-param: ret="+ret);
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);
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
);
231 //System.err.println("\tret="+ret);
235 void set(QName name
, Object value
, int type
)
240 Map vctx
= (Map
) variables
.getFirst();
241 vctx
.put(name
, value
);
244 Map pctx
= (Map
) parameters
.getFirst();
245 pctx
.put(name
, value
);
248 Map wctx
= (Map
) withParameters
.getFirst();
249 wctx
.put(name
, value
);
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();
267 for (Iterator i
= wctx
.entrySet().iterator(); i
.hasNext(); )
277 Map
.Entry entry
= (Map
.Entry
) i
.next();
278 Object key
= entry
.getKey();
279 if (!seen
.contains(key
))
283 buf
.append(entry
.getValue());
291 for (Iterator i
= variables
.iterator(); i
.hasNext(); )
293 Map ctx
= (Map
) i
.next();
294 for (Iterator j
= ctx
.entrySet().iterator(); j
.hasNext(); )
304 Map
.Entry entry
= (Map
.Entry
) j
.next();
305 Object key
= entry
.getKey();
306 if (!seen
.contains(key
))
310 buf
.append(entry
.getValue());
319 for (Iterator i
= parameters
.iterator(); i
.hasNext(); )
321 Map ctx
= (Map
) i
.next();
322 for (Iterator j
= ctx
.entrySet().iterator(); j
.hasNext(); )
332 Map
.Entry entry
= (Map
.Entry
) j
.next();
333 Object key
= entry
.getKey();
334 if (!seen
.contains(key
))
338 buf
.append(entry
.getValue());
344 return buf
.toString();