1. /*
  2. * Copyright 1999-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: FuncLast.java,v 1.15 2004/02/17 04:34:01 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.functions;
  20. import com.sun.org.apache.xml.internal.dtm.DTMIterator;
  21. import com.sun.org.apache.xpath.internal.XPathContext;
  22. import com.sun.org.apache.xpath.internal.axes.SubContextList;
  23. import com.sun.org.apache.xpath.internal.compiler.Compiler;
  24. import com.sun.org.apache.xpath.internal.objects.XNumber;
  25. import com.sun.org.apache.xpath.internal.objects.XObject;
  26. /**
  27. * Execute the Last() function.
  28. * @xsl.usage advanced
  29. */
  30. public class FuncLast extends Function
  31. {
  32. private boolean m_isTopLevel;
  33. /**
  34. * Figure out if we're executing a toplevel expression.
  35. * If so, we can't be inside of a predicate.
  36. */
  37. public void postCompileStep(Compiler compiler)
  38. {
  39. m_isTopLevel = compiler.getLocationPathDepth() == -1;
  40. }
  41. /**
  42. * Get the position in the current context node list.
  43. *
  44. * @param xctxt non-null reference to XPath runtime context.
  45. *
  46. * @return The number of nodes in the list.
  47. *
  48. * @throws javax.xml.transform.TransformerException
  49. */
  50. public int getCountOfContextNodeList(XPathContext xctxt)
  51. throws javax.xml.transform.TransformerException
  52. {
  53. // assert(null != m_contextNodeList, "m_contextNodeList must be non-null");
  54. // If we're in a predicate, then this will return non-null.
  55. SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList();
  56. // System.out.println("iter: "+iter);
  57. if (null != iter)
  58. return iter.getLastPos(xctxt);
  59. DTMIterator cnl = xctxt.getContextNodeList();
  60. int count;
  61. if(null != cnl)
  62. {
  63. count = cnl.getLength();
  64. // System.out.println("count: "+count);
  65. }
  66. else
  67. count = 0;
  68. return count;
  69. }
  70. /**
  71. * Execute the function. The function must return
  72. * a valid object.
  73. * @param xctxt The current execution context.
  74. * @return A valid XObject.
  75. *
  76. * @throws javax.xml.transform.TransformerException
  77. */
  78. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  79. {
  80. XNumber xnum = new XNumber((double) getCountOfContextNodeList(xctxt));
  81. // System.out.println("last: "+xnum.num());
  82. return xnum;
  83. }
  84. /**
  85. * No arguments to process, so this does nothing.
  86. */
  87. public void fixupVariables(java.util.Vector vars, int globalsSize)
  88. {
  89. // no-op
  90. }
  91. }