Bindings.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Bindings.java --
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.transform;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.util.Collection;
  34. import java.util.Collections;
  35. import java.util.HashMap;
  36. import java.util.HashSet;
  37. import java.util.Iterator;
  38. import java.util.LinkedList;
  39. import java.util.Map;
  40. import javax.xml.namespace.QName;
  41. import javax.xml.xpath.XPathVariableResolver;
  42. import org.w3c.dom.Node;
  43. /**
  44. * The set of variable bindings in effect for a stylesheet.
  45. *
  46. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  47. */
  48. public class Bindings
  49. implements XPathVariableResolver, Cloneable
  50. {
  51. static final int VARIABLE = 0;
  52. static final int PARAM = 1;
  53. static final int WITH_PARAM = 2;
  54. final Stylesheet stylesheet;
  55. /**
  56. * Global variables.
  57. */
  58. final LinkedList<Map<QName,Object>> variables;
  59. /**
  60. * Parameter value stack.
  61. */
  62. final LinkedList<Map<QName,Object>> parameters;
  63. /**
  64. * Argument (with-param) value stack.
  65. */
  66. final LinkedList<Map<QName,Object>> withParameters;
  67. /**
  68. * Only search globals.
  69. */
  70. boolean global;
  71. Bindings(Stylesheet stylesheet)
  72. {
  73. this.stylesheet = stylesheet;
  74. variables = new LinkedList<Map<QName,Object>>();
  75. parameters = new LinkedList<Map<QName,Object>>();
  76. withParameters = new LinkedList<Map<QName,Object>>();
  77. for (int i = 0; i < 3; i++)
  78. {
  79. push(i);
  80. }
  81. }
  82. public Object clone()
  83. {
  84. try
  85. {
  86. return (Bindings) super.clone();
  87. }
  88. catch (CloneNotSupportedException e)
  89. {
  90. throw new Error(e.getMessage());
  91. }
  92. }
  93. void push(int type)
  94. {
  95. switch (type)
  96. {
  97. case VARIABLE:
  98. variables.addFirst(new HashMap<QName,Object>());
  99. break;
  100. case PARAM:
  101. parameters.addFirst(new HashMap<QName,Object>());
  102. break;
  103. case WITH_PARAM:
  104. withParameters.addFirst(new HashMap<QName,Object>());
  105. break;
  106. }
  107. }
  108. void pop(int type)
  109. {
  110. switch (type)
  111. {
  112. case VARIABLE:
  113. variables.removeFirst();
  114. break;
  115. case PARAM:
  116. parameters.removeFirst();
  117. break;
  118. case WITH_PARAM:
  119. withParameters.removeFirst();
  120. break;
  121. }
  122. }
  123. public boolean containsKey(QName name, int type)
  124. {
  125. if (global)
  126. {
  127. Map<QName,Object> ctx1 = variables.getLast();
  128. Map<QName,Object> ctx2 = parameters.getLast();
  129. return (ctx1.containsKey(name) || ctx2.containsKey(name));
  130. }
  131. Iterator<Map<QName,Object>> i = null;
  132. switch (type)
  133. {
  134. case VARIABLE:
  135. i = variables.iterator();
  136. break;
  137. case PARAM:
  138. i = parameters.iterator();
  139. break;
  140. case WITH_PARAM:
  141. Map<QName,Object> ctx = withParameters.getFirst();
  142. return ctx.containsKey(name);
  143. }
  144. if (i != null)
  145. {
  146. while (i.hasNext())
  147. {
  148. Map<QName,Object> ctx = i.next();
  149. if (ctx.containsKey(name))
  150. {
  151. return true;
  152. }
  153. }
  154. }
  155. return false;
  156. }
  157. public Object get(QName name, Node context, int pos, int len)
  158. {
  159. if (global)
  160. {
  161. Map<QName,Object> ctx = variables.getLast();
  162. Object ret = ctx.get(name);
  163. if (ret == null)
  164. {
  165. ctx = parameters.getLast();
  166. ret = ctx.get(name);
  167. }
  168. return ret;
  169. }
  170. //System.err.println("bindings.get: "+name);
  171. //System.err.println("\t"+toString());
  172. Object ret = null;
  173. //if (parameters.size() > 1 && containsKey(name, PARAM))
  174. // check that template defines parameter
  175. {
  176. Map<QName,Object> cwp = withParameters.getFirst();
  177. ret = cwp.get(name);
  178. //System.err.println("\twith-param: ret="+ret);
  179. }
  180. if (ret == null)
  181. {
  182. for (Iterator<Map<QName,Object>> i = variables.iterator();
  183. i.hasNext() && ret == null; )
  184. {
  185. Map<QName,Object> vctx = i.next();
  186. ret = vctx.get(name);
  187. }
  188. //System.err.println("\tvariable: ret="+ret);
  189. }
  190. if (ret == null)
  191. {
  192. for (Iterator<Map<QName,Object>> i = parameters.iterator();
  193. i.hasNext() && ret == null; )
  194. {
  195. Map<QName,Object> pctx = i.next();
  196. ret = pctx.get(name);
  197. }
  198. //System.err.println("\tparam: ret="+ret);
  199. }
  200. /*if (ret instanceof Expr && context != null)
  201. {
  202. Expr expr = (Expr) ret;
  203. ret = expr.evaluate(context, 1, 1);
  204. }*/
  205. if (ret instanceof Node)
  206. {
  207. ret = Collections.singleton(ret);
  208. }
  209. if (ret == null)
  210. {
  211. ret = "";
  212. }
  213. //System.err.println("\tret="+ret);
  214. return ret;
  215. }
  216. void set(QName name, Object value, int type)
  217. {
  218. switch (type)
  219. {
  220. case VARIABLE:
  221. Map<QName,Object> vctx = variables.getFirst();
  222. vctx.put(name, value);
  223. break;
  224. case PARAM:
  225. Map<QName,Object> pctx = parameters.getFirst();
  226. pctx.put(name, value);
  227. break;
  228. case WITH_PARAM:
  229. Map<QName,Object> wctx = withParameters.getFirst();
  230. wctx.put(name, value);
  231. break;
  232. }
  233. //System.err.println("Set "+name+"="+value);
  234. }
  235. public Object resolveVariable(QName qName)
  236. {
  237. return get(qName, null, 1, 1);
  238. }
  239. public String toString()
  240. {
  241. CPStringBuilder buf = new CPStringBuilder();
  242. boolean next = false;
  243. Collection<QName> seen = new HashSet<QName>();
  244. Map<QName,Object> wctx = withParameters.getFirst();
  245. buf.append('(');
  246. for (Map.Entry<QName,Object> entry : wctx.entrySet())
  247. {
  248. if (next)
  249. {
  250. buf.append(',');
  251. }
  252. else
  253. {
  254. next = true;
  255. }
  256. QName key = entry.getKey();
  257. if (!seen.contains(key))
  258. {
  259. buf.append(key);
  260. buf.append('=');
  261. buf.append(entry.getValue());
  262. seen.add(key);
  263. }
  264. }
  265. buf.append(')');
  266. next = false;
  267. seen.clear();
  268. buf.append('{');
  269. for (Map<QName,Object> ctx : variables)
  270. {
  271. for (Map.Entry<QName,Object> entry : ctx.entrySet())
  272. {
  273. if (next)
  274. {
  275. buf.append(',');
  276. }
  277. else
  278. {
  279. next = true;
  280. }
  281. QName key = entry.getKey();
  282. if (!seen.contains(key))
  283. {
  284. buf.append(key);
  285. buf.append('=');
  286. buf.append(entry.getValue());
  287. seen.add(key);
  288. }
  289. }
  290. }
  291. buf.append('}');
  292. next = false;
  293. seen.clear();
  294. buf.append('[');
  295. for (Map<QName,Object> ctx : parameters)
  296. {
  297. for (Map.Entry<QName,Object> entry : ctx.entrySet())
  298. {
  299. if (next)
  300. {
  301. buf.append(',');
  302. }
  303. else
  304. {
  305. next = true;
  306. }
  307. QName key = entry.getKey();
  308. if (!seen.contains(key))
  309. {
  310. buf.append(key);
  311. buf.append('=');
  312. buf.append(entry.getValue());
  313. seen.add(key);
  314. }
  315. }
  316. }
  317. buf.append(']');
  318. return buf.toString();
  319. }
  320. }