1+ /*
2+ * Copyright 2014 Université de Montréal
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package org .ocpsoft .rewrite .transposition ;
17+
18+ import java .util .Locale ;
19+ import java .util .Map ;
20+ import java .util .MissingResourceException ;
21+ import java .util .ResourceBundle ;
22+ import java .util .concurrent .ConcurrentHashMap ;
23+
24+ import org .ocpsoft .rewrite .bind .Binding ;
25+ import org .ocpsoft .rewrite .context .EvaluationContext ;
26+ import org .ocpsoft .rewrite .event .Rewrite ;
27+ import org .ocpsoft .rewrite .exception .RewriteException ;
28+ import org .ocpsoft .rewrite .param .Parameter ;
29+ import org .ocpsoft .rewrite .param .Parameters ;
30+ import org .ocpsoft .rewrite .param .Transposition ;
31+
32+ /**
33+ * A {@link Transposition} responsible for translating values from their matching translation from a
34+ * {@link ResourceBundle}. The lookup in the properties file is case-sensitive.
35+ *
36+ * Requires inverted properties files in the form of translated_name=name_to_bind.
37+ *
38+ * TODO Should we allow to load the properties file in a different encoding? TODO Allow to enable case insensitive
39+ * and/or ascii-folding on property lookup
40+ *
41+ * @author Christian Gendreau
42+ */
43+ public class LocaleTransposition implements Transposition <String >
44+ {
45+
46+ // shared thread safe map between a String(representing the language) and a ResourceBundle.
47+ private static Map <String , ResourceBundle > bundleMap = new ConcurrentHashMap <String , ResourceBundle >();
48+
49+ private final String languageParam ;
50+ private final String bundleName ;
51+
52+ private LocaleTransposition (final String languageParam , final String bundleName )
53+ {
54+ this .languageParam = languageParam ;
55+ this .bundleName = bundleName ;
56+ }
57+
58+ /**
59+ * Create a {@link Binding} to a {@link ResourceBundle}, where the initial bound {@link Parameter} value is used as
60+ * the bundle lookup key. The resultant value of the bundle lookup is stored as the new bound {@link Parameter}
61+ * value.
62+ * <p>
63+ * For example, consider the following URL-based rule:
64+ *
65+ * <pre>
66+ * Configuration config = ConfigurationBuilder.begin()
67+ * .addRule(Join.path("/{lang}/{path}").to("/{path}"))
68+ * .where("path").transposedBy(LocaleTransposition.bundle("org.example.Paths", "lang"));
69+ * </pre>
70+ * <p>
71+ * In the above scenario, "bundleName" is the resource bundle name. The value of the {@link Parameter} "lang" is used
72+ * as the bundle {@link Locale}. The initial value of the {@link Parameter} "path" is used as the lookup key, and is
73+ * transposed to the value of the corresponding resource bundle entry. Once transposition has occurred, after rule
74+ * evaluation, subsequent references to the "path" {@link Parameter} will return the value from the
75+ * {@link ResourceBundle} entry.
76+ * <p>
77+ * When this example is applied to a URL of: "/de/bibliotek", assuming a bundle called "org.example.Paths_de" exists
78+ * and contains the an entry "bibliotek=library", the rule will forward to the new URL: "/library", because the value
79+ * of "path" has been transposed by {@link LocaleTransposition}.
80+ *
81+ *
82+ * @param bundleName Fully qualified name of the {@link ResourceBundle}
83+ * @param localeParam The name of the {@link Parameter} that contains the {@link Locale} code.
84+ *
85+ * @return new instance of LocaleBinding
86+ */
87+ public static LocaleTransposition bundle (final String bundleName , final String localeParam )
88+ {
89+ return new LocaleTransposition (localeParam , bundleName );
90+ }
91+
92+ @ Override
93+ public String transpose (Rewrite event , EvaluationContext context , String value )
94+ {
95+ String transposedValue = null ;
96+
97+ // FIXME this is currently failing due to the absence of the languageParam parameter in the context
98+ // Retrieve the value of lang from the context
99+ String targetLang = (String ) Parameters .retrieve (context , this .languageParam );
100+ if (value != null )
101+ {
102+ if (!bundleMap .containsKey (targetLang ))
103+ {
104+ Locale locale = new Locale (targetLang );
105+ try
106+ {
107+ ResourceBundle loadedBundle = ResourceBundle .getBundle (bundleName , locale );
108+ bundleMap .put (targetLang , loadedBundle );
109+ }
110+ catch (MissingResourceException e )
111+ {
112+ throw new RewriteException ("Error occurred during Locale transposition of parameter value [" + value
113+ + "] via bundle [" + bundleName + "_" + targetLang + "]" , e );
114+ }
115+ }
116+
117+ if (bundleMap .containsKey (targetLang )) {
118+ try
119+ {
120+ // can we received more than one path section? e.g./search/service/
121+ transposedValue = bundleMap .get (targetLang ).getString (value );
122+ }
123+ catch (MissingResourceException mrEx )
124+ {
125+ // if not found, do not translate and keep original value
126+ transposedValue = value ;
127+ }
128+ }
129+ else
130+ {
131+ // if language is not defined, do not translate and keep original value
132+ transposedValue = value ;
133+ }
134+ }
135+ return transposedValue ;
136+ }
137+ }
0 commit comments