Debian Codegraph @

net/sf/saxon/trans/CompoundMode.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;
9
10 import net.sf.saxon.expr.Component;
11 import net.sf.saxon.expr.ComponentBinding;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.om.NamePool;
15 import net.sf.saxon.style.StylesheetPackage;
16 import net.sf.saxon.trace.ExpressionPresenter;
17 import net.sf.saxon.trans.rules.*;
18
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23
24 /**
25 * A CompoundMode is a mode representing the templates contained within an xsl:override element in a using package
26 * together with the rules in the corresponding mode of the base package.
27 */
28 public class CompoundMode extends Mode {
29
30 private Mode base;
31 private SimpleMode overrides;
32 private int overridingPrecedence;
33
34 /**
35 * Create a compound Mode
36 * @param base the mode from the base (used) package
37 * @param overrides the mode containing (only) the overriding template rules from the using package
38 */
39
40 public CompoundMode(Mode base, SimpleMode overrides) {
41 super(base.getModeName());
42 if (!base.getModeName().equals(overrides.getModeName())) {
43 throw new AssertionError("Base and overriding modes must have the same name");
44 }
45 if (base.getModeName().equals(Mode.UNNAMED_MODE_NAME)) {
46 throw new AssertionError("Cannot override an unnamed mode");
47 }
48 if (base.getModeName().equals(Mode.OMNI_MODE)) {
49 throw new AssertionError("Cannot override mode='#all'");
50 }
51 this.base = base;
52 this.overrides = overrides;
53 this.mustBeTyped = base.mustBeTyped;
54 this.mustBeUntyped = base.mustBeUntyped;
55 this.overridingPrecedence = base.getMaxPrecedence() + 1;
56 }
57
58 /**
59 * Get the built-in template rules to be used with this Mode in the case where there is no
60 * explicit template rule
61 *
62 * @return the built-in rule set, defaulting to the TextOnlyCopyRuleSet if no other rule set has
63 * been supplied
64 */
65
66 public BuiltInRuleSet getBuiltInRuleSet() {
67 return base.getBuiltInRuleSet();
68 }
69
70 /**
71 * Get the active component of this mode. For a simple mode this is the mode itself;
72 * for a compound mode it is the "overriding" part
73 */
74 @Override
75 public SimpleMode getActivePart() {
76 return overrides;
77 }
78
79 /**
80 * Ask whether there are any template rules in this mode
81 * (a mode could exist merely because it is referenced in apply-templates)
82 *
83 * @return true if no template rules exist in this mode
84 */
85 @Override
86 public boolean isEmpty() {
87 return base.isEmpty() && overrides.isEmpty();
88 }
89
90 /**
91 * Get the maximum precedence of the rules in this mode
92 */
93 @Override
94 public int getMaxPrecedence() {
95 return overridingPrecedence;
96 }
97
98 /**
99 * Get the highest rank of the rules in this mode
100 * @return the highest rank
101 */
102
103 public int getMaxRank() {
104 return overrides.getMaxRank();
105 }
106
107 /**
108 * Compute a rank for each rule, as a combination of the precedence and priority, to allow
109 * rapid comparison. This method also checks that there are no conflicts for
110 * property values in different xsl:mode declarations
111 *
112 * @param start the lowest rank to use
113 * @throws XPathException if an error occurs processing the rules
114 */
115
116 public void computeRankings(int start) throws XPathException {
117 overrides.computeRankings(base.getMaxRank() + 1);
118 }
119
120 /**
121 * Walk over all the rules, applying a specified action to each one.
122 *
123 * @param action an action that is to be applied to all the rules in this Mode
124 * @throws XPathException if an error occurs processing any of the rules
125 */
126 @Override
127 public void processRules(RuleAction action) throws XPathException {
128 overrides.processRules(action);
129 base.processRules(action);
130 }
131
132 /**
133 * Get the "explicit namespaces" matched by this mode. Returns a set containing all the namespaces
134 * matched by specific template rules in this mode
135 *
136 * @param pool the NamePool for the configuration
137 * @return the set of all namespace URIs of names explicitly matched by rules in this mode
138 */
139 @Override
140 public Set<String> getExplicitNamespaces(NamePool pool) {
141 HashSet<String> r = new HashSet<String>();
142 r.addAll(base.getExplicitNamespaces(pool));
143 r.addAll(overrides.getExplicitNamespaces(pool));
144 return r;
145 }
146
147 /**
148 * Allocate slot numbers to all the external component references in this component
149 *
150 * @param pack the containing package
151 */
152
153 public void allocateAllBindingSlots(final StylesheetPackage pack) {
154 if (!bindingSlotsAllocated) {
155 List<ComponentBinding> baseBindings = base.getDeclaringComponent().getComponentBindings();
156 List<ComponentBinding> newBindings = new ArrayList<ComponentBinding>(baseBindings);
157 Component comp = getDeclaringComponent();
158 comp.setComponentBindings(newBindings);
159 SimpleMode.forceAllocateAllBindingSlots(pack, overrides, newBindings);
160 bindingSlotsAllocated = true;
161 }
162 }
163
164 /**
165 * Get the rule corresponding to a given item, by finding the best pattern match.
166 *
167 * @param item the item to be matched
168 * @param context the XPath dynamic evaluation context
169 * @return the best matching rule, if any (otherwise null).
170 * @throws XPathException if an error occurs matching a pattern
171 */
172
173 public Rule getRule(Item item, XPathContext context) throws XPathException {
174 Rule r = overrides.getRule(item, context);
175 if (r == null) {
176 r = base.getRule(item, context);
177 }
178 return r;
179 }
180
181 @Override
182 public int getStackFrameSlotsNeeded() {
183 return Math.max(base.getStackFrameSlotsNeeded(), overrides.getStackFrameSlotsNeeded());
184 }
185
186 /**
187 * Get the rule corresponding to a given item, by finding the best Pattern match.
188 *
189 * @param item the item to be matched
190 * @param context the XPath dynamic evaluation context
191 * @param filter a filter to select which rules should be considered
192 * @return the best matching rule, if any (otherwise null).
193 * @throws XPathException if an error occurs
194 */
195 @Override
196 public Rule getRule(Item item, XPathContext context, SimpleMode.RuleFilter filter) throws XPathException {
197 Rule r = overrides.getRule(item, context, filter);
198 if (r == null) {
199 r = base.getRule(item, context, filter);
200 }
201 return r;
202 }
203
204 /**
205 * Export expression structure. The abstract expression tree
206 * is written to the supplied outputstream.
207 *
208 * @param presenter the expression presenter used to generate the XML representation of the structure
209 */
210 @Override
211 public void exportTemplateRules(ExpressionPresenter presenter) throws XPathException {
212 overrides.exportTemplateRules(presenter);
213 base.exportTemplateRules(presenter);
214 }
215
216 /**
217 * Explain expression structure. The abstract expression tree
218 * is written to the supplied outputstream.
219 *
220 * @param presenter the expression presenter used to generate the XML representation of the structure
221 */
222 @Override
223 public void explainTemplateRules(ExpressionPresenter presenter) throws XPathException {
224 overrides.explainTemplateRules(presenter);
225 base.explainTemplateRules(presenter);
226 }
227 }