Debian Codegraph @

net/sf/saxon/style/XSLLeafNodeConstructor.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.*;
11 import net.sf.saxon.expr.instruct.SimpleNodeConstructor;
12 import net.sf.saxon.expr.parser.RetainedStaticContext;
13 import net.sf.saxon.functions.SystemFunction;
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.BuiltInAtomicType;
20 import net.sf.saxon.type.Type;
21 import net.sf.saxon.value.StringValue;
22 import net.sf.saxon.value.Whitespace;
23
24 /**
25 * Common superclass for XSLT elements whose content template produces a text
26 * value: xsl:text, xsl:value-of, xsl:attribute, xsl:comment, xsl:namespace, and xsl:processing-instruction
27 */
28
29 public abstract class XSLLeafNodeConstructor extends StyleElement {
30
31 //protected String stringValue = null;
32 /*@Nullable*/ protected Expression select = null;
33
34 /**
35 * Method for use by subclasses (processing-instruction and namespace) that take
36 * a name and a select attribute
37 *
38 * @return the expression defining the name attribute
39 * @throws XPathException if an error is detected
40 */
41
42 protected Expression prepareAttributesNameAndSelect() throws XPathException {
43
44 Expression name = null;
45 String nameAtt = null;
46 String selectAtt = null;
47
48 AttributeCollection atts = getAttributeList();
49
50 for (int a = 0; a < atts.getLength(); a++) {
51 String f = atts.getQName(a);
52 if (f.equals("name")) {
53 nameAtt = Whitespace.trim(atts.getValue(a));
54 name = makeAttributeValueTemplate(nameAtt, a);
55 } else if (f.equals("select")) {
56 selectAtt = atts.getValue(a);
57 select = makeExpression(selectAtt, a);
58 } else {
59 checkUnknownAttribute(atts.getNodeName(a));
60 }
61 }
62
63 if (nameAtt == null) {
64 reportAbsence("name");
65 }
66
67 return name;
68 }
69
70 /**
71 * Determine whether this node is an instruction.
72 *
73 * @return true - it is an instruction
74 */
75
76 public boolean isInstruction() {
77 return true;
78 }
79
80 /**
81 * Determine whether this type of element is allowed to contain a template-body
82 *
83 * @return true: yes, it may contain a template-body
84 */
85
86 public boolean mayContainSequenceConstructor() {
87 return true;
88 }
89
90 public void validate(ComponentDeclaration decl) throws XPathException {
91 if (select != null && hasChildNodes()) {
92 String errorCode = getErrorCodeForSelectPlusContent();
93 compileError("An " + getDisplayName() + " element with a select attribute must be empty", errorCode);
94 }
95 AxisIterator kids = iterateAxis(AxisInfo.CHILD);
96 NodeInfo first = kids.next();
97 if (select == null) {
98 if (first == null) {
99 // there are no child nodes and no select attribute
100 //stringValue = "";
101 select = new StringLiteral(StringValue.EMPTY_STRING);
102 select.setRetainedStaticContext(makeRetainedStaticContext());
103 } else {
104 if (kids.next() == null && !isExpandingText()) {
105 // there is exactly one child node
106 if (first.getNodeKind() == Type.TEXT) {
107 // it is a text node: optimize for this case
108 select = new StringLiteral(first.getStringValue());
109 select.setRetainedStaticContext(makeRetainedStaticContext());
110 }
111 }
112 }
113 }
114 }
115
116 /**
117 * Get the error code to be returned when the element has a select attribute but is not empty.
118 *
119 * @return the error code defined for this condition, for this particular instruction
120 */
121
122 protected abstract String getErrorCodeForSelectPlusContent();
123
124 protected void compileContent(Compilation exec, ComponentDeclaration decl, SimpleNodeConstructor inst, Expression separator) throws XPathException {
125 if (separator == null) {
126 separator = new StringLiteral(StringValue.SINGLE_SPACE);
127 }
128 try {
129 if (select == null) {
130 select = compileSequenceConstructor(exec, decl, true);
131 }
132 select = makeSimpleContentConstructor(select, separator, getStaticContext());
133 inst.setSelect(select);
134
135 } catch (XPathException err) {
136 compileError(err);
137 }
138 }
139
140 /**
141 * Construct an expression that implements the rules of "constructing simple content":
142 * given an expression to select the base sequence, and an expression to compute the separator,
143 * build an (unoptimized) expression to produce the value of the node as a string.
144 *
145 * @param select the expression that selects the base sequence
146 * @param separator the expression that computes the separator
147 * @param env the static context
148 * @return an expression that returns a string containing the string value of the constructed node
149 */
150
151 public static Expression makeSimpleContentConstructor(Expression select, Expression separator, StaticContext env) {
152 RetainedStaticContext rsc = select.getLocalRetainedStaticContext();
153 if (rsc == null) {
154 rsc = env.makeRetainedStaticContext();
155 }
156 // Merge adjacent text nodes
157 select = AdjacentTextNodeMerger.makeAdjacentTextNodeMerger(select);
158 // Atomize the result
159 select = Atomizer.makeAtomizer(select, null);
160 // Convert each atomic value to a string
161 select = new AtomicSequenceConverter(select, BuiltInAtomicType.STRING);
162 select.setRetainedStaticContext(rsc);
163 ((AtomicSequenceConverter) select).allocateConverterStatically(env.getConfiguration(), false);
164 // Join the resulting strings with a separator
165 if (select.getCardinality() != StaticProperty.EXACTLY_ONE) {
166 select = SystemFunction.makeCall("string-join", rsc, select, separator);
167 }
168 // All that's left for the instruction to do is to construct the right kind of node
169 return select;
170 }
171
172
173 }
174