Debian Codegraph @

net/sf/saxon/style/XSLAccept.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.om.NodeInfo;
12 import net.sf.saxon.om.StandardNames;
13 import net.sf.saxon.pattern.NameTest;
14 import net.sf.saxon.pattern.QNameTest;
15 import net.sf.saxon.trans.*;
16
17 /**
18 * Represents an xsl:accept element in an XSLT 3.0 package manifest.
19 */
20 public class XSLAccept extends XSLAcceptExpose {
21 @Override
22 protected void prepareAttributes() throws XPathException {
23 super.prepareAttributes();
24 }
25
26 /**
27 * Check that the stylesheet element is valid. This is called once for each element, after
28 * the entire tree has been built. As well as validation, it can perform first-time
29 * initialisation. The default implementation does nothing; it is normally overriden
30 * in subclasses.
31 *
32 * @param decl the declaration to be validated
33 * @throws net.sf.saxon.trans.XPathException
34 * if any error is found during validation
35 */
36 @Override
37 public void validate(ComponentDeclaration decl) throws XPathException {
38 NodeInfo parent = getParent();
39 if (!(parent instanceof XSLUsePackage)) {
40 compileError("Parent of xsl:accept must be xsl:use-package");
41 return;
42 }
43 StylesheetPackage pack = ((XSLUsePackage)parent).getUsedPackage();
44 if (pack != null) {
45 for (ComponentTest test : getExplicitComponentTests()) {
46 QNameTest nameTest = test.getQNameTest();
47 if (nameTest instanceof NameTest) {
48 int kind = test.getComponentKind();
49 SymbolicName sName = kind == StandardNames.XSL_FUNCTION ?
50 new SymbolicName.F(((NameTest) nameTest).getMatchingNodeName(), test.getArity()) :
51 new SymbolicName(kind, ((NameTest) nameTest).getMatchingNodeName());
52 Component comp = pack.getComponent(sName);
53 boolean found = false;
54 if (comp == null) {
55 if (kind == StandardNames.XSL_FUNCTION && test.getArity() == -1) {
56 // This will match any function of the required name, regardless of arity
57 for (int i = 0; i <= pack.getMaxFunctionArity(); i++) {
58 sName = new SymbolicName.F(((NameTest) nameTest).getMatchingNodeName(), i);
59 comp = pack.getComponent(sName);
60 if (comp != null) {
61 checkCompatibility(sName, comp.getVisibility(), getVisibility());
62 found = true;
63 }
64 }
65
66 }
67 } else {
68 checkCompatibility(sName, comp.getVisibility(), getVisibility());
69 found = true;
70 }
71 if (!found) {
72 compileError("No " + sName.toString() + " exists in the used package", "XTSE3030");
73 }
74 }
75 }
76 }
77 }
78
79
80 /**
81 * Accept a component from a used package, modifying its visibility if necessary
82 *
83 * @param component the component to be accepted; as a side-effect of this method, the
84 * visibility of the component may change
85 * @throws XPathException if the requested visibility is incompatible with the declared
86 * visibility
87 */
88 public void acceptComponent(Component component) throws XPathException {
89 for (ComponentTest test : getExplicitComponentTests()) {
90 if (test.matches(component.getActor())) {
91 // we have already checked that the visibility is compatible
92 component.setVisibility(getVisibility(), VisibilityProvenance.ACCEPTED);
93 return;
94 }
95 }
96 for (ComponentTest test : getWildcardComponentTests()) {
97 if (test.matches(component.getActor())) {
98 if (isCompatible(component.getVisibility(), getVisibility())) {
99 // set the visibility if it is compatible
100 component.setVisibility(getVisibility(), VisibilityProvenance.ACCEPTED);
101 return;
102 }
103 }
104 }
105 }
106
107 protected void checkCompatibility(SymbolicName name, Visibility declared, Visibility exposed) throws XPathException {
108 if (!isCompatible(declared, exposed)) {
109 String code = "XTSE3040";
110 compileError("The " + name + " is declared as " + declared.show() + " and cannot be accepted as " + exposed.show(), code);
111 }
112 }
113
114 public static boolean isCompatible(Visibility declared, Visibility exposed) {
115 // if (declared == null || declared == exposed) {
116 // return true;
117 // }
118 switch (declared) {
119 case PUBLIC:
120 return exposed == Visibility.PUBLIC || exposed == Visibility.PRIVATE ||
121 exposed == Visibility.FINAL || exposed == Visibility.HIDDEN;
122 case ABSTRACT:
123 return exposed == Visibility.ABSTRACT || exposed == Visibility.HIDDEN;
124 case FINAL:
125 return exposed == Visibility.PRIVATE ||
126 exposed == Visibility.FINAL || exposed == Visibility.HIDDEN;
127 default:
128 return false;
129 }
130 }
131 }