src/java.xml/share/classes/com/sun/org/apache/xpath/internal/patterns/UnionPattern.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.patterns;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
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.Expression;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.xpath.internal.ExpressionOwner;
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.XPathVisitor;
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
 * This class represents a union pattern, which can have multiple individual
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * StepPattern patterns.
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * @xsl.usage advanced
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
public class UnionPattern extends Expression
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
    static final long serialVersionUID = -6670449967116905820L;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
  /** Array of the contained step patterns to be tested.
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
   *  @serial  */
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
  private StepPattern[] m_patterns;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
   * No arguments to process, so this does nothing.
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
   */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    48
  public void fixupVariables(List<QName> vars, int globalsSize)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    for (int i = 0; i < m_patterns.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
      m_patterns[i].fixupVariables(vars, globalsSize);
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   * Tell if this expression or it's subexpressions can traverse outside
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
   * the current subtree.
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
   * @return true if traversal outside the context node's subtree can occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
   public boolean canTraverseOutsideSubtree()
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
   {
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
     if(null != m_patterns)
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
     {
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
      int n = m_patterns.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
      for (int i = 0; i < n; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
        if(m_patterns[i].canTraverseOutsideSubtree())
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
          return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
     }
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
     return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
   }
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   * Set the contained step patterns to be tested.
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   * @param patterns the contained step patterns to be tested.
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
  public void setPatterns(StepPattern[] patterns)
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    m_patterns = patterns;
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    if(null != patterns)
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
        for(int i = 0; i < patterns.length; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
                patterns[i].exprSetParent(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
   * Get the contained step patterns to be tested.
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
   * @return an array of the contained step patterns to be tested.
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
  public StepPattern[] getPatterns()
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    return m_patterns;
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
   * Test a node to see if it matches any of the patterns in the union.
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
   * @param xctxt XPath runtime context.
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
   * @return {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NODETEST},
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
   *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NONE},
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_NSWILD},
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_QNAME}, or
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   *         {@link com.sun.org.apache.xpath.internal.patterns.NodeTest#SCORE_OTHER}.
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
   * @throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
  public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    XObject bestScore = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    int n = m_patterns.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    for (int i = 0; i < n; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
      XObject score = m_patterns[i].execute(xctxt);
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
      if (score != NodeTest.SCORE_NONE)
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        if (null == bestScore)
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
          bestScore = score;
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        else if (score.num() > bestScore.num())
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
          bestScore = score;
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    if (null == bestScore)
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
      bestScore = NodeTest.SCORE_NONE;
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    return bestScore;
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
  class UnionPathPartOwner implements ExpressionOwner
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
        int m_index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
        UnionPathPartOwner(int index)
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
                m_index = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
     * @see ExpressionOwner#getExpression()
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
    public Expression getExpression()
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
      return m_patterns[m_index];
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
     * @see ExpressionOwner#setExpression(Expression)
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
    public void setExpression(Expression exp)
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        exp.exprSetParent(UnionPattern.this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
        m_patterns[m_index] = (StepPattern)exp;
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
   * @see com.sun.org.apache.xpath.internal.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
  public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
        visitor.visitUnionPattern(owner, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
        if(null != m_patterns)
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
                int n = m_patterns.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
                for(int i = 0; i < n; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
                        m_patterns[i].callVisitors(new UnionPathPartOwner(i), visitor);
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
   * @see Expression#deepEquals(Expression)
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
  public boolean deepEquals(Expression expr)
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
        if(!isSameClass(expr))
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
                return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
        UnionPattern up = (UnionPattern)expr;
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
        if(null != m_patterns)
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
                int n = m_patterns.length;
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
                if((null == up.m_patterns) || (up.m_patterns.length != n))
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
                        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
                for(int i = 0; i < n; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
                        if(!m_patterns[i].deepEquals(up.m_patterns[i]))
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
                                return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
        else if(up.m_patterns != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
                return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
}