Debian Codegraph @

net/sf/saxon/expr/Component.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.expr;
9
10 import net.sf.saxon.expr.instruct.Actor;
11 import net.sf.saxon.expr.instruct.AttributeSet;
12 import net.sf.saxon.expr.instruct.GlobalVariable;
13 import net.sf.saxon.expr.instruct.NamedTemplate;
14 import net.sf.saxon.om.Function;
15 import net.sf.saxon.om.StandardNames;
16 import net.sf.saxon.style.StylesheetPackage;
17 import net.sf.saxon.trace.ExpressionPresenter;
18 import net.sf.saxon.trans.Mode;
19 import net.sf.saxon.trans.Visibility;
20 import net.sf.saxon.trans.VisibilityProvenance;
21 import net.sf.saxon.trans.XPathException;
22 import net.sf.saxon.tree.util.FastStringBuffer;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27
28 /**
29 * Represents a component as defined in the XSLT 3.0 specification: for example a function, a named template,
30 * an attribute set, a global variable.
31 */
32
33 public class Component {
34
35 protected Actor actor;
36 private Visibility visibility;
37 private List<ComponentBinding> bindings = new ArrayList<ComponentBinding>();
38 private StylesheetPackage containingPackage;
39 private StylesheetPackage declaringPackage;
40 private VisibilityProvenance provenance;
41 private Component baseComponent;
42
43
44 private Component() {}
45
46 /**
47 * Create a component
48 * @param actor the compiled code that implements the component, for example a Template or Function
49 * @param visibility the visibility of the component
50 * @param provenance indicates where the visibility property came from
51 * @param containingPackage the package to which this component belongs
52 * @param declaringPackage the package in which the original declaration of the component appears
53 */
54
55 public static Component makeComponent(
56 Actor actor, Visibility visibility, VisibilityProvenance provenance, StylesheetPackage containingPackage, StylesheetPackage declaringPackage) {
57 Component c;
58 if (actor instanceof Mode) {
59 c = new M();
60 } else {
61 c = new Component();
62 }
63 c.actor = actor;
64 c.visibility = visibility;
65 c.provenance = provenance;
66 c.containingPackage = containingPackage;
67 c.declaringPackage = declaringPackage;
68 return c;
69 }
70
71 /**
72 * Get the component's binding vector; that is the list of external references to other components
73 *
74 * @return the binding vector, a list of component bindings. These are identified by a binding
75 * slot number held with the individual instruction (such as a call-template instruction or a global
76 * variable reference) that contains the external component reference.
77 */
78
79 public List<ComponentBinding> getComponentBindings() {
80 return bindings;
81 }
82
83 /**
84 * Set the component's binding vector; that is the list of external references to other components
85 *
86 * @param bindings the binding vector, a list of component bindings. These are identified by a binding
87 * slot number held with the individual instruction (such as a call-template instruction or a global
88 * variable reference) that contains the external component reference.
89 */
90
91 public void setComponentBindings(List<ComponentBinding> bindings) {
92 this.bindings = bindings;
93 }
94
95 /**
96 * Set the visibility of the component, and say whether it is explicit or defaulted
97 * @param visibility the visibility of the component
98 * @param provenance indicates where the visibility property came from
99 */
100
101 public void setVisibility(Visibility visibility, VisibilityProvenance provenance) {
102 this.visibility = visibility;
103 this.provenance = provenance;
104 }
105
106 /**
107 * Get the visibility of the component
108 *
109 * @return the component's visibility. In the declaring package this will be the original
110 * declared or exposed visibility; in a using package, it will be the visibility of the component
111 * within that package.
112 */
113
114 public Visibility getVisibility() {
115 return visibility;
116 }
117
118 /**
119 * Determine whether the visibility of the component is due to an explicit visibility attribute
120 * on the component declaration, or whether it was inferred, or came from an xsl:expose or xsl:accept
121 * declaration
122 * @return the provenance of the visibility property
123 */
124
125 public VisibilityProvenance getVisibilityProvenance() {
126 return provenance;
127 }
128
129 /**
130 * Ask whether this is a hidden component derived from an abstract component with no
131 * implementation
132 * @return true if this component is hidden, and its base component is abstract
133 */
134
135 public boolean isHiddenAbstractComponent() {
136 return visibility == Visibility.HIDDEN && baseComponent != null &&
137 baseComponent.getVisibility() == Visibility.ABSTRACT;
138 }
139
140 /**
141 * Get the actor (for example a compiled template, function, or variable) that is executed
142 * when this component is called
143 *
144 * @return the code forming the implementation of this component
145 */
146
147 public Actor getActor() {
148 return actor;
149 }
150
151 public int getComponentKind() {
152 if (actor instanceof NamedTemplate) {
153 return StandardNames.XSL_TEMPLATE;
154 } else if (actor instanceof GlobalVariable) {
155 return StandardNames.XSL_VARIABLE;
156 } else if (actor instanceof Function) {
157 return StandardNames.XSL_FUNCTION;
158 } else if (actor instanceof AttributeSet) {
159 return StandardNames.XSL_ATTRIBUTE_SET;
160 } else if (actor instanceof Mode) {
161 return StandardNames.XSL_MODE;
162 } else {
163 return -1;
164 }
165 }
166
167 /**
168 * Get the declaring package of this component
169 *
170 * @return the package in which the code of the component was originally declared
171 */
172
173 public StylesheetPackage getDeclaringPackage() {
174 return declaringPackage;
175 }
176
177 /**
178 * Get the containing package of this component
179 *
180 * @return the package that contains this (version of the) component
181 */
182
183 public StylesheetPackage getContainingPackage() {
184 return containingPackage;
185 }
186
187 /**
188 * Get the component from which this one is derived
189 * @return the component from which this one is derived. This is set when the component appears in the
190 * package as a result of xsl:use-package; a component C in the used package is modified to create a
191 * component D in the using package, and D.getBaseComponent() returns C. The value will be null
192 * in the case of a component whose declaring package is the same as its containing package, that is,
193 * an "original" component (including an overriding component)
194 */
195
196 public Component getBaseComponent() {
197 return baseComponent;
198 }
199
200 /**
201 * Set the component from which this one is derived
202 *
203 * @param original the component from which this one is derived. This is set when the component appears in the
204 * package as a result of xsl:use-package; a component C in the used package is modified to create a
205 * component D in the using package, and D.getBaseComponent() returns C. The value will be null
206 * in the case of a component whose declaring package is the same as its containing package, that is,
207 * an "original" component (including an overriding component)
208 */
209
210 public void setBaseComponent(Component original) {
211 baseComponent = original;
212 }
213
214 public void export(ExpressionPresenter out, Map<Component, Integer> componentIdMap,
215 Map<StylesheetPackage, Integer> packageIdMap) throws XPathException {
216 out.startElement("co");
217 int id = obtainComponentId(this, componentIdMap);
218 out.emitAttribute("id", ""+id);
219 if (getVisibilityProvenance() != VisibilityProvenance.DEFAULTED) {
220 out.emitAttribute("vis", getVisibility().toString());
221 }
222 String refs = listComponentReferences(componentIdMap);
223 out.emitAttribute("binds", refs);
224 if (baseComponent != null && getActor() == baseComponent.getActor()) {
225 int baseId = obtainComponentId(baseComponent, componentIdMap);
226 out.emitAttribute("base", ""+baseId);
227 out.emitAttribute("dpack", packageIdMap.get(declaringPackage)+"");
228 } else {
229 actor.export(out);
230 }
231 out.endElement();
232 }
233
234 public String listComponentReferences(Map<Component, Integer> componentIdMap) {
235 FastStringBuffer fsb = new FastStringBuffer(128);
236 for (ComponentBinding ref : getComponentBindings()) {
237 Component target = ref.getTarget();
238 int targetId = obtainComponentId(target, componentIdMap);
239 if (fsb.length() != 0) {
240 fsb.append(" ");
241 }
242 fsb.append("" + targetId);
243 }
244 return fsb.toString();
245 }
246
247 private int obtainComponentId(Component component, Map<Component, Integer> componentIdMap) {
248 Integer id = componentIdMap.get(component);
249 if (id == null) {
250 id = componentIdMap.size();
251 componentIdMap.put(component, id);
252 }
253 return id;
254 }
255
256 public static class M extends Component {
257
258 /**
259 * Get the actor (in this case a Mode) that is executed
260 * when this component is called
261 *
262 * @return the code forming the implementation of this component
263 */
264 @Override
265 public Mode getActor() {
266 return (Mode)super.getActor();
267 }
268
269 public void setActor(Mode m) {
270 this.actor = m;
271 }
272 }
273
274 }
275