Debian Codegraph @

net/sf/saxon/style/XSLPerformSort.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.sort.SortExpression;
13 import net.sf.saxon.expr.sort.SortKeyDefinitionList;
14 import net.sf.saxon.om.AttributeCollection;
15 import net.sf.saxon.om.AxisInfo;
16 import net.sf.saxon.om.NodeInfo;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.iter.AxisIterator;
19 import net.sf.saxon.type.Type;
20 import net.sf.saxon.value.Whitespace;
21
22
23 /**
24 * Handler for xsl:perform-sort elements in stylesheet (XSLT 2.0). <br>
25 */
26
27 public class XSLPerformSort extends StyleElement {
28
29 /*@Nullable*/ Expression select = null;
30
31 /**
32 * Determine whether this node is an instruction.
33 *
34 * @return true - it is an instruction
35 */
36
37 public boolean isInstruction() {
38 return true;
39 }
40
41 /**
42 * Determine whether this type of element is allowed to contain a template-body
43 *
44 * @return true: yes, it may contain a template-body
45 */
46
47 public boolean mayContainSequenceConstructor() {
48 return true;
49 }
50
51 /**
52 * Specify that xsl:sort is a permitted child
53 */
54
55 protected boolean isPermittedChild(StyleElement child) {
56 return (child instanceof XSLSort);
57 }
58
59 public void prepareAttributes() throws XPathException {
60
61 AttributeCollection atts = getAttributeList();
62
63 String selectAtt = null;
64
65 for (int a = 0; a < atts.getLength(); a++) {
66 String f = atts.getQName(a);
67 if (f.equals("select")) {
68 selectAtt = atts.getValue(a);
69 select = makeExpression(selectAtt, a);
70 } else {
71 checkUnknownAttribute(atts.getNodeName(a));
72 }
73 }
74
75 }
76
77 public void validate(ComponentDeclaration decl) throws XPathException {
78 checkSortComesFirst(true);
79
80 if (select != null) {
81 // if there is a select attribute, check that there are no children other than xsl:sort and xsl:fallback
82 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
83 while (true) {
84 NodeInfo child = (NodeInfo) kids.next();
85 if (child == null) {
86 break;
87 }
88 if (child instanceof XSLSort || child instanceof XSLFallback) {
89 // no action
90 } else if (child.getNodeKind() == Type.TEXT && !Whitespace.isWhite(child.getStringValueCS())) {
91 // with xml:space=preserve, white space nodes may still be there
92 compileError("Within xsl:perform-sort, significant text must not appear if there is a select attribute",
93 "XTSE1040");
94 } else {
95 ((StyleElement) child).compileError(
96 "Within xsl:perform-sort, child instructions are not allowed if there is a select attribute",
97 "XTSE1040");
98 }
99 }
100 }
101 select = typeCheck("select", select);
102 }
103
104 public Expression compile(Compilation compilation, ComponentDeclaration decl) throws XPathException {
105 SortKeyDefinitionList sortKeys = makeSortKeys(compilation, decl);
106 if (select != null) {
107 return new SortExpression(select, sortKeys);
108 } else {
109 Expression body = compileSequenceConstructor(compilation, decl, true);
110 if (body == null) {
111 body = Literal.makeEmptySequence();
112 }
113 try {
114 return new SortExpression(body.simplify(), sortKeys);
115 } catch (XPathException e) {
116 compileError(e);
117 return null;
118 }
119 }
120 }
121
122
123 }