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