View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.extensions.validator.core.initializer.configuration;
20  
21  import org.apache.myfaces.extensions.validator.internal.UsageInformation;
22  import org.apache.myfaces.extensions.validator.internal.UsageCategory;
23  
24  import java.util.List;
25  import java.util.ResourceBundle;
26  import java.util.Enumeration;
27  import java.util.ArrayList;
28  
29  /**
30   * Default implementation of the {@link StaticConfiguration} interface for property-file based configs.
31   *
32   * @since 1.x.1
33   */
34  @UsageInformation({UsageCategory.INTERNAL, UsageCategory.REUSE})
35  public class StaticResourceBundleConfiguration implements StaticConfiguration<String, String>
36  {
37      private String path;
38      private List<StaticConfigurationEntry<String, String>> mappings;
39  
40      public void setSourceOfMapping(String path)
41      {
42          this.path = path;
43          //force reload
44          mappings = null;
45      }
46  
47      public List<StaticConfigurationEntry<String, String>> getMapping()
48      {
49          if(mappings != null)
50          {
51              return mappings;
52          }
53  
54          mappings = new ArrayList<StaticConfigurationEntry<String, String>>();
55  
56          ResourceBundle mapping = ResourceBundle.getBundle(path);
57  
58          if (mapping == null)
59          {
60              //logging
61              return new ArrayList<StaticConfigurationEntry<String, String>>();
62          }
63  
64          Enumeration keys = mapping.getKeys();
65  
66          String metaDataKey;
67          String validationStrategyClassName;
68  
69          while (keys.hasMoreElements())
70          {
71              metaDataKey = (String) keys.nextElement();
72              validationStrategyClassName = mapping.getString(metaDataKey);
73  
74              addMapping(metaDataKey, validationStrategyClassName);
75          }
76          return mappings;
77      }
78  
79      private void addMapping(String metaDataKey, String validationStrategyClassName)
80      {
81          StaticConfigurationEntry<String, String> entry = new StaticConfigurationEntry<String, String>();
82          entry.setSource(metaDataKey);
83          entry.setTarget(validationStrategyClassName);
84          this.mappings.add(entry);
85      }
86  }