View Javadoc

1   /*
2    * $Id: LocalizedSourceContainer.java,v 1.1 2010/01/27 14:55:37 oeuillot Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.repository;
6   
7   import java.net.URL;
8   import java.util.ArrayList;
9   import java.util.List;
10  import java.util.Locale;
11  import java.util.Set;
12  
13  import javax.servlet.ServletConfig;
14  import javax.servlet.ServletException;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /**
20   * 
21   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
22   * @version $Revision: 1.1 $ $Date: 2010/01/27 14:55:37 $
23   */
24  public abstract class LocalizedSourceContainer extends SourceContainer {
25      private static final String REVISION = "$Revision: 1.1 $";
26  
27      private static final Log LOG = LogFactory
28              .getLog(LocalizedSourceContainer.class);
29  
30      private final String localizedSuffixes[];
31  
32      public LocalizedSourceContainer(ServletConfig config,
33              String repositoryType, Set modules, String charSet,
34              boolean canUseGzip, boolean canUseETag, boolean canUseHash,
35              String externalRepositoriesPropertyName, String repositoryVersion)
36              throws ServletException {
37          super(config, repositoryType, modules, charSet, canUseGzip, canUseETag,
38                  canUseHash, externalRepositoriesPropertyName, repositoryVersion);
39  
40          this.localizedSuffixes = getLocalizedSuffixes(config);
41      }
42  
43      protected URL getURL(String path) {
44          for (int i = 0; i < localizedSuffixes.length; i++) {
45              String p = path + localizedSuffixes[i];
46  
47              URL url = super.getURL(p);
48              if (url != null) {
49                  return url;
50              }
51          }
52  
53          return null;
54      }
55  
56      protected String[] getLocalizedSuffixes(ServletConfig config) {
57          Locale locale = Locale.getDefault();
58  
59          List l = new ArrayList();
60  
61          if (locale != null) {
62              String language = locale.getLanguage();
63              if (language != null && language.length() > 0) {
64                  String country = locale.getCountry();
65                  if (country != null && country.length() > 0) {
66                      String variant = locale.getVariant();
67                      if (variant != null && variant.length() > 0) {
68                          l.add("_" + language + "_" + country + "_" + variant);
69                      }
70  
71                      l.add("_" + language + "_" + country);
72                  }
73                  l.add("_" + language);
74              }
75          }
76  
77          l.add("");
78  
79          return (String[]) l.toArray(new String[l.size()]);
80      }
81  }