KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > admin > SetupLocalization


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

26 package org.snipsnap.net.admin;
27
28 import org.snipsnap.config.Configuration;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.Locale JavaDoc;
36 import java.util.Date JavaDoc;
37 import java.text.DateFormat JavaDoc;
38 import java.text.SimpleDateFormat JavaDoc;
39
40 public class SetupLocalization implements SetupHandler {
41   public String JavaDoc getName() {
42     return "localization";
43   }
44
45   private final static List JavaDoc countries = Arrays.asList(Locale.getISOCountries());
46   private final static List JavaDoc languages = Arrays.asList(Locale.getISOLanguages());
47
48   public Map JavaDoc setup(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Configuration config, Map JavaDoc errors) {
49     String JavaDoc 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 JavaDoc 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 JavaDoc df = new SimpleDateFormat JavaDoc(config.getWeblogDateFormat());
66       df.format(new Date JavaDoc());
67     } catch (Exception JavaDoc e) {
68       errors.put(Configuration.APP_WEBLOGDATEFORMAT, Configuration.APP_WEBLOGDATEFORMAT);
69     }
70     String JavaDoc 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 JavaDoc latStr = geoCoordinates.substring(0, commaIdx).trim();
76         String JavaDoc 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 JavaDoc 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