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 java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.maven.model.Build;
27 import org.apache.maven.model.Model;
28 import org.apache.maven.model.Reporting;
29 import org.apache.maven.model.Resource;
30 import org.codehaus.plexus.component.annotations.Component;
31
32 @Deprecated
33 @Component( role = PathTranslator.class )
34 public class DefaultPathTranslator
35 implements PathTranslator
36 {
37 private static final String[] BASEDIR_EXPRESSIONS = {"${basedir}", "${pom.basedir}", "${project.basedir}"};
38
39 public void alignToBaseDirectory( Model model, File basedir )
40 {
41 Build build = model.getBuild();
42
43 if ( build != null )
44 {
45 build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
46
47 build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
48
49 build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
50
51 for ( Resource resource : build.getResources() )
52 {
53 resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
54 }
55
56 for ( Resource resource : build.getTestResources() )
57 {
58 resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
59 }
60
61 if ( build.getFilters() != null )
62 {
63 List<String> filters = new ArrayList<String>();
64 for ( String filter : build.getFilters() )
65 {
66 filters.add( alignToBaseDirectory( filter, basedir ) );
67 }
68 build.setFilters( filters );
69 }
70
71 build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
72
73 build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
74 }
75
76 Reporting reporting = model.getReporting();
77
78 if ( reporting != null )
79 {
80 reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
81 }
82 }
83
84 public String alignToBaseDirectory( String path, File basedir )
85 {
86 if ( path == null )
87 {
88 return null;
89 }
90
91 String s = stripBasedirToken( path );
92
93 File file = new File( s );
94 if ( file.isAbsolute() )
95 {
96
97 s = file.getPath();
98 }
99 else if ( file.getPath().startsWith( File.separator ) )
100 {
101
102 s = file.getAbsolutePath();
103 }
104 else
105 {
106
107 s = new File( new File( basedir, s ).toURI().normalize() ).getAbsolutePath();
108 }
109
110 return s;
111 }
112
113 private String stripBasedirToken( String s )
114 {
115 if ( s != null )
116 {
117 String basedirExpr = null;
118 for ( int i = 0; i < BASEDIR_EXPRESSIONS.length; i++ )
119 {
120 basedirExpr = BASEDIR_EXPRESSIONS[i];
121 if ( s.startsWith( basedirExpr ) )
122 {
123 break;
124 }
125 else
126 {
127 basedirExpr = null;
128 }
129 }
130
131 if ( basedirExpr != null )
132 {
133 if ( s.length() > basedirExpr.length() )
134 {
135
136 s = chopLeadingFileSeparator( s.substring( basedirExpr.length() ) );
137 }
138 else
139 {
140 s = ".";
141 }
142 }
143 }
144
145 return s;
146 }
147
148
149
150
151
152
153
154
155 private String chopLeadingFileSeparator( String path )
156 {
157 if ( path != null )
158 {
159 if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
160 {
161 path = path.substring( 1 );
162 }
163 }
164 return path;
165 }
166
167 public void unalignFromBaseDirectory( Model model, File basedir )
168 {
169 Build build = model.getBuild();
170
171 if ( build != null )
172 {
173 build.setDirectory( unalignFromBaseDirectory( build.getDirectory(), basedir ) );
174
175 build.setSourceDirectory( unalignFromBaseDirectory( build.getSourceDirectory(), basedir ) );
176
177 build.setTestSourceDirectory( unalignFromBaseDirectory( build.getTestSourceDirectory(), basedir ) );
178
179 for ( Resource resource : build.getResources() )
180 {
181 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
182 }
183
184 for ( Resource resource : build.getTestResources() )
185 {
186 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
187 }
188
189 if ( build.getFilters() != null )
190 {
191 List<String> filters = new ArrayList<String>();
192 for ( String filter : build.getFilters() )
193 {
194 filters.add( unalignFromBaseDirectory( filter, basedir ) );
195 }
196 build.setFilters( filters );
197 }
198
199 build.setOutputDirectory( unalignFromBaseDirectory( build.getOutputDirectory(), basedir ) );
200
201 build.setTestOutputDirectory( unalignFromBaseDirectory( build.getTestOutputDirectory(), basedir ) );
202 }
203
204 Reporting reporting = model.getReporting();
205
206 if ( reporting != null )
207 {
208 reporting.setOutputDirectory( unalignFromBaseDirectory( reporting.getOutputDirectory(), basedir ) );
209 }
210 }
211
212 public String unalignFromBaseDirectory( String directory, File basedir )
213 {
214 String path = basedir.getPath();
215 if ( directory.startsWith( path ) )
216 {
217 directory = directory.substring( path.length() + 1 ).replace( '\\', '/' );
218 }
219 return directory;
220 }
221
222 }
223