14c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson/* 24c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one 34c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * or more contributor license agreements. See the NOTICE file 44c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * distributed with this work for additional information 54c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * regarding copyright ownership. The ASF licenses this file 64c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * to you under the Apache License, Version 2.0 (the "License"); 74c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * you may not use this file except in compliance with the License. 84c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * You may obtain a copy of the License at 94c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 104c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 114c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 124c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Unless required by applicable law or agreed to in writing, software 134c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 144c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 154c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * See the License for the specific language governing permissions and 164c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * limitations under the License. 174c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 184c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson/* 194c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * $Id: Counter.java 468645 2006-10-28 06:57:24Z minchau $ 204c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 214c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonpackage org.apache.xalan.transformer; 224c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 234c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport javax.xml.transform.TransformerException; 244c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 254c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.apache.xalan.templates.ElemNumber; 264c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.apache.xml.dtm.DTM; 274c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.apache.xpath.NodeSetDTM; 284c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonimport org.apache.xpath.XPathContext; 294c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 304c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson/** 314c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * A class that does incremental counting for support of xsl:number. 324c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * This class stores a cache of counted nodes (m_countNodes). 334c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * It tries to cache the counted nodes in document order... 344c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * the node count is based on its position in the cache list 354c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @xsl.usage internal 364c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 374c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilsonpublic class Counter 384c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson{ 394c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 404c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 414c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Set the maximum ammount the m_countNodes list can 424c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * grow to. 434c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 444c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson static final int MAXCOUNTNODES = 500; 454c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 464c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 474c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * The start count from where m_countNodes counts 484c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * from. In other words, the count of a given node 494c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * in the m_countNodes vector is node position + 504c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * m_countNodesStartCount. 514c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 524c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson int m_countNodesStartCount = 0; 534c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 544c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 554c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * A vector of all nodes counted so far. 564c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 574c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson NodeSetDTM m_countNodes; 584c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 594c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 604c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * The node from where the counting starts. This is needed to 614c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * find a counter if the node being counted is not immediatly 624c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * found in the m_countNodes vector. 634c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 644c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson int m_fromNode = DTM.NULL; 654c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 664c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 674c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * The owning xsl:number element. 684c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 694c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson ElemNumber m_numberElem; 704c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 714c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 724c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Value to store result of last getCount call, for benifit 734c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * of returning val from CountersTable.getCounterByCounted, 744c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * who calls getCount. 754c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 764c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson int m_countResult; 774c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 784c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 794c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Construct a counter object. 804c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 814c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param numberElem The owning xsl:number element. 824c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param countNodes A vector of all nodes counted so far. 834c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 844c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @throws TransformerException 854c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 864c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson Counter(ElemNumber numberElem, NodeSetDTM countNodes) throws TransformerException 874c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 884c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_countNodes = countNodes; 894c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_numberElem = numberElem; 904c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 914c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 924c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 934c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Construct a counter object. 944c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 954c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param numberElem The owning xsl:number element. 964c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 974c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @throws TransformerException 984c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 994c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson Counter(ElemNumber numberElem) throws TransformerException 1004c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 1014c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_numberElem = numberElem; 1024c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson }*/ 1034c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1044c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 1054c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Try and find a node that was previously counted. If found, 1064c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * return a positive integer that corresponds to the count. 1074c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 1084c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param support The XPath context to use 1094c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @param node The node to be counted. 1104c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 1114c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @return The count of the node, or -1 if not found. 1124c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 1134c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson int getPreviouslyCounted(XPathContext support, int node) 1144c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 1154c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1164c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson int n = m_countNodes.size(); 1174c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1184c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_countResult = 0; 1194c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1204c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson for (int i = n - 1; i >= 0; i--) 1214c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 1224c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson int countedNode = m_countNodes.elementAt(i); 1234c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1244c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson if (node == countedNode) 1254c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 1264c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1274c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson // Since the list is in backwards order, the count is 1284c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson // how many are in the rest of the list. 1294c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson m_countResult = i + 1 + m_countNodesStartCount; 1304c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1314c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson break; 1324c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 1334c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1344c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson DTM dtm = support.getDTM(countedNode); 1354c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1364c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson // Try to see if the given node falls after the counted node... 1374c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson // if it does, don't keep searching backwards. 1384c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson if (dtm.isNodeAfter(countedNode, node)) 1394c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson break; 1404c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 1414c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1424c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson return m_countResult; 1434c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 1444c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1454c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson /** 1464c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * Get the last node in the list. 1474c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * 1484c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson * @return the last node in the list. 1494c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson */ 1504c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson int getLast() 1514c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson { 1524c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1534c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson int size = m_countNodes.size(); 1544c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson 1554c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson return (size > 0) ? m_countNodes.elementAt(size - 1) : DTM.NULL; 1564c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson } 1574c7a0d97cf2b27790e6236965a1d798d710d7ec7Jesse Wilson} 158