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 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
;
49 import javax
.xml
.namespace
.QName
;
50 import javax
.xml
.xpath
.XPathVariableResolver
;
51 import org
.w3c
.dom
.Node
;
54 * The set of variable bindings in effect for a stylesheet.
56 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
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
;
71 final LinkedList variables
;
74 * Parameter value stack.
76 final LinkedList parameters
;
79 * Argument (with-param) value stack.
81 final LinkedList withParameters
;
84 * Only search globals.
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
++)
100 public Object
clone()
104 return (Bindings
) super.clone();
106 catch (CloneNotSupportedException e
)
108 throw new Error(e
.getMessage());
117 variables
.addFirst(new HashMap());
120 parameters
.addFirst(new HashMap());
123 withParameters
.addFirst(new HashMap());
133 variables
.removeFirst();
136 parameters
.removeFirst();
139 withParameters
.removeFirst();
144 public boolean containsKey(QName name
, int type
)
148 Map ctx1
= (Map
) variables
.getLast();
149 Map ctx2
= (Map
) parameters
.getLast();
150 return (ctx1
.containsKey(name
) || ctx2
.containsKey(name
));
156 i
= variables
.iterator();
159 i
= parameters
.iterator();
162 Map ctx
= (Map
) withParameters
.getFirst();
163 return ctx
.containsKey(name
);
169 Map ctx
= (Map
) i
.next();
170 if (ctx
.containsKey(name
))
179 public Object
get(QName name
, Node context
, int pos
, int len
)
183 Map ctx
= (Map
) variables
.getLast();
184 Object ret
= ctx
.get(name
);
187 ctx
= (Map
) parameters
.getLast();
192 //System.err.println("bindings.get: "+name);
193 //System.err.println("\t"+toString());
195 //if (parameters.size() > 1 && containsKey(name, PARAM))
196 // check that template defines parameter
198 Map cwp
= (Map
) withParameters
.getFirst();
200 //System.err.println("\twith-param: ret="+ret);
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);
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
);
233 //System.err.println("\tret="+ret);
237 void set(QName name
, Object value
, int type
)
242 Map vctx
= (Map
) variables
.getFirst();
243 vctx
.put(name
, value
);
246 Map pctx
= (Map
) parameters
.getFirst();
247 pctx
.put(name
, value
);
250 Map wctx
= (Map
) withParameters
.getFirst();
251 wctx
.put(name
, value
);
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();
269 for (Iterator i
= wctx
.entrySet().iterator(); i
.hasNext(); )
279 Map
.Entry entry
= (Map
.Entry
) i
.next();
280 Object key
= entry
.getKey();
281 if (!seen
.contains(key
))
285 buf
.append(entry
.getValue());
293 for (Iterator i
= variables
.iterator(); i
.hasNext(); )
295 Map ctx
= (Map
) i
.next();
296 for (Iterator j
= ctx
.entrySet().iterator(); j
.hasNext(); )
306 Map
.Entry entry
= (Map
.Entry
) j
.next();
307 Object key
= entry
.getKey();
308 if (!seen
.contains(key
))
312 buf
.append(entry
.getValue());
321 for (Iterator i
= parameters
.iterator(); i
.hasNext(); )
323 Map ctx
= (Map
) i
.next();
324 for (Iterator j
= ctx
.entrySet().iterator(); j
.hasNext(); )
334 Map
.Entry entry
= (Map
.Entry
) j
.next();
335 Object key
= entry
.getKey();
336 if (!seen
.contains(key
))
340 buf
.append(entry
.getValue());
346 return buf
.toString();