Debian Codegraph @

net/sf/saxon/trans/DecimalSymbols.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.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 * This class is modelled on Java's DecimalFormatSymbols, but it allows the use of any
20 * Unicode character to represent symbols such as the decimal point and the grouping
21 * separator, whereas DecimalFormatSymbols restricts these to a char (1-65535).
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 * Create a DecimalSymbols object with default values for all properties
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 * Get the decimal separator value
98 *
99 * @return the decimal separator value that has been explicitly set, or its default ('.')
100 */
101
102 public int getDecimalSeparator() {
103 return intValues[DECIMAL_SEPARATOR];
104 }
105
106 /**
107 * Get the grouping separator value
108 *
109 * @return the grouping separator value that has been explicitly set, or its default (',')
110 */
111
112 public int getGroupingSeparator() {
113 return intValues[GROUPING_SEPARATOR];
114 }
115
116 /**
117 * Get the digit symbol value
118 *
119 * @return the digit symbol value that has been explicitly set, or its default ('#')
120 */
121
122 public int getDigit() {
123 return intValues[DIGIT];
124 }
125
126 /**
127 * Get the minus sign value
128 *
129 * @return the minus sign value that has been explicitly set, or its default ('-')
130 */
131
132 public int getMinusSign() {
133 return intValues[MINUS_SIGN];
134 }
135
136 /**
137 * Get the percent symbol value
138 *
139 * @return the percent symbol value that has been explicitly set, or its default ('%')
140 */
141
142 public int getPercent() {
143 return intValues[PERCENT];
144 }
145
146 /**
147 * Get the per-mille symbol value
148 *
149 * @return the per-mille symbol value that has been explicitly set, or its default
150 */
151
152 public int getPerMille() {
153 return intValues[PER_MILLE];
154 }
155
156 /**
157 * Get the zero digit symbol value
158 *
159 * @return the zero digit symbol value that has been explicitly set, or its default ('0')
160 */
161
162 public int getZeroDigit() {
163 return intValues[ZERO_DIGIT];
164 }
165
166 /**
167 * Get the exponent separator symbol
168 * @return the exponent separator character that has been explicitly set, or its default ('e');
169 */
170
171 public int getExponentSeparator() { return intValues[EXPONENT_SEPARATOR]; }
172
173 /**
174 * Get the pattern separator value
175 *
176 * @return the pattern separator value that has been explicitly set, or its default (';')
177 */
178
179 public int getPatternSeparator() {
180 return intValues[PATTERN_SEPARATOR];
181 }
182
183 /**
184 * Get the infinity symbol value
185 *
186 * @return the infinity symbol value that has been explicitly set, or its default ('Infinity')
187 */
188
189 public String getInfinity() {
190 return infinityValue;
191 }
192
193 /**
194 * Get the NaN symbol value
195 *
196 * @return the NaN symbol value that has been explicitly set, or its default ('NaN')
197 */
198
199 public String getNaN() {
200 return NaNValue;
201 }
202
203 /**
204 * Set the character to be used as the decimal separator
205 *
206 * @param value the character to be used
207 * @throws XPathException if the value is not a single Unicode character (a surrogate pair is permitted)
208 */
209
210 public void setDecimalSeparator(String value) throws XPathException {
211 setProperty(DECIMAL_SEPARATOR, value, 0);
212 }
213
214 /**
215 * Set the character to be used as the grouping separator
216 *
217 * @param value the character to be used
218 * @throws XPathException if the value is not a single Unicode character (a surrogate pair is permitted)
219 */
220
221 public void setGroupingSeparator(String value) throws XPathException {
222 setProperty(GROUPING_SEPARATOR, value, 0);
223 }
224
225 /**
226 * Set the character to be used as the digit symbol (default is '#')
227 *
228 * @param value the character to be used
229 * @throws XPathException if the value is not a single Unicode character (a surrogate pair is permitted)
230 */
231
232 public void setDigit(String value) throws XPathException {
233 setProperty(DIGIT, value, 0);
234 }
235
236 /**
237 * Set the character to be used as the minus sign
238 *
239 * @param value the character to be used
240 * @throws XPathException if the value is not a single Unicode character (a surrogate pair is permitted)
241 */
242
243 public void setMinusSign(String value) throws XPathException {
244 setProperty(MINUS_SIGN, value, 0);
245 }
246
247 /**
248 * Set the character to be used as the percent sign
249 *
250 * @param value the character to be used
251 * @throws XPathException if the value is not a single Unicode character (a surrogate pair is permitted)
252 */
253
254 public void setPercent(String value) throws XPathException {
255 setProperty(PERCENT, value, 0);
256 }
257
258 /**
259 * Set the character to be used as the per-mille sign
260 *
261 * @param value the character to be used
262 * @throws XPathException if the value is not a single Unicode character (a surrogate pair is permitted)
263 */
264
265 public void setPerMille(String value) throws XPathException {
266 setProperty(PER_MILLE, value, 0);
267 }
268
269 /**
270 * Set the character to be used as the zero digit (which determines the digit family used in the output)
271 *
272 * @param value the character to be used
273 * @throws XPathException if the value is not a single Unicode character (a surrogate pair is permitted),
274 * or if it is not a character classified in Unicode as a digit with numeric value zero
275 */
276
277 public void setZeroDigit(String value) throws XPathException {
278 setProperty(ZERO_DIGIT, value, 0);
279 }
280
281 /**
282 * Set the character to be used as the exponent separator
283 *
284 * @param value the character to be used
285 * @throws XPathException if the value is not a single Unicode character (a surrogate pair is permitted)
286 */
287
288 public void setExponentSeparator(String value) throws XPathException {
289 setProperty(EXPONENT_SEPARATOR, value, 0);
290 }
291
292 /**
293 * Set the character to be used as the pattern separator (default ';')
294 *
295 * @param value the character to be used
296 * @throws XPathException if the value is not a single Unicode character (a surrogate pair is permitted)
297 */
298
299 public void setPatternSeparator(String value) throws XPathException {
300 setProperty(PATTERN_SEPARATOR, value, 0);
301 }
302
303 /**
304 * Set the string to be used to represent infinity
305 *
306 * @param value the string to be used
307 * @throws XPathException - should not happen
308 */
309
310 public void setInfinity(String value) throws XPathException {
311 setProperty(INFINITY, value, 0);
312 }
313
314 /**
315 * Set the string to be used to represent NaN
316 *
317 * @param value the string to be used
318 * @throws XPathException - should not happen
319 */
320
321 public void setNaN(String value) throws XPathException {
322 setProperty(NAN, value, 0);
323 }
324
325 /**
326 * Set the value of a property
327 *
328 * @param key the integer key of the property to be set
329 * @param value the value of the property as a string (in many cases, this must be a single character)
330 * @param precedence the precedence of the property value
331 * @throws XPathException if the property is invalid.
332 * This method does not check the consistency of different properties. If two different values are supplied
333 * for the same property at the same precedence, the method does not complain, but notes the fact, and if
334 * the inconsistency is not subsequently cleared by supplying another value at a higher precedence, the
335 * error is reported when the checkConsistency() method is subsequently called.
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 // ignore the new value
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 * Set one of the single-character properties. Used when reloading an exported package
385 * @param name the name of the property
386 * @param value the Unicode codepoint of the property value
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 * Get the Unicode codepoint corresponding to a String, which must represent a single Unicode character
420 *
421 * @param name the name of the property, for use in error messages
422 * @param value the input string, representing a single Unicode character, perhaps as a surrogate pair
423 * @return the corresponding Unicode codepoint
424 * @throws XPathException if the supplied string is not a single character
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 * Check that no character is used in more than one role
440 *
441 * @param name the name of the decimal format (null for the unnamed decimal format)
442 * @throws XPathException if the same character is used in conflicting rules, for example as decimal separator
443 * and also as grouping separator
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 * Report that a character is used in more than one role
510 *
511 * @param role1 the first role
512 * @param role2 the second role
513 * @param name the name of the decimal format (null for the unnamed decimal format)
514 * @throws XPathException (always)
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 * Check that the character declared as a zero-digit is indeed a valid zero-digit
528 *
529 * @param zeroDigit the value to be checked
530 * @return false if it is not a valid zero-digit
531 */
532
533 public static boolean isValidZeroDigit(int zeroDigit) {
534 return Arrays.binarySearch(zeroDigits, zeroDigit) >= 0;
535 }
536
537 /*@NotNull*/ 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 * Test if two sets of decimal format symbols are the same
543 *
544 * @param obj the other set of symbols
545 * @return true if the same characters/strings are assigned to each role in both sets of symbols.
546 * The precedences are not compared.
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