1 20 23 package org.outerj.daisy.books.publisher.impl.publicationprocess; 24 26 import java.util.Locale ; 27 import java.util.NoSuchElementException ; 28 29 import org.w3c.dom.Element ; 30 31 35 class NumeratorFormatter 36 { 40 private final static DecimalToRoman m_romanConvertTable[] = { 41 new DecimalToRoman(1000, "M", 900, "CM"), 42 new DecimalToRoman(500, "D", 400, "CD"), 43 new DecimalToRoman(100L, "C", 90L, "XC"), 44 new DecimalToRoman(50L, "L", 40L, "XL"), 45 new DecimalToRoman(10L, "X", 9L, "IX"), 46 new DecimalToRoman(5L, "V", 4L, "IV"), 47 new DecimalToRoman(1L, "I", 1L, "I") }; 48 49 52 private final static char[] m_alphaCountTable = { 'Z', 'A', 'B', 'C', 'D', 'E', 54 'F', 'G', 'H', 'I', 'J', 55 'K', 'L', 'M', 'N', 'O', 56 'P', 'Q', 'R', 'S', 'T', 57 'U', 'V', 'W', 'X', 'Y' }; 58 59 70 public static String int2alphaCount(int val) 71 { 72 char[] table = m_alphaCountTable; 73 int radix = table.length; 74 75 char buf[] = new char[100]; 79 80 int charPos = buf.length - 1; 83 int lookupIndex = 1; 86 int correction = 0; 111 112 do 114 { 115 116 correction = 120 ((lookupIndex == 0) || (correction != 0 && lookupIndex == radix - 1)) 121 ? (radix - 1) : 0; 122 123 lookupIndex = (val + correction) % radix; 125 126 val = (val / radix); 128 129 if (lookupIndex == 0 && val == 0) 131 break; 132 133 buf[charPos--] = table[lookupIndex]; 135 } 136 while (val > 0); 137 138 return new String (buf, charPos + 1, (buf.length - charPos - 1)); 139 } 140 141 150 public static String long2roman(long val, boolean prefixesAreOK) 151 { 152 153 if (val <= 0) 154 { 155 return "#E(" + val + ")"; 156 } 157 158 String roman = ""; 159 int place = 0; 160 161 if (val <= 3999L) 162 { 163 do 164 { 165 while (val >= m_romanConvertTable[place].m_postValue) 166 { 167 roman += m_romanConvertTable[place].m_postLetter; 168 val -= m_romanConvertTable[place].m_postValue; 169 } 170 171 if (prefixesAreOK) 172 { 173 if (val >= m_romanConvertTable[place].m_preValue) 174 { 175 roman += m_romanConvertTable[place].m_preLetter; 176 val -= m_romanConvertTable[place].m_preValue; 177 } 178 } 179 180 place++; 181 } 182 while (val > 0); 183 } 184 else 185 { 186 roman = "#error"; 187 } 188 189 return roman; 190 } } 192 193 196 class DecimalToRoman 197 { 198 199 210 public DecimalToRoman(long postValue, String postLetter, long preValue, 211 String preLetter) 212 { 213 214 this.m_postValue = postValue; 215 this.m_postLetter = postLetter; 216 this.m_preValue = preValue; 217 this.m_preLetter = preLetter; 218 } 219 220 221 public long m_postValue; 222 223 224 public String m_postLetter; 225 226 227 public long m_preValue; 228 229 230 public String m_preLetter; 231 } 232 | Popular Tags |