Coverage Report - org.webmacro.util.SparseProperties
 
Classes in this File Line Coverage Branch Coverage Complexity
SparseProperties
15%
5/32
20%
2/10
1.556
 
 1  
 /*
 2  
 * Copyright Open Doors Software. 1996-2002.
 3  
 *
 4  
 * Software is provided according to the MPL license.
 5  
 * Open Doors Software provides this
 6  
 * software on an as-is basis and make no representation as
 7  
 * to fitness for a specific purpose.
 8  
 *
 9  
 * Direct all questions and comments to support@opendoors.com
 10  
 */
 11  
 package org.webmacro.util;
 12  
 
 13  
 import java.util.Properties;
 14  
 import java.util.Enumeration;
 15  
 
 16  
 import javax.servlet.ServletRequest;
 17  
 
 18  
 /**
 19  
  * Implement a behavior allowing for undefined properties to return
 20  
  * a useful default value such as "" for a string, "0.00" for 
 21  
  * numeric properties, and so forth.
 22  
  *
 23  
  * @author Lane Sharman
 24  
  */
 25  
 public class SparseProperties extends Properties
 26  
 {
 27  
 
 28  
     /**
 29  
          * 
 30  
          */
 31  
         private static final long serialVersionUID = 1L;
 32  
         /** The global default value for all requests is "" */
 33  30
     protected Object globalDefault = new String("");
 34  
 
 35  
     public SparseProperties ()
 36  
     {
 37  30
         super();
 38  30
     }
 39  
 
 40  
     public SparseProperties (Properties defaults)
 41  
     {
 42  0
         super(defaults);
 43  0
     }
 44  
 
 45  
     public SparseProperties (Object globalDefault)
 46  
     {
 47  0
         super();
 48  0
         this.globalDefault = globalDefault;
 49  0
     }
 50  
 
 51  
     public SparseProperties (Properties defaults, Object globalDefault)
 52  
     {
 53  0
         super(defaults);
 54  0
         this.globalDefault = globalDefault;
 55  0
     }
 56  
     
 57  
     public static SparseProperties getTemplate()
 58  
     {
 59  0
       return getTemplate(null);
 60  
     }
 61  
     
 62  
     public static SparseProperties getTemplate(Properties template)
 63  
     {
 64  0
       SparseProperties p = null;
 65  0
       if (template == null)
 66  
       {
 67  0
         p = new SparseProperties();
 68  
       }
 69  
       else
 70  
       {
 71  0
         p = new SparseProperties(template);
 72  
       }
 73  0
       Enumeration e = p.keys();
 74  0
       while (e.hasMoreElements())
 75  
       {
 76  0
         p.put(e.nextElement(), null);
 77  
       }
 78  0
       return p;
 79  
     }
 80  
 
 81  
     /**
 82  
      * Gets the object but returns the default value if not present.
 83  
      */
 84  
     public Object get (Object key)
 85  
     {
 86  153
         Object o = super.get(key);
 87  153
         return (o == null) ? globalDefault : o;
 88  
     }
 89  
 
 90  
     /**
 91  
      * Gets the object but returns the default value if not present.
 92  
      */
 93  
     public Object get (Object key, Object defaultValue)
 94  
     {
 95  0
         Object o = super.get(key);
 96  0
         return (o == null) ? defaultValue : o;
 97  
     }
 98  
     
 99  
     /**
 100  
      * Adds all the Request attributes to this property object.
 101  
      */
 102  
     public void addRequestAttributes(ServletRequest request)
 103  
     {
 104  0
       System.out.println("Setting Request Properties:");
 105  0
       Enumeration e = request.getParameterNames();
 106  0
       while (e.hasMoreElements())
 107  
       {
 108  0
         String k = (String) e.nextElement();
 109  0
         this.setProperty(k, request.getParameter(k));
 110  0
         System.out.println(k + ":" + request.getParameter(k));
 111  0
       }
 112  0
     }
 113  
 }
 114