| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.instruct.Block; |
| 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.ma.arrays.ArrayItem; |
| 17 |
import net.sf.saxon.ma.arrays.ArrayItemType; |
| 18 |
import net.sf.saxon.ma.arrays.SquareArrayConstructor; |
| 19 |
import net.sf.saxon.ma.map.KeyValuePair; |
| 20 |
import net.sf.saxon.ma.map.MapItem; |
| 21 |
import net.sf.saxon.ma.map.MapType; |
| 22 |
import net.sf.saxon.ma.map.TupleType; |
| 23 |
import net.sf.saxon.om.GroundedValue; |
| 24 |
import net.sf.saxon.om.Item; |
| 25 |
import net.sf.saxon.om.SequenceIterator; |
| 26 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 27 |
import net.sf.saxon.trans.XPathException; |
| 28 |
import net.sf.saxon.type.AnyItemType; |
| 29 |
import net.sf.saxon.type.ItemType; |
| 30 |
import net.sf.saxon.type.TypeHierarchy; |
| 31 |
import net.sf.saxon.type.UType; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.Iterator; |
| 35 |
import java.util.List; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class LookupAllExpression extends UnaryExpression { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public LookupAllExpression(Expression base) { |
| 51 |
super(base); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
protected OperandRole getOperandRole() { |
| 56 |
return OperandRole.INSPECT; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public final ItemType getItemType() { |
| 68 |
ItemType base = getBaseExpression().getItemType(); |
| 69 |
if (base instanceof MapType) { |
| 70 |
return ((MapType)base).getValueType().getPrimaryType(); |
| 71 |
} else if (base instanceof ArrayItemType) { |
| 72 |
return ((ArrayItemType) base).getMemberType().getPrimaryType(); |
| 73 |
} else { |
| 74 |
return AnyItemType.getInstance(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
@Override |
| 87 |
public UType getStaticUType(UType contextItemType) { |
| 88 |
return getItemType().getUType(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 97 |
|
| 98 |
Configuration config = visitor.getConfiguration(); |
| 99 |
TypeHierarchy th = config.getTypeHierarchy(); |
| 100 |
|
| 101 |
|
| 102 |
getOperand().typeCheck(visitor, contextInfo); |
| 103 |
|
| 104 |
ItemType containerType = getBaseExpression().getItemType(); |
| 105 |
boolean isArrayLookup = containerType instanceof ArrayItemType; |
| 106 |
boolean isMapLookup = containerType instanceof MapType || containerType instanceof TupleType; |
| 107 |
|
| 108 |
if (!isArrayLookup && !isMapLookup) { |
| 109 |
if (th.relationship(containerType, MapType.ANY_MAP_TYPE) == TypeHierarchy.DISJOINT && |
| 110 |
th.relationship(containerType, ArrayItemType.getInstance()) == TypeHierarchy.DISJOINT) { |
| 111 |
XPathException err = new XPathException("The left-hand operand of '?' must be a map or an array; the supplied expression is of type " + containerType, "XPTY0004"); |
| 112 |
err.setLocation(getLocation()); |
| 113 |
err.setIsTypeError(true); |
| 114 |
err.setFailingExpression(this); |
| 115 |
throw err; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if (getBaseExpression() instanceof Literal) { |
| 120 |
return new Literal(iterate(visitor.makeDynamicContext()).materialize()); |
| 121 |
} |
| 122 |
|
| 123 |
return this; |
| 124 |
} |
| 125 |
|
| 126 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 127 |
getOperand().optimize(visitor, contextItemType); |
| 128 |
|
| 129 |
if (getBaseExpression() instanceof Literal) { |
| 130 |
return new Literal(iterate(visitor.makeDynamicContext()).materialize()); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
if (getBaseExpression() instanceof SquareArrayConstructor) { |
| 136 |
List<Expression> children = new ArrayList<>(); |
| 137 |
for (Operand o : getBaseExpression().operands()) { |
| 138 |
children.add(o.getChildExpression().copy(new RebindingMap())); |
| 139 |
} |
| 140 |
Expression[] childExpressions = children.toArray(new Expression[0]); |
| 141 |
Block block = new Block(childExpressions); |
| 142 |
ExpressionTool.copyLocationInfo(this, block); |
| 143 |
return block; |
| 144 |
} |
| 145 |
return this; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
@Override |
| 158 |
public int getCost() { |
| 159 |
return getBaseExpression().getCost() + 1; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
@Override |
| 171 |
public int getImplementationMethod() { |
| 172 |
return ITERATE_METHOD; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public LookupAllExpression copy(RebindingMap rebindings) { |
| 184 |
return new LookupAllExpression(getBaseExpression().copy(rebindings)); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public int computeCardinality() { |
| 192 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public boolean equals(Object other) { |
| 200 |
if (!(other instanceof LookupAllExpression)) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
LookupAllExpression p = (LookupAllExpression) other; |
| 204 |
return getBaseExpression().isEqual(p.getBaseExpression()); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public int computeHashCode() { |
| 212 |
return "LookupAll".hashCode() ^ getBaseExpression().hashCode(); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public SequenceIterator<?> iterate(final XPathContext context) throws XPathException { |
| 223 |
|
| 224 |
return new SequenceIterator() { |
| 225 |
|
| 226 |
SequenceIterator level0 = getBaseExpression().iterate(context); |
| 227 |
Iterator<?> level1 = null; |
| 228 |
|
| 229 |
SequenceIterator level2 = null; |
| 230 |
|
| 231 |
@Override |
| 232 |
public Item next() throws XPathException { |
| 233 |
if (level2 == null) { |
| 234 |
if (level1 == null) { |
| 235 |
Item base = level0.next(); |
| 236 |
if (base == null) { |
| 237 |
return null; |
| 238 |
} else if (base instanceof ArrayItem) { |
| 239 |
level1 = ((ArrayItem)base).members().iterator(); |
| 240 |
return next(); |
| 241 |
} else if (base instanceof MapItem) { |
| 242 |
level1 = ((MapItem)base).keyValuePairs().iterator(); |
| 243 |
return next(); |
| 244 |
} else { |
| 245 |
LookupExpression.mustBeArrayOrMap(LookupAllExpression.this, base); |
| 246 |
return null; |
| 247 |
} |
| 248 |
} else { |
| 249 |
if (level1.hasNext()) { |
| 250 |
Object nextEntry = level1.next(); |
| 251 |
if (nextEntry instanceof KeyValuePair) { |
| 252 |
GroundedValue value = ((KeyValuePair)nextEntry).value; |
| 253 |
level2 = value.iterate(); |
| 254 |
} else if (nextEntry instanceof GroundedValue) { |
| 255 |
level2 = ((GroundedValue) nextEntry).iterate(); |
| 256 |
} else { |
| 257 |
throw new IllegalStateException(); |
| 258 |
} |
| 259 |
return next(); |
| 260 |
} else { |
| 261 |
level1 = null; |
| 262 |
return next(); |
| 263 |
} |
| 264 |
} |
| 265 |
} else { |
| 266 |
Item next = level2.next(); |
| 267 |
if (next == null) { |
| 268 |
level2 = null; |
| 269 |
return next(); |
| 270 |
} else { |
| 271 |
return next; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
public void close() { |
| 278 |
if (level0 != null) { |
| 279 |
level0.close(); |
| 280 |
} |
| 281 |
if (level2 != null) { |
| 282 |
level2.close(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
}; |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public void export(ExpressionPresenter destination) throws XPathException { |
| 296 |
destination.startElement("lookupAll", this); |
| 297 |
getBaseExpression().export(destination); |
| 298 |
destination.endElement(); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public String toString() { |
| 310 |
return ExpressionTool.parenthesize(getBaseExpression()) + "?*"; |
| 311 |
} |
| 312 |
|
| 313 |
public String toShortString() { |
| 314 |
return getBaseExpression().toShortString() + "?*"; |
| 315 |
} |
| 316 |
|
| 317 |
} |
| 318 |
|