View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.resteasy.hypermedia.renderer.i18n;
17  
18  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.ACCESS_KEY;
19  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.CONTEXT_MENU;
20  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.CSS_CLASS;
21  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.DIR;
22  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.HELP;
23  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.HIDDEN;
24  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.ID;
25  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.LABEL;
26  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.MEDIA;
27  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.SHORT_LABEL;
28  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.TAB_INDEX;
29  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.TARGET;
30  import static de.smartics.resteasy.hypermedia.renderer.i18n.LinkMetadataKeys.TITLE;
31  
32  import java.util.Locale;
33  import java.util.MissingResourceException;
34  import java.util.ResourceBundle;
35  
36  import javax.enterprise.context.ApplicationScoped;
37  
38  import de.smartics.resteasy.hypermedia.renderer.I18nProvider;
39  import de.smartics.resteasy.hypermedia.renderer.LinkMetadata;
40  import de.smartics.resteasy.hypermedia.renderer.LocalizationProvider;
41  
42  /**
43   * A provider based on resource bundles.
44   */
45  @ApplicationScoped
46  public class ResourceBundleI18nProvider implements I18nProvider
47  {
48    // ********************************* Fields *********************************
49  
50    // --- constants ------------------------------------------------------------
51  
52    /**
53     * The key the provider looks up the location to search for link resource
54     * information.
55     * <p>
56     * The value of this constant is {@value}.
57     * </p>
58     */
59    public static final String SYSTEM_KEY = "de.smartics.hypermedia.link";
60  
61    /**
62     * The default location to search for link resource information.
63     * <p>
64     * The value of this constant is {@value}.
65     * </p>
66     */
67    public static final String DEFAULT_LOCATION = "de/smartics/hypermedia/link";
68  
69    // --- members --------------------------------------------------------------
70  
71    // ****************************** Initializer *******************************
72  
73    // ****************************** Constructors ******************************
74  
75    // ****************************** Inner Classes *****************************
76  
77    /**
78     * A provider of localized information for links based on resource bundles.
79     */
80    private static final class ResourceBundleLocalizationProvider implements
81        LocalizationProvider
82    {
83      // ******************************** Fields ********************************
84  
85      // --- constants ----------------------------------------------------------
86  
87      // --- members ------------------------------------------------------------
88  
89      /**
90       * The locale the provider provides resources for.
91       */
92      private final Locale locale;
93  
94      // ***************************** Initializer ******************************
95  
96      // ***************************** Constructors *****************************
97  
98      /**
99       * Default constructor.
100      *
101      * @param locale the locale the provider provides resources for.
102      */
103     private ResourceBundleLocalizationProvider(final Locale locale)
104     {
105       this.locale = locale;
106     }
107 
108     // ***************************** Inner Classes ****************************
109 
110     // ******************************** Methods *******************************
111 
112     // --- init ---------------------------------------------------------------
113 
114     // --- get&set ------------------------------------------------------------
115 
116     /**
117      * Returns the locale the provider provides resources for.
118      *
119      * @return the locale the provider provides resources for.
120      */
121     @Override
122     public Locale getLocale()
123     {
124       return locale;
125     }
126 
127     // --- business -----------------------------------------------------------
128 
129     @Override
130     public LinkMetadata getLinkMetadata(final String id)
131     {
132       final BundleAccessor accessor = new BundleAccessor(locale, id);
133       final LinkMetadata metadata = accessor.getLinkMetadata();
134       return metadata;
135     }
136 
137     // --- object basics ------------------------------------------------------
138 
139   }
140 
141   /**
142    * Helper to access the resources within a resource bundle.
143    */
144   private static final class BundleAccessor
145   {
146     // ******************************** Fields ********************************
147 
148     // --- constants ----------------------------------------------------------
149 
150     // --- members ------------------------------------------------------------
151 
152     /**
153      * The selected bundle.
154      */
155     private final ResourceBundle bundle;
156 
157     // ***************************** Initializer ******************************
158 
159     // ***************************** Constructors *****************************
160 
161     /**
162      * Default constructor.
163      *
164      * @param locale the locale the provider provides resources for.
165      * @param id the identifier of the link to fetch metadata.
166      */
167     private BundleAccessor(final Locale locale, final String id)
168     {
169       final String baseName = calcBaseName(id);
170       this.bundle = ResourceBundle.getBundle(baseName, locale);
171     }
172 
173     // ***************************** Inner Classes ****************************
174 
175     // ******************************** Methods *******************************
176 
177     // --- init ---------------------------------------------------------------
178 
179     private static String calcBaseName(final String id)
180     {
181       final String prefix = System.getProperty(SYSTEM_KEY, DEFAULT_LOCATION);
182       final String baseName = prefix + '/' + id;
183       return baseName;
184     }
185 
186     // --- get&set ------------------------------------------------------------
187 
188     // --- business -----------------------------------------------------------
189 
190     private LinkMetadata getLinkMetadata()
191     {
192       final LinkMetadata metadata = new LinkMetadata();
193 
194       metadata.setAccessKey(getValue(ACCESS_KEY));
195       metadata.setContextMenu(getValue(CONTEXT_MENU));
196       metadata.setCssClass(getValue(CSS_CLASS));
197       metadata.setDir(getValue(DIR));
198       metadata.setHelp(getValue(HELP));
199       metadata.setHidden(getValueAsBoolean(HIDDEN));
200       metadata.setId(getValue(ID));
201       metadata.setLabel(getValue(LABEL));
202       metadata.setMedia(getValue(MEDIA));
203       metadata.setShortLabel(getValue(SHORT_LABEL));
204       metadata.setTabIndex(getValueAsInt(TAB_INDEX));
205       metadata.setTarget(getValue(TARGET));
206       metadata.setTitle(getValue(TITLE));
207 
208       return metadata;
209     }
210 
211     private Integer getValueAsInt(final String key)
212     {
213       try
214       {
215         final String string = bundle.getString(key);
216         final Integer value = Integer.parseInt(string);
217         return value;
218       }
219       catch (final MissingResourceException e)
220       {
221         return null;
222       }
223     }
224 
225     private Boolean getValueAsBoolean(final String key)
226     {
227       try
228       {
229         final String string = bundle.getString(key);
230         final boolean value = Boolean.parseBoolean(string);
231         return value;
232       }
233       catch (final MissingResourceException e)
234       {
235         return null;
236       }
237     }
238 
239     private String getValue(final String key)
240     {
241       try
242       {
243         final String value = bundle.getString(key);
244         return value;
245       }
246       catch (final MissingResourceException e)
247       {
248         return null;
249       }
250     }
251 
252     // --- object basics ------------------------------------------------------
253   }
254 
255   // ********************************* Methods ********************************
256 
257   // --- init -----------------------------------------------------------------
258 
259   // --- get&set --------------------------------------------------------------
260 
261   @Override
262   public LocalizationProvider getProvider(final Locale locale)
263     throws NullPointerException
264   {
265     final ResourceBundleLocalizationProvider provider =
266         new ResourceBundleLocalizationProvider(locale);
267     return provider;
268   }
269 
270   // --- business -------------------------------------------------------------
271 
272   // --- object basics --------------------------------------------------------
273 
274 }