| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 11 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 13 |
import net.sf.saxon.pattern.NodeTest; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.tree.jiter.MonoIterator; |
| 16 |
import net.sf.saxon.type.AnyItemType; |
| 17 |
import net.sf.saxon.value.SequenceType; |
| 18 |
|
| 19 |
import java.io.PrintWriter; |
| 20 |
import java.io.StringWriter; |
| 21 |
import java.util.Iterator; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public final class Operand implements Iterable<Operand>, ExpressionOwner { |
| 28 |
|
| 29 |
private Expression parentExpression; |
| 30 |
private Expression childExpression; |
| 31 |
private OperandRole role; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public Operand(Expression parentExpression, Expression childExpression, OperandRole role) { |
| 41 |
this.parentExpression = parentExpression; |
| 42 |
this.role = role; |
| 43 |
setChildExpression(childExpression); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public Expression getParentExpression() { |
| 52 |
return parentExpression; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public Expression getChildExpression() { |
| 61 |
return childExpression; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public void setChildExpression(Expression childExpression) { |
| 72 |
if (childExpression != this.childExpression) { |
| 73 |
if (role.isConstrainedClass() && this.childExpression != null && childExpression.getClass() != this.childExpression.getClass()) { |
| 74 |
throw new AssertionError(); |
| 75 |
} |
| 76 |
this.childExpression = childExpression; |
| 77 |
parentExpression.adoptChildExpression(childExpression); |
| 78 |
parentExpression.resetLocalStaticProperties(); |
| 79 |
|
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
private static boolean DEBUG = false; |
| 84 |
public void detachChild() { |
| 85 |
if (DEBUG) { |
| 86 |
childExpression.setParentExpression(null); |
| 87 |
StringWriter sw = new StringWriter(); |
| 88 |
new XPathException("dummy").printStackTrace(new PrintWriter(sw)); |
| 89 |
childExpression = new ErrorExpression("child expression has been detached: " + sw.toString(), "ZZZ", false); |
| 90 |
ExpressionTool.copyLocationInfo(parentExpression, childExpression); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public OperandRole getOperandRole() { |
| 100 |
return role; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public void setOperandRole(OperandRole role) { |
| 109 |
this.role = role; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public boolean setsNewFocus() { |
| 118 |
return role.setsNewFocus(); |
| 119 |
} |
| 120 |
|
| 121 |
public boolean hasSpecialFocusRules() { |
| 122 |
return role.hasSpecialFocusRules(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public boolean hasSameFocus() { |
| 131 |
return role.hasSameFocus(); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public boolean isEvaluatedRepeatedly() { |
| 141 |
return role.isEvaluatedRepeatedly(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public OperandUsage getUsage() { |
| 150 |
return role.getUsage(); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void setUsage(OperandUsage usage) { |
| 159 |
role = new OperandRole(role.properties, usage, role.getRequiredType()); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public SequenceType getRequiredType() { |
| 168 |
return role.getRequiredType(); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public boolean isInChoiceGroup() { |
| 177 |
return role.isInChoiceGroup(); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public Iterator<Operand> iterator() { |
| 186 |
return new MonoIterator<Operand>(this); |
| 187 |
} |
| 188 |
|
| 189 |
public void typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 190 |
try { |
| 191 |
setChildExpression(getChildExpression().typeCheck(visitor, contextInfo)); |
| 192 |
} catch (XPathException e) { |
| 193 |
e.maybeSetLocation(getChildExpression().getLocation()); |
| 194 |
if (!e.isReportableStatically()) { |
| 195 |
visitor.getStaticContext().issueWarning( |
| 196 |
"Evaluation will always throw a dynamic error: " + e.getMessage(), getChildExpression().getLocation()); |
| 197 |
setChildExpression(new ErrorExpression(e)); |
| 198 |
} else { |
| 199 |
throw e; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
public void optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 205 |
try { |
| 206 |
setChildExpression(getChildExpression().optimize(visitor, contextInfo)); |
| 207 |
} catch (XPathException e) { |
| 208 |
e.maybeSetLocation(getChildExpression().getLocation()); |
| 209 |
if (!e.isReportableStatically()) { |
| 210 |
visitor.getStaticContext().issueWarning( |
| 211 |
"Evaluation will always throw a dynamic error: " + e.getMessage(), getChildExpression().getLocation()); |
| 212 |
setChildExpression(new ErrorExpression(e)); |
| 213 |
} else { |
| 214 |
throw e; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
public static OperandUsage typeDeterminedUsage(net.sf.saxon.type.ItemType type) { |
| 220 |
if (type.isPlainType()) { |
| 221 |
return OperandUsage.ABSORPTION; |
| 222 |
} else if (type instanceof NodeTest || type == AnyItemType.getInstance()) { |
| 223 |
return OperandUsage.NAVIGATION; |
| 224 |
} else { |
| 225 |
return OperandUsage.INSPECTION; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
} |
| 230 |
|