286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001-2005 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: SingleNodeCounter.java,v 1.2.4.1 2005/09/12 11:58:23 pvedula Exp $
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xalan.internal.xsltc.dom;
286N/A
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.DOM;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.Translet;
286N/Aimport com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
286N/Aimport com.sun.org.apache.xml.internal.dtm.Axis;
286N/A
286N/A
286N/A/**
286N/A * @author Jacek Ambroziak
286N/A * @author Santiago Pericas-Geertsen
286N/A */
286N/Apublic abstract class SingleNodeCounter extends NodeCounter {
286N/A static private final int[] EmptyArray = new int[] { };
286N/A DTMAxisIterator _countSiblings = null;
286N/A
286N/A public SingleNodeCounter(Translet translet,
286N/A DOM document,
286N/A DTMAxisIterator iterator) {
286N/A super(translet, document, iterator);
286N/A }
286N/A
286N/A public SingleNodeCounter(Translet translet,
286N/A DOM document,
286N/A DTMAxisIterator iterator,
286N/A boolean hasFrom) {
286N/A super(translet, document, iterator, hasFrom);
286N/A }
286N/A
286N/A public NodeCounter setStartNode(int node) {
286N/A _node = node;
286N/A _nodeType = _document.getExpandedTypeID(node);
286N/A _countSiblings = _document.getAxisIterator(Axis.PRECEDINGSIBLING);
286N/A return this;
286N/A }
286N/A
286N/A public String getCounter() {
286N/A int result;
286N/A if (_value != Integer.MIN_VALUE) {
286N/A //See Errata E24
286N/A if (_value == 0) return "0";
286N/A else if (Double.isNaN(_value)) return "NaN";
286N/A else if (_value < 0 && Double.isInfinite(_value)) return "-Infinity";
286N/A else if (Double.isInfinite(_value)) return "Infinity";
286N/A else result = (int) _value;
286N/A }
286N/A else {
286N/A int next = _node;
286N/A result = 0;
286N/A boolean matchesCount = matchesCount(next);
286N/A
286N/A if (!matchesCount) {
286N/A while ((next = _document.getParent(next)) > END) {
286N/A if (matchesCount(next)) {
286N/A break; // found target
286N/A }
286N/A if (matchesFrom(next)) {
286N/A next = END;
286N/A break; // no target found
286N/A }
286N/A }
286N/A }
286N/A
286N/A if (next != END) {
286N/A int from = next;
286N/A
286N/A if (!matchesCount && _hasFrom) {
286N/A // Target found, but need to check if ancestor matches from
286N/A while ((from = _document.getParent(from)) > END) {
286N/A if (matchesFrom(from)) {
286N/A break; // found from
286N/A }
286N/A }
286N/A }
286N/A
286N/A // Have we found ancestor matching from?
286N/A if (from != END) {
286N/A _countSiblings.setStartNode(next);
286N/A do {
286N/A if (matchesCount(next)) result++;
286N/A } while ((next = _countSiblings.next()) != END);
286N/A
286N/A return formatNumbers(result);
286N/A }
286N/A }
286N/A
286N/A // If no target found then pass the empty list
286N/A return formatNumbers(EmptyArray);
286N/A }
286N/A return formatNumbers(result);
286N/A }
286N/A
286N/A public static NodeCounter getDefaultNodeCounter(Translet translet,
286N/A DOM document,
286N/A DTMAxisIterator iterator) {
286N/A return new DefaultSingleNodeCounter(translet, document, iterator);
286N/A }
286N/A
286N/A static class DefaultSingleNodeCounter extends SingleNodeCounter {
286N/A public DefaultSingleNodeCounter(Translet translet,
286N/A DOM document, DTMAxisIterator iterator) {
286N/A super(translet, document, iterator);
286N/A }
286N/A
286N/A public NodeCounter setStartNode(int node) {
286N/A _node = node;
286N/A _nodeType = _document.getExpandedTypeID(node);
286N/A _countSiblings =
286N/A _document.getTypedAxisIterator(Axis.PRECEDINGSIBLING,
286N/A _document.getExpandedTypeID(node));
286N/A return this;
286N/A }
286N/A
286N/A public String getCounter() {
286N/A int result;
286N/A if (_value != Integer.MIN_VALUE) {
286N/A //See Errata E24
286N/A if (_value == 0) return "0";
286N/A else if (Double.isNaN(_value)) return "NaN";
286N/A else if (_value < 0 && Double.isInfinite(_value)) return "-Infinity";
286N/A else if (Double.isInfinite(_value)) return "Infinity";
286N/A else result = (int) _value;
286N/A }
286N/A else {
286N/A int next;
286N/A result = 1;
286N/A _countSiblings.setStartNode(_node);
286N/A while ((next = _countSiblings.next()) != END) {
286N/A result++;
286N/A }
286N/A }
286N/A return formatNumbers(result);
286N/A }
286N/A }
286N/A}