| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.pattern; |
| 9 | |
| 10 | import net.sf.saxon.expr.XPathContext; |
| 11 | import net.sf.saxon.expr.parser.ExpressionTool; |
| 12 | import net.sf.saxon.expr.parser.RebindingMap; |
| 13 | import net.sf.saxon.om.Item; |
| 14 | import net.sf.saxon.om.NodeInfo; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.type.ItemType; |
| 17 | import net.sf.saxon.type.Type; |
| 18 | import net.sf.saxon.type.UType; |
| 19 | |
| 20 | import java.util.HashSet; |
| 21 | import java.util.Set; |
| 22 | |
| 23 | /** |
| 24 | * A pattern formed as the union (or) of two other patterns |
| 25 | */ |
| 26 | |
| 27 | public class UnionPattern extends VennPattern { |
| 28 | |
| 29 | /** |
| 30 | * Constructor |
| 31 | * |
| 32 | * @param p1 the left-hand operand |
| 33 | * @param p2 the right-hand operand |
| 34 | */ |
| 35 | |
| 36 | public UnionPattern(Pattern p1, Pattern p2) { |
| 37 | super(p1, p2); |
| 38 | // default is to take the priority from the component patterns |
| 39 | setPriority(Double.NaN); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get an ItemType that all the items matching this pattern must satisfy |
| 44 | */ |
| 45 | @Override |
| 46 | public ItemType getItemType() { |
| 47 | ItemType t1 = p1.getItemType(); |
| 48 | ItemType t2 = p2.getItemType(); |
| 49 | return Type.getCommonSuperType(t1, t2); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get a UType indicating which kinds of items this Pattern can match. |
| 54 | * |
| 55 | * @return a UType indicating all the primitive types of item that the pattern can match. |
| 56 | */ |
| 57 | @Override |
| 58 | public UType getUType() { |
| 59 | return p1.getUType().union(p2.getUType()); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Determine if the supplied node matches the pattern |
| 64 | * |
| 65 | * @param item the node to be compared |
| 66 | * @return true if the node matches either of the operand patterns |
| 67 | */ |
| 68 | |
| 69 | public boolean matches(Item<?> item, XPathContext context) throws XPathException { |
| 70 | return p1.matches(item, context) || p2.matches(item, context); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Determine whether this pattern matches a given Node within the subtree rooted at a given |
| 75 | * anchor node. This method is used when the pattern is used for streaming. |
| 76 | * |
| 77 | * @param node The NodeInfo representing the Element or other node to be tested against the Pattern |
| 78 | * @param anchor The anchor node, which must match any AnchorPattern subpattern |
| 79 | * @param context The dynamic context. Only relevant if the pattern |
| 80 | * uses variables, or contains calls on functions such as document() or key(). |
| 81 | * @return true if the node matches the Pattern, false otherwise |
| 82 | */ |
| 83 | |
| 84 | public boolean matchesBeneathAnchor(NodeInfo node, NodeInfo anchor, XPathContext context) throws XPathException { |
| 85 | return p1.matchesBeneathAnchor(node, anchor, context) || |
| 86 | p2.matchesBeneathAnchor(node, anchor, context); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Convert the pattern to a typed pattern, in which an element name is treated as |
| 91 | * schema-element(N) |
| 92 | * |
| 93 | * @param val either "strict" or "lax" depending on the value of xsl:mode/@typed |
| 94 | * @return either the original pattern unchanged, or a new pattern as the result of the |
| 95 | * conversion |
| 96 | * @throws net.sf.saxon.trans.XPathException if the pattern cannot be converted |
| 97 | */ |
| 98 | @Override |
| 99 | public Pattern convertToTypedPattern(String val) throws XPathException { |
| 100 | Pattern np1 = p1.convertToTypedPattern(val); |
| 101 | Pattern np2 = p2.convertToTypedPattern(val); |
| 102 | if (p1 == np1 && p2 == np2) { |
| 103 | return this; |
| 104 | } else { |
| 105 | return new UnionPattern(np1, np2); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Determine whether this pattern is the same as another pattern |
| 111 | * |
| 112 | * @param other the other object |
| 113 | */ |
| 114 | |
| 115 | public boolean equals(/*@NotNull*/ Object other) { |
| 116 | if (other instanceof UnionPattern) { |
| 117 | Set<Pattern> s0 = new HashSet<>(10); |
| 118 | gatherComponentPatterns(s0); |
| 119 | Set<Pattern> s1 = new HashSet<>(10); |
| 120 | ((UnionPattern) other).gatherComponentPatterns(s1); |
| 121 | return s0.equals(s1); |
| 122 | } else { |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Hashcode supporting equals() |
| 129 | */ |
| 130 | |
| 131 | public int computeHashCode() { |
| 132 | return 0x9bd723a6 ^ p1.hashCode() ^ p2.hashCode(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get the relevant operator: "union", "intersect", or "except" |
| 137 | * |
| 138 | * @return the operator, as a string |
| 139 | */ |
| 140 | @Override |
| 141 | protected String getOperatorName() { |
| 142 | return "union"; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Copy a pattern. This makes a deep copy. |
| 147 | * |
| 148 | * @return the copy of the original pattern |
| 149 | * @param rebindings variables that need to be re-bound |
| 150 | */ |
| 151 | |
| 152 | /*@NotNull*/ |
| 153 | public Pattern copy(RebindingMap rebindings) { |
| 154 | UnionPattern n = new UnionPattern(p1.copy(rebindings), p2.copy(rebindings)); |
| 155 | ExpressionTool.copyLocationInfo(this, n); |
| 156 | return n; |
| 157 | } |
| 158 | |
| 159 | } |
| 160 |