Debian Codegraph @

net/sf/saxon/style/XSLForEachGroup.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.Expression;
11 import net.sf.saxon.expr.Literal;
12 import net.sf.saxon.expr.StringLiteral;
13 import net.sf.saxon.expr.instruct.ForEachGroup;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.expr.parser.RoleDiagnostic;
16 import net.sf.saxon.expr.parser.TypeChecker;
17 import net.sf.saxon.lib.NamespaceConstant;
18 import net.sf.saxon.lib.StringCollator;
19 import net.sf.saxon.om.AttributeCollection;
20 import net.sf.saxon.om.StandardNames;
21 import net.sf.saxon.pattern.Pattern;
22 import net.sf.saxon.trans.SaxonErrorCode;
23 import net.sf.saxon.trans.XPathException;
24 import net.sf.saxon.value.SequenceType;
25 import net.sf.saxon.value.Whitespace;
26
27 import java.net.URI;
28 import java.net.URISyntaxException;
29
30 /**
31 * Handler for xsl:for-each-group elements in stylesheet. This is a new instruction
32 * defined in XSLT 2.0
33 */
34
35 public final class XSLForEachGroup extends StyleElement {
36
37 /*@Nullable*/ private Expression select = null;
38 private Expression groupBy = null;
39 private Expression groupAdjacent = null;
40 private Pattern starting = null;
41 private Pattern ending = null;
42 private Expression collationName;
43 private boolean composite = false;
44
45 /**
46 * Determine whether this node is an instruction.
47 *
48 * @return true - it is an instruction
49 */
50
51 public boolean isInstruction() {
52 return true;
53 }
54
55 /**
56 * Specify that xsl:sort is a permitted child
57 */
58
59 protected boolean isPermittedChild(StyleElement child) {
60 return child instanceof XSLSort;
61 }
62
63 /**
64 * Determine whether this type of element is allowed to contain a template-body
65 *
66 * @return true: yes, it may contain a template-body
67 */
68
69 public boolean mayContainSequenceConstructor() {
70 return true;
71 }
72
73 public void prepareAttributes() throws XPathException {
74
75 AttributeCollection atts = getAttributeList();
76
77 String selectAtt = null;
78 String groupByAtt = null;
79 String groupAdjacentAtt = null;
80 String startingAtt = null;
81 String endingAtt = null;
82 String collationAtt = null;
83
84 for (int a = 0; a < atts.getLength(); a++) {
85 String f = atts.getQName(a);
86 if (f.equals("select")) {
87 selectAtt = atts.getValue(a);
88 select = makeExpression(selectAtt, a);
89 } else if (f.equals("group-by")) {
90 groupByAtt = atts.getValue(a);
91 groupBy = makeExpression(groupByAtt, a);
92 } else if (f.equals("group-adjacent")) {
93 groupAdjacentAtt = atts.getValue(a);
94 groupAdjacent = makeExpression(groupAdjacentAtt, a);
95 } else if (f.equals("group-starting-with")) {
96 startingAtt = atts.getValue(a);
97 } else if (f.equals("group-ending-with")) {
98 endingAtt = atts.getValue(a);
99 } else if (f.equals("collation")) {
100 collationAtt = Whitespace.trim(atts.getValue(a));
101 collationName = makeAttributeValueTemplate(collationAtt, a);
102 } else if (f.equals("bind-group")) {
103 compileError("The bind-group attribute has been dropped from the XSLT 3.0 specification", "XTSE0090");
104 } else if (f.equals("bind-grouping-key")) {
105 compileError("The bind-grouping-key attribute has been dropped from the XSLT 3.0 specification", "XTSE0090");
106 } else if (f.equals("composite")) {
107 composite = processBooleanAttribute("composite", atts.getValue(a));
108 } else {
109 checkUnknownAttribute(atts.getNodeName(a));
110 }
111 }
112
113 if (selectAtt == null) {
114 reportAbsence("select");
115 select = Literal.makeEmptySequence(); // for error recovery
116 }
117
118 int c = (groupByAtt == null ? 0 : 1) +
119 (groupAdjacentAtt == null ? 0 : 1) +
120 (startingAtt == null ? 0 : 1) +
121 (endingAtt == null ? 0 : 1);
122 if (c != 1) {
123 compileError("Exactly one of the attributes group-by, group-adjacent, group-starting-with, " +
124 "and group-ending-with must be specified", "XTSE1080");
125 }
126
127 if (startingAtt != null) {
128 starting = makePattern(startingAtt, "group-starting-with");
129 }
130
131 if (endingAtt != null) {
132 ending = makePattern(endingAtt, "group-ending-with");
133 }
134
135 if (collationAtt != null) {
136 if (groupBy == null && groupAdjacent == null) {
137 compileError("A collation may be specified only if group-by or group-adjacent is specified", "XTSE1090");
138 } else {
139 if (collationName instanceof StringLiteral) {
140 String collation = ((StringLiteral) collationName).getStringValue();
141 URI collationURI;
142 try {
143 collationURI = new URI(collation);
144 if (!collationURI.isAbsolute()) {
145 URI base = new URI(getBaseURI());
146 collationURI = base.resolve(collationURI);
147 collationName = new StringLiteral(collationURI.toString());
148 }
149 } catch (URISyntaxException err) {
150 compileError("Collation name '" + collationName + "' is not a valid URI", "XTDE1110");
151 collationName = new StringLiteral(NamespaceConstant.CODEPOINT_COLLATION_URI);
152 }
153 }
154 }
155 } else {
156 String defaultCollation = getDefaultCollationName();
157 if (defaultCollation != null) {
158 collationName = new StringLiteral(defaultCollation);
159 }
160 }
161
162 if (composite && (starting != null || ending != null)) {
163 compileError("The composite attribute cannot be used with " +
164 (starting == null ? "grouping-ending-with" : "group-starting-with"), "XTSE1090");
165 }
166 }
167
168 public void validate(ComponentDeclaration decl) throws XPathException {
169 checkSortComesFirst(false);
170 TypeChecker tc = getConfiguration().getTypeChecker(false);
171 select = typeCheck("select", select);
172
173 ExpressionVisitor visitor = makeExpressionVisitor();
174 if (groupBy != null) {
175 groupBy = typeCheck("group-by", groupBy);
176 try {
177 RoleDiagnostic role =
178 new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:for-each-group/group-by", 0);
179 groupBy = tc.staticTypeCheck(groupBy, SequenceType.ATOMIC_SEQUENCE, role, visitor);
180 } catch (XPathException err) {
181 compileError(err);
182 }
183 } else if (groupAdjacent != null) {
184 groupAdjacent = typeCheck("group-adjacent", groupAdjacent);
185 try {
186 RoleDiagnostic role =
187 new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:for-each-group/group-adjacent", 0);
188 role.setErrorCode("XTTE1100");
189 groupAdjacent = tc.staticTypeCheck(groupAdjacent,
190 composite ? SequenceType.ATOMIC_SEQUENCE : SequenceType.SINGLE_ATOMIC,
191 role, visitor);
192 } catch (XPathException err) {
193 compileError(err);
194 }
195 }
196
197 starting = typeCheck("starting", starting);
198 ending = typeCheck("ending", ending);
199
200 if ((starting != null || ending != null) && visitor.getStaticContext().getXPathVersion() < 30) {
201 try {
202 RoleDiagnostic role =
203 new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:for-each-group/select", 0);
204 role.setErrorCode("XTTE1120");
205 select = tc.staticTypeCheck(select, SequenceType.NODE_SEQUENCE, role, visitor);
206 } catch (XPathException err) {
207 String prefix = starting != null ?
208 "With group-starting-with attribute: " :
209 "With group-ending-with attribute: ";
210 compileError(prefix + err.getMessage(), err.getErrorCodeQName());
211 }
212 }
213 if (!hasChildNodes()) {
214 compileWarning("An empty xsl:for-each-group instruction has no effect", SaxonErrorCode.SXWN9009);
215 }
216
217 }
218
219 public Expression compile(Compilation compilation, ComponentDeclaration decl) throws XPathException {
220
221 StringCollator collator = null;
222 if (collationName instanceof StringLiteral) {
223 // if the collation name is constant, then we've already resolved it against the base URI
224 final String uri = ((StringLiteral) collationName).getStringValue();
225 collator = findCollation(uri, getBaseURI());
226 if (collator == null) {
227 compileError("The collation name '" + collationName + "' has not been defined", "XTDE1110");
228 }
229 }
230
231 byte algorithm = 0;
232 Expression key = null;
233 if (groupBy != null) {
234 algorithm = ForEachGroup.GROUP_BY;
235 key = groupBy;
236 } else if (groupAdjacent != null) {
237 algorithm = ForEachGroup.GROUP_ADJACENT;
238 key = groupAdjacent;
239 } else if (starting != null) {
240 algorithm = ForEachGroup.GROUP_STARTING;
241 key = starting;
242 } else if (ending != null) {
243 algorithm = ForEachGroup.GROUP_ENDING;
244 key = ending;
245 }
246
247 Expression action = compileSequenceConstructor(compilation, decl, true);
248 if (action == null) {
249 // body of for-each is empty: it's a no-op.
250 return Literal.makeEmptySequence();
251 }
252 try {
253
254 ForEachGroup instr = new ForEachGroup(select,
255 action.simplify(),
256 algorithm,
257 key,
258 collator,
259 collationName,
260 makeSortKeys(compilation, decl));
261 instr.setIsInFork(getParent().getFingerprint() == StandardNames.XSL_FORK);
262 instr.setComposite(composite);
263 return instr;
264 } catch (XPathException e) {
265 compileError(e);
266 return null;
267 }
268
269 }
270
271 }
272