| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 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.expr.parser.PathMap; |
| 15 |
import net.sf.saxon.lib.ParseOptions; |
| 16 |
import net.sf.saxon.lib.Validation; |
| 17 |
import net.sf.saxon.pattern.MultipleNodeKindTest; |
| 18 |
import net.sf.saxon.pattern.NodeKindTest; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.type.ItemType; |
| 21 |
import net.sf.saxon.type.SchemaType; |
| 22 |
import net.sf.saxon.type.TypeHierarchy; |
| 23 |
import net.sf.saxon.type.Untyped; |
| 24 |
import net.sf.saxon.value.SequenceType; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public abstract class ParentNodeConstructor extends Instruction |
| 32 |
implements ValidatingInstruction, InstructionWithComplexContent { |
| 33 |
|
| 34 |
private final static OperandRole SAME_FOCUS_CONTENT = |
| 35 |
new OperandRole(0, OperandUsage.ABSORPTION, SequenceType.ANY_SEQUENCE); |
| 36 |
|
| 37 |
protected Operand contentOp; |
| 38 |
private boolean lazyConstruction = false; |
| 39 |
private ParseOptions validationOptions = null; |
| 40 |
private String baseURI; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
protected boolean preservingTypes = true; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public ParentNodeConstructor() { |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public void setBaseURI(String uri) { |
| 63 |
baseURI = uri; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public void setLazyConstruction(boolean lazy) { |
| 74 |
lazyConstruction = lazy; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public final boolean isLazyConstruction() { |
| 84 |
return lazyConstruction; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public SchemaType getSchemaType() { |
| 95 |
return validationOptions == null ? null : validationOptions.getTopLevelType(); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public ParseOptions getValidationOptions() { |
| 106 |
return validationOptions; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void setValidationAction(int mode, SchemaType schemaType) { |
| 119 |
preservingTypes = mode == Validation.PRESERVE && schemaType == null; |
| 120 |
if (!preservingTypes) { |
| 121 |
if (validationOptions == null) { |
| 122 |
validationOptions = new ParseOptions(); |
| 123 |
} |
| 124 |
if (schemaType == Untyped.getInstance()) { |
| 125 |
validationOptions.setSchemaValidationMode(Validation.SKIP); |
| 126 |
} else { |
| 127 |
validationOptions.setSchemaValidationMode(mode); |
| 128 |
validationOptions.setTopLevelType(schemaType); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public int getValidationAction() { |
| 140 |
return validationOptions == null ? Validation.PRESERVE : validationOptions.getSchemaValidationMode(); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public void setNoNeedToStrip() { |
| 149 |
preservingTypes = true; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void setContentExpression(Expression content) { |
| 159 |
if (contentOp == null) { |
| 160 |
contentOp = new Operand(this, content, SAME_FOCUS_CONTENT); |
| 161 |
} else { |
| 162 |
contentOp.setChildExpression(content); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public Expression getContentExpression() { |
| 173 |
return contentOp == null ? null : contentOp.getChildExpression(); |
| 174 |
} |
| 175 |
|
| 176 |
public Operand getContentOperand() { |
| 177 |
return contentOp; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public int computeCardinality() { |
| 187 |
return StaticProperty.EXACTLY_ONE; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 192 |
typeCheckChildren(visitor, contextInfo); |
| 193 |
checkContentSequence(visitor.getStaticContext()); |
| 194 |
return this; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
@Override |
| 206 |
public boolean allowExtractingCommonSubexpressions() { |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
protected abstract void checkContentSequence(StaticContext env) throws XPathException; |
| 218 |
|
| 219 |
|
| 220 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 221 |
optimizeChildren(visitor, contextItemType); |
| 222 |
if (!Literal.isEmptySequence(getContentExpression())) { |
| 223 |
if (getContentExpression() instanceof Block) { |
| 224 |
setContentExpression(((Block) getContentExpression()).mergeAdjacentTextInstructions()); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if (visitor.isOptimizeForStreaming()) { |
| 230 |
visitor.obtainOptimizer().makeCopyOperationsExplicit(this, contentOp); |
| 231 |
} |
| 232 |
} |
| 233 |
if (visitor.getStaticContext().getPackageData().isSchemaAware()) { |
| 234 |
TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 235 |
if (getValidationAction() == Validation.STRIP) { |
| 236 |
if (getContentExpression().hasSpecialProperty(StaticProperty.ALL_NODES_UNTYPED) || |
| 237 |
th.relationship(getContentExpression().getItemType(), MultipleNodeKindTest.DOC_ELEM_ATTR) == TypeHierarchy.DISJOINT) { |
| 238 |
|
| 239 |
setNoNeedToStrip(); |
| 240 |
} |
| 241 |
} |
| 242 |
} else { |
| 243 |
setNoNeedToStrip(); |
| 244 |
} |
| 245 |
return this; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
@Override |
| 255 |
public final boolean mayCreateNewNodes() { |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
@Override |
| 268 |
public boolean alwaysCreatesNewNodes() { |
| 269 |
return true; |
| 270 |
} |
| 271 |
|
| 272 |
public int getCardinality() { |
| 273 |
return StaticProperty.EXACTLY_ONE; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 297 |
PathMap.PathMapNodeSet result = super.addToPathMap(pathMap, pathMapNodeSet); |
| 298 |
result.setReturnable(false); |
| 299 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 300 |
ItemType type = getItemType(); |
| 301 |
if (th.relationship(type, NodeKindTest.ELEMENT) != TypeHierarchy.DISJOINT || |
| 302 |
th.relationship(type, NodeKindTest.DOCUMENT) != TypeHierarchy.DISJOINT) { |
| 303 |
result.addDescendants(); |
| 304 |
} |
| 305 |
return new PathMap.PathMapNodeSet(pathMap.makeNewRoot(this)); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public boolean isPreservingTypes() { |
| 316 |
return preservingTypes; |
| 317 |
} |
| 318 |
|
| 319 |
public boolean isLocal() { |
| 320 |
return ExpressionTool.isLocalConstructor(this); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|