| 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.parser.ExpressionTool; |
| 12 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 13 |
import net.sf.saxon.om.AxisInfo; |
| 14 |
import net.sf.saxon.om.Item; |
| 15 |
import net.sf.saxon.om.SequenceIterator; |
| 16 |
import net.sf.saxon.pattern.*; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.ItemType; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public final class FirstItemExpression extends SingleItemFilter { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private FirstItemExpression(Expression base) { |
| 35 |
super(base); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static Expression makeFirstItemExpression(Expression base) { |
| 46 |
if (base instanceof FirstItemExpression) { |
| 47 |
return base; |
| 48 |
} else { |
| 49 |
return new FirstItemExpression(base); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public Expression copy(RebindingMap rebindings) { |
| 62 |
Expression e2 = new FirstItemExpression(getBaseExpression().copy(rebindings)); |
| 63 |
ExpressionTool.copyLocationInfo(this, e2); |
| 64 |
return e2; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
@Override |
| 76 |
public Pattern toPattern(Configuration config) throws XPathException { |
| 77 |
Pattern basePattern = getBaseExpression().toPattern(config); |
| 78 |
ItemType type = basePattern.getItemType(); |
| 79 |
if (type instanceof NodeTest) { |
| 80 |
Expression baseExpr = getBaseExpression(); |
| 81 |
if (baseExpr instanceof AxisExpression && |
| 82 |
((AxisExpression)baseExpr).getAxis() == AxisInfo.CHILD && basePattern instanceof NodeTestPattern) { |
| 83 |
return new SimplePositionalPattern((NodeTest) type, 1); |
| 84 |
} else { |
| 85 |
return new GeneralNodePattern(this, (NodeTest) type); |
| 86 |
} |
| 87 |
} else { |
| 88 |
|
| 89 |
return basePattern; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@Override |
| 103 |
public int getImplementationMethod() { |
| 104 |
return EVALUATE_METHOD; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public Item evaluateItem(XPathContext context) throws XPathException { |
| 112 |
SequenceIterator iter = getBaseExpression().iterate(context); |
| 113 |
Item result = iter.next(); |
| 114 |
iter.close(); |
| 115 |
return result; |
| 116 |
} |
| 117 |
|
| 118 |
public String getExpressionName() { |
| 119 |
return "first"; |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public String toShortString() { |
| 124 |
return getBaseExpression().toShortString() + "[1]"; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
@Override |
| 134 |
public String getStreamerName() { |
| 135 |
return "FirstItemExpression"; |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|