1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.transformer;
  58. import java.util.Hashtable;
  59. import java.util.Vector;
  60. //import org.w3c.dom.Node;
  61. import org.apache.xml.dtm.DTM;
  62. import javax.xml.transform.TransformerException;
  63. import org.apache.xpath.XPathContext;
  64. import org.apache.xpath.XPath;
  65. import org.apache.xpath.NodeSetDTM;
  66. import org.apache.xalan.templates.ElemNumber;
  67. /**
  68. * <meta name="usage" content="internal"/>
  69. * This is a table of counters, keyed by ElemNumber objects, each
  70. * of which has a list of Counter objects. This really isn't a true
  71. * table, it is more like a list of lists (there must be a technical
  72. * term for that...).
  73. */
  74. public class CountersTable extends Hashtable
  75. {
  76. /**
  77. * Construct a CountersTable.
  78. */
  79. public CountersTable(){}
  80. /**
  81. * Get the list of counters that corresponds to
  82. * the given ElemNumber object.
  83. *
  84. * @param numberElem the given xsl:number element.
  85. *
  86. * @return the list of counters that corresponds to
  87. * the given ElemNumber object.
  88. */
  89. Vector getCounters(ElemNumber numberElem)
  90. {
  91. Vector counters = (Vector) this.get(numberElem);
  92. return (null == counters) ? putElemNumber(numberElem) : counters;
  93. }
  94. /**
  95. * Put a counter into the table and create an empty
  96. * vector as it's value.
  97. *
  98. * @param numberElem the given xsl:number element.
  99. *
  100. * @return an empty vector to be used to store counts
  101. * for this number element.
  102. */
  103. Vector putElemNumber(ElemNumber numberElem)
  104. {
  105. Vector counters = new Vector();
  106. this.put(numberElem, counters);
  107. return counters;
  108. }
  109. /**
  110. * Place to collect new counters.
  111. */
  112. transient private NodeSetDTM m_newFound;
  113. /**
  114. * Add a list of counted nodes that were built in backwards document
  115. * order, or a list of counted nodes that are in forwards document
  116. * order.
  117. *
  118. * @param flist Vector of nodes built in forwards document order
  119. * @param blist Vector of nodes built in backwards document order
  120. */
  121. void appendBtoFList(NodeSetDTM flist, NodeSetDTM blist)
  122. {
  123. int n = blist.size();
  124. for (int i = (n - 1); i >= 0; i--)
  125. {
  126. flist.addElement(blist.item(i));
  127. }
  128. }
  129. // For diagnostics
  130. /** Number of counters created so far */
  131. transient int m_countersMade = 0;
  132. /**
  133. * Count forward until the given node is found, or until
  134. * we have looked to the given amount.
  135. *
  136. * @param support The XPath context to use
  137. * @param numberElem The given xsl:number element.
  138. * @param node The node to count.
  139. *
  140. * @return The node count, or 0 if not found.
  141. *
  142. * @throws TransformerException
  143. */
  144. public int countNode(XPathContext support, ElemNumber numberElem, int node)
  145. throws TransformerException
  146. {
  147. int count = 0;
  148. Vector counters = getCounters(numberElem);
  149. int nCounters = counters.size();
  150. // XPath countMatchPattern = numberElem.getCountMatchPattern(support, node);
  151. // XPath fromMatchPattern = numberElem.m_fromMatchPattern;
  152. int target = numberElem.getTargetNode(support, node);
  153. if (DTM.NULL != target)
  154. {
  155. for (int i = 0; i < nCounters; i++)
  156. {
  157. Counter counter = (Counter) counters.elementAt(i);
  158. count = counter.getPreviouslyCounted(support, target);
  159. if (count > 0)
  160. return count;
  161. }
  162. // In the loop below, we collect the nodes in backwards doc order, so
  163. // we don't have to do inserts, but then we store the nodes in forwards
  164. // document order, so we don't have to insert nodes into that list,
  165. // so that's what the appendBtoFList stuff is all about. In cases
  166. // of forward counting by one, this will mean a single node copy from
  167. // the backwards list (m_newFound) to the forwards list (counter.m_countNodes).
  168. count = 0;
  169. if (m_newFound == null)
  170. m_newFound = new NodeSetDTM(support.getDTMManager());
  171. for (; DTM.NULL != target;
  172. target = numberElem.getPreviousNode(support, target))
  173. {
  174. // First time in, we should not have to check for previous counts,
  175. // since the original target node was already checked in the
  176. // block above.
  177. if (0 != count)
  178. {
  179. for (int i = 0; i < nCounters; i++)
  180. {
  181. Counter counter = (Counter) counters.elementAt(i);
  182. int cacheLen = counter.m_countNodes.size();
  183. if ((cacheLen > 0)
  184. && (counter.m_countNodes.elementAt(cacheLen
  185. - 1) == target))
  186. {
  187. count += (cacheLen + counter.m_countNodesStartCount);
  188. if (cacheLen > 0)
  189. appendBtoFList(counter.m_countNodes, m_newFound);
  190. m_newFound.removeAllElements();
  191. return count;
  192. }
  193. }
  194. }
  195. m_newFound.addElement(target);
  196. count++;
  197. }
  198. // If we got to this point, then we didn't find a counter, so make
  199. // one and add it to the list.
  200. Counter counter = new Counter(numberElem, new NodeSetDTM(support.getDTMManager()));
  201. m_countersMade++; // for diagnostics
  202. appendBtoFList(counter.m_countNodes, m_newFound);
  203. m_newFound.removeAllElements();
  204. counters.addElement(counter);
  205. }
  206. return count;
  207. }
  208. }