View Javadoc

1   package org.apache.maven.project.path;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          if ( basedir == null )
42          {
43              return;
44          }
45  
46          Build build = model.getBuild();
47  
48          if ( build != null )
49          {
50              build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
51  
52              build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
53  
54              build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
55  
56              for ( Resource resource : build.getResources() )
57              {
58                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
59              }
60  
61              for ( Resource resource : build.getTestResources() )
62              {
63                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
64              }
65  
66              if ( build.getFilters() != null )
67              {
68                  List<String> filters = new ArrayList<String>();
69                  for ( String filter : build.getFilters() )
70                  {
71                      filters.add( alignToBaseDirectory( filter, basedir ) );
72                  }
73                  build.setFilters( filters );
74              }
75  
76              build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
77  
78              build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
79          }
80  
81          Reporting reporting = model.getReporting();
82  
83          if ( reporting != null )
84          {
85              reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
86          }
87      }
88  
89      public String alignToBaseDirectory( String path, File basedir )
90      {
91          if ( basedir == null )
92          {
93              return path;
94          }
95  
96          if ( path == null )
97          {
98              return null;
99          }
100 
101         String s = stripBasedirToken( path );
102 
103         File file = new File( s );
104         if ( file.isAbsolute() )
105         {
106             // path was already absolute, just normalize file separator and we're done
107             s = file.getPath();
108         }
109         else if ( file.getPath().startsWith( File.separator ) )
110         {
111             // drive-relative Windows path, don't align with project directory but with drive root
112             s = file.getAbsolutePath();
113         }
114         else
115         {
116             // an ordinary relative path, align with project directory
117             s = new File( new File( basedir, s ).toURI().normalize() ).getAbsolutePath();
118         }
119 
120         return s;
121     }
122 
123     private String stripBasedirToken( String s )
124     {
125         if ( s != null )
126         {
127             String basedirExpr = null;
128             for ( int i = 0; i < BASEDIR_EXPRESSIONS.length; i++ )
129             {
130                 basedirExpr = BASEDIR_EXPRESSIONS[i];
131                 if ( s.startsWith( basedirExpr ) )
132                 {
133                     break;
134                 }
135                 else
136                 {
137                     basedirExpr = null;
138                 }
139             }
140 
141             if ( basedirExpr != null )
142             {
143                 if ( s.length() > basedirExpr.length() )
144                 {
145                     // Take out basedir expression and the leading slash
146                     s = chopLeadingFileSeparator( s.substring( basedirExpr.length() ) );
147                 }
148                 else
149                 {
150                     s = ".";
151                 }
152             }
153         }
154 
155         return s;
156     }
157 
158     /**
159      * Removes the leading directory separator from the specified filesystem path (if any). For platform-independent
160      * behavior, this method accepts both the forward slash and the backward slash as separator.
161      *
162      * @param path The filesystem path, may be <code>null</code>.
163      * @return The altered filesystem path or <code>null</code> if the input path was <code>null</code>.
164      */
165     private String chopLeadingFileSeparator( String path )
166     {
167         if ( path != null )
168         {
169             if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
170             {
171                 path = path.substring( 1 );
172             }
173         }
174         return path;
175     }
176 
177     public void unalignFromBaseDirectory( Model model, File basedir )
178     {
179         if ( basedir == null )
180         {
181             return;
182         }
183 
184         Build build = model.getBuild();
185 
186         if ( build != null )
187         {
188             build.setDirectory( unalignFromBaseDirectory( build.getDirectory(), basedir ) );
189 
190             build.setSourceDirectory( unalignFromBaseDirectory( build.getSourceDirectory(), basedir ) );
191 
192             build.setTestSourceDirectory( unalignFromBaseDirectory( build.getTestSourceDirectory(), basedir ) );
193 
194             for ( Resource resource : build.getResources() )
195             {
196                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
197             }
198 
199             for ( Resource resource : build.getTestResources() )
200             {
201                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
202             }
203 
204             if ( build.getFilters() != null )
205             {
206                 List<String> filters = new ArrayList<String>();
207                 for ( String filter : build.getFilters() )
208                 {
209                     filters.add( unalignFromBaseDirectory( filter, basedir ) );
210                 }
211                 build.setFilters( filters );
212             }
213 
214             build.setOutputDirectory( unalignFromBaseDirectory( build.getOutputDirectory(), basedir ) );
215 
216             build.setTestOutputDirectory( unalignFromBaseDirectory( build.getTestOutputDirectory(), basedir ) );
217         }
218 
219         Reporting reporting = model.getReporting();
220 
221         if ( reporting != null )
222         {
223             reporting.setOutputDirectory( unalignFromBaseDirectory( reporting.getOutputDirectory(), basedir ) );
224         }
225     }
226 
227     public String unalignFromBaseDirectory( String path, File basedir )
228     {
229         if ( basedir == null )
230         {
231             return path;
232         }
233 
234         if ( path == null )
235         {
236             return null;
237         }
238 
239         path = path.trim();
240 
241         String base = basedir.getAbsolutePath();
242         if ( path.startsWith( base ) )
243         {
244             path = chopLeadingFileSeparator( path.substring( base.length() ) );
245         }
246 
247         if ( path.length() <= 0 )
248         {
249             path = ".";
250         }
251 
252         if ( !new File( path ).isAbsolute() )
253         {
254             path = path.replace( '\\', '/' );
255         }
256 
257         return path;
258     }
259 
260 }
261