package pt.linkare.ant.propreaders; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Locale; import pt.linkare.ant.InvalidPropertySpecException; import pt.linkare.ant.MenuMessage; public class LangStringPropertyReader extends AbstractPropertyReader{ public LangStringPropertyReader() { super(); } public String readProperty() throws InvalidPropertySpecException, UnsupportedEncodingException { return getLangString(); } public String getLangString() throws InvalidPropertySpecException, UnsupportedEncodingException { MenuMessage menuOptionsLang=new MenuMessage(); menuOptionsLang.setMessage(buildDefaultMessage(false)); List[] optionsAndValuesLang=null; if(getProperty().getMetaData("langs")!=null) { optionsAndValuesLang=buildLangOptions(getProperty().getMetaData("langs")); } else optionsAndValuesLang=buildLangOptions(); menuOptionsLang.setOptions(optionsAndValuesLang[0]); menuOptionsLang.setOptionValues(optionsAndValuesLang[1]); if(getProperty().getPropertyDefaultValue()!=null) return getInput().readMenuOptionOrDefault(menuOptionsLang, getProperty().getPropertyDefaultValue()); else return getInput().readMenuOption(menuOptionsLang); } @SuppressWarnings("unchecked") private List[] buildLangOptions(String metaData) throws InvalidPropertySpecException { List langs=parseOptions(metaData); List[] optionsValuesRetVal=(List[])new List[]{new ArrayList(langs.size()),new ArrayList(langs.size())}; List locales=new ArrayList(langs.size()); for(String lang:langs) { locales.add(new Locale(lang)); } for(Locale locale:locales) { optionsValuesRetVal[0].add(locale.getDisplayLanguage()); optionsValuesRetVal[1].add(locale.getLanguage()); } return optionsValuesRetVal; } @SuppressWarnings("unchecked") private List[] buildLangOptions() { Locale[] locales=Locale.getAvailableLocales(); List[] optionsValuesRetVal=(List[])new List[]{new ArrayList(locales.length),new ArrayList(locales.length)}; HashSet languagesDone=new HashSet(); optionsValuesRetVal[0]=new ArrayList(); optionsValuesRetVal[1]=new ArrayList(locales.length); for(Locale locale:locales) { if(!languagesDone.contains(locale.getLanguage())) { optionsValuesRetVal[0].add(locale.getDisplayLanguage()); optionsValuesRetVal[1].add(locale.getLanguage()); languagesDone.add(locale.getLanguage()); } } return optionsValuesRetVal; } }