| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.pattern; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 12 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 14 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.type.ItemType; |
| 17 |
import net.sf.saxon.value.SequenceType; |
| 18 |
|
| 19 |
import java.util.HashSet; |
| 20 |
import java.util.Set; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public abstract class VennPattern extends Pattern { |
| 28 |
|
| 29 |
protected Pattern p1, p2; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public VennPattern(Pattern p1, Pattern p2) { |
| 39 |
this.p1 = p1; |
| 40 |
this.p2 = p2; |
| 41 |
adoptChildExpression(p1); |
| 42 |
adoptChildExpression(p2); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
@Override |
| 56 |
public Iterable<Operand> operands() { |
| 57 |
return operandList( |
| 58 |
new Operand(this, p1, OperandRole.SAME_FOCUS_ACTION), |
| 59 |
new Operand(this, p2, OperandRole.SAME_FOCUS_ACTION)); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public Pattern simplify() throws XPathException { |
| 68 |
p1 = p1.simplify(); |
| 69 |
p2 = p2.simplify(); |
| 70 |
return this; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 81 |
mustBeNodePattern(p1); |
| 82 |
p1 = p1.typeCheck(visitor, contextItemType); |
| 83 |
mustBeNodePattern(p2); |
| 84 |
p2 = p2.typeCheck(visitor, contextItemType); |
| 85 |
return this; |
| 86 |
} |
| 87 |
|
| 88 |
private void mustBeNodePattern(Pattern p) throws XPathException { |
| 89 |
if (p instanceof NodeTestPattern) { |
| 90 |
ItemType it = p.getItemType(); |
| 91 |
if (!(it instanceof NodeTest)) { |
| 92 |
XPathException err = new XPathException("The operands of a union, intersect, or except pattern " + |
| 93 |
"must be patterns that match nodes", "XPTY0004"); |
| 94 |
err.setIsTypeError(true); |
| 95 |
throw err; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
@Override |
| 104 |
public void bindCurrent(LocalBinding binding) { |
| 105 |
p1.bindCurrent(binding); |
| 106 |
p2.bindCurrent(binding); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@Override |
| 118 |
public boolean isMotionless() { |
| 119 |
return p1.isMotionless() && p2.isMotionless(); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public int allocateSlots(SlotManager slotManager, int nextFree) { |
| 130 |
nextFree = p1.allocateSlots(slotManager, nextFree); |
| 131 |
nextFree = p2.allocateSlots(slotManager, nextFree); |
| 132 |
return nextFree; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public void gatherComponentPatterns(Set<Pattern> set) { |
| 142 |
if (p1 instanceof VennPattern) { |
| 143 |
((VennPattern) p1).gatherComponentPatterns(set); |
| 144 |
} else { |
| 145 |
set.add(p1); |
| 146 |
} |
| 147 |
if (p2 instanceof VennPattern) { |
| 148 |
((VennPattern) p2).gatherComponentPatterns(set); |
| 149 |
} else { |
| 150 |
set.add(p2); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public int getDependencies() { |
| 163 |
return p1.getDependencies() | p2.getDependencies(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public Pattern getLHS() { |
| 173 |
return p1; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public Pattern getRHS() { |
| 183 |
return p2; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
@Override |
| 194 |
public boolean matchesCurrentGroup() { |
| 195 |
return p1.matchesCurrentGroup() || p2.matchesCurrentGroup(); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public boolean equals(Object other) { |
| 206 |
if (other instanceof VennPattern) { |
| 207 |
Set<Pattern> s0 = new HashSet<>(10); |
| 208 |
gatherComponentPatterns(s0); |
| 209 |
Set<Pattern> s1 = new HashSet<>(10); |
| 210 |
((VennPattern) other).gatherComponentPatterns(s1); |
| 211 |
return s0.equals(s1); |
| 212 |
} else { |
| 213 |
return false; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public int computeHashCode() { |
| 222 |
return 0x9bd723a6 ^ p1.hashCode() ^ p2.hashCode(); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
protected abstract String getOperatorName(); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
@Override |
| 236 |
public String toString() { |
| 237 |
return p1 + " " + getOperatorName() + " " + p2; |
| 238 |
} |
| 239 |
|
| 240 |
public void export(ExpressionPresenter presenter) throws XPathException{ |
| 241 |
ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) presenter.getOptions(); |
| 242 |
presenter.startElement("p.venn"); |
| 243 |
presenter.emitAttribute("op", getOperatorName()); |
| 244 |
if (options.addStaticType) { |
| 245 |
SequenceType type = SequenceType.makeSequenceType(getItemType(), StaticProperty.ALLOWS_ONE); |
| 246 |
presenter.emitAttribute("sType", type.toExportString2()); |
| 247 |
} |
| 248 |
p1.export(presenter); |
| 249 |
p2.export(presenter); |
| 250 |
presenter.endElement(); |
| 251 |
} |
| 252 |
} |