1 16 19 package org.apache.xalan.transformer; 20 21 import java.util.Locale ; 22 import java.util.NoSuchElementException ; 23 24 import org.w3c.dom.Element ; 25 26 31 class NumeratorFormatter 32 { 33 34 35 protected Element m_xslNumberElement; 36 37 38 NumberFormatStringTokenizer m_formatTokenizer; 39 40 41 Locale m_locale; 42 43 44 java.text.NumberFormat m_formatter; 45 46 47 TransformerImpl m_processor; 48 49 53 private final static DecimalToRoman m_romanConvertTable[] = { 54 new DecimalToRoman(1000, "M", 900, "CM"), 55 new DecimalToRoman(500, "D", 400, "CD"), 56 new DecimalToRoman(100L, "C", 90L, "XC"), 57 new DecimalToRoman(50L, "L", 40L, "XL"), 58 new DecimalToRoman(10L, "X", 9L, "IX"), 59 new DecimalToRoman(5L, "V", 4L, "IV"), 60 new DecimalToRoman(1L, "I", 1L, "I") }; 61 62 66 private final static char[] m_alphaCountTable = { 'Z', 'A', 'B', 'C', 'D', 'E', 68 'F', 'G', 'H', 'I', 'J', 69 'K', 'L', 'M', 'N', 'O', 70 'P', 'Q', 'R', 'S', 'T', 71 'U', 'V', 'W', 'X', 'Y' }; 72 73 82 NumeratorFormatter(Element xslNumberElement, TransformerImpl processor) 83 { 84 m_xslNumberElement = xslNumberElement; 85 m_processor = processor; 86 } 88 100 protected String int2alphaCount(int val, char[] table) 101 { 102 103 int radix = table.length; 104 105 char buf[] = new char[100]; 109 110 int charPos = buf.length - 1; 113 int lookupIndex = 1; 116 int correction = 0; 141 142 do 144 { 145 146 correction = 150 ((lookupIndex == 0) || (correction != 0 && lookupIndex == radix - 1)) 151 ? (radix - 1) : 0; 152 153 lookupIndex = (val + correction) % radix; 155 156 val = (val / radix); 158 159 if (lookupIndex == 0 && val == 0) 161 break; 162 163 buf[charPos--] = table[lookupIndex]; 165 } 166 while (val > 0); 167 168 return new String (buf, charPos + 1, (buf.length - charPos - 1)); 169 } 170 171 180 String long2roman(long val, boolean prefixesAreOK) 181 { 182 183 if (val <= 0) 184 { 185 return "#E(" + val + ")"; 186 } 187 188 String roman = ""; 189 int place = 0; 190 191 if (val <= 3999L) 192 { 193 do 194 { 195 while (val >= m_romanConvertTable[place].m_postValue) 196 { 197 roman += m_romanConvertTable[place].m_postLetter; 198 val -= m_romanConvertTable[place].m_postValue; 199 } 200 201 if (prefixesAreOK) 202 { 203 if (val >= m_romanConvertTable[place].m_preValue) 204 { 205 roman += m_romanConvertTable[place].m_preLetter; 206 val -= m_romanConvertTable[place].m_preValue; 207 } 208 } 209 210 place++; 211 } 212 while (val > 0); 213 } 214 else 215 { 216 roman = "#error"; 217 } 218 219 return roman; 220 } 222 226 class NumberFormatStringTokenizer 227 { 228 229 230 private int currentPosition; 231 232 233 private int maxPosition; 234 235 236 private String str; 237 238 243 NumberFormatStringTokenizer(String str) 244 { 245 this.str = str; 246 maxPosition = str.length(); 247 } 248 249 253 void reset() 254 { 255 currentPosition = 0; 256 } 257 258 265 String nextToken() 266 { 267 268 if (currentPosition >= maxPosition) 269 { 270 throw new NoSuchElementException (); 271 } 272 273 int start = currentPosition; 274 275 while ((currentPosition < maxPosition) 276 && Character.isLetterOrDigit(str.charAt(currentPosition))) 277 { 278 currentPosition++; 279 } 280 281 if ((start == currentPosition) 282 && (!Character.isLetterOrDigit(str.charAt(currentPosition)))) 283 { 284 currentPosition++; 285 } 286 287 return str.substring(start, currentPosition); 288 } 289 290 295 boolean hasMoreTokens() 296 { 297 return (currentPosition >= maxPosition) ? false : true; 298 } 299 300 309 int countTokens() 310 { 311 312 int count = 0; 313 int currpos = currentPosition; 314 315 while (currpos < maxPosition) 316 { 317 int start = currpos; 318 319 while ((currpos < maxPosition) 320 && Character.isLetterOrDigit(str.charAt(currpos))) 321 { 322 currpos++; 323 } 324 325 if ((start == currpos) 326 && (Character.isLetterOrDigit(str.charAt(currpos)) == false)) 327 { 328 currpos++; 329 } 330 331 count++; 332 } 333 334 return count; 335 } 336 } } 338 | Popular Tags |