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., 59 Temple Place, Suite 330, 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 final Stylesheet stylesheet
;
65 final LinkedList variables
;
68 * Parameter value stack.
70 final LinkedList parameters
;
72 Bindings(Stylesheet stylesheet
)
74 this.stylesheet
= stylesheet
;
75 variables
= new LinkedList();
76 parameters
= new LinkedList();
85 return (Bindings
) super.clone();
87 catch (CloneNotSupportedException e
)
89 throw new Error(e
.getMessage());
93 void push(boolean global
)
97 variables
.addFirst(new HashMap());
101 parameters
.addFirst(new HashMap());
105 void pop(boolean global
)
109 variables
.removeFirst();
113 parameters
.removeFirst();
117 public boolean containsKey(String name
, boolean global
)
119 Iterator i
= global ? variables
.iterator() : parameters
.iterator();
122 Map ctx
= (Map
) i
.next();
123 if (ctx
.containsKey(name
))
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());
136 for (Iterator i
= variables
.iterator(); i
.hasNext() && ret
== null; )
138 Map vctx
= (Map
) i
.next();
139 ret
= vctx
.get(name
);
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
);
162 //System.err.println("\tret="+ret);
166 void set(String name
, Object value
, boolean global
)
170 Map context
= (Map
) variables
.getFirst();
171 context
.put(name
, value
);
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();
191 for (Iterator i
= variables
.iterator(); i
.hasNext(); )
193 Map ctx
= (Map
) i
.next();
194 for (Iterator j
= ctx
.entrySet().iterator(); j
.hasNext(); )
204 Map
.Entry entry
= (Map
.Entry
) j
.next();
205 Object key
= entry
.getKey();
206 if (!seen
.contains(key
))
210 buf
.append(entry
.getValue());
215 for (Iterator i
= parameters
.iterator(); i
.hasNext(); )
217 Map ctx
= (Map
) i
.next();
218 for (Iterator j
= ctx
.entrySet().iterator(); j
.hasNext(); )
228 Map
.Entry entry
= (Map
.Entry
) j
.next();
229 Object key
= entry
.getKey();
230 if (!seen
.contains(key
))
234 buf
.append(entry
.getValue());
240 return buf
.toString();