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: AnyNodeCounter.java,v 1.4 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 AnyNodeCounter extends NodeCounter {
  28. public AnyNodeCounter(Translet translet,
  29. DOM document, DTMAxisIterator iterator) {
  30. super(translet, document, iterator);
  31. }
  32. public NodeCounter setStartNode(int node) {
  33. _node = node;
  34. _nodeType = _document.getExpandedTypeID(node);
  35. return this;
  36. }
  37. public String getCounter() {
  38. int result;
  39. if (_value != Integer.MIN_VALUE) {
  40. result = _value;
  41. }
  42. else {
  43. int next = _node;
  44. final int root = _document.getDocument();
  45. result = 0;
  46. while (next >= root && !matchesFrom(next)) {
  47. if (matchesCount(next)) {
  48. ++result;
  49. }
  50. next--;
  51. //%HZ%: Is this the best way of finding the root? Is it better to check
  52. //%HZ%: parent(next)?
  53. /*
  54. if (next == root) {
  55. break;
  56. }
  57. else {
  58. --next;
  59. }
  60. */
  61. }
  62. }
  63. return formatNumbers(result);
  64. }
  65. public static NodeCounter getDefaultNodeCounter(Translet translet,
  66. DOM document,
  67. DTMAxisIterator iterator) {
  68. return new DefaultAnyNodeCounter(translet, document, iterator);
  69. }
  70. static class DefaultAnyNodeCounter extends AnyNodeCounter {
  71. public DefaultAnyNodeCounter(Translet translet,
  72. DOM document, DTMAxisIterator iterator) {
  73. super(translet, document, iterator);
  74. }
  75. public String getCounter() {
  76. int result;
  77. if (_value != Integer.MIN_VALUE) {
  78. result = _value;
  79. }
  80. else {
  81. int next = _node;
  82. result = 0;
  83. final int ntype = _document.getExpandedTypeID(_node);
  84. final int root = _document.getDocument();
  85. while (next >= 0) {
  86. if (ntype == _document.getExpandedTypeID(next)) {
  87. result++;
  88. }
  89. //%HZ%: Is this the best way of finding the root? Is it better to check
  90. //%HZ%: parent(next)?
  91. if (next == root) {
  92. break;
  93. }
  94. else {
  95. --next;
  96. }
  97. }
  98. }
  99. return formatNumbers(result);
  100. }
  101. }
  102. }