Debian Codegraph @

net/sf/saxon/style/XSLMerge.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.sort.MergeInstr;
11 import net.sf.saxon.expr.sort.MergeInstr.MergeSource;
12 import net.sf.saxon.expr.Expression;
13 import net.sf.saxon.expr.Literal;
14 import net.sf.saxon.expr.parser.RebindingMap;
15 import net.sf.saxon.expr.parser.RoleDiagnostic;
16 import net.sf.saxon.expr.sort.SortExpression;
17 import net.sf.saxon.expr.sort.SortKeyDefinition;
18 import net.sf.saxon.expr.sort.SortKeyDefinitionList;
19 import net.sf.saxon.om.AttributeCollection;
20 import net.sf.saxon.om.AxisInfo;
21 import net.sf.saxon.om.NodeInfo;
22 import net.sf.saxon.trans.Err;
23 import net.sf.saxon.trans.XPathException;
24 import net.sf.saxon.tree.iter.AxisIterator;
25 import net.sf.saxon.type.Type;
26 import net.sf.saxon.value.Whitespace;
27
28 import java.util.HashSet;
29 import java.util.Set;
30
31
32 /**
33 * Handler for xsl:merge elements in stylesheet. <br>
34 */
35 public class XSLMerge extends StyleElement {
36
37 private int numberOfMergeSources = 0;
38
39 /**
40 * Determine whether this node is an instruction.
41 *
42 * @return true - it is an instruction
43 */
44
45 public boolean isInstruction() {
46 return true;
47 }
48
49 /**
50 * Determine whether this type of element is allowed to contain a sequence constructor
51 *
52 * @return true: yes, it may contain a sequence constructor
53 */
54
55 public boolean mayContainSequenceConstructor() {
56 return false;
57 }
58
59 @Override
60 protected void prepareAttributes() throws XPathException {
61 AttributeCollection atts = getAttributeList();
62
63 for (int a = 0; a < atts.getLength(); a++) {
64 checkUnknownAttribute(atts.getNodeName(a));
65 }
66 }
67
68 public void validate(ComponentDeclaration decl) throws XPathException {
69 int childMask = 0;
70 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
71 NodeInfo child;
72 Set<String> mergeSourceNames = new HashSet<String>();
73 while ((child = kids.next()) != null) {
74 if (child instanceof XSLMergeSource) {
75 String name = ((XSLMergeSource)child).getSourceName();
76 if (mergeSourceNames.contains(name)) {
77 compileError("Duplicate xsl:merge-source/@name", "XTSE3190");
78 }
79 mergeSourceNames.add(name);
80 childMask = childMask | 1;
81 numberOfMergeSources++;
82 } else if (child instanceof XSLMergeAction) {
83 if ((childMask & 2) == 2) {
84 compileError("xsl:merge must have only one xsl:merge-action child element", "XTSE0010");
85 }
86 childMask = childMask | 2;
87 } else if (child.getNodeKind() == Type.TEXT) {
88 // with xml:space=preserve, white space nodes may still be there
89 if (!Whitespace.isWhite(child.getStringValueCS())) {
90 compileError("No character data is allowed within xsl:merge", "XXXX");
91 }
92 } else if (child instanceof XSLFallback) {
93 if ((childMask & 2) == 0) {
94 compileError("xsl:fallback child of xsl:merge can appear only after xsl:merge-action", "XTSE0010");
95 }
96 } else {
97 compileError("Child element " + Err.wrap(child.getDisplayName(), Err.ELEMENT) +
98 " is not allowed as a child of xsl:merge", "XTSE0010");
99 }
100 }
101 if (childMask == 1) {
102 compileError("xsl:merge element requires an xsl:merge-action", "XTSE0010");
103 } else if (childMask == 2) {
104 compileError("xsl:merge element requires at least one xsl:merge-source child element", "XTSE0010");
105 }
106 }
107
108 /**
109 * Check that the xsl:merge-source children have consistent merge key definitions, provided
110 * that all the merge key definitions are statically determined. If any attribute value templates
111 * are used in the merge key definitions, the validation is postponed until run-time
112 *
113 * @param sources the xsl:merge-source children
114 */
115
116
117 private void checkCompatibleMergeKeys(MergeSource[] sources) {
118 for (int i = 0; i < sources[0].mergeKeyDefinitions.size(); i++) {
119 if (!sources[0].mergeKeyDefinitions.getSortKeyDefinition(i).isFixed()) {
120 break;
121 }
122 for (int z = 1; z < sources.length; z++) {
123 if (!sources[z].mergeKeyDefinitions.getSortKeyDefinition(i).isFixed()) {
124 break;
125 }
126 // Both definitions are fixed: compare them now
127 if (!compareSortKeyDefinitions(sources[z].mergeKeyDefinitions.getSortKeyDefinition(i),
128 sources[0].mergeKeyDefinitions.getSortKeyDefinition(i))) {
129 compileError("The " + RoleDiagnostic.ordinal(i + 1) + " merge key definition of the "
130 + RoleDiagnostic.ordinal(z + 1) + " merge source is incompatible with the " +
131 RoleDiagnostic.ordinal(i + 1) + " merge key definition of the first merge source",
132 "XTDE2210");
133 }
134 }
135 }
136 }
137
138 /**
139 * Compare two sort key definitions whose defining attributes are known at compile time.
140 * The comparison compares all subexpressions other than the actual sort key
141 *
142 * @param sd1 the first sort key definition
143 * @param sd2 the second sort key definition
144 * @return true if the two sort key definitions are compatible according to the definition
145 * used by xsl:merge (the sort keys can be different, but the other properties of the sort
146 * must be the same)
147 */
148
149 private boolean compareSortKeyDefinitions(SortKeyDefinition sd1, SortKeyDefinition sd2) {
150
151 return sameFixedExpression(sd1.getLanguage(), sd2.getLanguage()) &&
152 sameFixedExpression(sd1.getOrder(), sd2.getOrder()) &&
153 sameFixedExpression(sd1.getCollationNameExpression(), sd2.getCollationNameExpression()) &&
154 sameFixedExpression(sd1.getCaseOrder(), sd2.getCaseOrder()) &&
155 sameFixedExpression(sd1.getDataTypeExpression(), sd2.getDataTypeExpression());
156 }
157
158 private boolean sameFixedExpression(Expression e1, Expression e2) {
159 return (e1==null && e2==null) || (e1!=null && e1.equals(e2));
160 }
161
162 @Override
163 public Expression compile(Compilation compilation, ComponentDeclaration decl)
164 throws XPathException {
165
166 MergeInstr merge = new MergeInstr();
167 int entries = numberOfMergeSources;
168 MergeSource[] sources = new MergeSource[entries];
169 Expression action = Literal.makeEmptySequence();
170 int w = 0;
171 int sortKeyDefLen = 0;
172 AxisIterator children = iterateAxis(AxisInfo.CHILD);
173 NodeInfo node;
174 while ((node = children.next()) != null) {
175 if (node instanceof XSLMergeSource) {
176 XSLMergeSource source = (XSLMergeSource) node;
177 SortKeyDefinitionList sortKeyDefs = source.makeSortKeys(compilation, decl);
178 if (sortKeyDefLen == 0) {
179 sortKeyDefLen = sortKeyDefs.size();
180 } else if (sortKeyDefLen != sortKeyDefs.size()) {
181 compileError("Each xsl:merge-source must have the same number of xsl:merge-key children", "XTSE2200");
182 }
183 Expression select = source.getSelect();
184 if (source.isSortBeforeMerge()) {
185 select = new SortExpression(select, sortKeyDefs.copy(new RebindingMap()));
186 }
187 MergeSource ms = source.makeMergeSource(merge, select);
188 ms.mergeKeyDefinitions = sortKeyDefs;
189 //ms.setRowSelect(select);
190 sources[w++] = ms;
191 } else if (node instanceof XSLMergeAction) {
192 action = ((XSLMergeAction) node).compileSequenceConstructor(compilation, decl, true);
193 if (action == null) {
194 action = Literal.makeEmptySequence();
195 }
196 try {
197 action = action.simplify();
198 } catch (XPathException e) {
199 compileError(e);
200 }
201
202 } else {
203 // fallback
204 }
205 }
206 checkCompatibleMergeKeys(sources);
207 merge.init(sources, action);
208 return merge;
209 }
210
211
212 }