1 26 package org.snipsnap.net.admin; 27 28 import org.snipsnap.config.Configuration; 29 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import java.util.Map ; 33 import java.util.List ; 34 import java.util.Arrays ; 35 import java.util.Locale ; 36 import java.util.Date ; 37 import java.text.DateFormat ; 38 import java.text.SimpleDateFormat ; 39 40 public class SetupLocalization implements SetupHandler { 41 public String getName() { 42 return "localization"; 43 } 44 45 private final static List countries = Arrays.asList(Locale.getISOCountries()); 46 private final static List languages = Arrays.asList(Locale.getISOLanguages()); 47 48 public Map setup(HttpServletRequest request, HttpServletResponse response, Configuration config, Map errors) { 49 String country = request.getParameter(Configuration.APP_COUNTRY); 50 if (countries.contains(country)) { 51 config.setCountry(country); 52 } else { 53 errors.put(Configuration.APP_COUNTRY, Configuration.APP_COUNTRY); 54 } 55 56 String language = request.getParameter(Configuration.APP_LANGUAGE); 57 if (languages.contains(language)) { 58 config.setLanguage(language); 59 } else { 60 errors.put(Configuration.APP_LANGUAGE, Configuration.APP_LANGUAGE); 61 } 62 config.setTimezone(request.getParameter(Configuration.APP_TIMEZONE)); 63 config.setWeblogDateFormat(request.getParameter(Configuration.APP_WEBLOGDATEFORMAT)); 64 try { 65 DateFormat df = new SimpleDateFormat (config.getWeblogDateFormat()); 66 df.format(new Date ()); 67 } catch (Exception e) { 68 errors.put(Configuration.APP_WEBLOGDATEFORMAT, Configuration.APP_WEBLOGDATEFORMAT); 69 } 70 String geoCoordinates = request.getParameter(Configuration.APP_GEOCOORDINATES); 71 if (null != geoCoordinates && !"".equals(geoCoordinates)) { 72 config.setGeoCoordinates(geoCoordinates); 73 int commaIdx = geoCoordinates.indexOf(','); 74 if (commaIdx > 0) { 75 String latStr = geoCoordinates.substring(0, commaIdx).trim(); 76 String lonStr = geoCoordinates.substring(commaIdx + 1).trim(); 77 if (latStr.length() == 0 || lonStr.length() == 0) { 78 errors.put(Configuration.APP_GEOCOORDINATES, Configuration.APP_GEOCOORDINATES); 79 } else { 80 try { 81 double latitude = Double.parseDouble(latStr); 82 double longitude = Double.parseDouble(lonStr); 83 if (latitude >= -90 && latitude <= 90 && longitude >= -180 && longitude <= 180) { 84 config.setGeoCoordinates(geoCoordinates); 85 } else { 86 errors.put(Configuration.APP_GEOCOORDINATES, Configuration.APP_GEOCOORDINATES + ".range"); 87 } 88 } catch (NumberFormatException e) { 89 errors.put(Configuration.APP_GEOCOORDINATES, Configuration.APP_GEOCOORDINATES + ".format"); 90 e.printStackTrace(); 91 } 92 } 93 } else { 94 errors.put(Configuration.APP_GEOCOORDINATES, Configuration.APP_GEOCOORDINATES); 95 } 96 } 97 return errors; 98 } 99 } 100 | Popular Tags |