| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.Literal; |
| 12 |
import net.sf.saxon.expr.StringLiteral; |
| 13 |
import net.sf.saxon.expr.instruct.Block; |
| 14 |
import net.sf.saxon.expr.instruct.Choose; |
| 15 |
import net.sf.saxon.expr.instruct.Message; |
| 16 |
import net.sf.saxon.functions.SystemFunction; |
| 17 |
import net.sf.saxon.om.AttributeCollection; |
| 18 |
import net.sf.saxon.om.StructuredQName; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public final class XSLAssert extends StyleElement { |
| 27 |
|
| 28 |
private Expression test = null; |
| 29 |
private Expression select = null; |
| 30 |
private Expression errorCode = null; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public boolean isInstruction() { |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public boolean mayContainSequenceConstructor() { |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
public void prepareAttributes() { |
| 53 |
|
| 54 |
String testAtt = null; |
| 55 |
String selectAtt = null; |
| 56 |
String errorCodeAtt = null; |
| 57 |
AttributeCollection atts = getAttributeList(); |
| 58 |
|
| 59 |
for (int a = 0; a < atts.getLength(); a++) { |
| 60 |
String f = atts.getQName(a); |
| 61 |
switch (f) { |
| 62 |
case "test": |
| 63 |
testAtt = atts.getValue(a); |
| 64 |
test = makeExpression(testAtt, a); |
| 65 |
break; |
| 66 |
case "select": |
| 67 |
selectAtt = atts.getValue(a); |
| 68 |
select = makeExpression(selectAtt, a); |
| 69 |
break; |
| 70 |
case "error-code": |
| 71 |
errorCodeAtt = atts.getValue(a); |
| 72 |
errorCode = makeAttributeValueTemplate(errorCodeAtt, a); |
| 73 |
break; |
| 74 |
default: |
| 75 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 76 |
break; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if (testAtt == null) { |
| 81 |
reportAbsence("test"); |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 87 |
select = typeCheck("select", select); |
| 88 |
test = typeCheck("test", test); |
| 89 |
if (errorCode == null) { |
| 90 |
errorCode = new StringLiteral("Q{http://www.w3.org/2005/xqt-errors}XTMM9001"); |
| 91 |
} else { |
| 92 |
errorCode = typeCheck("error-code", errorCode); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 97 |
if (exec.getCompilerInfo().isAssertionsEnabled()) { |
| 98 |
Expression b = compileSequenceConstructor(exec, decl, true); |
| 99 |
if (b != null) { |
| 100 |
if (select == null) { |
| 101 |
select = b; |
| 102 |
} else { |
| 103 |
select = Block.makeBlock(select, b); |
| 104 |
select.setLocation( |
| 105 |
allocateLocation()); |
| 106 |
} |
| 107 |
} |
| 108 |
if (select == null) { |
| 109 |
select = new StringLiteral("xsl:message (no content)"); |
| 110 |
} |
| 111 |
if (errorCode instanceof StringLiteral) { |
| 112 |
|
| 113 |
String code = ((StringLiteral) errorCode).getStringValue(); |
| 114 |
if (code.contains(":") && !code.startsWith("Q{")) { |
| 115 |
StructuredQName name = makeQName(code); |
| 116 |
errorCode = new StringLiteral(name.getEQName()); |
| 117 |
} |
| 118 |
} |
| 119 |
Message msg = new Message(select, new StringLiteral("yes"), errorCode); |
| 120 |
msg.setIsAssert(true); |
| 121 |
if (!(errorCode instanceof StringLiteral)) { |
| 122 |
|
| 123 |
msg.setRetainedStaticContext(makeRetainedStaticContext()); |
| 124 |
} |
| 125 |
Expression condition = SystemFunction.makeCall("not", test.getRetainedStaticContext(), test); |
| 126 |
|
| 127 |
return new Choose(new Expression[]{condition}, new Expression[]{msg}); |
| 128 |
} else { |
| 129 |
|
| 130 |
return Literal.makeEmptySequence(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
} |