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  /**
33   * DefaultPathTranslator
34   */
35  @Deprecated
36  @Component( role = PathTranslator.class )
37  public class DefaultPathTranslator
38      implements PathTranslator
39  {
40      private static final String[] BASEDIR_EXPRESSIONS = {"${basedir}", "${pom.basedir}", "${project.basedir}"};
41  
42      public void alignToBaseDirectory( Model model, File basedir )
43      {
44          if ( basedir == null )
45          {
46              return;
47          }
48  
49          Build build = model.getBuild();
50  
51          if ( build != null )
52          {
53              build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
54  
55              build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
56  
57              build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
58  
59              for ( Resource resource : build.getResources() )
60              {
61                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
62              }
63  
64              for ( Resource resource : build.getTestResources() )
65              {
66                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
67              }
68  
69              if ( build.getFilters() != null )
70              {
71                  List<String> filters = new ArrayList<>();
72                  for ( String filter : build.getFilters() )
73                  {
74                      filters.add( alignToBaseDirectory( filter, basedir ) );
75                  }
76                  build.setFilters( filters );
77              }
78  
79              build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
80  
81              build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
82          }
83  
84          Reporting reporting = model.getReporting();
85  
86          if ( reporting != null )
87          {
88              reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
89          }
90      }
91  
92      public String alignToBaseDirectory( String path, File basedir )
93      {
94          if ( basedir == null )
95          {
96              return path;
97          }
98  
99          if ( path == null )
100         {
101             return null;
102         }
103 
104         String s = stripBasedirToken( path );
105 
106         File file = new File( s );
107         if ( file.isAbsolute() )
108         {
109             // path was already absolute, just normalize file separator and we're done
110             s = file.getPath();
111         }
112         else if ( file.getPath().startsWith( File.separator ) )
113         {
114             // drive-relative Windows path, don't align with project directory but with drive root
115             s = file.getAbsolutePath();
116         }
117         else
118         {
119             // an ordinary relative path, align with project directory
120             s = new File( new File( basedir, s ).toURI().normalize() ).getAbsolutePath();
121         }
122 
123         return s;
124     }
125 
126     private String stripBasedirToken( String s )
127     {
128         if ( s != null )
129         {
130             String basedirExpr = null;
131             for ( String expression : BASEDIR_EXPRESSIONS )
132             {
133                 if ( s.startsWith( expression ) )
134                 {
135                     basedirExpr = expression;
136                     break;
137                 }
138             }
139 
140             if ( basedirExpr != null )
141             {
142                 if ( s.length() > basedirExpr.length() )
143                 {
144                     // Take out basedir expression and the leading slash
145                     s = chopLeadingFileSeparator( s.substring( basedirExpr.length() ) );
146                 }
147                 else
148                 {
149                     s = ".";
150                 }
151             }
152         }
153 
154         return s;
155     }
156 
157     /**
158      * Removes the leading directory separator from the specified filesystem path (if any). For platform-independent
159      * behavior, this method accepts both the forward slash and the backward slash as separator.
160      *
161      * @param path The filesystem path, may be <code>null</code>.
162      * @return The altered filesystem path or <code>null</code> if the input path was <code>null</code>.
163      */
164     private String chopLeadingFileSeparator( String path )
165     {
166         if ( path != null )
167         {
168             if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
169             {
170                 path = path.substring( 1 );
171             }
172         }
173         return path;
174     }
175 
176     public void unalignFromBaseDirectory( Model model, File basedir )
177     {
178         if ( basedir == null )
179         {
180             return;
181         }
182 
183         Build build = model.getBuild();
184 
185         if ( build != null )
186         {
187             build.setDirectory( unalignFromBaseDirectory( build.getDirectory(), basedir ) );
188 
189             build.setSourceDirectory( unalignFromBaseDirectory( build.getSourceDirectory(), basedir ) );
190 
191             build.setTestSourceDirectory( unalignFromBaseDirectory( build.getTestSourceDirectory(), basedir ) );
192 
193             for ( Resource resource : build.getResources() )
194             {
195                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
196             }
197 
198             for ( Resource resource : build.getTestResources() )
199             {
200                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
201             }
202 
203             if ( build.getFilters() != null )
204             {
205                 List<String> filters = new ArrayList<>();
206                 for ( String filter : build.getFilters() )
207                 {
208                     filters.add( unalignFromBaseDirectory( filter, basedir ) );
209                 }
210                 build.setFilters( filters );
211             }
212 
213             build.setOutputDirectory( unalignFromBaseDirectory( build.getOutputDirectory(), basedir ) );
214 
215             build.setTestOutputDirectory( unalignFromBaseDirectory( build.getTestOutputDirectory(), basedir ) );
216         }
217 
218         Reporting reporting = model.getReporting();
219 
220         if ( reporting != null )
221         {
222             reporting.setOutputDirectory( unalignFromBaseDirectory( reporting.getOutputDirectory(), basedir ) );
223         }
224     }
225 
226     public String unalignFromBaseDirectory( String path, File basedir )
227     {
228         if ( basedir == null )
229         {
230             return path;
231         }
232 
233         if ( path == null )
234         {
235             return null;
236         }
237 
238         path = path.trim();
239 
240         String base = basedir.getAbsolutePath();
241         if ( path.startsWith( base ) )
242         {
243             path = chopLeadingFileSeparator( path.substring( base.length() ) );
244         }
245 
246         if ( path.length() <= 0 )
247         {
248             path = ".";
249         }
250 
251         if ( !new File( path ).isAbsolute() )
252         {
253             path = path.replace( '\\', '/' );
254         }
255 
256         return path;
257     }
258 
259 }
260