| 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.ContextItemStaticInfo; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 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.tree.jiter.MonoIterator; |
| 17 |
import net.sf.saxon.type.ItemType; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public abstract class UnaryExpression extends Expression { |
| 24 |
|
| 25 |
private Operand operand; |
| 26 |
|
| 27 |
public UnaryExpression(Expression p0) { |
| 28 |
operand = new Operand(this, p0, getOperandRole()); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
ExpressionTool.copyLocationInfo(p0, this); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
public Expression getBaseExpression() { |
| 37 |
return operand.getChildExpression(); |
| 38 |
} |
| 39 |
|
| 40 |
public void setBaseExpression(Expression child) { |
| 41 |
operand.setChildExpression(child); |
| 42 |
} |
| 43 |
|
| 44 |
public Operand getOperand() { |
| 45 |
return operand; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public Iterable<Operand> operands() { |
| 50 |
return () -> new MonoIterator<>(operand); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected abstract OperandRole getOperandRole(); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 67 |
operand.typeCheck(visitor, contextInfo); |
| 68 |
|
| 69 |
|
| 70 |
try { |
| 71 |
if (getBaseExpression() instanceof Literal) { |
| 72 |
Expression e2 = Literal.makeLiteral( |
| 73 |
iterate(visitor.getStaticContext().makeEarlyEvaluationContext()).materialize(), this); |
| 74 |
ExpressionTool.copyLocationInfo(this, e2); |
| 75 |
return e2; |
| 76 |
} |
| 77 |
|
| 78 |
} catch (Exception err) { |
| 79 |
|
| 80 |
|
| 81 |
} |
| 82 |
return this; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 102 |
operand.optimize(visitor, contextInfo); |
| 103 |
|
| 104 |
Expression base = getBaseExpression(); |
| 105 |
try { |
| 106 |
if (base instanceof Literal) { |
| 107 |
return Literal.makeLiteral( |
| 108 |
iterate(visitor.getStaticContext().makeEarlyEvaluationContext()).materialize(), this); |
| 109 |
} |
| 110 |
} catch (XPathException err) { |
| 111 |
|
| 112 |
|
| 113 |
} |
| 114 |
return this; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public int computeSpecialProperties() { |
| 124 |
return getBaseExpression().getSpecialProperties(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public int computeCardinality() { |
| 132 |
return getBaseExpression().getCardinality(); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public ItemType getItemType() { |
| 145 |
return getBaseExpression().getItemType(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public boolean equals( Object other) { |
| 153 |
return other != null && this.getClass().equals(other.getClass()) && |
| 154 |
this.getBaseExpression().isEqual(((UnaryExpression) other).getBaseExpression()); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public int computeHashCode() { |
| 163 |
return ("UnaryExpression " + getClass()).hashCode() ^ getBaseExpression().hashCode(); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public String toString() { |
| 173 |
return getExpressionName() + "(" + getBaseExpression() + ")"; |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public String toShortString() { |
| 178 |
return getExpressionName() + "(" + getBaseExpression().toShortString() + ")"; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public void export(ExpressionPresenter out) throws XPathException { |
| 187 |
String name = getExpressionName(); |
| 188 |
if (name == null) { |
| 189 |
out.startElement("unaryOperator", this); |
| 190 |
String op = displayOperator(out.getConfiguration()); |
| 191 |
if (op != null) { |
| 192 |
out.emitAttribute("op", op); |
| 193 |
} |
| 194 |
} else { |
| 195 |
out.startElement(name, this); |
| 196 |
} |
| 197 |
getBaseExpression().export(out); |
| 198 |
out.endElement(); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
protected String displayOperator(Configuration config) { |
| 210 |
return null; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
} |