src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncLast.java
author joehw
Thu, 21 Dec 2017 09:29:24 -0800
changeset 48409 5ab69533994b
parent 47359 e1a6c0168741
permissions -rw-r--r--
8193568: @LastModified tag in license header Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     5
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    20
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
package com.sun.org.apache.xpath.internal.functions;
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
import com.sun.org.apache.xml.internal.dtm.DTMIterator;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    24
import com.sun.org.apache.xml.internal.utils.QName;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.xpath.internal.XPathContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.xpath.internal.axes.SubContextList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.xpath.internal.compiler.Compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.xpath.internal.objects.XNumber;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xpath.internal.objects.XObject;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    30
import java.util.List;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * Execute the Last() function.
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * @xsl.usage advanced
48409
5ab69533994b 8193568: @LastModified tag in license header
joehw
parents: 47359
diff changeset
    36
 * @LastModified: Oct 2017
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
public class FuncLast extends Function
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
    static final long serialVersionUID = 9205812403085432943L;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
  private boolean m_isTopLevel;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
   * Figure out if we're executing a toplevel expression.
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
   * If so, we can't be inside of a predicate.
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
  public void postCompileStep(Compiler compiler)
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    m_isTopLevel = compiler.getLocationPathDepth() == -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
   * Get the position in the current context node list.
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
   * @param xctxt non-null reference to XPath runtime context.
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   * @return The number of nodes in the list.
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
   * @throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
  public int getCountOfContextNodeList(XPathContext xctxt)
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
          throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    // assert(null != m_contextNodeList, "m_contextNodeList must be non-null");
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    // If we're in a predicate, then this will return non-null.
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    // System.out.println("iter: "+iter);
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    if (null != iter)
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
      return iter.getLastPos(xctxt);
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
    DTMIterator cnl = xctxt.getContextNodeList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    int count;
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    if(null != cnl)
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
      count = cnl.getLength();
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
      // System.out.println("count: "+count);
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
      count = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    return count;
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
   * Execute the function.  The function must return
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
   * a valid object.
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
   * @param xctxt The current execution context.
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
   * @return A valid XObject.
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
   * @throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    XNumber xnum = new XNumber((double) getCountOfContextNodeList(xctxt));
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    // System.out.println("last: "+xnum.num());
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    return xnum;
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
   * No arguments to process, so this does nothing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
   */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   104
  public void fixupVariables(List<QName> vars, int globalsSize)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    // no-op
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
}