1 package net.sf.saxon.trans; 2 3 import net.sf.saxon.functions.FormatNumber2; 4 5 import java.io.Serializable ; 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 11 15 16 public class DecimalFormatManager implements Serializable { 17 18 private DecimalSymbols defaultDFS; 19 private HashMap formatTable; private boolean usingOriginalDefault = true; 21 22 25 26 public DecimalFormatManager() { 27 formatTable = new HashMap (10); 28 DecimalSymbols d = new DecimalSymbols(); 29 setDefaults(d); 30 defaultDFS = d; 31 } 32 33 36 37 public static void setDefaults(DecimalSymbols d) { 38 d.decimalSeparator = ('.'); 39 d.groupingSeparator = (','); 40 d.infinity = ("Infinity"); 41 d.minusSign = ('-'); 42 d.NaN = ("NaN"); 43 d.percent = ('%'); 44 d.permill = ('\u2030'); 45 d.zeroDigit = ('0'); 46 d.digit = ('#'); 47 d.patternSeparator = (';'); 48 } 49 50 55 56 public void setDefaultDecimalFormat(DecimalSymbols dfs, int precedence) 57 throws StaticError { 58 if (!usingOriginalDefault) { 59 if (!dfs.equals(defaultDFS)) { 60 StaticError err = new StaticError( 61 "There are two conflicting definitions of the default decimal format"); 62 err.setErrorCode("XTSE1290"); 63 throw err; 64 } 65 } 66 defaultDFS = dfs; 67 usingOriginalDefault = false; 68 setNamedDecimalFormat("", "", dfs, precedence); 69 } 71 72 76 77 public void fixupDefaultDefault() throws StaticError { 78 if (usingOriginalDefault) { 79 setNamedDecimalFormat("", "", defaultDFS, -1000); 80 } 81 } 82 83 86 87 public DecimalSymbols getDefaultDecimalFormat() { 88 return defaultDFS; 89 } 90 91 99 100 public void setNamedDecimalFormat(String uri, String localName, DecimalSymbols dfs, int precedence) 101 throws StaticError { 102 String dfskey = localName + '#' + uri; 103 Object o = formatTable.get(dfskey); 104 if (o != null) { 105 if (o instanceof List ) { 106 for (Iterator iter = ((List )o).iterator(); iter.hasNext(); ) { 108 FormatNumber2 call = (FormatNumber2)iter.next(); 109 call.fixup(dfs); 110 } 111 } else { 112 DecimalFormatInfo info = (DecimalFormatInfo)o; 113 DecimalSymbols old = info.dfs; 114 int oldPrecedence = info.precedence; 115 if (precedence < oldPrecedence) { 116 return; 117 } 118 if (precedence==oldPrecedence && !dfs.equals(old)) { 119 StaticError err = new StaticError("There are two conflicting definitions of the named decimal-format"); 120 err.setErrorCode("XTSE1290"); 121 throw err; 122 } 123 } 124 } 125 DecimalFormatInfo dfi = new DecimalFormatInfo(); 126 dfi.dfs = dfs; 127 dfi.precedence = precedence; 128 formatTable.put(dfskey, dfi); 129 } 130 131 136 137 public void registerUsage(String uri, String localName, FormatNumber2 call) { 138 String dfskey = localName + '#' + uri; 139 Object o = formatTable.get(dfskey); 140 if (o == null) { 141 List list = new ArrayList (10); 143 list.add(call); 144 formatTable.put(dfskey, list); 145 } else if (o instanceof List ) { 146 List list = (List )o; 148 list.add(call); 149 } else { 150 DecimalFormatInfo dfi = (DecimalFormatInfo)o; 152 call.fixup(dfi.dfs); 153 } 154 } 155 156 163 164 public DecimalSymbols getNamedDecimalFormat(String uri, String localName) { 165 String dfskey = localName + '#' + uri; 166 DecimalFormatInfo dfi = ((DecimalFormatInfo)formatTable.get(dfskey)); 167 if (dfi == null) { 168 return null; 169 } 170 return dfi.dfs; 171 } 172 173 private static class DecimalFormatInfo implements Serializable { 174 public DecimalSymbols dfs; 175 public int precedence; 176 } 177 178 179 } 180 181 | Popular Tags |