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