1 package org.apache.maven.project.path;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.model.Build;
23 import org.apache.maven.model.Model;
24 import org.apache.maven.model.Reporting;
25 import org.apache.maven.model.Resource;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31
32
33
34
35 public class DefaultPathTranslator
36 implements PathTranslator
37 {
38 private static final String[] BASEDIR_EXPRESSIONS = {
39 "${basedir}",
40 "${pom.basedir}",
41 "${project.basedir}"
42 };
43
44 public void alignToBaseDirectory( Model model, File basedir )
45 {
46 if ( basedir == null )
47 {
48 return;
49 }
50
51 Build build = model.getBuild();
52
53 if ( build != null )
54 {
55 build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
56
57 build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
58
59 build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
60
61
62
63
64 for ( Iterator i = build.getResources().iterator(); i.hasNext(); )
65 {
66 Resource resource = (Resource) i.next();
67
68 resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
69 }
70
71 for ( Iterator i = build.getTestResources().iterator(); i.hasNext(); )
72 {
73 Resource resource = (Resource) i.next();
74
75 resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
76 }
77
78 if ( build.getFilters() != null )
79 {
80 List filters = new ArrayList();
81 for ( Iterator i = build.getFilters().iterator(); i.hasNext(); )
82 {
83 String filter = (String) i.next();
84
85 filters.add( alignToBaseDirectory( filter, basedir ) );
86 }
87 build.setFilters( filters );
88 }
89
90 build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
91
92 build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
93 }
94
95 Reporting reporting = model.getReporting();
96
97 if ( reporting != null )
98 {
99 reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
100 }
101 }
102
103
104
105
106 public String alignToBaseDirectory( String path, File basedir )
107 {
108 if ( basedir == null )
109 {
110 return path;
111 }
112
113 if ( path == null )
114 {
115 return null;
116 }
117
118 String s = stripBasedirToken( path );
119
120 File file = new File( s );
121 if ( file.isAbsolute() )
122 {
123
124 s = file.getPath();
125 }
126 else if ( file.getPath().startsWith( File.separator ) )
127 {
128
129 s = file.getAbsolutePath();
130 }
131 else
132 {
133
134 s = new File( new File( basedir, s ).toURI().normalize() ).getAbsolutePath();
135 }
136
137 return s;
138 }
139
140 private String stripBasedirToken( String s )
141 {
142 if ( s != null )
143 {
144 String basedirExpr = null;
145 for ( int i = 0; i < BASEDIR_EXPRESSIONS.length; i++ )
146 {
147 basedirExpr = BASEDIR_EXPRESSIONS[i];
148 if ( s.startsWith( basedirExpr ) )
149 {
150 break;
151 }
152 else
153 {
154 basedirExpr = null;
155 }
156 }
157
158 if ( basedirExpr != null )
159 {
160 if ( s.length() > basedirExpr.length() )
161 {
162
163 s = chopLeadingFileSeparator( s.substring( basedirExpr.length() ) );
164 }
165 else
166 {
167 s = ".";
168 }
169 }
170 }
171
172 return s;
173 }
174
175
176
177
178
179
180
181
182 private String chopLeadingFileSeparator( String path )
183 {
184 if ( path != null )
185 {
186 if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
187 {
188 path = path.substring( 1 );
189 }
190 }
191 return path;
192 }
193
194
195
196
197 public void unalignFromBaseDirectory( Model model, File basedir )
198 {
199 if ( basedir == null )
200 {
201 return;
202 }
203
204 Build build = model.getBuild();
205
206 if ( build != null )
207 {
208 build.setDirectory( unalignFromBaseDirectory( build.getDirectory(), basedir ) );
209
210 build.setSourceDirectory( unalignFromBaseDirectory( build.getSourceDirectory(), basedir ) );
211
212 build.setTestSourceDirectory( unalignFromBaseDirectory( build.getTestSourceDirectory(), basedir ) );
213
214 build.setScriptSourceDirectory( unalignFromBaseDirectory( build.getScriptSourceDirectory(), basedir ) );
215
216 for ( Iterator i = build.getResources().iterator(); i.hasNext(); )
217 {
218 Resource resource = (Resource) i.next();
219
220 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
221 }
222
223 for ( Iterator i = build.getTestResources().iterator(); i.hasNext(); )
224 {
225 Resource resource = (Resource) i.next();
226
227 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
228 }
229
230 if ( build.getFilters() != null )
231 {
232 List filters = new ArrayList();
233 for ( Iterator i = build.getFilters().iterator(); i.hasNext(); )
234 {
235 String filter = (String) i.next();
236
237 filters.add( unalignFromBaseDirectory( filter, basedir ) );
238 }
239 build.setFilters( filters );
240 }
241
242 build.setOutputDirectory( unalignFromBaseDirectory( build.getOutputDirectory(), basedir ) );
243
244 build.setTestOutputDirectory( unalignFromBaseDirectory( build.getTestOutputDirectory(), basedir ) );
245 }
246
247 Reporting reporting = model.getReporting();
248
249 if ( reporting != null )
250 {
251 reporting.setOutputDirectory( unalignFromBaseDirectory( reporting.getOutputDirectory(), basedir ) );
252 }
253 }
254
255
256
257
258 public String unalignFromBaseDirectory( String path, File basedir )
259 {
260 if ( basedir == null )
261 {
262 return path;
263 }
264
265 if ( path == null )
266 {
267 return null;
268 }
269
270 path = path.trim();
271
272 String base = basedir.getAbsolutePath();
273 if ( path.startsWith( base ) )
274 {
275 path = chopLeadingFileSeparator( path.substring( base.length() ) );
276 }
277
278 if ( !new File( path ).isAbsolute() )
279 {
280 path = path.replace( '\\', '/' );
281 }
282
283 return path;
284 }
285
286 }
287