Debian Codegraph @

net/sf/saxon/trans/rules/RuleManager.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.trans.rules;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.Component;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.expr.instruct.TemplateRule;
14 import net.sf.saxon.expr.parser.ExpressionTool;
15 import net.sf.saxon.expr.parser.Token;
16 import net.sf.saxon.om.Item;
17 import net.sf.saxon.om.StructuredQName;
18 import net.sf.saxon.pattern.*;
19 import net.sf.saxon.style.StylesheetModule;
20 import net.sf.saxon.style.StylesheetPackage;
21 import net.sf.saxon.trace.ExpressionPresenter;
22 import net.sf.saxon.trans.*;
23
24 import java.util.Collection;
25 import java.util.HashMap;
26
27 /**
28 * <B>RuleManager</B> maintains a set of template rules, one set for each mode
29 */
30
31 public final class RuleManager {
32
33 private StylesheetPackage stylesheetPackage;
34 private Configuration config;
35 private SimpleMode unnamedMode; // template rules with default mode
36 private HashMap<StructuredQName, Mode> modes;
37 // tables of rules for non-default modes
38 private SimpleMode omniMode = null; //template rules that specify mode="all"
39 private int recoveryPolicy;
40 private boolean unnamedModeExplicit;
41 private CompilerInfo compilerInfo; // We may need access to information on the compilation as distinct from the configuration
42 private int nextSequenceNumber = 0;
43
44 /**
45 * create a RuleManager and initialise variables.
46 */
47
48 public RuleManager(StylesheetPackage pack) {
49 this(pack, pack.getConfiguration().getDefaultXsltCompilerInfo());
50 }
51
52 public RuleManager(StylesheetPackage pack, CompilerInfo compilerInfo) {
53 this.stylesheetPackage = pack;
54 this.config = pack.getConfiguration();
55 this.compilerInfo = compilerInfo;
56 this.unnamedMode = config.makeMode(Mode.UNNAMED_MODE_NAME, this.compilerInfo);
57 Component c = unnamedMode.makeDeclaringComponent(Visibility.PRIVATE, stylesheetPackage);
58 c.setVisibility(Visibility.PRIVATE, VisibilityProvenance.DEFAULTED);
59 this.stylesheetPackage.addComponent(c);
60 this.unnamedMode.setRecoveryPolicy(recoveryPolicy);
61 this.modes = new HashMap<>(5);
62 }
63
64 /**
65 * Say that the unnamed mode has been explicitly declared in an xsl:mode declaration
66 *
67 * @param declared true if it has been explicitly declared
68 */
69
70 public void setUnnamedModeExplicit(boolean declared) {
71 unnamedModeExplicit = declared;
72 }
73
74 /**
75 * Ask whether the unnamed mode has been explicitly declared in an xsl:mode declaration
76 *
77 * @return true if it has been explicitly declared
78 */
79
80 public boolean isUnnamedModeExplicit() {
81 return unnamedModeExplicit;
82 }
83
84 /**
85 * Set the compiler information specifically.
86 *
87 * @param compilerInfo the compiler options in use
88 */
89 public void setCompilerInfo(CompilerInfo compilerInfo) {
90 this.compilerInfo = compilerInfo;
91 }
92
93 /**
94 * Set the policy for handling recoverable errors. Note that for some errors the decision can be
95 * made at run-time, but for the "ambiguous template match" error, the decision is (since 9.2)
96 * fixed at compile time.
97 *
98 * @param policy the recovery policy to be used. The options are {@link net.sf.saxon.Configuration#RECOVER_SILENTLY},
99 * {@link net.sf.saxon.Configuration#RECOVER_WITH_WARNINGS}, or {@link net.sf.saxon.Configuration#DO_NOT_RECOVER}.
100 * @since 9.2
101 */
102
103 public void setRecoveryPolicy(int policy) {
104 recoveryPolicy = policy;
105 unnamedMode.setRecoveryPolicy(policy);
106 }
107
108 /**
109 * Get the policy for handling recoverable errors. Note that for some errors the decision can be
110 * made at run-time, but for the "ambiguous template match" error, the decision is (since 9.2)
111 * fixed at compile time.
112 *
113 * @return the current policy.
114 * @since 9.2
115 */
116
117 public int getRecoveryPolicy() {
118 return recoveryPolicy;
119 }
120
121 /**
122 * Get all registered modes
123 *
124 * @return a collection containing all registered modes excluding the unnamed mode
125 */
126
127 public Collection<Mode> getAllNamedModes() {
128 return modes.values();
129 }
130
131 /**
132 * Get the mode object for the unnamed mode
133 *
134 * @return the unnamed mode
135 */
136
137 /*@NotNull*/
138 public SimpleMode getUnnamedMode() {
139 return unnamedMode;
140 }
141
142 /**
143 * Get the Mode object for a named mode. If there is not one already registered.
144 * a new Mode is created. This uses a makeMode() method in the configuration
145 *
146 * @param modeName The name of the mode. Supply null to get the default
147 * mode or Mode.ALL_MODES to get the Mode object containing "mode=all" rules
148 * @param createIfAbsent if true, then if the mode does not already exist it will be created.
149 * If false, then if the mode does not already exist the method returns null.
150 * But if the requested mode is the omnimode, it is considered to always exist,
151 * and is therefore created regardless.
152 * @return the Mode with this name
153 */
154
155
156 /*@Nullable*/
157 public Mode obtainMode(StructuredQName modeName, boolean createIfAbsent) {
158 if (modeName == null || modeName.equals(Mode.UNNAMED_MODE_NAME)) {
159 return unnamedMode;
160 }
161 if (modeName.equals(Mode.OMNI_MODE)) {
162 if (omniMode == null) {
163 omniMode = config.makeMode(modeName, compilerInfo);
164 omniMode.setRecoveryPolicy(recoveryPolicy);
165 }
166 return omniMode;
167 }
168
169 Mode m = modes.get(modeName);
170 if (m == null && createIfAbsent) {
171 m = config.makeMode(modeName, compilerInfo);
172 m.setRecoveryPolicy(recoveryPolicy);
173 modes.put(modeName, m);
174 Component c = m.makeDeclaringComponent(Visibility.PRIVATE, stylesheetPackage);
175 c.setVisibility(Visibility.PRIVATE, VisibilityProvenance.DEFAULTED);
176 stylesheetPackage.addComponent(c);
177 }
178 return m;
179 }
180
181 public void registerMode(Mode mode) {
182 modes.put(mode.getModeName(), mode);
183 }
184
185 public boolean existsOmniMode() {
186 return omniMode != null;
187 }
188
189 public int allocateSequenceNumber() {
190 return nextSequenceNumber++;
191 }
192
193 /**
194 * Register a template for a particular pattern.
195 *
196 * @param pattern Must be a valid Pattern.
197 * @param eh The Template to be used
198 * @param mode The processing mode to which this template applies
199 * @param module The stylesheet module containing the template rule
200 * @param priority The priority of the rule: if an element matches several patterns, the
201 * one with highest priority is used. The value is NaN if no explicit
202 * priority was specified
203 * @param position The relative position of the rule in declaration order
204 * @param part Zero for a "real" rule; an incremented integer for rules generated
205 * by splitting a real rule on a union pattern, in cases where no user-specified
206 * priority is supplied.
207 * @return the number of (sub-)rules registered
208 * @see Pattern
209 */
210
211 public int registerRule(Pattern pattern, TemplateRule eh,
212 Mode mode, StylesheetModule module, double priority, int position, int part) {
213
214 // for a union pattern, register the parts separately
215 // Technically this is only necessary if using default priorities and if the priorities
216 // of the two halves are different. However, splitting increases the chance that the pattern
217 // can be matched by hashing on the element name, so we do it always. But we need to do it
218 // in such a way that next-match only processes the template once (test case next-match-024)
219 if (pattern instanceof UnionPattern) {
220 UnionPattern up = (UnionPattern) pattern;
221 Pattern p1 = up.getLHS();
222 Pattern p2 = up.getRHS();
223 int lhsParts = registerRule(p1, eh, mode, module, priority, position, part);
224 int rhsParts = registerRule(p2, eh, mode, module, priority, position, lhsParts);
225 return lhsParts + rhsParts;
226 }
227 // some union patterns end up as a CombinedNodeTest. Need to split these.
228 // (Same reasoning as above)
229 if (pattern instanceof NodeTestPattern &&
230 pattern.getItemType() instanceof CombinedNodeTest &&
231 ((CombinedNodeTest) pattern.getItemType()).getOperator() == Token.UNION) {
232 CombinedNodeTest cnt = (CombinedNodeTest) pattern.getItemType();
233 NodeTest[] nt = cnt.getComponentNodeTests();
234 final NodeTestPattern nt0 = new NodeTestPattern(nt[0]);
235 ExpressionTool.copyLocationInfo(pattern, nt0);
236 int lhsParts = registerRule(nt0, eh, mode, module, priority, position, part);
237 final NodeTestPattern nt1 = new NodeTestPattern(nt[1]);
238 ExpressionTool.copyLocationInfo(pattern, nt1);
239 int rhsParts = registerRule(nt1, eh, mode, module, priority, position, lhsParts);
240 return lhsParts + rhsParts;
241 }
242 if (Double.isNaN(priority)) {
243 priority = pattern.getDefaultPriority();
244 } else {
245 // Priorities were user-allocated, so the rule-splitting is purely an optimization, so
246 // we give all sub-rules the same part number, meaning that next-match will only
247 // evaluate one of them
248 part = 0;
249 }
250
251 if (mode instanceof SimpleMode) {
252 ((SimpleMode) mode).addRule(pattern, eh, module, module.getPrecedence(), priority, position, part);
253 } else {
254 mode.getActivePart().addRule(pattern, eh, module, mode.getMaxPrecedence(), priority, position, part);
255 }
256 return 1;
257 }
258
259 /**
260 * Get the template rule matching a given item whose import precedence
261 * is in a particular range. This is used to support the xsl:apply-imports function
262 *
263 * @param item The item to be matched
264 * @param mode The mode for which a rule is required
265 * @param min The minimum import precedence that the rule must have
266 * @param max The maximum import precedence that the rule must have
267 * @param c The Controller for the transformation
268 * @return The template rule to be invoked
269 * @throws XPathException if an error occurs matching a pattern
270 */
271
272 public Rule getTemplateRule(Item item, Mode mode, int min, int max, XPathContext c)
273 throws XPathException {
274 // DO NOT DELETE: USED FROM BYTECODE
275 if (mode == null) {
276 mode = unnamedMode;
277 }
278 return mode.getRule(item, min, max, c);
279 }
280
281 /**
282 * Allocate rankings to the rules within each mode. This method must be called when all
283 * the rules in each mode are known. This method also checks that there are no conflicts for
284 * property values in different xsl:mode declarations
285 *
286 * @throws XPathException if an error occurs
287 */
288
289 public void computeRankings() throws XPathException {
290 unnamedMode.computeRankings(0);
291 for (Mode mode : modes.values()) {
292 mode.computeRankings(0);
293 }
294 }
295
296 /**
297 * Invert streamable templates in all streamable modes
298 *
299 * @throws XPathException if the templates are not streamable
300 */
301
302 public void invertStreamableTemplates() throws XPathException {
303 unnamedMode.invertStreamableTemplates();
304 for (Mode mode : modes.values()) {
305 mode.getActivePart().invertStreamableTemplates();
306 }
307 }
308
309 /**
310 * Check all modes for conflicts in property values
311 *
312 * @throws XPathException if a mode has conflicting property values
313 */
314
315 public void checkConsistency() throws XPathException {
316 unnamedMode.checkForConflictingProperties();
317 for (Mode mode : modes.values()) {
318 mode.getActivePart().checkForConflictingProperties();
319 }
320 }
321
322 /**
323 * Explain (that is, output the expression tree) for all template rules
324 *
325 * @param presenter the object used to present the output
326 */
327
328 public void explainTemplateRules(ExpressionPresenter presenter) throws XPathException {
329 unnamedMode.explain(presenter);
330 for (Mode mode : modes.values()) {
331 mode.explain(presenter);
332 }
333 }
334
335 /**
336 * Optimization of template rules
337 * Only invoked when rule optimization has been turned on.
338 * In this case we know the modes are instances of at least ModeEE
339 */
340 public void optimizeRules() {
341 unnamedMode.optimizeRules();
342 for (Mode mode : modes.values()) {
343 mode.getActivePart().optimizeRules();
344 }
345 }
346 }
347