Coverage details for org.chwf.converter.Converter

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.converter;
34  
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.Map;
38  
39 import org.chwf.config.Config;
40 import org.chwf.config.ConfigFactory;
41  
42 /**
43  * Converter class. It is the superclass for more specific converters.
44  *
45  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
46  */
472public abstract class Converter {
48  
49   /** Default converter. */
502  private static final DefaultConverter DEFAULT_CONVERTER =
51     new DefaultConverter();
52  
53   /** Key for converter config map. */
54   private static final String CONVERTER_CONFIG_MAP_KEY = "datatype";
55  
56   /** Map containing all predefined converters. */
571  private static final Map CONVERTER_MAP = initConvertersMap();
58  
59   /** The converter's datatype. */
60   private String type;
61  
62   /**
63    * Initialize converter map from configuration.
64    *
65    * @return The converter map.
66    * @throws ConversionConfigurationException If there is a problem with the
67    * converter configuration.
68    */
69   private static Map initConvertersMap()
70     throws ConversionConfigurationException {
711    Map converters = new HashMap();
721    addConverters(converters, ConfigFactory.getConfig(DefaultConverter.class));
731    addConverters(converters, ConfigFactory.getConfig(Converter.class));
741    return converters;
75   }
76  
77   /**
78    * Add converters to the given map from the configuration.
79    *
80    * @param converters The converter map.
81    * @param config The configuration to retrieve converter information.
82    */
83   private static void addConverters(Map converters, Config config) {
842    Map converterConfig = config.getMap(CONVERTER_CONFIG_MAP_KEY);
852    Iterator i = converterConfig.keySet().iterator();
86     try {
8733      while (i.hasNext()) {
8829        String type = i.next().toString();
8929        String converterName = converterConfig.get(type).toString();
9029        Converter converter =
91           (Converter) Class.forName(converterName).newInstance();
9229        converter.type = type;
9329        converters.put(type, converter);
94       }
952    } catch (Exception ex) {
960      throw new ConversionConfigurationException(ex);
97     }
982  }
99  
100   /**
101    * Factory method that gets the converter for the given data type. This
102    * methods uses one of the following:<p>
103    *
104    * <ul>
105    * <li>The converter class specified in <b>Converter.properties</b>.</li>
106    * <li>A {@link org.chwf.converter.DefaultConverter}.</li>
107    * </ul><p>
108    *
109    * @param type The class being converted.
110    * @return The converter for the class.
111    */
112   public static Converter getConverter(Class type) {
113140    return getConverter(type.getName());
114   }
115  
116   /**
117    * Factory method that gets the converter for the given data type. This
118    * methods uses one of the following:<p>
119    *
120    * <ul>
121    * <li>The converter class specified in <b>Converter.properties</b>.</li>
122    * <li>A {@link org.chwf.converter.DefaultConverter}.</li>
123    * </ul><p>
124    *
125    * @param type The datatype being converted.
126    * @return The converter for the datatype.
127    */
128   public static Converter getConverter(String type) {
129252    Converter converter = (Converter) CONVERTER_MAP.get(type);
130252    if (converter != null) {
131218      return converter;
132     } else {
13334      return DEFAULT_CONVERTER;
134     }
135   }
136  
137   /**
138    * Utility method that converts the object to a string using the converter
139    * for its class.<p>
140    *
141    * @param object The object.
142    * @return The string value of the object.
143    */
144   public static String convert(Object object) {
14536    if (object == null) {
1460      return null;
147     } else {
14836      return getConverter(object.getClass()).format(object);
149     }
150   }
151  
152   /** No-op constructor.<p> */
15330  protected Converter() {
15430  }
155  
156   /**
157    * Parse the string value to the conversion type. Supported only for simple
158    * datatypes.<p>
159    *
160    * @param value The value being parsed.
161    * @return The resulting object.
162    * @throws ConversionException If parsing fails.
163    */
164   public abstract Object parse(String value) throws ConversionException;
165  
166   /**
167    * Format the conversion type as a string.<p>
168    *
169    * @param value The value being format.
170    * @return The resulting string.
171    */
172   public abstract String format(Object value);
173  
174   /**
175    * The converter's datatype, or <code>null</code> if the type is unknown.<p>
176    *
177    * @return The converter's datatype.
178    */
179   public String getType() {
18033    return this.type;
181   }
182  
183   /**
184    * Returns true if the converted type is simple. If the type is not simple,
185    * then the <code>parse()</code> method will not function. Any type that
186    * uses the <code>DefaultConverter</code> is by definition not simple.<p>
187    *
188    * @return true if the converted type is simple.
189    */
190   public boolean isSimpleType() {
19193    return true;
192   }
193 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.