Debian Codegraph @

net/sf/saxon/style/XSLMode.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.Configuration;
11 import net.sf.saxon.expr.Component;
12 import net.sf.saxon.expr.accum.Accumulator;
13 import net.sf.saxon.lib.NamespaceConstant;
14 import net.sf.saxon.om.AttributeCollection;
15 import net.sf.saxon.om.StandardNames;
16 import net.sf.saxon.om.StructuredQName;
17 import net.sf.saxon.trans.*;
18 import net.sf.saxon.trans.rules.*;
19 import net.sf.saxon.value.Whitespace;
20
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.Set;
24
25 /**
26 * Handler for xsl:mode elements in stylesheet.
27 * The xsl:mode element defines the properties of a mode. The mode is identified
28 * by the name attribute, defaulting to the unnamed default mode (which may also be
29 * written "#default").
30 * <p>The attribute streamable="yes|no" which
31 * indicates whether templates in this mode are to be processed by streaming.</p>
32 * <p>The attribute on-no-match indicates which family of template rules
33 * should be used to process nodes when there is no explicit match</p>
34 */
35
36 public class XSLMode extends StyleElement {
37
38 private SimpleMode mode;
39 private Set<? extends Accumulator> accumulators;
40 private boolean prepared = false;
41 private boolean streamable = false;
42 private boolean failOnMultipleMatch = false;
43 private boolean warningOnNoMatch = false;
44 private boolean warningOnMultipleMatch = true;
45 private boolean traceMatching = false;
46 private BuiltInRuleSet defaultRules = TextOnlyCopyRuleSet.getInstance();
47
48 /**
49 * Ask whether this node is a declaration, that is, a permitted child of xsl:stylesheet
50 * (including xsl:include and xsl:import).
51 *
52 * @return true for this element
53 */
54
55 @Override
56 public boolean isDeclaration() {
57 return true;
58 }
59
60 /**
61 * Determine whether this node is an instruction.
62 *
63 * @return false - it is a declaration
64 */
65
66 public boolean isInstruction() {
67 return false;
68 }
69
70 /**
71 * Get a name identifying the object of the expression, for example a function name, template name,
72 * variable name, key name, element name, etc. This is used only where the name is known statically.
73 * If there is no name, the value will be null.
74 */
75
76 public StructuredQName getObjectName() {
77 StructuredQName qn = super.getObjectName();
78 if (qn == null) {
79 String nameAtt = Whitespace.trim(getAttributeValue("", "name"));
80 if (nameAtt == null) {
81 return Mode.UNNAMED_MODE_NAME;
82 }
83 try {
84 qn = makeQName(nameAtt);
85 setObjectName(qn);
86 } catch (XPathException err) {
87 return new StructuredQName("saxon", NamespaceConstant.SAXON, "badly-named-mode-" + generateId());
88 }
89 }
90 return qn;
91 }
92
93 /**
94 * Method supplied by declaration elements to add themselves to a stylesheet-level index
95 *
96 * @param decl the Declaration being indexed. (This corresponds to the StyleElement object
97 * except in cases where one module is imported several times with different precedence.)
98 * @param top the outermost XSLStylesheet element
99 * @throws XPathException if any error is encountered
100 */
101 @Override
102 public void index(ComponentDeclaration decl, PrincipalStylesheetModule top) throws XPathException {
103 StructuredQName name = getObjectName();
104 SymbolicName sName = new SymbolicName(StandardNames.XSL_MODE, name);
105 HashMap<SymbolicName, Component> componentIndex = top.getStylesheetPackage().getComponentIndex();
106 // see if there is already a named template with this precedence
107 if (!name.equals(Mode.UNNAMED_MODE_NAME)) {
108 Component other = componentIndex.get(sName);
109 if (other != null && other.getDeclaringPackage() != top.getStylesheetPackage()) {
110 compileError("Mode " + name.getDisplayName() +
111 " conflicts with a public named mode in package " +
112 other.getDeclaringPackage().getPackageName(), "XTSE3050");
113
114 }
115 }
116 mode = (SimpleMode)top.getRuleManager().obtainMode(name, true);
117 if (name.equals(Mode.UNNAMED_MODE_NAME)) {
118 top.getRuleManager().setUnnamedModeExplicit(true);
119 } else if (mode.getDeclaringComponent().getDeclaringPackage() != getContainingPackage()) {
120 compileError("Mode name conflicts with a mode in a used package", "XTSE3050");
121 } else {
122 top.indexMode(decl);
123 Visibility declaredVisibility = getDeclaredVisibility();
124 Visibility actualVisibility = declaredVisibility == null ? Visibility.PRIVATE : declaredVisibility;
125 VisibilityProvenance provenance = declaredVisibility == null ? VisibilityProvenance.DEFAULTED : VisibilityProvenance.EXPLICIT;
126 mode.getDeclaringComponent().setVisibility(actualVisibility, provenance);
127 }
128 }
129
130 public void prepareAttributes() throws XPathException {
131
132 String nameAtt = null;
133 String visibilityAtt = null;
134
135 if (prepared) {
136 return;
137 }
138 prepared = true;
139
140 AttributeCollection atts = getAttributeList();
141 Visibility visibility = Visibility.PRIVATE;
142
143 for (int a = 0; a < atts.getLength(); a++) {
144 String f = atts.getQName(a);
145 switch (f) {
146 case "streamable":
147 streamable = processStreamableAtt(atts.getValue(a));
148 break;
149 case "name":
150 nameAtt = Whitespace.trim(atts.getValue(a));
151 if (!nameAtt.equals("#default")) {
152 try {
153 setObjectName(makeQName(nameAtt));
154 } catch (XPathException err) {
155 compileError(err);
156 }
157 }
158 break;
159 case "use-accumulators":
160 String useAccAtt = atts.getValue(a);
161 accumulators = getPrincipalStylesheetModule().getStylesheetPackage()
162 .getAccumulatorRegistry().getUsedAccumulators(useAccAtt, this);
163
164 break;
165 case "on-multiple-match": {
166 String att = Whitespace.trim(atts.getValue(a));
167 switch (att) {
168 case "fail":
169 failOnMultipleMatch = true;
170 break;
171 case "use-last":
172 failOnMultipleMatch = false;
173 break;
174 default:
175 invalidAttribute(f, "fail|use-last");
176 break;
177 }
178 break;
179 }
180 case "on-no-match":
181 String value = Whitespace.trim(atts.getValue(a));
182 assert value != null;
183 switch (value) {
184 case "text-only-copy":
185 // no action, this is the default
186 break;
187 case "shallow-copy":
188 defaultRules = ShallowCopyRuleSet.getInstance();
189 break;
190 case "deep-copy":
191 defaultRules = DeepCopyRuleSet.getInstance();
192 break;
193 case "shallow-skip":
194 defaultRules = ShallowSkipRuleSet.getInstance();
195 break;
196 case "deep-skip":
197 defaultRules = DeepSkipRuleSet.getInstance();
198 break;
199 case "fail":
200 defaultRules = FailRuleSet.getInstance();
201 break;
202 default:
203 invalidAttribute(f, "text-only-copy|shallow-copy|deep-copy|shallow-skip|deep-skip|fail");
204 break;
205 }
206 break;
207 case "warning-on-multiple-match": {
208 String att = Whitespace.trim(atts.getValue(a));
209 warningOnMultipleMatch = processBooleanAttribute("warning-on-multiple-match", att);
210 break;
211 }
212 case "warning-on-no-match": {
213 String att = Whitespace.trim(atts.getValue(a));
214 warningOnNoMatch = processBooleanAttribute("warning-on-no-match", att);
215 break;
216 }
217 case "typed": {
218 String att = Whitespace.trim(atts.getValue(a));
219 checkAttributeValue("typed", att, false, new String[]{
220 "0", "1", "false", "lax", "no", "strict", "true", "unspecified", "yes"});
221 break;
222 }
223 case "visibility":
224 visibilityAtt = Whitespace.trim(atts.getValue(a));
225 visibility = interpretVisibilityValue(visibilityAtt, "");
226 if (visibility == Visibility.ABSTRACT) {
227 invalidAttribute(f, "public|private|final");
228 }
229 //if (visibility != Visibility.PRIVATE) {
230 mode.setDeclaredVisibility(visibility);
231 //}
232
233 break;
234 default:
235 if (atts.getURI(a).equals(NamespaceConstant.SAXON) && atts.getLocalName(a).equals("trace")) {
236 String att = Whitespace.trim(atts.getValue(a));
237 traceMatching = processBooleanAttribute("saxon:trace", att);
238 } else {
239 checkUnknownAttribute(atts.getNodeName(a));
240 }
241 break;
242 }
243 }
244
245 if (nameAtt == null && visibilityAtt != null && mode.getDeclaredVisibility() != Visibility.PRIVATE) {
246 compileError("The unnamed mode must be private", "XTSE0020");
247 }
248
249 RuleManager manager = getCompilation().getPrincipalStylesheetModule().getRuleManager();
250 if (getObjectName() == null) {
251 mode = manager.getUnnamedMode();
252 } else {
253 Mode m = manager.obtainMode(getObjectName(), true);
254 if (m instanceof SimpleMode) {
255 mode = (SimpleMode)m;
256 } else {
257 compileError("Mode name refers to an overridden mode");
258 mode = manager.getUnnamedMode();
259 }
260 }
261
262 mode.setStreamable(streamable);
263 if (streamable) {
264 Mode omniMode = manager.obtainMode(Mode.OMNI_MODE, true);
265 omniMode.setStreamable(true);
266 }
267 if (warningOnNoMatch) {
268 defaultRules = new RuleSetWithWarnings(defaultRules);
269 }
270 mode.setBuiltInRuleSet(defaultRules);
271
272 int recoveryPolicy;
273 if (failOnMultipleMatch) {
274 recoveryPolicy = Configuration.DO_NOT_RECOVER;
275 } else if (warningOnMultipleMatch) {
276 recoveryPolicy = Configuration.RECOVER_WITH_WARNINGS;
277 } else {
278 recoveryPolicy = Configuration.RECOVER_SILENTLY;
279 }
280 mode.setRecoveryPolicy(recoveryPolicy);
281 mode.obtainDeclaringComponent(this);
282 mode.setModeTracing(traceMatching);
283 }
284
285 public void validate(ComponentDeclaration decl) throws XPathException {
286 checkTopLevel("XTSE0010", false);
287 AttributeCollection atts = getAttributeList();
288
289 for (int a = 0; a < atts.getLength(); a++) {
290 String f = atts.getQName(a);
291 if (f.equals("streamable") || f.equals("on-multiple-match") || f.equals("on-no-match") ||
292 f.equals("warning-on-multiple-match") || f.equals("warning-on-no-match") || f.equals("typed")) {
293 String att = Whitespace.trim(atts.getValue(a));
294 String normalizedAtt;
295 if ("true".equals(att)||"1".equals(att)){
296 normalizedAtt = "yes";
297 } else if ("false".equals(att)||"0".equals(att)){
298 normalizedAtt = "no";
299 } else {
300 normalizedAtt = att;
301 }
302 mode.getActivePart().setExplicitProperty(f, normalizedAtt, decl.getPrecedence());
303 if (mode.isMustBeTyped() && getContainingPackage().getTargetEdition().matches("JS\\d?")) {
304 compileWarning("In Saxon-JS, all data is untyped", "XTTE3110");
305 }
306 } else if (f.equals("use-accumulators") && accumulators != null /*Can be null after an error*/) {
307 String[] names = new String[accumulators.size()];
308 int i=0;
309 for (Accumulator acc: accumulators) {
310 names[i++] = acc.getAccumulatorName().getEQName();
311 }
312 Arrays.sort(names);
313 String allNames = Arrays.toString(names);
314 mode.getActivePart().setExplicitProperty(f, allNames, decl.getPrecedence());
315 }
316 }
317 checkEmpty();
318 checkTopLevel("XTSE0010", false);
319 }
320
321 public void compileDeclaration(Compilation compilation, ComponentDeclaration decl) throws XPathException {
322 StylesheetPackage pack = getPrincipalStylesheetModule().getStylesheetPackage();
323 Component c = pack.getComponent(mode.getSymbolicName());
324 if (c == null) {
325 throw new AssertionError();
326 }
327 if (accumulators != null) {
328 mode.setAccumulators(accumulators);
329 }
330 }
331
332
333 }