001/* 002 * Copyright © 2012, 2013, 2014 Royal Botanic Gardens, Kew. 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 005 * 006 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 007 * 008 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 009 */ 010package org.kew.rmf.transformers; 011 012import java.util.HashMap; 013import java.util.Map; 014 015/** 016 * This transformer splits a string into a series of words. The word delimiter is any 017 * sequence of non-alphanumeric characters. It then iterates over the "words" and 018 * converts any Roman numerals to their Arabic equivalent, then concatenates these 019 * converted words back into a single string, using the space character to separate words. 020 * @author nn00kg 021 * 022 */ 023public class RomanNumeralTransformer implements Transformer{ 024 025 private static Map<String,String> map = new HashMap<String,String>(); 026 private final static String[] BASIC_ROMAN_NUMBERS = { "M", "CM", "D", "CD", 027 "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; 028 private final static int[] BASIC_VALUES = { 1000, 900, 500, 400, 100, 90, 029 50, 40, 10, 9, 5, 4, 1 }; 030 031 static{ 032 for (int i = 1; i <= 5000; i++){ 033 map.put(toRomanValue(i), Integer.toString(i)); 034 } 035 } 036 037 public static String toRomanValue(int arabicValue) { 038 StringBuffer sb = new StringBuffer(); 039 int remainder = arabicValue; 040 for (int i = 0; i < BASIC_VALUES.length; i++) { 041 while (remainder >= BASIC_VALUES[i]) { 042 sb.append(BASIC_ROMAN_NUMBERS[i]); 043 remainder -= BASIC_VALUES[i]; 044 } 045 } 046 return sb.toString(); 047 } 048 049 @Override 050 public String transform(String s) { 051 String[] words = s.replaceAll("[^A-Za-z0-9]", " ").replaceAll("\\s+", " ").split(" "); 052 String[] converted_words = new String[words.length]; 053 for (int i = 0; i < words.length; i++) { 054 String roman = map.get(words[i].toUpperCase()); 055 if (roman != null) 056 converted_words[i] = roman; 057 else 058 converted_words[i] = words[i]; 059 } 060 StringBuffer sb = new StringBuffer(); 061 for (String converted_word : converted_words){ 062 if (sb.length() > 0) 063 sb.append(" "); 064 sb.append(converted_word); 065 } 066 return sb.toString(); 067 } 068 069}