001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: AnyNodeCounter.java 468651 2006-10-28 07:04:25Z minchau $
020 */
021
022 package org.apache.xalan.xsltc.dom;
023
024 import org.apache.xalan.xsltc.DOM;
025 import org.apache.xalan.xsltc.Translet;
026 import org.apache.xml.dtm.DTMAxisIterator;
027
028 /**
029 * @author Jacek Ambroziak
030 * @author Santiago Pericas-Geertsen
031 */
032 public abstract class AnyNodeCounter extends NodeCounter {
033 public AnyNodeCounter(Translet translet,
034 DOM document, DTMAxisIterator iterator) {
035 super(translet, document, iterator);
036 }
037
038 public NodeCounter setStartNode(int node) {
039 _node = node;
040 _nodeType = _document.getExpandedTypeID(node);
041 return this;
042 }
043
044 public String getCounter() {
045 int result;
046 if (_value != Integer.MIN_VALUE) {
047 //See Errata E24
048 if (_value == 0) return "0";
049 else if (Double.isNaN(_value)) return "NaN";
050 else if (_value < 0 && Double.isInfinite(_value)) return "-Infinity";
051 else if (Double.isInfinite(_value)) return "Infinity";
052 else return formatNumbers((int)_value);
053 }
054 else {
055 int next = _node;
056 final int root = _document.getDocument();
057 result = 0;
058 while (next >= root && !matchesFrom(next)) {
059 if (matchesCount(next)) {
060 ++result;
061 }
062 next--;
063 //%HZ%: Is this the best way of finding the root? Is it better to check
064 //%HZ%: parent(next)?
065 /*
066 if (next == root) {
067 break;
068 }
069 else {
070 --next;
071 }
072 */
073 }
074 }
075 return formatNumbers(result);
076 }
077
078 public static NodeCounter getDefaultNodeCounter(Translet translet,
079 DOM document,
080 DTMAxisIterator iterator) {
081 return new DefaultAnyNodeCounter(translet, document, iterator);
082 }
083
084 static class DefaultAnyNodeCounter extends AnyNodeCounter {
085 public DefaultAnyNodeCounter(Translet translet,
086 DOM document, DTMAxisIterator iterator) {
087 super(translet, document, iterator);
088 }
089
090 public String getCounter() {
091 int result;
092 if (_value != Integer.MIN_VALUE) {
093 //See Errata E24
094 if (_value == 0) return "0";
095 else if (Double.isNaN(_value)) return "NaN";
096 else if (_value < 0 && Double.isInfinite(_value)) return "-Infinity";
097 else if (Double.isInfinite(_value)) return "Infinity";
098 else result = (int) _value;
099 }
100 else {
101 int next = _node;
102 result = 0;
103 final int ntype = _document.getExpandedTypeID(_node);
104 final int root = _document.getDocument();
105 while (next >= 0) {
106 if (ntype == _document.getExpandedTypeID(next)) {
107 result++;
108 }
109 //%HZ%: Is this the best way of finding the root? Is it better to check
110 //%HZ%: parent(next)?
111 if (next == root) {
112 break;
113 }
114 else {
115 --next;
116 }
117 }
118 }
119 return formatNumbers(result);
120 }
121 }
122 }