src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncPosition.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44797 jaxp/src/java.xml/share/classes/com/sun/org/apache/xpath/internal/functions/FuncPosition.java@8b3b3b911b8a
child 47359 e1a6c0168741
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
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;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.xpath.internal.XPathContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.xpath.internal.axes.SubContextList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.xpath.internal.compiler.Compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xpath.internal.objects.XNumber;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xpath.internal.objects.XObject;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * Execute the Position() function.
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * @xsl.usage advanced
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
public class FuncPosition extends Function
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
    static final long serialVersionUID = -9092846348197271582L;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
  private boolean m_isTopLevel;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
   * Figure out if we're executing a toplevel expression.
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
   * If so, we can't be inside of a predicate.
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
  public void postCompileStep(Compiler compiler)
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    m_isTopLevel = compiler.getLocationPathDepth() == -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
   * Get the position in the current context node list.
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
   * @param xctxt Runtime XPath context.
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
   * @return The current position of the itteration in the context node list,
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
   *         or -1 if there is no active context node list.
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
  public int getPositionInContextNodeList(XPathContext xctxt)
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    // System.out.println("FuncPosition- entry");
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
    // If we're in a predicate, then this will return non-null.
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    if (null != iter)
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
      int prox = iter.getProximityPosition(xctxt);
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
      // System.out.println("FuncPosition- prox: "+prox);
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
      return prox;
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    DTMIterator cnl = xctxt.getContextNodeList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    if (null != cnl)
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
      int n = cnl.getCurrentNode();
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
      if(n == DTM.NULL)
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        if(cnl.getCurrentPos() == 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
          return 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
        // Then I think we're in a sort.  See sort21.xsl. So the iterator has
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
        // already been spent, and is not on the node we're processing.
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
        // It's highly possible that this is an issue for other context-list
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
        // functions.  Shouldn't be a problem for last(), and it shouldn't be
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
        // a problem for current().
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
          cnl = cnl.cloneWithReset();
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
        catch(CloneNotSupportedException cnse)
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
          throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(cnse);
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
        int currentNode = xctxt.getContextNode();
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        // System.out.println("currentNode: "+currentNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
        while(DTM.NULL != (n = cnl.nextNode()))
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
          if(n == currentNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
            break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
      // System.out.println("n: "+n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
      // System.out.println("FuncPosition- cnl.getCurrentPos(): "+cnl.getCurrentPos());
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
      return cnl.getCurrentPos();
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    // System.out.println("FuncPosition - out of guesses: -1");
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    return -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   * Execute the function.  The function must return
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   * a valid object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   * @param xctxt The current execution context.
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   * @return A valid XObject.
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   * @throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
  public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    double pos = (double) getPositionInContextNodeList(xctxt);
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
    return new XNumber(pos);
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
   * No arguments to process, so this does nothing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
  public void fixupVariables(java.util.Vector vars, int globalsSize)
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    // no-op
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
}