| 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.om.StructuredQName; |
| 11 | |
| 12 | import java.util.HashMap; |
| 13 | import java.util.Map; |
| 14 | |
| 15 | /** |
| 16 | * DecimalFormatManager manages the collection of named and unnamed decimal formats, for use by the |
| 17 | * format-number() function. |
| 18 | * <p>In XSLT 2.0, there is a single set of decimal formats shared by the whole stylesheet. In XQuery 3.0, however, |
| 19 | * each query module has its own set of decimal formats, and in XSLT 3.0 decimal formats are local to a package. |
| 20 | * The DecimalFormatManager to use is therefore linked from the format-number() call on the expression tree.</p> |
| 21 | * |
| 22 | * @author Michael H. Kay |
| 23 | */ |
| 24 | |
| 25 | public class DecimalFormatManager { |
| 26 | |
| 27 | private DecimalSymbols defaultDFS; |
| 28 | private HashMap<StructuredQName, DecimalSymbols> formatTable; // table for named decimal formats |
| 29 | private int language; |
| 30 | private int languageLevel; |
| 31 | |
| 32 | /** |
| 33 | * create a DecimalFormatManager and initialise variables |
| 34 | */ |
| 35 | |
| 36 | public DecimalFormatManager(int language, int languageLevel) { |
| 37 | formatTable = new HashMap<StructuredQName, DecimalSymbols>(10); |
| 38 | defaultDFS = new DecimalSymbols(language, languageLevel); |
| 39 | this.language = language; |
| 40 | this.languageLevel = languageLevel; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the default decimal-format. |
| 45 | * |
| 46 | * @return the default (unnamed) decimal format |
| 47 | */ |
| 48 | |
| 49 | public DecimalSymbols getDefaultDecimalFormat() { |
| 50 | return defaultDFS; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get a named decimal-format registered using setNamedDecimalFormat |
| 55 | * |
| 56 | * @param qName The name of the decimal format |
| 57 | * @return the DecimalSymbols object corresponding to the given name, if any |
| 58 | * or null if not set. |
| 59 | */ |
| 60 | |
| 61 | /*@Nullable*/ |
| 62 | public DecimalSymbols getNamedDecimalFormat(StructuredQName qName) { |
| 63 | DecimalSymbols ds = formatTable.get(qName); |
| 64 | if (ds == null) { |
| 65 | return null; |
| 66 | // following two lines had been added to the code since 9.4, but they break XSLT test error089 |
| 67 | // ds = new DecimalSymbols(); |
| 68 | // formatTable.put(qName, ds); |
| 69 | } |
| 70 | return ds; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get a named decimal-format registered using setNamedDecimalFormat if it exists; |
| 75 | * create it if it does not |
| 76 | * |
| 77 | * @param qName The name of the decimal format |
| 78 | * @return the DecimalSymbols object corresponding to the given name, if it exists, |
| 79 | * or a newly created DecimalSymbols object otherwise |
| 80 | */ |
| 81 | |
| 82 | public DecimalSymbols obtainNamedDecimalFormat(StructuredQName qName) { |
| 83 | DecimalSymbols ds = formatTable.get(qName); |
| 84 | if (ds == null) { |
| 85 | ds = new DecimalSymbols(language, languageLevel); |
| 86 | formatTable.put(qName, ds); |
| 87 | } |
| 88 | return ds; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the names of all named decimal formats that have been registered |
| 93 | * @return the collection of names |
| 94 | */ |
| 95 | |
| 96 | public Iterable<StructuredQName> getDecimalFormatNames() { |
| 97 | return formatTable.keySet(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Check the consistency of all DecimalSymbols objects owned by this DecimalFormatManager |
| 102 | * |
| 103 | * @throws XPathException if any inconsistencies are found |
| 104 | */ |
| 105 | |
| 106 | public void checkConsistency() throws XPathException { |
| 107 | defaultDFS.checkConsistency(null); |
| 108 | for (Map.Entry<StructuredQName, DecimalSymbols> entry : formatTable.entrySet()) { |
| 109 | entry.getValue().checkConsistency(entry.getKey()); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } |
| 114 |