1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2009 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.util.codelist;
18
19 import java.io.File;
20 import java.net.URL;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Locale;
24 import java.util.ResourceBundle;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 /**
29 * This class implements the base functionality for enums which
30 * represent codelists and implement the Interface Codelist.
31 *
32 * Since the extensibility of enum types is not supported by the
33 * language construct, the functional implementation is provided
34 * in this utility class.
35 *
36 * @svnLink $Revision: 4014 $;$Date: 2009-12-01 12:19:40 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/codelist/CodelistUtility.java $
37 *
38 * @author Jonas Hauenstein (JHN)
39 */
40 public class CodelistUtility {
41
42 /**
43 * The ResourceBundle base name.
44 */
45 private String resBaseName;
46
47 /**
48 * Constructor.
49 *
50 * @param resourceBaseName Base name of the associated ResourceBundle
51 */
52 public CodelistUtility(String resourceBaseName) {
53 resBaseName = resourceBaseName;
54 }
55
56 /**
57 * Getter for the short textual description of the code in a given language.
58 *
59 * This method returns the String with the property suffix ".short".
60 *
61 * @param code Code for which the text is loaded
62 * @param lang Java Locale of the desired language for the returned text
63 * @return The short textual description of the code
64 */
65 public String loadShortText(String code, Locale lang) {
66 ResourceBundle bundle = ResourceBundle.getBundle(resBaseName, lang);
67 return bundle.getString(code + ".short");
68 }
69
70 /**
71 * Getter for the long textual description of the code in a given language.
72 *
73 * This method returns the String with the property suffix ".long".
74 *
75 * @param code Code for which the text is loaded
76 * @param lang Java Locale of the desired language for the returned text
77 * @return The long textual description of the code
78 */
79 public String loadLongText(String code, Locale lang) {
80 ResourceBundle bundle = ResourceBundle.getBundle(resBaseName, lang);
81 return bundle.getString(code + ".long");
82 }
83
84
85 /**
86 * Returns a list of all java locales for which a translation of
87 * the textual description is available.
88 *
89 * Returns an empty list if no translations were found.
90 *
91 * @return List of java locales
92 */
93 public List<Locale> getLocales() {
94 return getAvailableLocales(resBaseName);
95 }
96
97 /**
98 * Returns a list of all java locales for which a .properties file
99 * corresponding to the ResourceBundle base name is available.
100 *
101 * Returns an empty list if no .properties files were found for
102 * the given base name.
103 *
104 * @param resourceBaseName Base name of the ResourceBundle
105 * @return List of java locales
106 */
107 public static List<Locale> getAvailableLocales(String resourceBaseName) {
108 String subdir = "";
109 String pbname = resourceBaseName;
110 List<Locale> loclist = new ArrayList<Locale>();
111
112 //check for subdir syntax and reconstruct path if necessary (separated by . or /)
113 if (resourceBaseName.contains(".")) {
114 int li = resourceBaseName.lastIndexOf(".");
115 subdir = resourceBaseName.substring(0, li);
116 subdir.replace(".", "/");
117 pbname = resourceBaseName.substring(li + 1);
118 } else if (resourceBaseName.contains("/")) {
119 int li = resourceBaseName.lastIndexOf("/");
120 subdir = resourceBaseName.substring(0, li);
121 pbname = resourceBaseName.substring(li + 1);
122 }
123
124 //get list of corresponding .properties files
125 File pdir = null;
126 try {
127 ClassLoader cld = Thread.currentThread().getContextClassLoader();
128 URL purl = cld.getResource(subdir);
129 pdir = new File(purl.getFile());
130 } catch (NullPointerException e) {
131 return loclist;
132 }
133 if (pdir.exists()) {
134 String[] flist = pdir.list();
135
136 //regexp to match all .properties files including mask for locales
137 Pattern bfpattern = Pattern.compile("^" + pbname + "_(([a-z]{2})(_([A-Z]{2})){0,1})\\.properties$");
138
139 for (String e : flist) {
140 Matcher m = bfpattern.matcher(e);
141 if (m.find()) {
142 //create new locale depending on regexp result
143 //(single _ or double _ if language variant is specified)
144 Locale l = (m.group(4) == null) ? new Locale(m.group(1)) : new Locale(m.group(2), m.group(4));
145 loclist.add(l);
146 }
147 }
148 }
149 return loclist;
150 }
151
152 }