src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncPosition.java
author joehw
Wed, 18 Oct 2017 13:25:49 -0700
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 48409 5ab69533994b
permissions -rw-r--r--
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked Reviewed-by: lancea, rriggs, mullan
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.
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     3
 * @LastModified: Oct 2017
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * 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
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * 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
    10
 * (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
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    21
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xpath.internal.functions;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import com.sun.org.apache.xml.internal.dtm.DTM;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
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
    26
import com.sun.org.apache.xml.internal.utils.QName;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.xpath.internal.XPathContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.xpath.internal.axes.SubContextList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xpath.internal.compiler.Compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xpath.internal.objects.XNumber;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
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
    32
import java.util.List;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * Execute the Position() function.
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * @xsl.usage advanced
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
public class FuncPosition extends Function
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
    static final long serialVersionUID = -9092846348197271582L;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
  private boolean m_isTopLevel;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
   * Figure out if we're executing a toplevel expression.
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
   * If so, we can't be inside of a predicate.
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
  public void postCompileStep(Compiler compiler)
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
    m_isTopLevel = compiler.getLocationPathDepth() == -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
   * Get the position in the current context node list.
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
   * @param xctxt Runtime XPath context.
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
   * @return The current position of the itteration in the context node list,
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   *         or -1 if there is no active context node list.
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
  public int getPositionInContextNodeList(XPathContext xctxt)
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    // System.out.println("FuncPosition- entry");
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    // If we're in a predicate, then this will return non-null.
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    if (null != iter)
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
      int prox = iter.getProximityPosition(xctxt);
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
      // System.out.println("FuncPosition- prox: "+prox);
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
      return prox;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    DTMIterator cnl = xctxt.getContextNodeList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    if (null != cnl)
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
      int n = cnl.getCurrentNode();
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
      if(n == DTM.NULL)
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
        if(cnl.getCurrentPos() == 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
          return 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
        // Then I think we're in a sort.  See sort21.xsl. So the iterator has
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
        // already been spent, and is not on the node we're processing.
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        // It's highly possible that this is an issue for other context-list
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        // functions.  Shouldn't be a problem for last(), and it shouldn't be
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        // a problem for current().
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
          cnl = cnl.cloneWithReset();
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        catch(CloneNotSupportedException cnse)
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
          throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(cnse);
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        int currentNode = xctxt.getContextNode();
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        // System.out.println("currentNode: "+currentNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
        while(DTM.NULL != (n = cnl.nextNode()))
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
          if(n == currentNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
      // System.out.println("n: "+n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
      // System.out.println("FuncPosition- cnl.getCurrentPos(): "+cnl.getCurrentPos());
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
      return cnl.getCurrentPos();
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
    // System.out.println("FuncPosition - out of guesses: -1");
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    return -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   * Execute the function.  The function must return
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   * a valid object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
   * @param xctxt The current execution context.
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   * @return A valid XObject.
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
   * @throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
  public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
    double pos = (double) getPositionInContextNodeList(xctxt);
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
    return new XNumber(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
   * No arguments to process, so this does nothing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
   */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   133
  public void fixupVariables(List<QName> vars, int globalsSize)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    // no-op
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
}