Debian Codegraph @

net/sf/saxon/style/XSLAcceptExpose.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.om.*;
11 import net.sf.saxon.pattern.*;
12 import net.sf.saxon.trans.ComponentTest;
13 import net.sf.saxon.trans.Visibility;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.type.Type;
16 import net.sf.saxon.value.Whitespace;
17
18 import java.util.HashSet;
19 import java.util.Set;
20 import java.util.StringTokenizer;
21
22 /**
23 * Handler for xsl:accept and xsl:expose elements in stylesheet.
24 */
25 public abstract class XSLAcceptExpose extends StyleElement {
26
27 private Set<ComponentTest> explicitComponentTests = new HashSet<ComponentTest>();
28 private Set<ComponentTest> wildcardComponentTests = new HashSet<ComponentTest>();
29 private Visibility visibility;
30
31 public Visibility getVisibility() {
32 return visibility;
33 }
34
35 public Set<ComponentTest> getExplicitComponentTests() throws XPathException {
36 prepareAttributes();
37 return explicitComponentTests;
38 }
39
40 public Set<ComponentTest> getWildcardComponentTests() throws XPathException {
41 prepareAttributes();
42 return wildcardComponentTests;
43 }
44
45 @Override
46 protected void prepareAttributes() throws XPathException {
47
48 if (visibility != null) {
49 return;
50 }
51
52 String componentAtt = null;
53 String namesAtt = null;
54 String visibilityAtt = null;
55
56 AttributeCollection atts = getAttributeList();
57 for (int a = 0; a < atts.getLength(); a++) {
58 String f = atts.getQName(a);
59 if (f.equals("names")) {
60 namesAtt = Whitespace.trim(atts.getValue(a));
61 } else if (f.equals("component")) {
62 componentAtt = Whitespace.trim(atts.getValue(a));
63 } else if (f.equals("visibility")) {
64 visibilityAtt = Whitespace.trim(atts.getValue(a));
65 } else {
66 checkUnknownAttribute(atts.getNodeName(a));
67 }
68 }
69
70 if (visibilityAtt == null) {
71 reportAbsence("visibility");
72 visibility = Visibility.PRIVATE;
73 } else {
74 visibility = interpretVisibilityValue(visibilityAtt, this instanceof XSLAccept ? "ha" : "");
75 if (visibility == null) {
76 visibility = Visibility.PRIVATE; // fall back in case of errors
77 }
78 }
79
80
81 int componentTypeCode = StandardNames.XSL_FUNCTION;
82 if (componentAtt == null) {
83 reportAbsence("component");
84 } else {
85 String local = Whitespace.trim(componentAtt);
86 if (local.equals("function")) {
87 componentTypeCode = StandardNames.XSL_FUNCTION;
88 } else if (local.equals("template")) {
89 componentTypeCode = StandardNames.XSL_TEMPLATE;
90 } else if (local.equals("variable")) {
91 componentTypeCode = StandardNames.XSL_VARIABLE;
92 } else if (local.equals("attribute-set")) {
93 componentTypeCode = StandardNames.XSL_ATTRIBUTE_SET;
94 } else if (local.equals("mode")) {
95 componentTypeCode = StandardNames.XSL_MODE;
96 } else if (local.equals("*")) {
97 // spec change bug 29478
98 componentTypeCode = -1;
99 } else {
100 compileError("The component type is not one of the allowed names (function, template, variable, attribute-set, or mode)",
101 "XTSE0020");
102 return;
103 }
104 }
105
106 if (namesAtt == null) {
107 reportAbsence("names");
108 namesAtt = "";
109 }
110 StringTokenizer st = new StringTokenizer(namesAtt, " \t\r\n");
111 while (st.hasMoreTokens()) {
112 final String tok = st.nextToken();
113 QNameTest test;
114 int hash = tok.lastIndexOf('#');
115 if (hash > 0 && tok.indexOf('}', hash) < 0) { // ignore any '#' within a namespace URI of an EQName
116 if (componentTypeCode == -1) {
117 compileError("When component='*' is specified, all names must be wildcards",
118 this instanceof XSLAccept ? "XTSE3032" : "XTSE3022");
119 } else {
120 StructuredQName name = makeQName(tok.substring(0, hash));
121 test = new NameTest(Type.ELEMENT, name.getURI(), name.getLocalPart(), getNamePool());
122 int arity = 0;
123 try {
124 arity = Integer.parseInt(tok.substring(hash + 1));
125 } catch (Exception err) {
126 compileError("Malformed function arity in '" + tok + "'");
127 }
128 explicitComponentTests.add(new ComponentTest(componentTypeCode, test, arity));
129 }
130
131 } else if (tok.equals("*")) {
132 test = AnyNodeTest.getInstance();
133 addWildCardTest(componentTypeCode, test);
134 } else if (tok.endsWith(":*")) {
135 if (tok.length() == 2) {
136 compileError("No prefix before ':*'");
137 }
138 String prefix = tok.substring(0, tok.length() - 2);
139 final String uri = getURIForPrefix(prefix, false);
140 test = new NamespaceTest(getNamePool(), Type.ELEMENT, uri);
141 addWildCardTest(componentTypeCode, test);
142 } else if (tok.startsWith("Q{") && tok.endsWith("}*")) {
143 final String uri = tok.substring(2, tok.length() - 2);
144 test = new NamespaceTest(getNamePool(), Type.ELEMENT, uri);
145 wildcardComponentTests.add(new ComponentTest(componentTypeCode, test, -1));
146 } else if (tok.startsWith("*:")) {
147 if (tok.length() == 2) {
148 compileError("No local name after '*:'");
149 }
150 final String localname = tok.substring(2);
151 test = new LocalNameTest(getNamePool(), Type.ELEMENT, localname);
152 addWildCardTest(componentTypeCode, test);
153
154 } else {
155 if (componentTypeCode == -1) {
156 compileError("When component='*' is specified, all names must be wildcards",
157 this instanceof XSLAccept ? "XTSE3032" : "XTSE3022");
158 } else {
159 StructuredQName name;
160 try {
161 name = makeQName(tok);
162 test = new NameTest(Type.ELEMENT, name.getURI(), name.getLocalPart(), getNamePool());
163 explicitComponentTests.add(new ComponentTest(componentTypeCode, test, -1));
164 } catch (XPathException e) {
165 compileErrorInAttribute("Invalid QName in names attribute: " + tok + " (" + e.getMessage() + ")", e.getErrorCodeLocalPart(), "names");
166 }
167
168 }
169 }
170
171 }
172 }
173
174 private void addWildCardTest(int componentTypeCode, QNameTest test) {
175 if (componentTypeCode == -1) {
176 wildcardComponentTests.add(new ComponentTest(StandardNames.XSL_FUNCTION, test, -1));
177 wildcardComponentTests.add(new ComponentTest(StandardNames.XSL_TEMPLATE, test, -1));
178 wildcardComponentTests.add(new ComponentTest(StandardNames.XSL_VARIABLE, test, -1));
179 wildcardComponentTests.add(new ComponentTest(StandardNames.XSL_ATTRIBUTE_SET, test, -1));
180 wildcardComponentTests.add(new ComponentTest(StandardNames.XSL_MODE, test, -1));
181 } else {
182 wildcardComponentTests.add(new ComponentTest(componentTypeCode, test, -1));
183 }
184 }
185
186
187
188
189 }