View Javadoc

1   /*
2    * Copyright 2010-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.testdoc.report.resource;
17  
18  import java.net.URL;
19  import java.util.Locale;
20  import java.util.MissingResourceException;
21  import java.util.ResourceBundle;
22  
23  import org.apache.commons.lang.NullArgumentException;
24  import org.apache.commons.lang.StringUtils;
25  
26  /**
27   * Locates category resources on the class path.
28   *
29   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
30   * @version $Revision:591 $
31   */
32  public abstract class AbstractClassPathCategoryResourceProvider implements
33      CategoryResourceProvider
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    /**
40     * The name of the package that contains the resources provided by this
41     * instance. This package contains the resource bundle file
42     * <code>CategoryResourceBundle</code> and the optional icons.
43     */
44    private final String resourceLocationName;
45  
46    /**
47     * The bundle to load from.
48     */
49    private final String baseBundleName;
50  
51    // --- members --------------------------------------------------------------
52  
53    // ****************************** Initializer *******************************
54  
55    // ****************************** Constructors ******************************
56  
57    /**
58     * Default constructor.
59     *
60     * @param resourcePackageName the name of the package that contains the
61     *          resources provided by this instance. This package contains the
62     *          resource bundle file <code>CategoryResourceBundle</code> and the
63     *          optional icons.
64     */
65    protected AbstractClassPathCategoryResourceProvider(
66        final String resourcePackageName)
67    {
68      this.resourceLocationName = resourcePackageName.replace('.', '/');
69      this.baseBundleName = resourcePackageName + '.' + "CategoryResourceBundle";
70    }
71  
72    // ****************************** Inner Classes *****************************
73  
74    // ********************************* Methods ********************************
75  
76    // --- init -----------------------------------------------------------------
77  
78    // --- get&set --------------------------------------------------------------
79  
80    // --- business -------------------------------------------------------------
81  
82    /**
83     * {@inheritDoc}
84     *
85     * @see CategoryResourceProvider#getResource(Locale, Class)
86     */
87    @Override
88    public CategoryResource getResource(final Locale locale,
89        final Class<?> categoryType) throws NullArgumentException,
90      IconNotFoundException
91    {
92      checkArguments(locale, categoryType);
93  
94      final ResourceBundle bundle =
95          ResourceBundle.getBundle(baseBundleName, locale);
96      final String key = categoryType.getName();
97      try
98      {
99        final String label = bundle.getString(key);
100       final URL iconLocation = calcIconLocation(bundle, key);
101       return new CategoryResource(label, iconLocation);
102     }
103     catch (final MissingResourceException e)
104     {
105       return null;
106     }
107   }
108 
109   private static void checkArguments(final Locale locale,
110       final Class<?> categoryType)
111   {
112     if (locale == null)
113     {
114       throw new NullArgumentException("locale");
115     }
116     if (categoryType == null)
117     {
118       throw new NullArgumentException("categoryType");
119     }
120   }
121 
122   // --- object basics --------------------------------------------------------
123 
124   private URL calcIconLocation(final ResourceBundle bundle, final String key)
125     throws IconNotFoundException
126   {
127     try
128     {
129       final String resourceId = bundle.getString(key + ".icon");
130       if (StringUtils.isNotBlank(resourceId))
131       {
132         final String resourceLocation = resourceLocationName + '/' + resourceId;
133         final ClassLoader loader =
134             Thread.currentThread().getContextClassLoader();
135         final URL url = loader.getResource(resourceLocation);
136         if(url == null)
137         {
138           throw new IconNotFoundException(resourceId);
139         }
140         return url;
141       }
142     }
143     catch (final MissingResourceException e)
144     {
145       // returns null in the end
146     }
147     return null;
148   }
149 }