| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.om.StructuredQName; |
| 12 |
import net.sf.saxon.regex.UnicodeString; |
| 13 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 14 |
import net.sf.saxon.z.IntHashMap; |
| 15 |
|
| 16 |
import java.util.Arrays; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class DecimalSymbols { |
| 24 |
|
| 25 |
public static final int DECIMAL_SEPARATOR = 0; |
| 26 |
public static final int GROUPING_SEPARATOR = 1; |
| 27 |
public static final int DIGIT = 2; |
| 28 |
public static final int MINUS_SIGN = 3; |
| 29 |
public static final int PERCENT = 4; |
| 30 |
public static final int PER_MILLE = 5; |
| 31 |
public static final int ZERO_DIGIT = 6; |
| 32 |
public static final int EXPONENT_SEPARATOR = 7; |
| 33 |
public static final int PATTERN_SEPARATOR = 8; |
| 34 |
public static final int INFINITY = 9; |
| 35 |
public static final int NAN = 10; |
| 36 |
|
| 37 |
private static final int ERR_NOT_SINGLE_CHAR = 0; |
| 38 |
private static final int ERR_NOT_UNICODE_DIGIT = 1; |
| 39 |
private static final int ERR_SAME_CHAR_IN_TWO_ROLES = 2; |
| 40 |
private static final int ERR_TWO_VALUES_FOR_SAME_PROPERTY = 3; |
| 41 |
|
| 42 |
private static String[] XSLT_CODES = {"XTSE0020", "XTSE1295", "XTSE1300", "XTSE1290"}; |
| 43 |
private static String[] XQUERY_CODES = {"XQST0097", "XQST0097", "XQST0098", "XQST0114"}; |
| 44 |
private String[] errorCodes = XSLT_CODES; |
| 45 |
|
| 46 |
|
| 47 |
private String infinityValue; |
| 48 |
private String NaNValue; |
| 49 |
|
| 50 |
public final static String[] propertyNames = { |
| 51 |
"decimal-separator", |
| 52 |
"grouping-separator", |
| 53 |
"digit", |
| 54 |
"minus-sign", |
| 55 |
"percent", |
| 56 |
"per-mille", |
| 57 |
"zero-digit", |
| 58 |
"exponent-separator", |
| 59 |
"pattern-separator", |
| 60 |
"infinity", |
| 61 |
"NaN" |
| 62 |
}; |
| 63 |
|
| 64 |
private int[] intValues = new int[propertyNames.length - 2]; |
| 65 |
private int[] precedences = new int[propertyNames.length]; |
| 66 |
private boolean[] inconsistent = new boolean[propertyNames.length]; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public DecimalSymbols(int language, int languageLevel) { |
| 73 |
intValues[DECIMAL_SEPARATOR] = '.'; |
| 74 |
intValues[GROUPING_SEPARATOR] = ','; |
| 75 |
intValues[DIGIT] = '#'; |
| 76 |
intValues[MINUS_SIGN] = '-'; |
| 77 |
intValues[PERCENT] = '%'; |
| 78 |
intValues[PER_MILLE] = '\u2030'; |
| 79 |
intValues[ZERO_DIGIT] = '0'; |
| 80 |
intValues[EXPONENT_SEPARATOR] = 'e'; |
| 81 |
intValues[PATTERN_SEPARATOR] = ';'; |
| 82 |
infinityValue = "Infinity"; |
| 83 |
NaNValue = "NaN"; |
| 84 |
Arrays.fill(precedences, Integer.MIN_VALUE); |
| 85 |
setHostLanguage(language, languageLevel); |
| 86 |
} |
| 87 |
|
| 88 |
public void setHostLanguage(int language, int languageLevel) { |
| 89 |
if (language == Configuration.XQUERY) { |
| 90 |
errorCodes = XQUERY_CODES; |
| 91 |
} else { |
| 92 |
errorCodes = XSLT_CODES; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public int getDecimalSeparator() { |
| 103 |
return intValues[DECIMAL_SEPARATOR]; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public int getGroupingSeparator() { |
| 113 |
return intValues[GROUPING_SEPARATOR]; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public int getDigit() { |
| 123 |
return intValues[DIGIT]; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public int getMinusSign() { |
| 133 |
return intValues[MINUS_SIGN]; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public int getPercent() { |
| 143 |
return intValues[PERCENT]; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public int getPerMille() { |
| 153 |
return intValues[PER_MILLE]; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public int getZeroDigit() { |
| 163 |
return intValues[ZERO_DIGIT]; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public int getExponentSeparator() { return intValues[EXPONENT_SEPARATOR]; } |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public int getPatternSeparator() { |
| 180 |
return intValues[PATTERN_SEPARATOR]; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public String getInfinity() { |
| 190 |
return infinityValue; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public String getNaN() { |
| 200 |
return NaNValue; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public void setDecimalSeparator(String value) throws XPathException { |
| 211 |
setProperty(DECIMAL_SEPARATOR, value, 0); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public void setGroupingSeparator(String value) throws XPathException { |
| 222 |
setProperty(GROUPING_SEPARATOR, value, 0); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public void setDigit(String value) throws XPathException { |
| 233 |
setProperty(DIGIT, value, 0); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public void setMinusSign(String value) throws XPathException { |
| 244 |
setProperty(MINUS_SIGN, value, 0); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public void setPercent(String value) throws XPathException { |
| 255 |
setProperty(PERCENT, value, 0); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public void setPerMille(String value) throws XPathException { |
| 266 |
setProperty(PER_MILLE, value, 0); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public void setZeroDigit(String value) throws XPathException { |
| 278 |
setProperty(ZERO_DIGIT, value, 0); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public void setExponentSeparator(String value) throws XPathException { |
| 289 |
setProperty(EXPONENT_SEPARATOR, value, 0); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
public void setPatternSeparator(String value) throws XPathException { |
| 300 |
setProperty(PATTERN_SEPARATOR, value, 0); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public void setInfinity(String value) throws XPathException { |
| 311 |
setProperty(INFINITY, value, 0); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public void setNaN(String value) throws XPathException { |
| 322 |
setProperty(NAN, value, 0); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public void setProperty(int key, String value, int precedence) throws XPathException { |
| 339 |
String name = propertyNames[key]; |
| 340 |
if (key <= PATTERN_SEPARATOR) { |
| 341 |
int intValue = singleChar(name, value); |
| 342 |
if (precedence > precedences[key]) { |
| 343 |
intValues[key] = intValue; |
| 344 |
precedences[key] = precedence; |
| 345 |
inconsistent[key] = false; |
| 346 |
} else if (precedence == precedences[key]) { |
| 347 |
if (intValue != intValues[key]) { |
| 348 |
inconsistent[key] = true; |
| 349 |
} |
| 350 |
} else { |
| 351 |
|
| 352 |
} |
| 353 |
if (key == ZERO_DIGIT && !isValidZeroDigit(intValue)) { |
| 354 |
throw new XPathException("The value of the zero-digit attribute must be a Unicode digit with value zero", |
| 355 |
errorCodes[ERR_NOT_UNICODE_DIGIT]); |
| 356 |
} |
| 357 |
} else if (key == INFINITY) { |
| 358 |
if (precedence > precedences[key]) { |
| 359 |
infinityValue = value; |
| 360 |
precedences[key] = precedence; |
| 361 |
inconsistent[key] = false; |
| 362 |
} else if (precedence == precedences[key]) { |
| 363 |
if (!infinityValue.equals(value)) { |
| 364 |
inconsistent[key] = true; |
| 365 |
} |
| 366 |
} |
| 367 |
} else if (key == NAN) { |
| 368 |
if (precedence > precedences[key]) { |
| 369 |
NaNValue = value; |
| 370 |
precedences[key] = precedence; |
| 371 |
inconsistent[key] = false; |
| 372 |
} else if (precedence == precedences[key]) { |
| 373 |
if (!NaNValue.equals(value)) { |
| 374 |
inconsistent[key] = false; |
| 375 |
} |
| 376 |
} |
| 377 |
} else { |
| 378 |
throw new IllegalArgumentException(); |
| 379 |
} |
| 380 |
|
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
public void setIntProperty(String name, int value) { |
| 390 |
for (int i=0; i<propertyNames.length; i++) { |
| 391 |
if (propertyNames[i].equals(name)) { |
| 392 |
intValues[i] = value; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
public void export(StructuredQName name, ExpressionPresenter out) { |
| 398 |
DecimalSymbols defaultSymbols = new DecimalSymbols(Configuration.XSLT, 31); |
| 399 |
out.startElement("decimalFormat"); |
| 400 |
if (name != null) { |
| 401 |
out.emitAttribute("name", name); |
| 402 |
} |
| 403 |
for (int i=0; i<intValues.length; i++) { |
| 404 |
int propValue = intValues[i]; |
| 405 |
if (propValue != defaultSymbols.intValues[i]) { |
| 406 |
out.emitAttribute(propertyNames[i], propValue + ""); |
| 407 |
} |
| 408 |
} |
| 409 |
if (!"Infinity".equals(getInfinity())) { |
| 410 |
out.emitAttribute("infinity", getInfinity()); |
| 411 |
} |
| 412 |
if (!"NaN".equals(getNaN())) { |
| 413 |
out.emitAttribute("NaN", getNaN()); |
| 414 |
} |
| 415 |
out.endElement(); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
private int singleChar(String name, String value) throws XPathException { |
| 427 |
UnicodeString us = UnicodeString.makeUnicodeString(value); |
| 428 |
if (us.uLength() != 1) { |
| 429 |
XPathException err = new XPathException("Attribute " + name + " should be a single character", |
| 430 |
errorCodes[ERR_NOT_SINGLE_CHAR]); |
| 431 |
err.setIsStaticError(true); |
| 432 |
throw err; |
| 433 |
} |
| 434 |
return us.uCharAt(0); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
public void checkConsistency(StructuredQName name) throws XPathException { |
| 447 |
|
| 448 |
for (int i = 0; i < 10; i++) { |
| 449 |
if (inconsistent[i]) { |
| 450 |
XPathException err = new XPathException( |
| 451 |
"Inconsistency in " + |
| 452 |
(name == null ? "unnamed decimal format. " : "decimal format " + name.getDisplayName() + ". ") + |
| 453 |
"There are two inconsistent values for decimal-format property " + propertyNames[i] + |
| 454 |
" at the same import precedence"); |
| 455 |
err.setErrorCode(errorCodes[ERR_TWO_VALUES_FOR_SAME_PROPERTY]); |
| 456 |
err.setIsStaticError(true); |
| 457 |
throw err; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
IntHashMap<String> map = new IntHashMap<String>(20); |
| 462 |
map.put(getDecimalSeparator(), "decimal-separator"); |
| 463 |
|
| 464 |
if (map.get(getGroupingSeparator()) != null) { |
| 465 |
duplicate("grouping-separator", map.get(getGroupingSeparator()), name); |
| 466 |
} |
| 467 |
map.put(getGroupingSeparator(), "grouping-separator"); |
| 468 |
|
| 469 |
if (map.get(getPercent()) != null) { |
| 470 |
duplicate("percent", map.get(getPercent()), name); |
| 471 |
} |
| 472 |
map.put(getPercent(), "percent"); |
| 473 |
|
| 474 |
if (map.get(getPerMille()) != null) { |
| 475 |
duplicate("per-mille", map.get(getPerMille()), name); |
| 476 |
} |
| 477 |
map.put(getPerMille(), "per-mille"); |
| 478 |
|
| 479 |
if (map.get(getDigit()) != null) { |
| 480 |
duplicate("digit", map.get(getDigit()), name); |
| 481 |
} |
| 482 |
map.put(getDigit(), "digit"); |
| 483 |
|
| 484 |
if (map.get(getPatternSeparator()) != null) { |
| 485 |
duplicate("pattern-separator", map.get(getPatternSeparator()), name); |
| 486 |
} |
| 487 |
map.put(getPatternSeparator(), "pattern-separator"); |
| 488 |
|
| 489 |
if (map.get(getExponentSeparator()) != null) { |
| 490 |
duplicate("exponent-separator", map.get(getExponentSeparator()), name); |
| 491 |
} |
| 492 |
map.put(getExponentSeparator(), "exponent-separator"); |
| 493 |
|
| 494 |
int zero = getZeroDigit(); |
| 495 |
for (int i = zero; i < zero + 10; i++) { |
| 496 |
if (map.get(i) != null) { |
| 497 |
XPathException err = new XPathException( |
| 498 |
"Inconsistent properties in " + |
| 499 |
(name == null ? "unnamed decimal format. " : "decimal format " + name.getDisplayName() + ". ") + |
| 500 |
"The same character is used as digit " + (i - zero) + |
| 501 |
" in the chosen digit family, and as the " + map.get(i)); |
| 502 |
err.setErrorCode(errorCodes[ERR_SAME_CHAR_IN_TWO_ROLES]); |
| 503 |
throw err; |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
private void duplicate(String role1, String role2, StructuredQName name) throws XPathException { |
| 518 |
XPathException err = new XPathException( |
| 519 |
"Inconsistent properties in " + |
| 520 |
(name == null ? "unnamed decimal format. " : "decimal format " + name.getDisplayName() + ". ") + |
| 521 |
"The same character is used as the " + role1 + " and as the " + role2); |
| 522 |
err.setErrorCode(errorCodes[ERR_SAME_CHAR_IN_TWO_ROLES]); |
| 523 |
throw err; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
public static boolean isValidZeroDigit(int zeroDigit) { |
| 534 |
return Arrays.binarySearch(zeroDigits, zeroDigit) >= 0; |
| 535 |
} |
| 536 |
|
| 537 |
static int[] zeroDigits = {0x0030, 0x0660, 0x06f0, 0x0966, 0x09e6, 0x0a66, 0x0ae6, 0x0b66, 0x0be6, 0x0c66, |
| 538 |
0x0ce6, 0x0d66, 0x0e50, 0x0ed0, 0x0f20, 0x1040, 0x17e0, 0x1810, 0x1946, 0x19d0, |
| 539 |
0xff10, 0x104a0, 0x1d7ce, 0x1d7d8, 0x1d7e2, 0x1d7ec, 0x1d7f6}; |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
public boolean equals(Object obj) { |
| 550 |
if (!(obj instanceof DecimalSymbols)) { |
| 551 |
return false; |
| 552 |
} |
| 553 |
DecimalSymbols o = (DecimalSymbols) obj; |
| 554 |
return getDecimalSeparator() == o.getDecimalSeparator() && |
| 555 |
getGroupingSeparator() == o.getGroupingSeparator() && |
| 556 |
getDigit() == o.getDigit() && |
| 557 |
getMinusSign() == o.getMinusSign() && |
| 558 |
getPercent() == o.getPercent() && |
| 559 |
getPerMille() == o.getPerMille() && |
| 560 |
getZeroDigit() == o.getZeroDigit() && |
| 561 |
getPatternSeparator() == o.getPatternSeparator() && |
| 562 |
getInfinity().equals(o.getInfinity()) && |
| 563 |
getNaN().equals(o.getNaN()); |
| 564 |
} |
| 565 |
|
| 566 |
public int hashCode() { |
| 567 |
return getDecimalSeparator() + (37 * getGroupingSeparator()) + (41 * getDigit()); |
| 568 |
} |
| 569 |
|
| 570 |
} |
| 571 |
|