NodeNumberNode.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* NodeNumberNode.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 java.util.ArrayList;
  33. import java.util.Collection;
  34. import java.util.Collections;
  35. import java.util.Iterator;
  36. import java.util.List;
  37. import javax.xml.transform.TransformerException;
  38. import org.w3c.dom.Node;
  39. import gnu.xml.xpath.Expr;
  40. import gnu.xml.xpath.Pattern;
  41. import gnu.xml.xpath.Selector;
  42. import gnu.xml.xpath.UnionExpr;
  43. /**
  44. * A template node representing the XSL <code>number</code> instruction
  45. * with no <code>value</code> expression, i.e. the value is computed from
  46. * the document position of the context node.
  47. *
  48. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  49. */
  50. final class NodeNumberNode
  51. extends AbstractNumberNode
  52. {
  53. static final int SINGLE = 0;
  54. static final int MULTIPLE = 1;
  55. static final int ANY = 2;
  56. final int level;
  57. final Pattern count;
  58. final Pattern from;
  59. NodeNumberNode(int level, Pattern count, Pattern from,
  60. TemplateNode format, String lang,
  61. int letterValue, String groupingSeparator, int groupingSize)
  62. {
  63. super(format, lang, letterValue, groupingSeparator, groupingSize);
  64. this.level = level;
  65. this.count = count;
  66. this.from = from;
  67. }
  68. TemplateNode clone(Stylesheet stylesheet)
  69. {
  70. TemplateNode ret = new NodeNumberNode(level,
  71. (count == null) ? null :
  72. (Pattern) count.clone(stylesheet),
  73. (from == null) ? from :
  74. (Pattern) from.clone(stylesheet),
  75. format, lang, letterValue,
  76. groupingSeparator, groupingSize);
  77. if (children != null)
  78. {
  79. ret.children = children.clone(stylesheet);
  80. }
  81. if (next != null)
  82. {
  83. ret.next = next.clone(stylesheet);
  84. }
  85. return ret;
  86. }
  87. int[] compute(Stylesheet stylesheet, Node context, int pos, int len)
  88. throws TransformerException
  89. {
  90. /*if (from != null)
  91. {
  92. Object ret = from.evaluate(context, pos, len);
  93. if (ret instanceof Collection)
  94. {
  95. Collection ns = (Collection) ret;
  96. if (ns.size() > 0)
  97. {
  98. List list = new ArrayList(ns);
  99. Collections.sort(list, documentOrderComparator);
  100. context = (Node) list.get(0);
  101. }
  102. else
  103. {
  104. return new int[0];
  105. }
  106. }
  107. else
  108. {
  109. return new int[0];
  110. }
  111. }*/
  112. Node current = context;
  113. switch (level)
  114. {
  115. case SINGLE:
  116. if (from == null)
  117. {
  118. while (context != null && !countMatches(current, context))
  119. {
  120. context = context.getParentNode();
  121. }
  122. }
  123. else
  124. {
  125. while (context != null && !countMatches(current, context) &&
  126. !fromMatches(context))
  127. {
  128. context = context.getParentNode();
  129. }
  130. }
  131. return (context == null) ? new int[0] :
  132. new int[] { (context == current) ? pos : getIndex(current, context) };
  133. case MULTIPLE:
  134. List ancestors = new ArrayList();
  135. while (context != null)
  136. {
  137. if (countMatches(current, context))
  138. {
  139. if (from == null || fromMatches(context))
  140. {
  141. ancestors.add(context);
  142. }
  143. }
  144. context = context.getParentNode();
  145. }
  146. Collections.sort(ancestors, documentOrderComparator);
  147. int[] ret = new int[ancestors.size()];
  148. for (int i = 0; i < ret.length; i++)
  149. {
  150. ret[i] = getIndex(current, (Node) ancestors.get(i));
  151. }
  152. return ret;
  153. case ANY:
  154. Expr preceding = new Selector(Selector.PRECEDING,
  155. Collections.EMPTY_LIST);
  156. Expr ancestorOrSelf = new Selector(Selector.ANCESTOR_OR_SELF,
  157. Collections.EMPTY_LIST);
  158. Expr any = new UnionExpr(preceding, ancestorOrSelf);
  159. Object eval = any.evaluate(context, pos, len);
  160. if (eval instanceof Collection)
  161. {
  162. Collection ns = (Collection) eval;
  163. List candidates = new ArrayList();
  164. for (Iterator i = ns.iterator(); i.hasNext(); )
  165. {
  166. Node candidate = (Node) i.next();
  167. if (countMatches(current, candidate))
  168. {
  169. candidates.add(candidate);
  170. if (from != null && from.matches(candidate))
  171. {
  172. break;
  173. }
  174. }
  175. }
  176. return new int[] { candidates.size() };
  177. }
  178. return new int[0];
  179. default:
  180. throw new TransformerException("invalid level");
  181. }
  182. }
  183. boolean countMatches(Node current, Node node)
  184. {
  185. if (count == null)
  186. {
  187. int cnt = current.getNodeType();
  188. int nnt = node.getNodeType();
  189. if (cnt != nnt)
  190. {
  191. return false;
  192. }
  193. if (nnt == Node.ELEMENT_NODE || nnt == Node.ATTRIBUTE_NODE)
  194. {
  195. String curi = current.getNamespaceURI();
  196. String nuri = node.getNamespaceURI();
  197. if ((curi == null && nuri != null) ||
  198. (curi != null && !curi.equals(nuri)))
  199. {
  200. return false;
  201. }
  202. String cn = current.getLocalName();
  203. if (cn == null)
  204. {
  205. cn = current.getNodeName();
  206. }
  207. String nn = node.getLocalName();
  208. if (nn == null)
  209. {
  210. nn = node.getNodeName();
  211. }
  212. if (!cn.equals(nn))
  213. {
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. else
  220. {
  221. return count.matches(node);
  222. }
  223. }
  224. boolean fromMatches(Node node)
  225. {
  226. for (Node ctx = node.getParentNode(); ctx != null;
  227. ctx = ctx.getParentNode())
  228. {
  229. if (from.matches(ctx))
  230. {
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. int getIndex(Node current, Node node)
  237. {
  238. int index = 0;
  239. do
  240. {
  241. do
  242. {
  243. node = node.getPreviousSibling();
  244. }
  245. while (node != null && !countMatches(current, node));
  246. index++;
  247. }
  248. while (node != null);
  249. return index;
  250. }
  251. }