1 package net.sf.saxon.style; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.instruct.Executable; 4 import net.sf.saxon.om.AttributeCollection; 5 import net.sf.saxon.om.Name; 6 import net.sf.saxon.om.NamespaceException; 7 import net.sf.saxon.om.QNameException; 8 import net.sf.saxon.trans.DecimalFormatManager; 9 import net.sf.saxon.trans.DecimalSymbols; 10 import net.sf.saxon.trans.StaticError; 11 import net.sf.saxon.trans.XPathException; 12 import net.sf.saxon.value.StringValue; 13 14 import java.util.HashMap ; 15 16 19 20 public class XSLDecimalFormat extends StyleElement { 21 22 boolean prepared = false; 23 24 String name; 25 String decimalSeparator; 26 String groupingSeparator; 27 String infinity; 28 String minusSign; 29 String NaN; 30 String percent; 31 String perMille; 32 String zeroDigit; 33 String digit; 34 String patternSeparator; 35 36 public void prepareAttributes() throws XPathException { 37 38 if (prepared) { 39 return; 40 } 41 prepared = true; 42 43 AttributeCollection atts = getAttributeList(); 44 45 for (int a=0; a<atts.getLength(); a++) { 46 int nc = atts.getNameCode(a); 47 String f = getNamePool().getClarkName(nc); 48 if (f==StandardNames.NAME) { 49 name = atts.getValue(a).trim(); 50 } else if (f==StandardNames.DECIMAL_SEPARATOR) { 51 decimalSeparator = atts.getValue(a); 52 } else if (f==StandardNames.GROUPING_SEPARATOR) { 53 groupingSeparator = atts.getValue(a); 54 } else if (f==StandardNames.INFINITY) { 55 infinity = atts.getValue(a); 56 } else if (f==StandardNames.MINUS_SIGN) { 57 minusSign = atts.getValue(a); 58 } else if (f==StandardNames.NAN) { 59 NaN = atts.getValue(a); 60 } else if (f==StandardNames.PERCENT) { 61 percent = atts.getValue(a); 62 } else if (f==StandardNames.PER_MILLE) { 63 perMille = atts.getValue(a); 64 } else if (f==StandardNames.ZERO_DIGIT) { 65 zeroDigit = atts.getValue(a); 66 } else if (f==StandardNames.DIGIT) { 67 digit = atts.getValue(a); 68 } else if (f==StandardNames.PATTERN_SEPARATOR) { 69 patternSeparator = atts.getValue(a); 70 } else { 71 checkUnknownAttribute(nc); 72 } 73 } 74 } 75 76 public void validate() throws XPathException { 77 checkTopLevel(null); 78 checkEmpty(); 79 } 80 81 public DecimalSymbols makeDecimalFormatSymbols() throws XPathException { 82 DecimalSymbols d = new DecimalSymbols(); 83 DecimalFormatManager.setDefaults(d); 84 if (decimalSeparator!=null) { 85 d.decimalSeparator = (toChar(decimalSeparator)); 86 } 87 if (groupingSeparator!=null) { 88 d.groupingSeparator = (toChar(groupingSeparator)); 89 } 90 if (infinity!=null) { 91 d.infinity = (infinity); 92 } 93 if (minusSign!=null) { 94 d.minusSign = (toChar(minusSign)); 95 } 96 if (NaN!=null) { 97 d.NaN = (NaN); 98 } 99 if (percent!=null) { 100 d.percent = (toChar(percent)); 101 } 102 if (perMille!=null) { 103 d.permill = (toChar(perMille)); 104 } 105 if (zeroDigit!=null) { 106 d.zeroDigit = (toChar(zeroDigit)); 107 } 108 if (digit!=null) { 109 d.digit = (toChar(digit)); 110 } 111 if (patternSeparator!=null) { 112 d.patternSeparator = (toChar(patternSeparator)); 113 } 114 checkDistinctRoles(d); 115 return d; 116 } 117 118 122 123 private void checkDistinctRoles(DecimalSymbols dfs) throws XPathException { 124 HashMap map = new HashMap (20); 125 Integer c = new Integer (dfs.decimalSeparator); 126 map.put(c, StandardNames.DECIMAL_SEPARATOR); 127 128 c = new Integer (dfs.groupingSeparator); 129 if (map.get(c) != null) { 130 duplicate(StandardNames.GROUPING_SEPARATOR, (String )map.get(c)); 131 } 132 map.put(c, StandardNames.GROUPING_SEPARATOR); 133 134 c = new Integer (dfs.percent); 135 if (map.get(c) != null) { 136 duplicate(StandardNames.PERCENT, (String )map.get(c)); 137 } 138 map.put(c, StandardNames.PERCENT); 139 140 c = new Integer (dfs.permill); 141 if (map.get(c) != null) { 142 duplicate(StandardNames.PER_MILLE, (String )map.get(c)); 143 } 144 map.put(c, StandardNames.PER_MILLE); 145 146 c = new Integer (dfs.zeroDigit); 147 if (map.get(c) != null) { 148 duplicate(StandardNames.ZERO_DIGIT, (String )map.get(c)); 149 } 150 map.put(c, StandardNames.ZERO_DIGIT); 151 152 c = new Integer (dfs.digit); 153 if (map.get(c) != null) { 154 duplicate(StandardNames.DIGIT, (String )map.get(c)); 155 } 156 map.put(c, StandardNames.DIGIT); 157 158 c = new Integer (dfs.patternSeparator); 159 if (map.get(c) != null) { 160 duplicate(StandardNames.PATTERN_SEPARATOR, (String )map.get(c)); 161 } 162 map.put(c, StandardNames.PATTERN_SEPARATOR); 163 } 164 165 private void duplicate(String role1, String role2) throws XPathException { 166 compileError("The same character is used as the " + role1 + 167 " and as the " + role2, "XTSE1300"); 168 } 169 170 public void register() throws XPathException 171 { 172 prepareAttributes(); 173 DecimalSymbols d = makeDecimalFormatSymbols(); 174 DecimalFormatManager dfm = getPrincipalStylesheet().getDecimalFormatManager(); 175 if (name==null) { 176 try { 177 dfm.setDefaultDecimalFormat(d, getPrecedence()); 178 } catch (StaticError err) { 179 compileError(err.getMessage(), err.getErrorCodeLocalPart()); 180 } 181 } else { 182 try { 183 makeNameCode(name); String [] parts = Name.getQNameParts(name); 185 String uri = getURIForPrefix(parts[0], false); 186 try { 187 dfm.setNamedDecimalFormat(uri, parts[1], d, getPrecedence()); 188 } catch (StaticError err) { 189 compileError(err.getMessage(), err.getErrorCodeLocalPart()); 190 } 191 } catch (XPathException err) { 192 compileError("Invalid decimal format name. " + err.getMessage(), "XTSE0020"); 193 } catch (QNameException err) { 194 compileError("Invalid decimal format name. " + err.getMessage(), "XTSE0020"); 195 } catch (NamespaceException err) { 196 compileError("Invalid decimal format name. " + err.getMessage(), "XTSE0280"); 197 } 198 } 199 } 200 201 public Expression compile(Executable exec) throws XPathException { 202 return null; 203 } 204 205 211 private int toChar(String s) throws XPathException { 212 int[] e = StringValue.expand(s); 213 if (e.length!=1) 214 compileError("Attribute \"" + s + "\" should be a single character", "XTSE0020"); 215 return e[0]; 216 } 217 218 } 219 | Popular Tags |