| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.ma.arrays; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.oper.OperandArray; |
| 12 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 16 |
import net.sf.saxon.om.GroundedValue; |
| 17 |
import net.sf.saxon.om.Item; |
| 18 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.type.*; |
| 21 |
import net.sf.saxon.value.Cardinality; |
| 22 |
import net.sf.saxon.value.SequenceType; |
| 23 |
|
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class SquareArrayConstructor extends Expression { |
| 34 |
|
| 35 |
|
| 36 |
private OperandArray operanda; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public SquareArrayConstructor(List<Expression> children) { |
| 45 |
Expression[] kids = children.toArray(new Expression[0]); |
| 46 |
for (Expression e : children) { |
| 47 |
adoptChildExpression(e); |
| 48 |
} |
| 49 |
setOperanda(new OperandArray(this, kids, OperandRole.NAVIGATE)); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
protected void setOperanda(OperandArray operanda) { |
| 60 |
this.operanda = operanda; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public OperandArray getOperanda() { |
| 70 |
return operanda; |
| 71 |
} |
| 72 |
|
| 73 |
public Operand getOperand(int i) { |
| 74 |
return operanda.getOperand(i); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public Iterable<Operand> operands() { |
| 79 |
return operanda.operands(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public String getExpressionName() { |
| 84 |
return "SquareArrayConstructor"; |
| 85 |
} |
| 86 |
|
| 87 |
public String getStreamerName() { |
| 88 |
return "ArrayBlock"; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
public int computeSpecialProperties() { |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@Override |
| 103 |
public boolean equals(Object other) { |
| 104 |
if (!(other instanceof SquareArrayConstructor)) { |
| 105 |
return false; |
| 106 |
} else { |
| 107 |
SquareArrayConstructor ab2 = (SquareArrayConstructor) other; |
| 108 |
if (ab2.getOperanda().getNumberOfOperands() != getOperanda().getNumberOfOperands()) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
for (int i = 0; i < getOperanda().getNumberOfOperands(); i++) { |
| 112 |
if (!getOperanda().getOperand(i).equals(ab2.getOperanda().getOperand(i))) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
} |
| 116 |
return true; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public int computeHashCode() { |
| 125 |
int h = 0x878b92a0; |
| 126 |
for (Operand o : operands()) { |
| 127 |
h ^= o.getChildExpression().hashCode(); |
| 128 |
} |
| 129 |
return h; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 134 |
Expression e = super.typeCheck(visitor, contextInfo); |
| 135 |
if (e != this) { |
| 136 |
return e; |
| 137 |
} |
| 138 |
return preEvaluate(visitor); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 143 |
Expression e = super.optimize(visitor, contextInfo); |
| 144 |
if (e != this) { |
| 145 |
return e; |
| 146 |
} |
| 147 |
return preEvaluate(visitor); |
| 148 |
} |
| 149 |
|
| 150 |
private Expression preEvaluate(ExpressionVisitor visitor) { |
| 151 |
boolean allFixed = false; |
| 152 |
for (Operand o : operands()) { |
| 153 |
if (!(o.getChildExpression() instanceof Literal)) { |
| 154 |
return this; |
| 155 |
} |
| 156 |
} |
| 157 |
try { |
| 158 |
return Literal.makeLiteral(evaluateItem(visitor.makeDynamicContext()), this); |
| 159 |
} catch (XPathException e) { |
| 160 |
return this; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public Expression copy(RebindingMap rebindings) { |
| 173 |
List<Expression> m2 = new ArrayList<>(getOperanda().getNumberOfOperands()); |
| 174 |
for (Operand o : operands()) { |
| 175 |
m2.add(o.getChildExpression().copy(rebindings)); |
| 176 |
} |
| 177 |
SquareArrayConstructor b2 = new SquareArrayConstructor(m2); |
| 178 |
ExpressionTool.copyLocationInfo(this, b2); |
| 179 |
return b2; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public final ItemType getItemType() { |
| 190 |
ItemType contentType = null; |
| 191 |
int contentCardinality = StaticProperty.EXACTLY_ONE; |
| 192 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 193 |
for (Expression e : getOperanda().operandExpressions()) { |
| 194 |
if (contentType == null) { |
| 195 |
contentType = e.getItemType(); |
| 196 |
contentCardinality = e.getCardinality(); |
| 197 |
} else { |
| 198 |
contentType = Type.getCommonSuperType(contentType, e.getItemType(), th); |
| 199 |
contentCardinality = Cardinality.union(contentCardinality, e.getCardinality()); |
| 200 |
} |
| 201 |
} |
| 202 |
if (contentType == null) { |
| 203 |
contentType = ErrorType.getInstance(); |
| 204 |
} |
| 205 |
return new ArrayItemType(SequenceType.makeSequenceType(contentType, contentCardinality)); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
@Override |
| 216 |
public UType getStaticUType(UType contextItemType) { |
| 217 |
return UType.FUNCTION; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public final int computeCardinality() { |
| 225 |
|
| 226 |
return StaticProperty.EXACTLY_ONE; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public void export(ExpressionPresenter out) throws XPathException { |
| 235 |
out.startElement("arrayBlock", this); |
| 236 |
for (Operand o : operands()) { |
| 237 |
o.getChildExpression().export(out); |
| 238 |
} |
| 239 |
out.endElement(); |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public String toShortString() { |
| 244 |
int n = getOperanda().getNumberOfOperands(); |
| 245 |
switch (n) { |
| 246 |
case 0: |
| 247 |
return "[]"; |
| 248 |
case 1: |
| 249 |
return "[" + getOperanda().getOperand(0).getChildExpression().toShortString() + "]"; |
| 250 |
case 2: |
| 251 |
return "[" + getOperanda().getOperand(0).getChildExpression().toShortString() + ", " + |
| 252 |
getOperanda().getOperand(1).getChildExpression().toShortString() + "]"; |
| 253 |
default: |
| 254 |
return "[" + getOperanda().getOperand(0).getChildExpression().toShortString() + ", ...]"; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public int getImplementationMethod() { |
| 265 |
return EVALUATE_METHOD; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
@Override |
| 283 |
public Item evaluateItem(XPathContext context) throws XPathException { |
| 284 |
List<GroundedValue<?>> value = new ArrayList<>(getOperanda().getNumberOfOperands()); |
| 285 |
for (Operand o : operands()) { |
| 286 |
GroundedValue<?> s = ExpressionTool.eagerEvaluate(o.getChildExpression(), context); |
| 287 |
value.add(s); |
| 288 |
} |
| 289 |
return new SimpleArrayItem(value); |
| 290 |
} |
| 291 |
|
| 292 |
} |