| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.Literal; |
| 13 |
import net.sf.saxon.expr.accum.Accumulator; |
| 14 |
import net.sf.saxon.expr.accum.AccumulatorRegistry; |
| 15 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 16 |
import net.sf.saxon.lib.NamespaceConstant; |
| 17 |
import net.sf.saxon.lib.ParseOptions; |
| 18 |
import net.sf.saxon.lib.Validation; |
| 19 |
import net.sf.saxon.om.*; |
| 20 |
import net.sf.saxon.pattern.NodeKindTest; |
| 21 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 22 |
import net.sf.saxon.trans.XPathException; |
| 23 |
import net.sf.saxon.value.Whitespace; |
| 24 |
|
| 25 |
import java.util.HashSet; |
| 26 |
import java.util.Set; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class XSLSourceDocument extends StyleElement { |
| 33 |
|
| 34 |
private Expression href = null; |
| 35 |
private Set<Accumulator> accumulators = new HashSet<>(); |
| 36 |
private boolean streaming = false; |
| 37 |
private ParseOptions parseOptions; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public boolean isInstruction() { |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public boolean mayContainSequenceConstructor() { |
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
protected boolean isWithinDeclaredStreamableConstruct() { |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
public void prepareAttributes() throws XPathException { |
| 66 |
|
| 67 |
parseOptions = new ParseOptions(getConfiguration().getParseOptions()); |
| 68 |
|
| 69 |
AttributeCollection atts = getAttributeList(); |
| 70 |
|
| 71 |
String hrefAtt = null; |
| 72 |
String validationAtt = null; |
| 73 |
String typeAtt = null; |
| 74 |
String useAccumulatorsAtt = null; |
| 75 |
|
| 76 |
for (int a = 0; a < atts.getLength(); a++) { |
| 77 |
String f = atts.getQName(a); |
| 78 |
if (f.equals("href")) { |
| 79 |
hrefAtt = atts.getValue(a); |
| 80 |
href = makeAttributeValueTemplate(hrefAtt, a); |
| 81 |
} else if (f.equals("validation")) { |
| 82 |
validationAtt = Whitespace.trim(atts.getValue(a)); |
| 83 |
} else if (f.equals("type")) { |
| 84 |
typeAtt = Whitespace.trim(atts.getValue(a)); |
| 85 |
} else if (f.equals("use-accumulators")) { |
| 86 |
useAccumulatorsAtt = Whitespace.trim(atts.getValue(a)); |
| 87 |
} else if (f.equals("streamable")) { |
| 88 |
streaming = processStreamableAtt(atts.getValue(a)); |
| 89 |
} else if (NamespaceConstant.SAXON.equals(atts.getURI(a))) { |
| 90 |
String local = atts.getLocalName(a); |
| 91 |
getConfiguration().checkLicensedFeature(Configuration.LicenseFeature.PROFESSIONAL_EDITION, |
| 92 |
atts.getQName(a), |
| 93 |
getPackageData().getLocalLicenseId()); |
| 94 |
switch (local) { |
| 95 |
case "dtd-validation": |
| 96 |
parseOptions.setDTDValidationMode(processBooleanAttribute(f, atts.getValue(a)) ? Validation.STRICT : Validation.SKIP); |
| 97 |
break; |
| 98 |
case "expand-attribute-defaults": |
| 99 |
parseOptions.setExpandAttributeDefaults(processBooleanAttribute(f, atts.getValue(a))); |
| 100 |
break; |
| 101 |
case "line-numbering": |
| 102 |
parseOptions.setLineNumbering(processBooleanAttribute(f, atts.getValue(a))); |
| 103 |
break; |
| 104 |
case "xinclude": |
| 105 |
parseOptions.setXIncludeAware(processBooleanAttribute(f, atts.getValue(a))); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
break; |
| 110 |
case "validation-params": |
| 111 |
|
| 112 |
break; |
| 113 |
case "strip-space": |
| 114 |
String value = Whitespace.normalizeWhitespace(atts.getValue(a)).toString(); |
| 115 |
switch (value) { |
| 116 |
case "#all": |
| 117 |
parseOptions.setSpaceStrippingRule(AllElementsSpaceStrippingRule.getInstance()); |
| 118 |
break; |
| 119 |
case "#none": |
| 120 |
parseOptions.setSpaceStrippingRule(NoElementsSpaceStrippingRule.getInstance()); |
| 121 |
break; |
| 122 |
case "#ignorable": |
| 123 |
parseOptions.setSpaceStrippingRule(IgnorableSpaceStrippingRule.getInstance()); |
| 124 |
break; |
| 125 |
case "#default": |
| 126 |
parseOptions.setSpaceStrippingRule(null); |
| 127 |
break; |
| 128 |
default: |
| 129 |
invalidAttribute("saxon:strip-space", "#all|#none|#ignorable|#default"); |
| 130 |
break; |
| 131 |
} |
| 132 |
break; |
| 133 |
default: |
| 134 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 135 |
break; |
| 136 |
} |
| 137 |
} else { |
| 138 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (hrefAtt == null) { |
| 143 |
reportAbsence("href"); |
| 144 |
} |
| 145 |
|
| 146 |
if (validationAtt != null) { |
| 147 |
int validation = validateValidationAttribute(validationAtt); |
| 148 |
parseOptions.setSchemaValidationMode(validation); |
| 149 |
} |
| 150 |
|
| 151 |
if (typeAtt != null) { |
| 152 |
if (!isSchemaAware()) { |
| 153 |
compileError("The @type attribute is available only with a schema-aware XSLT processor", "XTSE1660"); |
| 154 |
} |
| 155 |
parseOptions.setSchemaValidationMode(Validation.BY_TYPE); |
| 156 |
parseOptions.setTopLevelType(getSchemaType(typeAtt)); |
| 157 |
} |
| 158 |
|
| 159 |
if (typeAtt != null && validationAtt != null) { |
| 160 |
compileError("The @validation and @type attributes are mutually exclusive", "XTSE1505"); |
| 161 |
} |
| 162 |
|
| 163 |
if (useAccumulatorsAtt == null) { |
| 164 |
useAccumulatorsAtt = ""; |
| 165 |
} |
| 166 |
|
| 167 |
AccumulatorRegistry registry = getPrincipalStylesheetModule().getStylesheetPackage().getAccumulatorRegistry(); |
| 168 |
accumulators = registry.getUsedAccumulators(useAccumulatorsAtt, this); |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 174 |
|
| 175 |
href = typeCheck("select", href); |
| 176 |
if (!hasChildNodes()) { |
| 177 |
compileWarning("An empty xsl:source-document instruction has no effect", SaxonErrorCode.SXWN9009); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 183 |
Configuration config = getConfiguration(); |
| 184 |
if (parseOptions.getSpaceStrippingRule() == null) { |
| 185 |
parseOptions.setSpaceStrippingRule(getPackageData().getSpaceStrippingRule()); |
| 186 |
} |
| 187 |
parseOptions.setApplicableAccumulators(accumulators); |
| 188 |
Expression action = compileSequenceConstructor(exec, decl, false); |
| 189 |
if (action == null) { |
| 190 |
|
| 191 |
return Literal.makeEmptySequence(); |
| 192 |
} |
| 193 |
try { |
| 194 |
ExpressionVisitor visitor = makeExpressionVisitor(); |
| 195 |
action = action.simplify(); |
| 196 |
action = action.typeCheck(visitor, config.makeContextItemStaticInfo(NodeKindTest.DOCUMENT, false)); |
| 197 |
|
| 198 |
return config.makeStreamInstruction( |
| 199 |
href, action, streaming, parseOptions, null, allocateLocation(), |
| 200 |
makeRetainedStaticContext()); |
| 201 |
|
| 202 |
} catch (XPathException err) { |
| 203 |
compileError(err); |
| 204 |
return null; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
} |