| 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.ForEach; |
| 14 |
import net.sf.saxon.expr.sort.SortExpression; |
| 15 |
import net.sf.saxon.expr.sort.SortKeyDefinitionList; |
| 16 |
import net.sf.saxon.lib.NamespaceConstant; |
| 17 |
import net.sf.saxon.om.AttributeCollection; |
| 18 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.value.Cardinality; |
| 21 |
import net.sf.saxon.value.Whitespace; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class XSLForEach extends StyleElement { |
| 29 |
|
| 30 |
private Expression select = null; |
| 31 |
private boolean containsTailCall = false; |
| 32 |
private Expression threads = null; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public boolean isInstruction() { |
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected boolean isPermittedChild(StyleElement child) { |
| 49 |
return (child instanceof XSLSort); |
| 50 |
} |
| 51 |
|
| 52 |
protected boolean markTailCalls() { |
| 53 |
assert select != null; |
| 54 |
if (Cardinality.allowsMany(select.getCardinality())) { |
| 55 |
return false; |
| 56 |
} else { |
| 57 |
StyleElement last = getLastChildInstruction(); |
| 58 |
containsTailCall = last != null && last.markTailCalls(); |
| 59 |
return containsTailCall; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public boolean mayContainSequenceConstructor() { |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
public void prepareAttributes() throws XPathException { |
| 74 |
|
| 75 |
AttributeCollection atts = getAttributeList(); |
| 76 |
|
| 77 |
String selectAtt = null; |
| 78 |
|
| 79 |
for (int a = 0; a < atts.getLength(); a++) { |
| 80 |
String f = atts.getQName(a); |
| 81 |
if (f.equals("select")) { |
| 82 |
selectAtt = atts.getValue(a); |
| 83 |
select = makeExpression(selectAtt, a); |
| 84 |
} else if (atts.getLocalName(a).equals("threads") && atts.getURI(a).equals(NamespaceConstant.SAXON)) { |
| 85 |
String threadsAtt = Whitespace.trim(atts.getValue(a)); |
| 86 |
threads = makeAttributeValueTemplate(threadsAtt, a); |
| 87 |
if (getCompilation().getCompilerInfo().isCompileWithTracing()) { |
| 88 |
compileWarning("saxon:threads - no multithreading takes place when compiling with trace enabled", |
| 89 |
SaxonErrorCode.SXWN9012); |
| 90 |
threads = new StringLiteral("0"); |
| 91 |
} else if (!"EE".equals(getConfiguration().getEditionCode())) { |
| 92 |
compileWarning("saxon:threads - ignored when not running Saxon-EE", |
| 93 |
SaxonErrorCode.SXWN9013); |
| 94 |
threads = new StringLiteral("0"); |
| 95 |
} |
| 96 |
} else { |
| 97 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
if (selectAtt == null) { |
| 102 |
reportAbsence("select"); |
| 103 |
select = Literal.makeEmptySequence(); |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 109 |
checkSortComesFirst(false); |
| 110 |
select = typeCheck("select", select); |
| 111 |
if (!hasChildNodes()) { |
| 112 |
compileWarning("An empty xsl:for-each instruction has no effect", SaxonErrorCode.SXWN9009); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public Expression compile(Compilation compilation, ComponentDeclaration decl) throws XPathException { |
| 117 |
SortKeyDefinitionList sortKeys = makeSortKeys(compilation, decl); |
| 118 |
Expression sortedSequence = select; |
| 119 |
if (sortKeys != null) { |
| 120 |
sortedSequence = new SortExpression(select, sortKeys); |
| 121 |
} |
| 122 |
|
| 123 |
Expression block = compileSequenceConstructor(compilation, decl, true); |
| 124 |
if (block == null) { |
| 125 |
|
| 126 |
return Literal.makeEmptySequence(); |
| 127 |
} |
| 128 |
try { |
| 129 |
ForEach result = new ForEach(sortedSequence, block.simplify(), containsTailCall, threads); |
| 130 |
result.setInstruction(true); |
| 131 |
result.setLocation(allocateLocation()); |
| 132 |
return result; |
| 133 |
} catch (XPathException err) { |
| 134 |
compileError(err); |
| 135 |
return null; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
} |
| 141 |
|