19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one 39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file 49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information 59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file 69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the "License"); 79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License. 89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at 99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software 139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and 169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License. 179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: Counter.java 468645 2006-10-28 06:57:24Z minchau $ 209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xalan.transformer; 229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport javax.xml.transform.TransformerException; 249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xalan.templates.ElemNumber; 269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTM; 279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.NodeSetDTM; 289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.XPathContext; 299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/** 319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * A class that does incremental counting for support of xsl:number. 329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class stores a cache of counted nodes (m_countNodes). 339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * It tries to cache the counted nodes in document order... 349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the node count is based on its position in the cache list 359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @xsl.usage internal 369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class Counter 389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{ 399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Set the maximum ammount the m_countNodes list can 429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * grow to. 439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson static final int MAXCOUNTNODES = 500; 459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The start count from where m_countNodes counts 489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * from. In other words, the count of a given node 499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * in the m_countNodes vector is node position + 509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * m_countNodesStartCount. 519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int m_countNodesStartCount = 0; 539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * A vector of all nodes counted so far. 569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson NodeSetDTM m_countNodes; 589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The node from where the counting starts. This is needed to 619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * find a counter if the node being counted is not immediatly 629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * found in the m_countNodes vector. 639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int m_fromNode = DTM.NULL; 659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The owning xsl:number element. 689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson ElemNumber m_numberElem; 709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Value to store result of last getCount call, for benifit 739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * of returning val from CountersTable.getCounterByCounted, 749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * who calls getCount. 759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int m_countResult; 779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Construct a counter object. 809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param numberElem The owning xsl:number element. 829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param countNodes A vector of all nodes counted so far. 839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws TransformerException 859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Counter(ElemNumber numberElem, NodeSetDTM countNodes) throws TransformerException 879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_countNodes = countNodes; 899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_numberElem = numberElem; 909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Construct a counter object. 949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param numberElem The owning xsl:number element. 969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws TransformerException 989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson Counter(ElemNumber numberElem) throws TransformerException 1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_numberElem = numberElem; 1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson }*/ 1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Try and find a node that was previously counted. If found, 1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * return a positive integer that corresponds to the count. 1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param support The XPath context to use 1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param node The node to be counted. 1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return The count of the node, or -1 if not found. 1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int getPreviouslyCounted(XPathContext support, int node) 1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int n = m_countNodes.size(); 1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_countResult = 0; 1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson for (int i = n - 1; i >= 0; i--) 1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int countedNode = m_countNodes.elementAt(i); 1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (node == countedNode) 1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Since the list is in backwards order, the count is 1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // how many are in the rest of the list. 1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson m_countResult = i + 1 + m_countNodesStartCount; 1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson DTM dtm = support.getDTM(countedNode); 1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Try to see if the given node falls after the counted node... 1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // if it does, don't keep searching backwards. 1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (dtm.isNodeAfter(countedNode, node)) 1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return m_countResult; 1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Get the last node in the list. 1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the last node in the list. 1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int getLast() 1519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int size = m_countNodes.size(); 1549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return (size > 0) ? m_countNodes.elementAt(size - 1) : DTM.NULL; 1569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson} 158