1 package com.thoughtworks.xstream.converters.extended; 2 3 import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter; 4 5 import java.util.Locale ; 6 7 13 public class LocaleConverter extends AbstractBasicConverter { 14 15 public boolean canConvert(Class type) { 16 return type.equals(Locale .class); 17 } 18 19 protected Object fromString(String str) { 20 int[] underscorePositions = underscorePositions(str); 21 String language, country, variant; 22 if (underscorePositions[0] == -1) { language = str; 24 country = ""; 25 variant = ""; 26 } else if (underscorePositions[1] == -1) { language = str.substring(0, underscorePositions[0]); 28 country = str.substring(underscorePositions[0] + 1); 29 variant = ""; 30 } else { language = str.substring(0, underscorePositions[0]); 32 country = str.substring(underscorePositions[0] + 1, underscorePositions[1]); 33 variant = str.substring(underscorePositions[1] + 1); 34 } 35 return new Locale (language, country, variant); 36 } 37 38 private int[] underscorePositions(String in) { 39 int[] result = new int[2]; 40 for (int i = 0; i < result.length; i++) { 41 int last = i == 0 ? 0 : result[i - 1]; 42 result[i] = in.indexOf('_', last + 1); 43 } 44 return result; 45 } 46 47 } | Popular Tags |