Debian Codegraph @

net/sf/saxon/style/AttributeValueTemplate.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.parser.RetainedStaticContext;
12 import net.sf.saxon.expr.parser.Token;
13 import net.sf.saxon.expr.parser.XPathParser;
14 import net.sf.saxon.functions.SystemFunction;
15 import net.sf.saxon.om.GroundedValue;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.BuiltInAtomicType;
18 import net.sf.saxon.type.TypeHierarchy;
19 import net.sf.saxon.value.Cardinality;
20 import net.sf.saxon.value.IntegerValue;
21 import net.sf.saxon.value.StringValue;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 /**
27 * This class represents an attribute value template. The class allows an AVT to be parsed, and
28 * can construct an Expression that returns the effective value of the AVT.
29 * <p>This is an abstract class that is never instantiated, it contains static methods only.</p>
30 */
31
32 public abstract class AttributeValueTemplate {
33
34 private AttributeValueTemplate() {
35 }
36
37
38 /**
39 * Static factory method to create an AVT from an XSLT string representation. The
40 * method is also used for text value templates.
41 * @param avt the attribute value template (or TVT) as written.
42 * @param env the static context
43 * @return an expression that computes the value of the attribute / content, as a string.
44 * In the case of a TVT this must be further processed to create a text node.
45 */
46
47 public static Expression make(String avt, StaticContext env) throws XPathException {
48
49 int languageLevel = env.getXPathVersion();
50 List<Expression> components = new ArrayList<>(5);
51
52 int i0, i1, i8, i9;
53 int len = avt.length();
54 int last = 0;
55 while (last < len) {
56
57 i0 = avt.indexOf("{", last);
58 i1 = avt.indexOf("{{", last);
59 i8 = avt.indexOf("}", last);
60 i9 = avt.indexOf("}}", last);
61
62 if ((i0 < 0 || len < i0) && (i8 < 0 || len < i8)) { // found end of string
63 addStringComponent(components, avt, last, len);
64 break;
65 } else if (i8 >= 0 && (i0 < 0 || i8 < i0)) { // found a "}"
66 if (i8 != i9) { // a "}" that isn't a "}}"
67 XPathException err = new XPathException("Closing curly brace in attribute value template \"" + avt.substring(0, len) + "\" must be doubled");
68 err.setErrorCode("XTSE0370");
69 err.setIsStaticError(true);
70 throw err;
71 }
72 addStringComponent(components, avt, last, i8 + 1);
73 last = i8 + 2;
74 } else if (i1 >= 0 && i1 == i0) { // found a doubled "{{"
75 addStringComponent(components, avt, last, i1 + 1);
76 last = i1 + 2;
77 } else if (i0 >= 0) { // found a single "{"
78 if (i0 > last) {
79 addStringComponent(components, avt, last, i0);
80 }
81 Expression exp;
82 XPathParser parser = env.getConfiguration().newExpressionParser("XP", false, languageLevel);
83 //parser.setDefaultContainer(container);
84 parser.setLanguage(XPathParser.XPATH, 31);
85 parser.setAllowAbsentExpression(true);
86 exp = parser.parse(avt, i0 + 1, Token.RCURLY, env);
87 exp.setRetainedStaticContext(env.makeRetainedStaticContext());
88 exp = exp.simplify();
89 last = parser.getTokenizer().currentTokenStartOffset + 1;
90
91 if (env instanceof ExpressionContext && ((ExpressionContext)env).getStyleElement() instanceof XSLAnalyzeString
92 && isIntegerOrIntegerPair(exp)) {
93 env.issueWarning("Found {" + showIntegers(exp) + "} in regex attribute: perhaps {{" +
94 showIntegers(exp) + "}} was intended? (The attribute is an AVT, so curly braces should be doubled)", exp.getLocation());
95 }
96
97 if (env.isInBackwardsCompatibleMode()) {
98 components.add(makeFirstItem(exp, env));
99 } else {
100 components.add(XSLLeafNodeConstructor.makeSimpleContentConstructor(
101 exp,
102 new StringLiteral(StringValue.SINGLE_SPACE), env).simplify());
103 }
104
105 } else {
106 throw new IllegalStateException("Internal error parsing AVT");
107 }
108 }
109
110 Expression result;
111
112 // is it empty?
113
114 if (components.isEmpty()) {
115 result = new StringLiteral(StringValue.EMPTY_STRING);
116 }
117
118 // is it a single component?
119
120 else if (components.size() == 1) {
121 result = components.get(0).simplify();
122 }
123
124 // otherwise, return an expression that concatenates the components
125
126 else {
127 Expression[] args = new Expression[components.size()];
128 components.toArray(args);
129 Expression fn = SystemFunction.makeCall("concat", new RetainedStaticContext(env), args);
130 result = fn.simplify();
131 }
132
133 result.setLocation(env.getContainingLocation());
134 return result;
135
136 }
137
138 /**
139 * Used to detect warning condition when braces are undoubled in the regex attribute of xsl:analyze-string
140 *
141 * @param exp an expression
142 * @return true if the expression is an integer literal or a pair of two integer literals
143 */
144
145 private static boolean isIntegerOrIntegerPair(Expression exp) {
146 if (exp instanceof Literal) {
147 GroundedValue val = ((Literal) exp).getValue();
148 if (val instanceof IntegerValue) {
149 return true;
150 }
151 if (val.getLength() == 2) {
152 return val.itemAt(0) instanceof IntegerValue && val.itemAt(1) instanceof IntegerValue;
153 }
154 }
155 return false;
156 }
157
158 /**
159 * Used to report warning condition when braces are undoubled in the regex attribute of xsl:analyze-string
160 *
161 * @param exp an expression
162 * @return string representation of an integer literal or a pair of two integer literals
163 */
164
165 private static String showIntegers(Expression exp) {
166 if (exp instanceof Literal) {
167 GroundedValue val = ((Literal) exp).getValue();
168 if (val instanceof IntegerValue) {
169 return val.toString();
170 }
171 if (val.getLength() == 2) {
172 if (val.itemAt(0) instanceof IntegerValue && val.itemAt(1) instanceof IntegerValue) {
173 return val.itemAt(0).toString() + "," + val.itemAt(1).toString();
174 }
175 }
176 }
177 return "";
178 }
179
180 private static void addStringComponent(List<Expression> components, String avt, int start, int end) {
181 if (start < end) {
182 components.add(new StringLiteral(avt.substring(start, end)));
183 }
184 }
185
186 /**
187 * Make an expression that extracts the first item of a sequence, after atomization
188 */
189
190 /*@NotNull*/
191 public static Expression makeFirstItem(Expression exp, StaticContext env) {
192 if (Literal.isEmptySequence(exp)) {
193 return exp;
194 }
195 final TypeHierarchy th = env.getConfiguration().getTypeHierarchy();
196 if (!exp.getItemType().isPlainType()) {
197 exp = Atomizer.makeAtomizer(exp, null);
198 }
199 if (Cardinality.allowsMany(exp.getCardinality())) {
200 exp = FirstItemExpression.makeFirstItemExpression(exp);
201 }
202 if (!th.isSubType(exp.getItemType(), BuiltInAtomicType.STRING)) {
203 exp = new AtomicSequenceConverter(exp, BuiltInAtomicType.STRING);
204 ((AtomicSequenceConverter) exp).allocateConverterStatically(env.getConfiguration(), false);
205 }
206 return exp;
207 }
208
209 }
210