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