| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.style; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.ContextItemExpression; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.NumberSequenceFormatter; |
| 13 |
import net.sf.saxon.expr.StringLiteral; |
| 14 |
import net.sf.saxon.expr.instruct.NumberInstruction; |
| 15 |
import net.sf.saxon.expr.instruct.ValueOf; |
| 16 |
import net.sf.saxon.expr.number.NumberFormatter; |
| 17 |
import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 18 |
import net.sf.saxon.om.AttributeCollection; |
| 19 |
import net.sf.saxon.pattern.Pattern; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.value.SequenceType; |
| 22 |
import net.sf.saxon.value.Whitespace; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class XSLNumber extends StyleElement { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private int level; |
| 33 |
private Pattern count = null; |
| 34 |
private Pattern from = null; |
| 35 |
private Expression select = null; |
| 36 |
private Expression value = null; |
| 37 |
private Expression format = null; |
| 38 |
private Expression groupSize = null; |
| 39 |
private Expression groupSeparator = null; |
| 40 |
private Expression letterValue = null; |
| 41 |
private Expression lang = null; |
| 42 |
private Expression ordinal = null; |
| 43 |
private Expression startAt = null; |
| 44 |
private NumberFormatter formatter = null; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public boolean isInstruction() { |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
public void prepareAttributes() throws XPathException { |
| 58 |
|
| 59 |
AttributeCollection atts = getAttributeList(); |
| 60 |
|
| 61 |
String selectAtt = null; |
| 62 |
String valueAtt = null; |
| 63 |
String countAtt = null; |
| 64 |
String fromAtt = null; |
| 65 |
String levelAtt = null; |
| 66 |
String formatAtt = null; |
| 67 |
String gsizeAtt = null; |
| 68 |
String gsepAtt = null; |
| 69 |
String langAtt = null; |
| 70 |
String letterValueAtt = null; |
| 71 |
String ordinalAtt = null; |
| 72 |
String startAtAtt = null; |
| 73 |
|
| 74 |
for (int a = 0; a < atts.getLength(); a++) { |
| 75 |
String f = atts.getQName(a); |
| 76 |
if (f.equals("select")) { |
| 77 |
selectAtt = atts.getValue(a); |
| 78 |
select = makeExpression(selectAtt, a); |
| 79 |
} else if (f.equals("value")) { |
| 80 |
valueAtt = atts.getValue(a); |
| 81 |
value = makeExpression(valueAtt, a); |
| 82 |
} else if (f.equals("count")) { |
| 83 |
countAtt = atts.getValue(a); |
| 84 |
} else if (f.equals("from")) { |
| 85 |
fromAtt = atts.getValue(a); |
| 86 |
} else if (f.equals("level")) { |
| 87 |
levelAtt = Whitespace.trim(atts.getValue(a)); |
| 88 |
} else if (f.equals("format")) { |
| 89 |
formatAtt = atts.getValue(a); |
| 90 |
format = makeAttributeValueTemplate(formatAtt, a); |
| 91 |
} else if (f.equals("lang")) { |
| 92 |
langAtt = atts.getValue(a); |
| 93 |
lang = makeAttributeValueTemplate(langAtt, a); |
| 94 |
} else if (f.equals("letter-value")) { |
| 95 |
letterValueAtt = Whitespace.trim(atts.getValue(a)); |
| 96 |
letterValue = makeAttributeValueTemplate(letterValueAtt, a); |
| 97 |
} else if (f.equals("grouping-size")) { |
| 98 |
gsizeAtt = Whitespace.trim(atts.getValue(a)); |
| 99 |
} else if (f.equals("grouping-separator")) { |
| 100 |
gsepAtt = atts.getValue(a); |
| 101 |
} else if (f.equals("ordinal")) { |
| 102 |
ordinalAtt = atts.getValue(a); |
| 103 |
ordinal = makeAttributeValueTemplate(ordinalAtt, a); |
| 104 |
} else if (f.equals("start-at")) { |
| 105 |
startAtAtt = atts.getValue(a); |
| 106 |
startAt = makeAttributeValueTemplate(startAtAtt, a); |
| 107 |
} else { |
| 108 |
checkUnknownAttribute(atts.getNodeName(a)); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
if (valueAtt != null) { |
| 114 |
if (selectAtt != null) { |
| 115 |
compileError("The select attribute and value attribute must not both be present", "XTSE0975"); |
| 116 |
} |
| 117 |
if (countAtt != null) { |
| 118 |
compileError("The count attribute and value attribute must not both be present", "XTSE0975"); |
| 119 |
} |
| 120 |
if (fromAtt != null) { |
| 121 |
compileError("The from attribute and value attribute must not both be present", "XTSE0975"); |
| 122 |
} |
| 123 |
if (levelAtt != null) { |
| 124 |
compileError("The level attribute and value attribute must not both be present", "XTSE0975"); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if (countAtt != null) { |
| 129 |
count = makePattern(countAtt, "count"); |
| 130 |
} |
| 131 |
|
| 132 |
if (fromAtt != null) { |
| 133 |
from = makePattern(fromAtt, "from"); |
| 134 |
} |
| 135 |
|
| 136 |
if (levelAtt == null) { |
| 137 |
level = NumberInstruction.SINGLE; |
| 138 |
} else if (levelAtt.equals("single")) { |
| 139 |
level = NumberInstruction.SINGLE; |
| 140 |
} else if (levelAtt.equals("multiple")) { |
| 141 |
level = NumberInstruction.MULTI; |
| 142 |
} else if (levelAtt.equals("any")) { |
| 143 |
level = NumberInstruction.ANY; |
| 144 |
} else { |
| 145 |
invalidAttribute("level", "single|any|multiple"); |
| 146 |
} |
| 147 |
|
| 148 |
if (level == NumberInstruction.SINGLE && from == null && count == null) { |
| 149 |
level = NumberInstruction.SIMPLE; |
| 150 |
} |
| 151 |
|
| 152 |
if (formatAtt != null) { |
| 153 |
if (format instanceof StringLiteral) { |
| 154 |
formatter = new NumberFormatter(); |
| 155 |
formatter.prepare(((StringLiteral) format).getStringValue()); |
| 156 |
} |
| 157 |
|
| 158 |
} else { |
| 159 |
formatter = new NumberFormatter(); |
| 160 |
formatter.prepare("1"); |
| 161 |
} |
| 162 |
|
| 163 |
if (gsepAtt != null && gsizeAtt != null) { |
| 164 |
|
| 165 |
groupSize = makeAttributeValueTemplate(gsizeAtt, getAttributeList().getIndex("", "group-size")); |
| 166 |
groupSeparator = makeAttributeValueTemplate(gsepAtt, getAttributeList().getIndex("", "group-separator")); |
| 167 |
} |
| 168 |
|
| 169 |
if (startAtAtt != null) { |
| 170 |
if (startAtAtt.indexOf('{') < 0 && !startAtAtt.matches("-?[0-9]+(\\s+-?[0-9]+)*")) { |
| 171 |
compileErrorInAttribute("Invalid format for start-at attribute", "XTSE0020", "start-at"); |
| 172 |
} |
| 173 |
} else { |
| 174 |
startAt = new StringLiteral("1"); |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
public void validate(ComponentDeclaration decl) throws XPathException { |
| 180 |
checkEmpty(); |
| 181 |
|
| 182 |
select = typeCheck("select", select); |
| 183 |
value = typeCheck("value", value); |
| 184 |
format = typeCheck("format", format); |
| 185 |
groupSize = typeCheck("group-size", groupSize); |
| 186 |
groupSeparator = typeCheck("group-separator", groupSeparator); |
| 187 |
letterValue = typeCheck("letter-value", letterValue); |
| 188 |
ordinal = typeCheck("ordinal", ordinal); |
| 189 |
lang = typeCheck("lang", lang); |
| 190 |
from = typeCheck("from", from); |
| 191 |
count = typeCheck("count", count); |
| 192 |
startAt = typeCheck("start-at", startAt); |
| 193 |
|
| 194 |
String errorCode = "XTTE1000"; |
| 195 |
if (value == null && select == null) { |
| 196 |
errorCode = "XTTE0990"; |
| 197 |
ContextItemExpression implicitSelect = new ContextItemExpression(); |
| 198 |
implicitSelect.setLocation(allocateLocation()); |
| 199 |
implicitSelect.setErrorCodeForUndefinedContext(errorCode); |
| 200 |
select = implicitSelect; |
| 201 |
} |
| 202 |
|
| 203 |
if (select != null) { |
| 204 |
try { |
| 205 |
RoleDiagnostic role = |
| 206 |
new RoleDiagnostic(RoleDiagnostic.INSTRUCTION, "xsl:number/select", 0); |
| 207 |
role.setErrorCode(errorCode); |
| 208 |
select = getConfiguration().getTypeChecker(false).staticTypeCheck(select, |
| 209 |
SequenceType.SINGLE_NODE, |
| 210 |
role, makeExpressionVisitor()); |
| 211 |
} catch (XPathException err) { |
| 212 |
compileError(err); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException { |
| 218 |
boolean valueSpecified = value != null; |
| 219 |
if (value == null) { |
| 220 |
value = new NumberInstruction( |
| 221 |
select, |
| 222 |
level, |
| 223 |
count, |
| 224 |
from); |
| 225 |
value.setLocation(allocateLocation()); |
| 226 |
} |
| 227 |
NumberSequenceFormatter numFormatter = new NumberSequenceFormatter( |
| 228 |
value, |
| 229 |
format, |
| 230 |
groupSize, |
| 231 |
groupSeparator, |
| 232 |
letterValue, |
| 233 |
ordinal, |
| 234 |
startAt, |
| 235 |
lang, |
| 236 |
formatter, |
| 237 |
xPath10ModeIsEnabled() && valueSpecified); |
| 238 |
numFormatter.setLocation(allocateLocation()); |
| 239 |
ValueOf inst = new ValueOf(numFormatter, false, false); |
| 240 |
inst.setLocation(allocateLocation()); |
| 241 |
inst.setIsNumberingInstruction(); |
| 242 |
return inst; |
| 243 |
} |
| 244 |
|
| 245 |
} |