1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: SingleNodeCounter.java,v 1.5 2004/02/16 22:54:59 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.dom;
  20. import com.sun.org.apache.xalan.internal.xsltc.DOM;
  21. import com.sun.org.apache.xalan.internal.xsltc.Translet;
  22. import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  23. /**
  24. * @author Jacek Ambroziak
  25. * @author Santiago Pericas-Geertsen
  26. */
  27. public abstract class SingleNodeCounter extends NodeCounter {
  28. static private final int[] EmptyArray = new int[] { };
  29. DTMAxisIterator _countSiblings = null;
  30. public SingleNodeCounter(Translet translet,
  31. DOM document,
  32. DTMAxisIterator iterator) {
  33. super(translet, document, iterator);
  34. }
  35. public NodeCounter setStartNode(int node) {
  36. _node = node;
  37. _nodeType = _document.getExpandedTypeID(node);
  38. _countSiblings = _document.getAxisIterator(PRECEDINGSIBLING);
  39. return this;
  40. }
  41. public String getCounter() {
  42. int result;
  43. if (_value != Integer.MIN_VALUE) {
  44. result = _value;
  45. }
  46. else {
  47. int next = _node;
  48. result = 0;
  49. if (!matchesCount(next)) {
  50. while ((next = _document.getParent(next)) > END) {
  51. if (matchesCount(next)) {
  52. break; // found target
  53. }
  54. if (matchesFrom(next)) {
  55. next = END;
  56. break; // no target found
  57. }
  58. }
  59. }
  60. if (next != END) {
  61. _countSiblings.setStartNode(next);
  62. do {
  63. if (matchesCount(next)) result++;
  64. } while ((next = _countSiblings.next()) != END);
  65. }
  66. else {
  67. // If no target found then pass the empty list
  68. return formatNumbers(EmptyArray);
  69. }
  70. }
  71. return formatNumbers(result);
  72. }
  73. public static NodeCounter getDefaultNodeCounter(Translet translet,
  74. DOM document,
  75. DTMAxisIterator iterator) {
  76. return new DefaultSingleNodeCounter(translet, document, iterator);
  77. }
  78. static class DefaultSingleNodeCounter extends SingleNodeCounter {
  79. public DefaultSingleNodeCounter(Translet translet,
  80. DOM document, DTMAxisIterator iterator) {
  81. super(translet, document, iterator);
  82. }
  83. public NodeCounter setStartNode(int node) {
  84. _node = node;
  85. _nodeType = _document.getExpandedTypeID(node);
  86. _countSiblings =
  87. _document.getTypedAxisIterator(PRECEDINGSIBLING,
  88. _document.getExpandedTypeID(node));
  89. return this;
  90. }
  91. public String getCounter() {
  92. int result;
  93. if (_value != Integer.MIN_VALUE) {
  94. result = _value;
  95. }
  96. else {
  97. int next;
  98. result = 1;
  99. _countSiblings.setStartNode(_node);
  100. while ((next = _countSiblings.next()) != END) {
  101. result++;
  102. }
  103. }
  104. return formatNumbers(result);
  105. }
  106. }
  107. }