| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public abstract class AttributeValueTemplate { |
| 33 |
|
| 34 |
private AttributeValueTemplate() { |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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)) { |
| 63 |
addStringComponent(components, avt, last, len); |
| 64 |
break; |
| 65 |
} else if (i8 >= 0 && (i0 < 0 || i8 < i0)) { |
| 66 |
if (i8 != i9) { |
| 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) { |
| 75 |
addStringComponent(components, avt, last, i1 + 1); |
| 76 |
last = i1 + 2; |
| 77 |
} else if (i0 >= 0) { |
| 78 |
if (i0 > last) { |
| 79 |
addStringComponent(components, avt, last, i0); |
| 80 |
} |
| 81 |
Expression exp; |
| 82 |
XPathParser parser = env.getConfiguration().newExpressionParser("XP", false, languageLevel); |
| 83 |
|
| 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 |
|
| 113 |
|
| 114 |
if (components.isEmpty()) { |
| 115 |
result = new StringLiteral(StringValue.EMPTY_STRING); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
else if (components.size() == 1) { |
| 121 |
result = components.get(0).simplify(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 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 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 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 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 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 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 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 |
|