Coverage Report - org.apache.tapestry.util.text.LocalizedProperties
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalizedProperties
0%
0/23
0%
0/4
1.444
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.util.text;
 16  
 
 17  
 import java.io.IOException;
 18  
 import java.io.InputStream;
 19  
 import java.io.Reader;
 20  
 import java.util.HashMap;
 21  
 import java.util.Map;
 22  
 
 23  
 /**
 24  
  * A version of java.util.Properties that can read the properties from files
 25  
  * using an encoding other than ISO-8859-1. All non-latin characters are read
 26  
  * correctly using the given encoding and no longer need to be quoted using
 27  
  * native2ascii. In addition, the properties may be stored in an arbitrary map,
 28  
  * rather than only in Properties. For example, using LinkedHashMap will
 29  
  * preserve the order of the properties as defined in the file.
 30  
  * 
 31  
  * @author mb
 32  
  * @since 4.0
 33  
  */
 34  
 public class LocalizedProperties
 35  
 {
 36  
 
 37  
     private Map _propertyMap;
 38  
 
 39  
     /**
 40  
      * Create a new object with an empty property storage.
 41  
      */
 42  
     public LocalizedProperties()
 43  
     {
 44  0
         this(new HashMap());
 45  0
     }
 46  
 
 47  
     /**
 48  
      * Use the provided argument as the storage location for the properties
 49  
      * managed by this object. This allows different types of Map
 50  
      * implementations to be used, such as a LinkedHashMap to preserve the order
 51  
      * of the keys, for example. The provided map may contain the default
 52  
      * property values as well.
 53  
      * 
 54  
      * @param propertyMap
 55  
      *            the map where properties are to be stored
 56  
      */
 57  
     public LocalizedProperties(Map propertyMap)
 58  0
     {
 59  0
         _propertyMap = propertyMap;
 60  0
     }
 61  
 
 62  
     /**
 63  
      * Returns the property value corresponding the provided key. If there is no
 64  
      * such property, or the value in the provided map is not of type String,
 65  
      * null is returned.
 66  
      * 
 67  
      * @param key
 68  
      *            the property key
 69  
      * @return the value of the property, or null if there is no such property
 70  
      */
 71  
     public String getProperty(String key)
 72  
     {
 73  0
         Object value = _propertyMap.get(key);
 74  0
         if (value instanceof String) return (String) value;
 75  0
         return null;
 76  
     }
 77  
 
 78  
     /**
 79  
      * Returns the property value corresponding to the provided key, or the
 80  
      * provided default value if no such property exists.
 81  
      * 
 82  
      * @param key
 83  
      *            the property key
 84  
      * @param defaultValue
 85  
      *            the default value of the property
 86  
      * @return the value of the property, or the default value if there is no
 87  
      *         such property
 88  
      */
 89  
     public String getProperty(String key, String defaultValue)
 90  
     {
 91  0
         String value = getProperty(key);
 92  0
         if (value != null) return value;
 93  0
         return defaultValue;
 94  
     }
 95  
 
 96  
     /**
 97  
      * Stores a property value.
 98  
      * 
 99  
      * @param key
 100  
      *            the property key
 101  
      * @param value
 102  
      *            the property value
 103  
      */
 104  
     public void setProperty(String key, String value)
 105  
     {
 106  0
         _propertyMap.put(key, value);
 107  0
     }
 108  
 
 109  
     /**
 110  
      * Returns the map containing all properties. The map can be used to
 111  
      * enumerate the properties or their keys.
 112  
      * 
 113  
      * @return a map containing the properties
 114  
      */
 115  
     public Map getPropertyMap()
 116  
     {
 117  0
         return _propertyMap;
 118  
     }
 119  
 
 120  
     /**
 121  
      * Loads the properties from the given stream using the default character
 122  
      * encoding. This method operates in the same way as the equivalent method
 123  
      * in {@link java.util.Properties}, but it also handles non-ascii symbols.
 124  
      * 
 125  
      * @param ins
 126  
      *            the stream to load the properties from
 127  
      * @throws IOException
 128  
      */
 129  
     public void load(InputStream ins)
 130  
         throws IOException
 131  
     {
 132  0
         LocalizedPropertiesLoader loader = new LocalizedPropertiesLoader(ins);
 133  0
         loader.load(_propertyMap);
 134  0
     }
 135  
 
 136  
     /**
 137  
      * Loads the properties from the given stream using the provided character
 138  
      * encoding. This method operates in the same way as the equivalent method
 139  
      * in {@link java.util.Properties}, but it also handles non-ascii symbols.
 140  
      * 
 141  
      * @param ins
 142  
      *            the stream to load the properties from
 143  
      * @param encoding
 144  
      *            the encoding the use when parsing the stream
 145  
      * @throws IOException
 146  
      */
 147  
     public void load(InputStream ins, String encoding)
 148  
         throws IOException
 149  
     {
 150  0
         LocalizedPropertiesLoader loader = new LocalizedPropertiesLoader(ins,
 151  
                 encoding);
 152  0
         loader.load(_propertyMap);
 153  0
     }
 154  
 
 155  
     /**
 156  
      * Loads the properties from the given reader. This method operates in the
 157  
      * same way as the equivalent method in {@link java.util.Properties}, but
 158  
      * it also handles non-ascii symbols.
 159  
      * 
 160  
      * @param reader
 161  
      *            the reader to load the properties from
 162  
      * @throws IOException
 163  
      */
 164  
     public void load(Reader reader)
 165  
         throws IOException
 166  
     {
 167  0
         LocalizedPropertiesLoader loader = new LocalizedPropertiesLoader(reader);
 168  0
         loader.load(_propertyMap);
 169  0
     }
 170  
 }