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 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   * Default implementation of {@link PathTranslator}.
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              // TODO: MNG-3731
62  //            build.setScriptSourceDirectory( alignToBaseDirectory( build.getScriptSourceDirectory(), basedir ) );
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              Reporting reporting = model.getReporting();
95              if ( reporting != null )
96              {
97                  reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
98              }
99          }
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     public String alignToBaseDirectory( String path, File basedir )
106     {
107         if ( basedir == null )
108         {
109             return path;
110         }
111 
112         if ( path == null )
113         {
114             return null;
115         }
116 
117         String s = stripBasedirToken( path );
118 
119         File file = new File( s );
120         if ( file.isAbsolute() )
121         {
122             // path was already absolute, just normalize file separator and we're done
123             s = file.getPath();
124         }
125         else if ( file.getPath().startsWith( File.separator ) )
126         {
127             // drive-relative Windows path, don't align with project directory but with drive root
128             s = file.getAbsolutePath();
129         }
130         else
131         {
132             // an ordinary relative path, align with project directory
133             s = new File( new File( basedir, s ).toURI().normalize() ).getAbsolutePath();
134         }
135 
136         return s;
137     }
138 
139     private String stripBasedirToken( String s )
140     {
141         if ( s != null )
142         {
143             String basedirExpr = null;
144             for ( int i = 0; i < BASEDIR_EXPRESSIONS.length; i++ )
145             {
146                 basedirExpr = BASEDIR_EXPRESSIONS[i];
147                 if ( s.startsWith( basedirExpr ) )
148                 {
149                     break;
150                 }
151                 else
152                 {
153                     basedirExpr = null;
154                 }
155             }
156 
157             if ( basedirExpr != null )
158             {
159                 if ( s.length() > basedirExpr.length() )
160                 {
161                     // Take out basedir expression and the leading slash
162                     s = chopLeadingFileSeparator( s.substring( basedirExpr.length() ) );
163                 }
164                 else
165                 {
166                     s = ".";
167                 }
168             }
169         }
170 
171         return s;
172     }
173 
174     /**
175      * Removes the leading directory separator from the specified filesystem path (if any). For platform-independent
176      * behavior, this method accepts both the forward slash and the backward slash as separator.
177      *
178      * @param path The filesystem path, may be <code>null</code>.
179      * @return The altered filesystem path or <code>null</code> if the input path was <code>null</code>.
180      */
181     private String chopLeadingFileSeparator( String path )
182     {
183         if ( path != null )
184         {
185             if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
186             {
187                 path = path.substring( 1 );
188             }
189         }
190         return path;
191     }
192 
193     /**
194      * {@inheritDoc}
195      */
196     public void unalignFromBaseDirectory( Model model, File basedir )
197     {
198         if ( basedir == null )
199         {
200             return;
201         }
202 
203         Build build = model.getBuild();
204 
205         if ( build != null )
206         {
207             build.setDirectory( unalignFromBaseDirectory( build.getDirectory(), basedir ) );
208 
209             build.setSourceDirectory( unalignFromBaseDirectory( build.getSourceDirectory(), basedir ) );
210 
211             build.setTestSourceDirectory( unalignFromBaseDirectory( build.getTestSourceDirectory(), basedir ) );
212 
213             build.setScriptSourceDirectory( unalignFromBaseDirectory( build.getScriptSourceDirectory(), basedir ) );
214 
215             for ( Iterator i = build.getResources().iterator(); i.hasNext(); )
216             {
217                 Resource resource = (Resource) i.next();
218 
219                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
220             }
221 
222             for ( Iterator i = build.getTestResources().iterator(); i.hasNext(); )
223             {
224                 Resource resource = (Resource) i.next();
225 
226                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
227             }
228 
229             if ( build.getFilters() != null )
230             {
231                 List filters = new ArrayList();
232                 for ( Iterator i = build.getFilters().iterator(); i.hasNext(); )
233                 {
234                     String filter = (String) i.next();
235 
236                     filters.add( unalignFromBaseDirectory( filter, basedir ) );
237                 }
238                 build.setFilters( filters );
239             }
240 
241             build.setOutputDirectory( unalignFromBaseDirectory( build.getOutputDirectory(), basedir ) );
242 
243             build.setTestOutputDirectory( unalignFromBaseDirectory( build.getTestOutputDirectory(), basedir ) );
244 
245             Reporting reporting = model.getReporting();
246             if ( reporting != null )
247             {
248                 reporting.setOutputDirectory( unalignFromBaseDirectory( reporting.getOutputDirectory(), basedir ) );
249             }
250         }
251     }
252 
253     /**
254      * {@inheritDoc}
255      */
256     public String unalignFromBaseDirectory( String path, File basedir )
257     {
258         if ( basedir == null )
259         {
260             return path;
261         }
262 
263         if ( path == null )
264         {
265             return null;
266         }
267 
268         path = path.trim();
269 
270         String base = basedir.getAbsolutePath();
271         if ( path.startsWith( base ) )
272         {
273             path = chopLeadingFileSeparator( path.substring( base.length() ) );
274         }
275 
276         if ( !new File( path ).isAbsolute() )
277         {
278             path = path.replace( '\\', '/' );
279         }
280 
281         return path;
282     }
283 
284 }
285