View Javadoc

1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   //   
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //   
8   // * Redistributions of source code must retain the above copyright notice,  
9   //   this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //   this list of conditions and the following disclaimer in the documentation  
12  //   and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //   nor the names of its contributors may be used to endorse or promote products  
15  //   derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.pico;
30  
31  import org.nanocontainer.reflection.Converter;
32  import org.nanocontainer.reflection.InvalidConversionException;
33  import org.nanocontainer.reflection.StringToObjectConverter;
34  
35  /***
36   * Converts strings to a number of predefined Java classes.
37   *
38   * <p>Created on Jan 8, 2004</p>
39   * @author <a href="Rafal.Krzewski">rafal@caltha.pl</a>
40   * @version $Id: LedgeStringToObjectConverter.java,v 1.5 2005/01/21 06:30:39 rafal Exp $
41   */
42  public class LedgeStringToObjectConverter extends StringToObjectConverter
43  {
44      /***
45       * Creates a new converter instance.
46       */
47      public LedgeStringToObjectConverter()
48      {
49          super();
50          register(Integer.TYPE, 
51              new Converter() 
52              {
53                  public Object convert(String in)
54                  {
55                      return in == null ? new Integer(0) : Integer.valueOf(in);
56                  }    
57              }
58          );
59          register(Long.TYPE, 
60              new Converter() 
61              {
62                  public Object convert(String in)
63                  {
64                      return in == null ? new Long(0) : Long.valueOf(in);
65                  }    
66              }
67          );
68          register(Boolean.TYPE,
69              new Converter()
70              {
71                  public Object convert(String in)
72                  {
73                      if (in == null || in.length() == 0) {
74                          return Boolean.FALSE;
75                      }
76                      char c = in.toLowerCase().charAt(0);
77                      return c == '1' || c == 'y' || c == 't' ? Boolean.TRUE : Boolean.FALSE;
78                  }
79              });
80          register(Class.class,
81              new Converter()
82              {
83                  public Object convert(String in)
84                  {
85                      try
86                      {
87                          return Class.forName(in);
88                      }
89                      catch(ClassNotFoundException e)
90                      {
91                          throw new InvalidConversionException("class "+in+" not found");
92                      }
93                  }
94              }
95          );
96      }
97  }