Debian Codegraph @

net/sf/saxon/style/XSLResultDocument.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.style;
9
10 import net.sf.saxon.expr.ErrorExpression;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.Literal;
13 import net.sf.saxon.expr.StringLiteral;
14 import net.sf.saxon.expr.instruct.ResultDocument;
15 import net.sf.saxon.lib.Feature;
16 import net.sf.saxon.lib.NamespaceConstant;
17 import net.sf.saxon.lib.SaxonOutputKeys;
18 import net.sf.saxon.lib.Validation;
19 import net.sf.saxon.om.*;
20 import net.sf.saxon.trans.SaxonErrorCode;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.tree.iter.AxisIterator;
23 import net.sf.saxon.type.SchemaType;
24 import net.sf.saxon.value.EmptySequence;
25 import net.sf.saxon.value.Whitespace;
26
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Properties;
31
32 /**
33 * An xsl:result-document element in the stylesheet.
34 * <p>
35 * The xsl:result-document element takes an attribute href="filename". The filename will
36 * often contain parameters, e.g. {position()} to ensure that a different file is produced
37 * for each element instance.
38 * <p>
39 * There is a further attribute "format" which determines the format of the
40 * output file, it identifies the name of an xsl:output element containing the output
41 * format details.
42 */
43
44 public class XSLResultDocument extends StyleElement {
45
46 public static final HashSet<String> fans = new HashSet<>(40); // formatting attribute names
47
48 static {
49 fans.add("allow-duplicate-names");
50 fans.add("build-tree");
51 fans.add("byte-order-mark");
52 fans.add("cdata-section-elements");
53 fans.add("doctype-public");
54 fans.add("doctype-system");
55 fans.add("encoding");
56 fans.add("escape-uri-attributes");
57 fans.add("html-version");
58 fans.add("include-content-type");
59 fans.add("indent");
60 fans.add("item-separator");
61 fans.add("json-node-output-method");
62 fans.add("media-type");
63 fans.add("method");
64 fans.add("normalization-form");
65 fans.add("omit-xml-declaration");
66 fans.add("output-version");
67 fans.add("parameter-document");
68 fans.add("standalone");
69 fans.add("suppress-indentation");
70 fans.add("undeclare-prefixes");
71
72 fans.add(SaxonOutputKeys.ATTRIBUTE_ORDER);
73 fans.add(SaxonOutputKeys.CANONICAL);
74 fans.add(SaxonOutputKeys.CHARACTER_REPRESENTATION);
75 fans.add(SaxonOutputKeys.DOUBLE_SPACE);
76 fans.add(SaxonOutputKeys.INDENT_SPACES);
77 fans.add(SaxonOutputKeys.LINE_LENGTH);
78 fans.add(SaxonOutputKeys.NEWLINE);
79 fans.add(SaxonOutputKeys.NEXT_IN_CHAIN);
80 fans.add(SaxonOutputKeys.RECOGNIZE_BINARY);
81 fans.add(SaxonOutputKeys.REQUIRE_WELL_FORMED);
82 fans.add(SaxonOutputKeys.PROPERTY_ORDER);
83 fans.add(SaxonOutputKeys.SINGLE_QUOTES);
84 fans.add(SaxonOutputKeys.SUPPLY_SOURCE_LOCATOR);
85
86 }
87
88 private Expression href;
89 private StructuredQName formatQName; // used when format is a literal string
90 private Expression formatExpression; // used when format is an AVT
91 private int validationAction = Validation.STRIP;
92 private SchemaType schemaType = null;
93 private Map<StructuredQName, Expression> serializationAttributes = new HashMap<>(10);
94 private boolean async = true;
95
96 /**
97 * Determine whether this node is an instruction.
98 *
99 * @return true - it is an instruction
100 */
101
102 public boolean isInstruction() {
103 return true;
104 }
105
106 /**
107 * Determine whether this type of element is allowed to contain a template-body
108 *
109 * @return true: yes, it may contain a template-body
110 */
111
112 public boolean mayContainSequenceConstructor() {
113 return true;
114 }
115
116 public void prepareAttributes() throws XPathException {
117 AttributeCollection atts = getAttributeList();
118
119 String formatAttribute = null;
120 String hrefAttribute = null;
121 String validationAtt = null;
122 String typeAtt = null;
123 String useCharacterMapsAtt = null;
124
125
126 for (int a = 0; a < atts.getLength(); a++) {
127 StructuredQName name = atts.getNodeName(a).getStructuredQName();
128 String f = name.getClarkName();
129 if (f.equals("format")) {
130 formatAttribute = Whitespace.trim(atts.getValue(a));
131 formatExpression = makeAttributeValueTemplate(formatAttribute, a);
132 } else if (f.equals("href")) {
133 hrefAttribute = Whitespace.trim(atts.getValue(a));
134 href = makeAttributeValueTemplate(hrefAttribute, a);
135 } else if (f.equals("validation")) {
136 validationAtt = Whitespace.trim(atts.getValue(a));
137 } else if (f.equals("type")) {
138 typeAtt = Whitespace.trim(atts.getValue(a));
139 } else if (f.equals("use-character-maps")) {
140 useCharacterMapsAtt = Whitespace.trim(atts.getValue(a));
141 } else if (fans.contains(f) || (f.startsWith("{") && !StandardNames.SAXON_ASYCHRONOUS.equals(f))) {
142 // this is a serialization attribute
143 String val = atts.getValue(a);
144 if (!f.equals(SaxonOutputKeys.ITEM_SEPARATOR) && !f.equals(SaxonOutputKeys.NEWLINE)) {
145 val = Whitespace.trim(atts.getValue(a));
146 }
147 Expression exp = makeAttributeValueTemplate(val, a);
148 serializationAttributes.put(name, exp);
149 } else if (name.getLocalPart().equals("asynchronous") && name.hasURI(NamespaceConstant.SAXON)) {
150 async = processBooleanAttribute("saxon:asynchronous", atts.getValue(a));
151 if (getCompilation().getCompilerInfo().isCompileWithTracing()) {
152 async = false;
153 } else if (!"EE".equals(getConfiguration().getEditionCode())) {
154 compileWarning("saxon:asynchronous - ignored when not running Saxon-EE",
155 SaxonErrorCode.SXWN9013);
156 async = false;
157 }
158 } else {
159 checkUnknownAttribute(atts.getNodeName(a));
160 }
161 }
162
163 if (formatAttribute != null) {
164 if (formatExpression instanceof StringLiteral) {
165 try {
166 formatQName = makeQName(((StringLiteral) formatExpression).getStringValue());
167 formatExpression = null;
168 } catch (XPathException err) {
169 compileError(err.getMessage(), "XTDE1460");
170 }
171 } else {
172 getPrincipalStylesheetModule().setNeedsDynamicOutputProperties(true);
173 }
174 }
175
176 if (validationAtt == null) {
177 validationAction = getDefaultValidation();
178 } else {
179 validationAction = validateValidationAttribute(validationAtt);
180 }
181 if (typeAtt != null) {
182 if (!isSchemaAware()) {
183 compileError("The @type attribute is available only with a schema-aware XSLT processor", "XTSE1660");
184 }
185 schemaType = getSchemaType(typeAtt);
186 validationAction = Validation.BY_TYPE;
187 }
188
189 if (typeAtt != null && validationAtt != null) {
190 compileError("The @validation and @type attributes are mutually exclusive", "XTSE1505");
191 }
192
193 if (useCharacterMapsAtt != null) {
194 String s = XSLOutput.prepareCharacterMaps(this, useCharacterMapsAtt, new Properties());
195 serializationAttributes.put(new StructuredQName("", "", "use-character-maps"),
196 new StringLiteral(s));
197 }
198 }
199
200 public void validate(ComponentDeclaration decl) throws XPathException {
201 if (href != null && !getConfiguration().getBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS)) {
202 compileError("xsl:result-document is disabled when extension functions are disabled");
203 }
204 href = typeCheck("href", href);
205 formatExpression = typeCheck("format", formatExpression);
206
207 for (StructuredQName prop : serializationAttributes.keySet()) {
208 final Expression exp1 = serializationAttributes.get(prop);
209 final Expression exp2 = typeCheck(prop.getDisplayName(), exp1);
210 if (exp1 != exp2) {
211 serializationAttributes.put(prop, exp2);
212 }
213 }
214
215 getContainingPackage().setCreatesSecondaryResultDocuments(true);
216
217 }
218
219 public static StructuredQName METHOD = new StructuredQName("", "", "method");
220 public static StructuredQName BUILD_TREE = new StructuredQName("", "", "build-tree");
221
222 /*@Nullable*/
223 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
224
225 // Check that the call is not within xsl:variable or xsl:function.
226 // This is a dynamic error, but worth detecting statically.
227 // In fact this is a bit of a fudge. If a function or variable is inlined, we sometimes don't detect
228 // XTDE1480 at run-time. Doing this static check improves our chances, though it won't catch all cases.
229 AxisIterator ai = iterateAxis(AxisInfo.ANCESTOR);
230 NodeInfo node;
231 while ((node = ai.next()) != null) {
232 if (node instanceof XSLGeneralVariable || node instanceof XSLFunction) {
233 issueWarning("An xsl:result-document instruction inside " + node.getDisplayName() +
234 " will always fail at run-time", this);
235 return new ErrorExpression("Call to xsl:result-document while in temporary output state", "XTDE1480", false);
236 }
237 }
238
239 Properties globalProps;
240 if (formatExpression == null) {
241 try {
242 globalProps = getPrincipalStylesheetModule().gatherOutputProperties(formatQName);
243 } catch (XPathException err) {
244 compileError("Named output format has not been defined", "XTDE1460");
245 return null;
246 }
247 } else {
248 globalProps = new Properties();
249 getPrincipalStylesheetModule().setNeedsDynamicOutputProperties(true);
250 }
251
252 // If no serialization method was specified, we can work it out statically if the
253 // first contained instruction is a literal result element. This saves effort at run-time.
254
255 String method = null;
256 if (formatExpression == null &&
257 globalProps.getProperty("method") == null &&
258 serializationAttributes.get(METHOD) == null) {
259 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
260 NodeInfo first = kids.next();
261 if (first instanceof LiteralResultElement) {
262 if (first.getURI().equals(NamespaceConstant.XHTML) && first.getLocalPart().equals("html")) {
263 method = getEffectiveVersion() == 10 ? "xml" : "xhtml";
264 } else if (first.getLocalPart().equalsIgnoreCase("html") && first.getURI().isEmpty()) {
265 method = "html";
266 } else {
267 method = "xml";
268 }
269 globalProps.setProperty("method", method);
270 }
271 }
272
273 Properties localProps = new Properties();
274
275 HashSet<StructuredQName> fixed = new HashSet<>(10);
276 NamespaceResolver namespaceResolver = getStaticContext().getNamespaceResolver();
277 for (StructuredQName property : serializationAttributes.keySet()) {
278 Expression exp = serializationAttributes.get(property);
279 if (exp instanceof StringLiteral) {
280 String s = ((StringLiteral) exp).getStringValue();
281 String lname = property.getLocalPart();
282 String uri = property.getURI();
283 try {
284
285 ResultDocument.setSerializationProperty(localProps, uri, lname, s,
286 namespaceResolver, false, exec.getConfiguration());
287 fixed.add(property);
288 if (property.equals(METHOD)) {
289 method = s;
290 }
291 } catch (XPathException e) {
292 if (NamespaceConstant.SAXON.equals(e.getErrorCodeNamespace())) {
293 compileWarning(e.getMessage(), e.getErrorCodeQName());
294 } else {
295 e.setErrorCode("XTSE0020");
296 compileError(e);
297 }
298 }
299 }
300 }
301 for (StructuredQName p : fixed) {
302 serializationAttributes.remove(p);
303 }
304
305 ResultDocument inst = new ResultDocument(globalProps,
306 localProps,
307 href,
308 formatExpression,
309 validationAction,
310 schemaType,
311 serializationAttributes,
312 getContainingPackage().getCharacterMapIndex()
313 );
314
315 Expression content = compileSequenceConstructor(exec, decl, true);
316 if (content == null) {
317 content = Literal.makeLiteral(EmptySequence.getInstance());
318 }
319 inst.setContentExpression(content);
320 inst.setAsynchronous(async);
321 return inst;
322 }
323
324 }
325