src/core/net/sf/basedb/util/extensions/xml/PathConverter.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   A value converter that can be used on all setter methods
5482 10 Nov 10 nicklas 28   that has been annotated with the {@link PathSetter} and
5482 10 Nov 10 nicklas 29   {@link PathType} annotations. The values are converted using the 
5482 10 Nov 10 nicklas 30   following rules:
4170 07 Mar 08 nicklas 31   
4170 07 Mar 08 nicklas 32   <ul>
4198 28 Mar 08 nicklas 33   <li>If the value starts with '/', the root path is added to the start
5946 03 Feb 12 nicklas 34     of the value, for example, <code>/images/copy.png --&gt; /base2/images/copy.png</code>.
4170 07 Mar 08 nicklas 35   <li>If the value starts with '~', the path to the home directory for
4170 07 Mar 08 nicklas 36     the current extension is added, for example,
4198 28 Mar 08 nicklas 37     <code>~/images/myimage.png --&gt; /base2/extensions/my-extensions.jar/images/myimage.png</code>
5482 10 Nov 10 nicklas 38   <li>If the path type is {@link PathType#CONTEXT_RELATIVE} the context-path is
5946 03 Feb 12 nicklas 39     removed from the final path. Eg. <code>/base2/images/copy.png --&gt; /images/copy.png</code>
4170 07 Mar 08 nicklas 40   </ul>
4170 07 Mar 08 nicklas 41
4170 07 Mar 08 nicklas 42   <p>
4170 07 Mar 08 nicklas 43   Note 1! It is only the start of the value that is checked. If a '~' is
4170 07 Mar 08 nicklas 44   present in the middle of a value, it is not replaced.
4170 07 Mar 08 nicklas 45
4170 07 Mar 08 nicklas 46   <p>
4170 07 Mar 08 nicklas 47   Note 2! Using the {@link VariableSetter} annotation and values like
5946 03 Feb 12 nicklas 48   <code>$ROOT$/images/copy.png</code> would have the same result. The only
4170 07 Mar 08 nicklas 49   reason to use this annotation is that it allows for a shorter notation.
4170 07 Mar 08 nicklas 50
4170 07 Mar 08 nicklas 51   @author nicklas
4170 07 Mar 08 nicklas 52   @version 2.7
4198 28 Mar 08 nicklas 53   @base.modified $Date:2008-03-20 12:15:25 +0100 (Thu, 20 Mar 2008) $
4170 07 Mar 08 nicklas 54 */
4170 07 Mar 08 nicklas 55 public class PathConverter 
4170 07 Mar 08 nicklas 56   implements ValueConverter
4170 07 Mar 08 nicklas 57 {
4170 07 Mar 08 nicklas 58
4170 07 Mar 08 nicklas 59   private String root;
4170 07 Mar 08 nicklas 60   private String home;
4170 07 Mar 08 nicklas 61   /**
4170 07 Mar 08 nicklas 62     Create a new empty path converter. Use {@link #setRoot(String)}
4170 07 Mar 08 nicklas 63     and {@link #setHome(String)} to initialise it.
4170 07 Mar 08 nicklas 64   */
4170 07 Mar 08 nicklas 65   public PathConverter()
4170 07 Mar 08 nicklas 66   {}
4170 07 Mar 08 nicklas 67   
4170 07 Mar 08 nicklas 68   /**
4170 07 Mar 08 nicklas 69     Create a new initialised path converter.
4170 07 Mar 08 nicklas 70     
4170 07 Mar 08 nicklas 71     @param root The path to the root directory (/)
4170 07 Mar 08 nicklas 72     @param home The path to the home directory (~)
4170 07 Mar 08 nicklas 73   */
4170 07 Mar 08 nicklas 74   public PathConverter(String root, String home)
4170 07 Mar 08 nicklas 75   {
4170 07 Mar 08 nicklas 76     this.home = home;
4170 07 Mar 08 nicklas 77     this.root = root;
4170 07 Mar 08 nicklas 78   }
4170 07 Mar 08 nicklas 79   
4170 07 Mar 08 nicklas 80   /*
4170 07 Mar 08 nicklas 81     From the ValueConverter interface
4170 07 Mar 08 nicklas 82     ----------------------------------
4170 07 Mar 08 nicklas 83   */
4170 07 Mar 08 nicklas 84   @Override
4170 07 Mar 08 nicklas 85   public String convert(String in, Method method) 
4170 07 Mar 08 nicklas 86   {
4170 07 Mar 08 nicklas 87     if (!method.isAnnotationPresent(PathSetter.class)) return in;
4170 07 Mar 08 nicklas 88     if (root != null && in.startsWith("/"))
4170 07 Mar 08 nicklas 89     {
4170 07 Mar 08 nicklas 90       in = root + in;
4170 07 Mar 08 nicklas 91     }
4170 07 Mar 08 nicklas 92     else if (home != null && in.startsWith("~"))
4170 07 Mar 08 nicklas 93     {
4170 07 Mar 08 nicklas 94       in = home + in.substring(1);
4170 07 Mar 08 nicklas 95     }
5482 10 Nov 10 nicklas 96     // Remove the 'root' part if the path should be context relative
5482 10 Nov 10 nicklas 97     PathType pathType = method.getAnnotation(PathSetter.class).pathType();
5482 10 Nov 10 nicklas 98     if (pathType == PathType.CONTEXT_RELATIVE)
5482 10 Nov 10 nicklas 99     {
5482 10 Nov 10 nicklas 100       if (in.startsWith(root)) in = in.substring(root.length());
5482 10 Nov 10 nicklas 101     }
4170 07 Mar 08 nicklas 102     return in;    
4170 07 Mar 08 nicklas 103   }
4170 07 Mar 08 nicklas 104   // ---------------------------------------
4170 07 Mar 08 nicklas 105
4170 07 Mar 08 nicklas 106   /**
4170 07 Mar 08 nicklas 107     Set the value of the root path.
4170 07 Mar 08 nicklas 108   */
4170 07 Mar 08 nicklas 109   public void setRoot(String root)
4170 07 Mar 08 nicklas 110   {
4170 07 Mar 08 nicklas 111     this.root = root;
4170 07 Mar 08 nicklas 112   }
4170 07 Mar 08 nicklas 113   /**
4170 07 Mar 08 nicklas 114     Get the value of the root path.
4170 07 Mar 08 nicklas 115   */
4170 07 Mar 08 nicklas 116   public String getRoot()
4170 07 Mar 08 nicklas 117   {
4170 07 Mar 08 nicklas 118     return root;
4170 07 Mar 08 nicklas 119   }
4170 07 Mar 08 nicklas 120
4170 07 Mar 08 nicklas 121   
4170 07 Mar 08 nicklas 122   /**
4170 07 Mar 08 nicklas 123     Set the value of the home path.
4170 07 Mar 08 nicklas 124   */
4170 07 Mar 08 nicklas 125   public void setHome(String home)
4170 07 Mar 08 nicklas 126   {
4170 07 Mar 08 nicklas 127     this.home = home;
4170 07 Mar 08 nicklas 128   }
4170 07 Mar 08 nicklas 129   
4170 07 Mar 08 nicklas 130   /**
4170 07 Mar 08 nicklas 131     Get the value of the home path.
4170 07 Mar 08 nicklas 132   */
4170 07 Mar 08 nicklas 133   public String getHome()
4170 07 Mar 08 nicklas 134   {
4170 07 Mar 08 nicklas 135     return home;
4170 07 Mar 08 nicklas 136   }
4170 07 Mar 08 nicklas 137   
4170 07 Mar 08 nicklas 138 }