Debian Codegraph @

net/sf/saxon/style/XSLExpose.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.Component;
11 import net.sf.saxon.expr.instruct.GlobalVariable;
12 import net.sf.saxon.expr.instruct.NamedTemplate;
13 import net.sf.saxon.expr.instruct.UserFunction;
14 import net.sf.saxon.om.StandardNames;
15 import net.sf.saxon.om.StructuredQName;
16 import net.sf.saxon.pattern.NameTest;
17 import net.sf.saxon.pattern.QNameTest;
18 import net.sf.saxon.trans.*;
19
20 import java.util.List;
21
22 /**
23 * Represents an xsl:expose element in an XSLT 3.0 package manifest.
24 */
25 public class XSLExpose extends XSLAcceptExpose {
26
27 protected void checkCompatibility(SymbolicName name, Visibility declared, Visibility exposed) throws XPathException {
28 if (!isCompatible(declared, exposed)) {
29 String code = "XTSE3010";
30 compileError("The " + name + " is declared as " + declared.show() + " and cannot be exposed as " + exposed.show(), code);
31 }
32 }
33
34 public static boolean isCompatible(Visibility declared, Visibility exposed) {
35 if (declared == null || declared == exposed) {
36 return true;
37 }
38 switch (declared) {
39 case PUBLIC:
40 return exposed == Visibility.PUBLIC || exposed == Visibility.PRIVATE ||
41 exposed == Visibility.FINAL || exposed == Visibility.HIDDEN;
42 case ABSTRACT:
43 return exposed == Visibility.ABSTRACT || exposed == Visibility.HIDDEN;
44 case FINAL:
45 return exposed == Visibility.PRIVATE ||
46 exposed == Visibility.FINAL || exposed == Visibility.HIDDEN;
47 default:
48 return false;
49 }
50 }
51
52 /**
53 * Check that the stylesheet element is valid. This is called once for each element, after
54 * the entire tree has been built. As well as validation, it can perform first-time
55 * initialisation. The default implementation does nothing; it is normally overriden
56 * in subclasses.
57 *
58 * @param decl the declaration to be validated
59 * @throws net.sf.saxon.trans.XPathException
60 * if any error is found during validation
61 */
62 @Override
63 public void validate(ComponentDeclaration decl) throws XPathException {
64 PrincipalStylesheetModule psm = getPrincipalStylesheetModule();
65 Visibility exposedVisibility = getVisibility();
66
67 // The following code checks that explicit references to components (as distinct from
68 // wildcards) refer to actual components, and that the exposed visibility is consistent
69 // with the declared visibility. It doesn't actually change the component's visibility property.
70 // This is done later, in PrincipalStylesheetModuleEE#adjustExposedVisibility.
71
72 for (ComponentTest test : getExplicitComponentTests()) {
73 QNameTest nameTest = test.getQNameTest();
74 if (nameTest instanceof NameTest) {
75 StructuredQName qName = ((NameTest) nameTest).getMatchingNodeName();
76 int kind = test.getComponentKind();
77 SymbolicName sName = kind == StandardNames.XSL_FUNCTION ?
78 new SymbolicName.F(((NameTest) nameTest).getMatchingNodeName(), test.getArity()) :
79 new SymbolicName(kind, ((NameTest) nameTest).getMatchingNodeName());
80 boolean found = false;
81 switch(kind) {
82 case StandardNames.XSL_TEMPLATE: {
83 NamedTemplate template = psm.getNamedTemplate(qName);
84 found = template != null;
85 if (found) {
86 Visibility declared = template.getDeclaredVisibility();
87 checkCompatibility(template.getSymbolicName(), declared, exposedVisibility);
88 //template.getDeclaringComponent().setVisibility(exposedVisibility, false);
89 }
90 break;
91 }
92 case StandardNames.XSL_VARIABLE: {
93 SourceBinding binding = psm.getGlobalVariableBinding(qName);
94 if (binding != null && !(binding.getSourceElement() instanceof XSLGlobalParam)) {
95 found = true;
96 }
97 if (found) {
98 GlobalVariable var = ((XSLGlobalVariable) binding.getSourceElement()).getCompiledVariable();
99 Visibility declared = var.getDeclaredVisibility();
100 checkCompatibility(var.getSymbolicName(), declared, getVisibility());
101 //var.getDeclaringComponent().setVisibility(exposedVisibility, false);
102 }
103 break;
104 }
105 case StandardNames.XSL_ATTRIBUTE_SET: {
106 List<ComponentDeclaration> declarations = psm.getAttributeSetDeclarations(qName);
107 found = declarations != null && !declarations.isEmpty();
108 if (found) {
109 Visibility declared = declarations.get(0).getSourceElement().getDeclaredVisibility();
110 checkCompatibility(sName, declared, getVisibility());
111 }
112 break;
113 }
114 case StandardNames.XSL_MODE:
115 Mode mode = psm.getRuleManager().obtainMode(qName, false);
116 found = mode != null;
117 if (found) {
118 checkCompatibility(sName, mode.getDeclaredVisibility(), getVisibility());
119 }
120 if (getVisibility() == Visibility.ABSTRACT) {
121 // obviously wrong, though I don't see a rule in the spec
122 compileError("The visibility of a mode cannot be abstract");
123 }
124 break;
125 case StandardNames.XSL_FUNCTION:
126 StylesheetPackage pack = psm.getStylesheetPackage();
127 if (test.getArity() == -1) {
128 // This will match any function of the required name, regardless of arity
129 for (int i = 0; i <= pack.getMaxFunctionArity(); i++) {
130 sName = new SymbolicName.F(((NameTest) nameTest).getMatchingNodeName(), i);
131 Component fn = pack.getComponent(sName);
132 if (fn != null) {
133 found = true;
134 UserFunction userFunction = (UserFunction)fn.getActor();
135 checkCompatibility(sName, userFunction.getDeclaredVisibility(), getVisibility());
136 }
137 }
138 } else {
139 Component fn = pack.getComponent(sName);
140 found = fn != null;
141 if (found) {
142 UserFunction userFunction = (UserFunction) fn.getActor();
143 checkCompatibility(sName, userFunction.getDeclaredVisibility(), getVisibility());
144 }
145 }
146 break;
147 }
148 if (!found) {
149 compileError("No " + sName.toString() + " exists in the containing package", "XTSE3020");
150 }
151
152 }
153 }
154
155 }
156
157 }