Coverage Report - us.paulevans.basicxslt.UserPreferences
 
Classes in this File Line Coverage Branch Coverage Complexity
UserPreferences
84%
47/56
100%
10/10
0
 
 1  
 /*
 2  
         Copyright 2006 Paul Evans 
 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 us.paulevans.basicxslt;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.io.OutputStream;
 20  
 import java.util.Arrays;
 21  
 import java.util.Enumeration;
 22  
 import java.util.HashMap;
 23  
 import java.util.Map;
 24  
 import java.util.Properties;
 25  
 
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 import org.apache.commons.lang.exception.ExceptionUtils;
 28  
 import org.apache.log4j.Logger;
 29  
 
 30  
 /**
 31  
  * Mechanism to store and retreive the user preferences for the tool
 32  
  * @author pevans
 33  
  *
 34  
  */
 35  12
 public class UserPreferences extends Properties {
 36  
         
 37  
     // logger object...
 38  1
     private static final Logger logger = Logger.getLogger(
 39  
                     UserPreferences.class);
 40  
 
 41  
         // instance members...
 42  
         private String currentConfiguration;
 43  
         
 44  
         /**
 45  
          * Returns the value for property aName; if the value is empty/null, then
 46  
          * aDefaultValue is returned.
 47  
          * @param aName
 48  
          * @param aDefaultValue
 49  
          */
 50  
         public String getProperty(String aName, String aDefaultValue) {
 51  
                 
 52  
                 String value;
 53  
                 
 54  1
                 value = getProperty(aName);
 55  1
                 if (StringUtils.isBlank(value)) {
 56  1
                         value = aDefaultValue;
 57  
                 }
 58  1
                 return value;
 59  
         }
 60  
         
 61  
         /**
 62  
          * Returns the value for property aName
 63  
          * @param aName
 64  
          */
 65  
         public String getProperty(String aName) {
 66  4
                 return super.getProperty(currentConfiguration + "." + aName);
 67  
         }
 68  
         
 69  
         /**
 70  
          * Sets the property
 71  
          * @param aName
 72  
          * @param aValue
 73  
          */
 74  
         public Object setProperty(String aName, String aValue) {
 75  6
                 if (aName == null) {
 76  2
                         throw new NullPointerException();
 77  
                 }
 78  4
                 return super.setProperty(currentConfiguration + "." + aName, aValue);
 79  
         }
 80  
         
 81  
         /**
 82  
          * Returns the value for property aName; the "currentConfiguration" prefix
 83  
          * is not applied to the property-name.
 84  
          * @param aName
 85  
          * @return
 86  
          */
 87  
         public String getPropertyNoPrefix(String aName) {
 88  52
                 return super.getProperty(aName);
 89  
         }
 90  
         
 91  
         /**
 92  
          * Sets the property - this method will not prefix aName with the current
 93  
          * configuration name
 94  
          * @param aName
 95  
          * @param aValue
 96  
          * @return
 97  
          */
 98  
         public Object setPropertyNoPrefix(String aName, String aValue) {
 99  61
                 return super.setProperty(aName, aValue);
 100  
         }
 101  
         
 102  
         /**
 103  
          * Sets the configuration prefix string used in subsequent
 104  
          * property-retrieval attempts.
 105  
          * @param aConfiguration
 106  
          * @param aMakeAsDefault
 107  
          */
 108  
         public void setConfiguration(String aConfiguration, 
 109  
                         boolean aMakeAsDefault) {
 110  11
                 currentConfiguration = aConfiguration;
 111  11
                 if (aMakeAsDefault) {
 112  10
                         setPropertyNoPrefix(AppConstants.DEFAULT_CONFIGURATION_PROP, 
 113  
                                         currentConfiguration);
 114  
                 }                
 115  11
         }
 116  
         
 117  
         /**
 118  
          * Returns the configuration prefix string
 119  
          * @return
 120  
          */
 121  
         public String getConfiguration() {
 122  2
                 return currentConfiguration;
 123  
         }
 124  
         
 125  
         /**
 126  
          * Initializes this object setting the "current configuration" prefix
 127  
          * string and returns it.
 128  
          * @return
 129  
          */
 130  
         public String loadDefaultConfiguration() {
 131  2
                 currentConfiguration = getPropertyNoPrefix(
 132  
                                 AppConstants.DEFAULT_CONFIGURATION_PROP);
 133  2
                 if (StringUtils.isBlank(currentConfiguration)) {
 134  1
                         currentConfiguration = AppConstants.DEFAULT_CONFIGURATION;
 135  
                 }
 136  2
                 return currentConfiguration;
 137  
         }
 138  
 
 139  
         /**
 140  
          * Copies the current preferences using the configuration prefix string
 141  
          * aNewConfig
 142  
          * @param aNewConfig
 143  
          */
 144  
         public void copyCurrentPreferences(String aNewConfig) {
 145  
                 
 146  
                 String fullPropName, propNameSuffix;
 147  
                 Enumeration propertyNames;
 148  
                 
 149  1
                 propertyNames = propertyNames();
 150  134
                 while (propertyNames.hasMoreElements()) {
 151  133
                         fullPropName = (String)propertyNames.nextElement();        
 152  133
                         if (fullPropName.startsWith(currentConfiguration + ".")) {
 153  46
                                 propNameSuffix = fullPropName.substring(
 154  
                                                 fullPropName.indexOf(".")+1);
 155  46
                                 setPropertyNoPrefix(aNewConfig + "." + propNameSuffix, 
 156  
                                         getPropertyNoPrefix(fullPropName));        
 157  46
                         }
 158  
                 }
 159  1
         }
 160  
 
 161  
         /**
 162  
          * Clears all those properties in the current configuration that start
 163  
          * with "xsl_"
 164  
          *
 165  
          */
 166  
         public void clearDynamicPrefs() {
 167  
                 
 168  
                 Enumeration propertyNames;
 169  
                 String propertyName;
 170  
                 
 171  1
                 propertyNames = propertyNames();
 172  134
                 while(propertyNames.hasMoreElements()) {
 173  133
                         propertyName = (String)propertyNames.nextElement();
 174  133
                         if (propertyName.startsWith(currentConfiguration + ".xsl_") ||
 175  
                         propertyName.startsWith(
 176  
                                         currentConfiguration + ".xml_identity_transform")) {
 177  26
                                 remove(propertyName);                
 178  26
                         }
 179  
                 }
 180  1
         }
 181  
 
 182  
         /**
 183  
          * Persists userPrefs to its properties file on disk.
 184  
          */
 185  
         public void persistUserPrefs() throws IOException {
 186  
                 
 187  
                 OutputStream out;
 188  
                 
 189  0
                 out = null;
 190  
                 try {
 191  0
                     store(out = Utils.getUserPrefsOutputStream(), 
 192  
                             LabelStringFactory.getInstance().getString(
 193  
                                             LabelStringFactory.TOOL_USERPREFERENCES_TITLE));
 194  0
                     out.flush();
 195  0
                 } catch (IOException aException) {
 196  
                         // log and re-throw...
 197  0
                         logger.error(ExceptionUtils.getFullStackTrace(aException));
 198  0
                         throw aException;
 199  
                 } finally {
 200  0
                         Utils.closeQuietly(out);
 201  0
                 }
 202  0
         }
 203  
         
 204  
         /**
 205  
          * Returns all of the configurations that exist for the user
 206  
          * @return
 207  
          */
 208  
         public String[] getAllConfigurations() {
 209  
                 
 210  
                 Map<String,String> configurations;
 211  
                 Enumeration propertyNames;
 212  
                 String propertyName, configuration;
 213  
                 String configs[];
 214  
                 int index;
 215  
                 
 216  1
                 configurations = new HashMap<String,String>();
 217  1
                 propertyNames = propertyNames();
 218  134
                 while(propertyNames.hasMoreElements()) {
 219  133
                         propertyName = (String)propertyNames.nextElement();
 220  133
                         if ((!propertyName.startsWith(currentConfiguration)) &&
 221  
                                 (index = propertyName.indexOf(".")) != -1) {
 222  84
                                 configuration = propertyName.substring(0, index);
 223  84
                                 configurations.put(configuration, configuration); 
 224  84
                         }                                                        
 225  
                 }
 226  1
                 configs = (String[])configurations.values().toArray(
 227  
                                 new String[configurations.size()]);
 228  1
                 Arrays.sort(configs);
 229  1
                 return configs;                
 230  
         }
 231  
 }