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