| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.instruct; |
| 9 |
|
| 10 |
import net.sf.saxon.Controller; |
| 11 |
import net.sf.saxon.event.*; |
| 12 |
import net.sf.saxon.expr.*; |
| 13 |
import net.sf.saxon.lib.ParseOptions; |
| 14 |
import net.sf.saxon.lib.Validation; |
| 15 |
import net.sf.saxon.ma.arrays.ArrayItemType; |
| 16 |
import net.sf.saxon.ma.map.MapType; |
| 17 |
import net.sf.saxon.om.Item; |
| 18 |
import net.sf.saxon.om.NamespaceBinding; |
| 19 |
import net.sf.saxon.om.NodeInfo; |
| 20 |
import net.sf.saxon.om.NodeName; |
| 21 |
import net.sf.saxon.pattern.NodeKindTest; |
| 22 |
import net.sf.saxon.pattern.NodeTest; |
| 23 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 24 |
import net.sf.saxon.trans.XPathException; |
| 25 |
import net.sf.saxon.type.*; |
| 26 |
import net.sf.saxon.value.Cardinality; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public abstract class ElementCreator extends ParentNodeConstructor { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected boolean inheritNamespacesToChildren = true; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
protected boolean inheritNamespacesFromParent = true; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public ElementCreator() { |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public ItemType getItemType() { |
| 71 |
return NodeKindTest.ELEMENT; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public int getCardinality() { |
| 76 |
return StaticProperty.EXACTLY_ONE; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public void setInheritNamespacesToChildren(boolean inherit) { |
| 86 |
inheritNamespacesToChildren = inherit; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public boolean isInheritNamespacesToChildren() { |
| 96 |
return inheritNamespacesToChildren; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public void setInheritNamespacesFromParent(boolean inherit) { |
| 107 |
inheritNamespacesFromParent = inherit; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public boolean isInheritNamespacesFromParent() { |
| 118 |
return inheritNamespacesFromParent; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public int computeSpecialProperties() { |
| 130 |
int p = super.computeSpecialProperties() | |
| 131 |
StaticProperty.SINGLE_DOCUMENT_NODESET; |
| 132 |
if (getValidationAction() == Validation.STRIP) { |
| 133 |
p |= StaticProperty.ALL_NODES_UNTYPED; |
| 134 |
} |
| 135 |
return p; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public void suppressValidation(int parentValidationMode) { |
| 144 |
if (getValidationAction() == parentValidationMode && getSchemaType() == null) { |
| 145 |
|
| 146 |
setValidationAction(Validation.PRESERVE, null); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
protected void checkContentSequence(StaticContext env) throws XPathException { |
| 159 |
Operand[] components; |
| 160 |
if (getContentExpression() instanceof Block) { |
| 161 |
components = ((Block) getContentExpression()).getOperanda(); |
| 162 |
} else { |
| 163 |
components = new Operand[]{contentOp}; |
| 164 |
} |
| 165 |
|
| 166 |
boolean foundChild = false; |
| 167 |
boolean foundPossibleChild = false; |
| 168 |
for (Operand o : components) { |
| 169 |
Expression component = o.getChildExpression(); |
| 170 |
ItemType it = component.getItemType(); |
| 171 |
if (it.isAtomicType()) { |
| 172 |
foundChild = true; |
| 173 |
} else if (it instanceof FunctionItemType && !(it instanceof ArrayItemType)) { |
| 174 |
String which = it instanceof MapType ? "map" : "function"; |
| 175 |
XPathException de = new XPathException( |
| 176 |
"Cannot add a " + which + " as a child of a constructed element"); |
| 177 |
de.setErrorCode(isXSLT() ? "XTDE0450" : "XQTY0105"); |
| 178 |
de.setLocator(component.getLocation()); |
| 179 |
de.setIsTypeError(true); |
| 180 |
throw de; |
| 181 |
} else if (it instanceof NodeTest) { |
| 182 |
boolean maybeEmpty = Cardinality.allowsZero(component.getCardinality()); |
| 183 |
UType possibleNodeKinds = it.getUType(); |
| 184 |
if (possibleNodeKinds.overlaps(UType.TEXT)) { |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if (component instanceof ValueOf && |
| 190 |
((ValueOf) component).getSelect() instanceof StringLiteral) { |
| 191 |
String value = ((StringLiteral) ((ValueOf) component).getSelect()).getStringValue(); |
| 192 |
if (value.isEmpty()) { |
| 193 |
|
| 194 |
} else { |
| 195 |
foundChild = true; |
| 196 |
} |
| 197 |
} else { |
| 198 |
foundPossibleChild = true; |
| 199 |
} |
| 200 |
} else if (!possibleNodeKinds.overlaps(UType.CHILD_NODE_KINDS)) { |
| 201 |
if (maybeEmpty) { |
| 202 |
foundPossibleChild = true; |
| 203 |
} else { |
| 204 |
foundChild = true; |
| 205 |
} |
| 206 |
} else if (foundChild && possibleNodeKinds == UType.ATTRIBUTE && !maybeEmpty) { |
| 207 |
XPathException de = new XPathException( |
| 208 |
"Cannot create an attribute node after creating a child of the containing element"); |
| 209 |
de.setErrorCode(isXSLT() ? "XTDE0410" : "XQTY0024"); |
| 210 |
de.setLocator(component.getLocation()); |
| 211 |
throw de; |
| 212 |
} else if (foundChild && possibleNodeKinds == UType.NAMESPACE && !maybeEmpty) { |
| 213 |
XPathException de = new XPathException( |
| 214 |
"Cannot create a namespace node after creating a child of the containing element"); |
| 215 |
de.setErrorCode(isXSLT() ? "XTDE0410" : "XQTY0024"); |
| 216 |
de.setLocator(component.getLocation()); |
| 217 |
throw de; |
| 218 |
} else if ((foundChild || foundPossibleChild) && possibleNodeKinds == UType.ATTRIBUTE) { |
| 219 |
env.issueWarning( |
| 220 |
"Creating an attribute here will fail if previous instructions create any children", |
| 221 |
component.getLocation()); |
| 222 |
} else if ((foundChild || foundPossibleChild) && possibleNodeKinds == UType.NAMESPACE) { |
| 223 |
env.issueWarning( |
| 224 |
"Creating a namespace node here will fail if previous instructions create any children", |
| 225 |
component.getLocation()); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public abstract NodeName getElementName(XPathContext context, NodeInfo copiedNode) throws XPathException; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public abstract String getNewBaseURI(XPathContext context, NodeInfo copiedNode); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public abstract void outputNamespaceNodes( |
| 266 |
XPathContext context, Receiver receiver, NodeName nameCode, NodeInfo copiedNode) |
| 267 |
throws XPathException; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public NamespaceBinding[] getActiveNamespaces() throws XPathException { |
| 277 |
return null; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public int getImplementationMethod() { |
| 286 |
return Expression.PROCESS_METHOD | Expression.EVALUATE_METHOD; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public TailCall processLeavingTail(XPathContext context) |
| 298 |
throws XPathException { |
| 299 |
return processLeavingTail(context, null); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public final TailCall processLeavingTail(XPathContext context, NodeInfo copiedNode) |
| 312 |
throws XPathException { |
| 313 |
|
| 314 |
try { |
| 315 |
|
| 316 |
NodeName elemName = getElementName(context, copiedNode); |
| 317 |
SchemaType typeCode = getValidationAction() == Validation.PRESERVE ? AnyType.getInstance() : Untyped.getInstance(); |
| 318 |
|
| 319 |
Receiver out = context.getReceiver(); |
| 320 |
Receiver saved = out; |
| 321 |
boolean pop = false; |
| 322 |
Receiver elemOut = out; |
| 323 |
if (!preservingTypes) { |
| 324 |
ParseOptions options = new ParseOptions(getValidationOptions()); |
| 325 |
options.setTopLevelElement(elemName.getStructuredQName()); |
| 326 |
context.getConfiguration().prepareValidationReporting(context, options); |
| 327 |
Receiver validator = context.getConfiguration().getElementValidator(out, options, getLocation()); |
| 328 |
|
| 329 |
if (validator != out) { |
| 330 |
out = new TreeReceiver(validator); |
| 331 |
context.setReceiver(out); |
| 332 |
pop = true; |
| 333 |
} |
| 334 |
elemOut = out; |
| 335 |
} |
| 336 |
|
| 337 |
if (elemOut.getSystemId() == null) { |
| 338 |
elemOut.setSystemId(getNewBaseURI(context, copiedNode)); |
| 339 |
} |
| 340 |
int properties = inheritNamespacesToChildren ? 0 : ReceiverOptions.DISINHERIT_NAMESPACES; |
| 341 |
if (!inheritNamespacesFromParent) { |
| 342 |
properties |= ReceiverOptions.REFUSE_NAMESPACES; |
| 343 |
} |
| 344 |
|
| 345 |
elemOut.startElement(elemName, typeCode, getLocation(), properties); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
outputNamespaceNodes(context, elemOut, elemName, copiedNode); |
| 350 |
|
| 351 |
|
| 352 |
getContentExpression().process(context); |
| 353 |
|
| 354 |
|
| 355 |
elemOut.endElement(); |
| 356 |
|
| 357 |
if (pop) { |
| 358 |
context.setReceiver(saved); |
| 359 |
} |
| 360 |
return null; |
| 361 |
|
| 362 |
} catch (XPathException e) { |
| 363 |
e.maybeSetLocation(getLocation()); |
| 364 |
e.maybeSetContext(context); |
| 365 |
throw e; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public Item evaluateItem(XPathContext context) throws XPathException { |
| 375 |
return constructElement(context, null); |
| 376 |
|
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
private NodeInfo constructElement(XPathContext context, NodeInfo copiedNode) throws XPathException { |
| 388 |
try { |
| 389 |
Controller controller = context.getController(); |
| 390 |
Receiver saved = context.getReceiver(); |
| 391 |
SequenceOutputter seq = controller.allocateSequenceOutputter(1); |
| 392 |
seq.getPipelineConfiguration().setHostLanguage(getPackageData().getHostLanguage()); |
| 393 |
|
| 394 |
NodeName elemName = getElementName(context, copiedNode); |
| 395 |
SchemaType typeCode = getValidationAction() == Validation.PRESERVE ? AnyType.getInstance() : Untyped.getInstance(); |
| 396 |
|
| 397 |
SequenceReceiver ini = seq; |
| 398 |
if (!preservingTypes) { |
| 399 |
ParseOptions options = new ParseOptions(getValidationOptions()); |
| 400 |
options.setTopLevelElement(elemName.getStructuredQName()); |
| 401 |
controller.getConfiguration().prepareValidationReporting(context, options); |
| 402 |
Receiver validator = controller.getConfiguration().getElementValidator(ini, options, getLocation()); |
| 403 |
|
| 404 |
if (ini.getSystemId() == null) { |
| 405 |
ini.setSystemId(getNewBaseURI(context, copiedNode)); |
| 406 |
} |
| 407 |
if (validator == ini) { |
| 408 |
context.setReceiver(ini); |
| 409 |
} else { |
| 410 |
TreeReceiver tr = new TreeReceiver(validator); |
| 411 |
tr.setPipelineConfiguration(seq.getPipelineConfiguration()); |
| 412 |
context.setReceiver(tr); |
| 413 |
ini = tr; |
| 414 |
} |
| 415 |
} else { |
| 416 |
context.setReceiver(ini); |
| 417 |
if (ini.getSystemId() == null) { |
| 418 |
ini.setSystemId(getNewBaseURI(context, copiedNode)); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
ini.open(); |
| 423 |
int properties = inheritNamespacesToChildren ? 0 : ReceiverOptions.DISINHERIT_NAMESPACES; |
| 424 |
if (!inheritNamespacesFromParent) { |
| 425 |
properties |= ReceiverOptions.REFUSE_NAMESPACES; |
| 426 |
} |
| 427 |
ini.startElement(elemName, typeCode, getLocation(), properties); |
| 428 |
|
| 429 |
|
| 430 |
outputNamespaceNodes(context, ini, elemName, null); |
| 431 |
|
| 432 |
getContentExpression().process(context); |
| 433 |
|
| 434 |
ini.endElement(); |
| 435 |
ini.close(); |
| 436 |
context.setReceiver(saved); |
| 437 |
|
| 438 |
|
| 439 |
NodeInfo result = (NodeInfo) seq.popLastItem(); |
| 440 |
seq.reset(); |
| 441 |
return result; |
| 442 |
|
| 443 |
} catch (XPathException err) { |
| 444 |
err.maybeSetLocation(getLocation()); |
| 445 |
err.maybeSetContext(context); |
| 446 |
throw err; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
protected void exportValidationAndType(ExpressionPresenter out) { |
| 451 |
if (getValidationAction() != Validation.SKIP && getValidationAction() != Validation.BY_TYPE) { |
| 452 |
out.emitAttribute("validation", Validation.toString(getValidationAction())); |
| 453 |
} |
| 454 |
if (getValidationAction() == Validation.BY_TYPE) { |
| 455 |
SchemaType type = getSchemaType(); |
| 456 |
if (type != null) { |
| 457 |
out.emitAttribute("type", type.getStructuredQName()); |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
protected String getInheritanceFlags() { |
| 463 |
String flags = ""; |
| 464 |
if (!inheritNamespacesFromParent) { |
| 465 |
flags += "P"; |
| 466 |
} |
| 467 |
if (!inheritNamespacesToChildren) { |
| 468 |
flags += "C"; |
| 469 |
} |
| 470 |
return flags; |
| 471 |
} |
| 472 |
|
| 473 |
public void setInheritanceFlags(String flags) { |
| 474 |
inheritNamespacesFromParent = !flags.contains("P"); |
| 475 |
inheritNamespacesToChildren = !flags.contains("C"); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
@Override |
| 485 |
public String getStreamerName() { |
| 486 |
return "ElementCreator"; |
| 487 |
} |
| 488 |
} |
| 489 |
|