src/core/net/sf/basedb/util/extensions/xml/ValueConverter.java

Code
Comments
Other
Rev Date Author Line
4170 07 Mar 08 nicklas 1 /**
4479 05 Sep 08 jari 2   $Id$
4170 07 Mar 08 nicklas 3
4170 07 Mar 08 nicklas 4   Copyright (C) Authors contributing to this file.
4170 07 Mar 08 nicklas 5
4170 07 Mar 08 nicklas 6   This file is part of BASE - BioArray Software Environment.
4170 07 Mar 08 nicklas 7   Available at http://base.thep.lu.se/
4170 07 Mar 08 nicklas 8
4170 07 Mar 08 nicklas 9   BASE is free software; you can redistribute it and/or
4170 07 Mar 08 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
4170 07 Mar 08 nicklas 12   of the License, or (at your option) any later version.
4170 07 Mar 08 nicklas 13
4170 07 Mar 08 nicklas 14   BASE is distributed in the hope that it will be useful,
4170 07 Mar 08 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4170 07 Mar 08 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4170 07 Mar 08 nicklas 17   GNU General Public License for more details.
4170 07 Mar 08 nicklas 18
4170 07 Mar 08 nicklas 19   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4170 07 Mar 08 nicklas 21 */
4170 07 Mar 08 nicklas 22 package net.sf.basedb.util.extensions.xml;
4170 07 Mar 08 nicklas 23
4170 07 Mar 08 nicklas 24 import java.lang.reflect.Method;
4170 07 Mar 08 nicklas 25
4170 07 Mar 08 nicklas 26 /**
4170 07 Mar 08 nicklas 27   Interface for classes that wants to inspect/convert 
4170 07 Mar 08 nicklas 28   parameters from the XML file before they are passed 
4170 07 Mar 08 nicklas 29   to factory setter methods.
4170 07 Mar 08 nicklas 30   
4170 07 Mar 08 nicklas 31   <p>
4170 07 Mar 08 nicklas 32   Instances of this class are registered with an XML loader
4170 07 Mar 08 nicklas 33   with the {@link XmlLoader#addValueConverter(ValueConverter)} method. 
4170 07 Mar 08 nicklas 34   Before a factory setter method is called, all registered
4198 28 Mar 08 nicklas 35   converters will get the chance to {@link #convert(String, Method)} 
4170 07 Mar 08 nicklas 36   the parameter value from the XML file. The converters are
4170 07 Mar 08 nicklas 37   called in the order they are registered.
4170 07 Mar 08 nicklas 38   
4170 07 Mar 08 nicklas 39   <p>
4170 07 Mar 08 nicklas 40   A typical implementation of a converter is to check if the
4170 07 Mar 08 nicklas 41   setter method has been annotated with some specific annotation.
4170 07 Mar 08 nicklas 42   If, so the value is converted, otherwise it is returned without
4170 07 Mar 08 nicklas 43   modification.
4170 07 Mar 08 nicklas 44   
4170 07 Mar 08 nicklas 45   <pre class="code">
4170 07 Mar 08 nicklas 46 // Add a prefix to all strings.
4208 07 Apr 08 nicklas 47 &#64;Override
4170 07 Mar 08 nicklas 48 public String convert(String in, Method method) 
4170 07 Mar 08 nicklas 49 {
4170 07 Mar 08 nicklas 50    if (!method.isAnnotationPresent(AddPrefix.class)) return in;
4170 07 Mar 08 nicklas 51    return in == null ? PREFIX : PREFIX + in;
4170 07 Mar 08 nicklas 52 }
4170 07 Mar 08 nicklas 53 </pre>
4170 07 Mar 08 nicklas 54   
4170 07 Mar 08 nicklas 55   <p>
4170 07 Mar 08 nicklas 56   Note! Since each registered converter is invoked everytime a
4170 07 Mar 08 nicklas 57   factory setter method is about to be called, we recommend that
4170 07 Mar 08 nicklas 58   the {@link #convert(String, Method)} method is kept as quick
4170 07 Mar 08 nicklas 59   as possible.
4170 07 Mar 08 nicklas 60   
4170 07 Mar 08 nicklas 61   @author nicklas
4170 07 Mar 08 nicklas 62   @version 2.7
4198 28 Mar 08 nicklas 63   @base.modified $Date:2008-03-20 12:15:25 +0100 (Thu, 20 Mar 2008) $
4170 07 Mar 08 nicklas 64 */
4170 07 Mar 08 nicklas 65 public interface ValueConverter
4170 07 Mar 08 nicklas 66 {
4170 07 Mar 08 nicklas 67   /**
4208 07 Apr 08 nicklas 68     Convert a value. 
4170 07 Mar 08 nicklas 69     
4170 07 Mar 08 nicklas 70     @param in The value to convert
4170 07 Mar 08 nicklas 71     @param method The factory setter method
4170 07 Mar 08 nicklas 72     @return The converted or original value
4170 07 Mar 08 nicklas 73   */
4170 07 Mar 08 nicklas 74   public String convert(String in, Method method);
4170 07 Mar 08 nicklas 75
4170 07 Mar 08 nicklas 76 }