4170 |
07 Mar 08 |
nicklas |
1 |
/** |
4479 |
05 Sep 08 |
jari |
$Id$ |
4170 |
07 Mar 08 |
nicklas |
3 |
|
4170 |
07 Mar 08 |
nicklas |
Copyright (C) Authors contributing to this file. |
4170 |
07 Mar 08 |
nicklas |
5 |
|
4170 |
07 Mar 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4170 |
07 Mar 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4170 |
07 Mar 08 |
nicklas |
8 |
|
4170 |
07 Mar 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4170 |
07 Mar 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
4170 |
07 Mar 08 |
nicklas |
of the License, or (at your option) any later version. |
4170 |
07 Mar 08 |
nicklas |
13 |
|
4170 |
07 Mar 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4170 |
07 Mar 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4170 |
07 Mar 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4170 |
07 Mar 08 |
nicklas |
GNU General Public License for more details. |
4170 |
07 Mar 08 |
nicklas |
18 |
|
4170 |
07 Mar 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
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 |
A value converter that can be used on all setter methods |
5482 |
10 Nov 10 |
nicklas |
that has been annotated with the {@link PathSetter} and |
5482 |
10 Nov 10 |
nicklas |
{@link PathType} annotations. The values are converted using the |
5482 |
10 Nov 10 |
nicklas |
following rules: |
4170 |
07 Mar 08 |
nicklas |
31 |
|
4170 |
07 Mar 08 |
nicklas |
<ul> |
4198 |
28 Mar 08 |
nicklas |
<li>If the value starts with '/', the root path is added to the start |
5946 |
03 Feb 12 |
nicklas |
of the value, for example, <code>/images/copy.png --> /base2/images/copy.png</code>. |
4170 |
07 Mar 08 |
nicklas |
<li>If the value starts with '~', the path to the home directory for |
4170 |
07 Mar 08 |
nicklas |
the current extension is added, for example, |
4198 |
28 Mar 08 |
nicklas |
<code>~/images/myimage.png --> /base2/extensions/my-extensions.jar/images/myimage.png</code> |
5482 |
10 Nov 10 |
nicklas |
<li>If the path type is {@link PathType#CONTEXT_RELATIVE} the context-path is |
5946 |
03 Feb 12 |
nicklas |
removed from the final path. Eg. <code>/base2/images/copy.png --> /images/copy.png</code> |
4170 |
07 Mar 08 |
nicklas |
</ul> |
4170 |
07 Mar 08 |
nicklas |
41 |
|
4170 |
07 Mar 08 |
nicklas |
<p> |
4170 |
07 Mar 08 |
nicklas |
Note 1! It is only the start of the value that is checked. If a '~' is |
4170 |
07 Mar 08 |
nicklas |
present in the middle of a value, it is not replaced. |
4170 |
07 Mar 08 |
nicklas |
45 |
|
4170 |
07 Mar 08 |
nicklas |
<p> |
4170 |
07 Mar 08 |
nicklas |
Note 2! Using the {@link VariableSetter} annotation and values like |
5946 |
03 Feb 12 |
nicklas |
<code>$ROOT$/images/copy.png</code> would have the same result. The only |
4170 |
07 Mar 08 |
nicklas |
reason to use this annotation is that it allows for a shorter notation. |
4170 |
07 Mar 08 |
nicklas |
50 |
|
4170 |
07 Mar 08 |
nicklas |
@author nicklas |
4170 |
07 Mar 08 |
nicklas |
@version 2.7 |
4198 |
28 Mar 08 |
nicklas |
@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 |
Create a new empty path converter. Use {@link #setRoot(String)} |
4170 |
07 Mar 08 |
nicklas |
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 |
Create a new initialised path converter. |
4170 |
07 Mar 08 |
nicklas |
70 |
|
4170 |
07 Mar 08 |
nicklas |
@param root The path to the root directory (/) |
4170 |
07 Mar 08 |
nicklas |
@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 |
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 |
// 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 |
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 |
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 |
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 |
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 |
} |