1 10 package org.mmbase.util.transformers; 11 12 import java.util.regex.*; 13 14 22 23 public class RomanTransformer extends StringTransformer { 24 25 public static final Pattern NUMERIC = Pattern.compile("\\d+"); 26 public static final Pattern ROMAN = Pattern.compile("(?i)[ivxlcdm]+"); 27 28 29 32 public static final int I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000; 33 34 38 public static int romanToDecimal(char r) { 39 if (r == 'i') return I; 40 if (r == 'v') return V; 41 if (r == 'x') return X; 42 if (r == 'l') return L; 43 if (r == 'c') return C; 44 if (r == 'd') return D; 45 if (r == 'm') return M; 46 return 0; 47 } 48 52 53 public static char decimalToRomanDigit(int i) { 54 switch(i) { 55 case M: return 'm'; 56 case D: return 'd'; 57 case C: return 'c'; 58 case L: return 'l'; 59 case X: return 'x'; 60 case V: return 'v'; 61 case I: return 'i'; 62 default: return ' '; 63 } 64 } 65 66 69 public static int romanToDecimal(String roman) { 70 roman = roman.toLowerCase(); 71 int tot = 0; 72 int mode = I; 73 for (int i = roman.length() - 1; i >= 0 ; i--) { 74 int value = romanToDecimal(roman.charAt(i)); 75 76 if (value > mode) mode = value; 77 if (value < mode) { 78 tot -= value; 79 } else { 80 tot += value; 81 } 82 } 83 84 return tot; 85 } 86 90 public static String decimalToRoman(int value) { 91 if (value < 1 || value > 3999) { 92 return "" + value; 95 } 96 final StringBuffer buf = new StringBuffer (); 97 int mode = M; 98 while (value > 0) { 99 while (value < mode) mode /= 10; 100 if (value >= 9 * mode && mode < M) { 101 buf.append(decimalToRomanDigit(mode)); 102 buf.append(decimalToRomanDigit(mode * 10)); 103 value -= 9 * mode; 104 continue; 105 } 106 if (value >= 4 * mode && mode < M) { 107 if (value < 5 * mode) { 108 buf.append(decimalToRomanDigit(mode)); 109 value += mode; 110 } 111 buf.append(decimalToRomanDigit(5 * mode)); 112 value -= 5 * mode; 113 } 114 while (value >= mode) { 115 buf.append(decimalToRomanDigit(mode)); 116 value -= mode; 117 } 118 } 119 return buf.toString(); 120 } 121 122 123 public String transform(String r) { 125 try { 126 int i = Integer.parseInt(r); 127 return decimalToRoman(i); 128 } catch (Exception e) { 129 return r; 130 } 131 } 132 133 public String transformBack(String r) { 135 return "" + romanToDecimal(r); 136 } 137 138 public String toString() { 139 return "ROMAN"; 140 } 141 142 143 146 public static void main(String argv[]) { 147 if (argv.length == 0) { 148 System.out.println("Use roman or decimal argument"); 149 return; 150 } 151 if (argv.length == 1) { 152 if (NUMERIC.matcher(argv[0]).matches()) { 153 System.out.println(decimalToRoman(Integer.parseInt(argv[0]))); 154 } else { 155 System.out.println(romanToDecimal(argv[0])); 156 } 157 } 158 } 159 160 } 161 | Popular Tags |