View Javadoc

1   package net.sourceforge.argval.impl;
2   
3   
4   import java.util.HashMap;
5   import java.util.Iterator;
6   import java.util.Map;
7   import java.util.Properties;
8   
9   import net.sourceforge.argval.ConfigurationManager;
10  import net.sourceforge.argval.utils.PropertiesUtil;
11  import net.sourceforge.argval.utils.impl.PropertiesUtilImpl;
12  
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  
17  /**
18   * This ConfigurationManager contains helper methods for the class {@link ArgumentValidationImpl}.
19   * 
20   * @author T. Verhagen
21   */
22  public class PropertiesConfigurationManager implements ConfigurationManager {
23      /** The logging instance. */
24      private transient static Logger logger = LoggerFactory.getLogger(PropertiesConfigurationManager.class);
25      
26      private PropertiesUtil propertiesUtil = null;
27      
28      public PropertiesConfigurationManager() {
29          propertiesUtil = new PropertiesUtilImpl();
30      }
31  
32      
33      /* (non-Javadoc)
34       * @see net.sourceforge.argval.impl.ConfigurationManager#createPatternProperties(java.util.Map)
35       */
36      public Properties createPatternProperties(Map<String, String> patternMap) { // <String, >
37          if (patternMap == null || patternMap.size() == 0) {
38              throw new IllegalArgumentException("Argument 'patternMap' should not be empty.");
39          }
40          Properties properties = new Properties();
41          
42          StringBuffer strBuf = new StringBuffer();
43          for (Iterator<String> iter = patternMap.keySet().iterator(); iter.hasNext(); ) {
44              String patternName = (String) iter.next();
45              strBuf.append(patternName).append(" ");
46              properties.put(
47                      propertiesUtil.createKey(ConfigurationManager.PROP_ARG_VAL, patternName, ConfigurationManager.PROP_POSTFIX_REG_EXP), 
48                      patternMap.get(patternName));
49          }
50          
51          // TODO [2008.09.12 tv] Is this still needed, to have patterns inserted?
52          // Could we use the new 'list' inside properties structure?
53          properties.put(ConfigurationManager.PROP_ARG_VAL_PATTERNS, strBuf);
54          logger.info("Create properties: " + propertiesUtil.toString(properties));
55          return properties;
56      }
57  
58      
59      /* (non-Javadoc)
60       * @see net.sourceforge.argval.impl.ConfigurationManager#createPatternMapOldSchool(java.util.Properties)
61       */
62      public Map<String, String> createPatternMapOldSchool(Properties properties) {
63          Map<String, String> patternMap = new HashMap<String, String>();
64          for (Iterator<String> iter = propertiesUtil.getPropertyAsSet(properties, ConfigurationManager.PROP_ARG_VAL_PATTERNS).iterator(); iter.hasNext(); ) {
65              String patternName = (String)iter.next();
66              
67              String patternKey = propertiesUtil.createKey(ConfigurationManager.PROP_ARG_VAL, patternName, ConfigurationManager.PROP_POSTFIX_REG_EXP);
68              if (properties.getProperty(patternKey) == null) {
69                  logger.warn("No regular expression was found for the pattern name '" + patternName + "'.");
70                  continue;
71              }
72              String regExpStr = properties.getProperty(patternKey);
73              logger.debug("found reg exp: " + regExpStr);
74              patternMap.put(patternName, regExpStr);
75              logger.debug("Added pattern '" + patternName + "' with reg exp '" + regExpStr + "' to the patternMap.");
76          }
77          
78          return patternMap;
79      }
80  
81      
82      /* (non-Javadoc)
83       * @see net.sourceforge.argval.impl.ConfigurationManager#createPatternMap(java.util.Properties)
84       */
85      public Map<String, String> createPatternMap(Properties properties) {
86          Map<String, String> patternMap = new HashMap<String, String>();
87          Map<String, Properties> patternPropertiesMap = propertiesUtil.getPropertiesMap(properties, PROP_ARG_VAL_PATTERN);
88          for (Iterator<String> iter = patternPropertiesMap.keySet().iterator(); iter.hasNext(); ) {
89              String patternName = (String)iter.next();
90              
91              String patternKey = ConfigurationManager.PROP_POSTFIX_REG_EXP;
92              
93              if (((Properties)patternPropertiesMap.get(patternName)).getProperty(patternKey) == null) {
94                  logger.warn("No regular expression was found for the pattern name '" + patternName + "'.");
95                  continue;
96              }
97              String regExpStr = ((Properties)patternPropertiesMap.get(patternName)).getProperty(patternKey);
98              logger.debug("found reg exp: " + regExpStr);
99              patternMap.put(patternName, regExpStr);
100             logger.debug("Added pattern '" + patternName + "' with reg exp '" + regExpStr + "' to the patternMap.");
101         }
102         
103         return patternMap;
104     }
105     
106     
107     /* (non-Javadoc)
108      * @see net.sourceforge.argval.impl.ConfigurationManager#createDateFormatMap(java.util.Properties)
109      */
110     public Map<String, String> createDateFormatMap(Properties properties) {
111         Map<String, String> dateFormatMap = new HashMap<String, String>();
112         Map<String, Properties> dateFormatPropertiesMap = propertiesUtil.getPropertiesMap(properties, PROP_ARG_VAL_DATE_FORMAT);
113         for (Iterator<String> iter = dateFormatPropertiesMap.keySet().iterator(); iter.hasNext(); ) {
114             String dateFormatName = (String)iter.next();
115             
116             String patternKey = ConfigurationManager.PROP_POSTFIX_PATTERN;
117             if (((Properties)dateFormatPropertiesMap.get(dateFormatName)).getProperty(patternKey) == null) {
118                 logger.warn("No simple date format was found for the date format name '" + dateFormatName + "'.");
119                 continue;
120             }
121             String datePattern = ((Properties)dateFormatPropertiesMap.get(dateFormatName)).getProperty(patternKey); //properties.getProperty(patternKey);
122             logger.debug("found date pattern: " + datePattern);
123             dateFormatMap.put(dateFormatName, datePattern);
124             logger.debug("Added date format name '" + dateFormatName + "' with date pattern '" + datePattern + "' to the dateFormatMap.");
125         }
126         
127         return dateFormatMap;
128     }
129 
130 }