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