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.Resource;
25  
26  import java.io.File;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  public class DefaultPathTranslator
32      implements PathTranslator
33  {
34  
35      public void alignToBaseDirectory( Model model, File basedir )
36      {
37          Build build = model.getBuild();
38  
39          if ( build != null )
40          {
41              build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
42  
43              build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
44  
45              build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
46  
47              for ( Iterator i = build.getResources().iterator(); i.hasNext(); )
48              {
49                  Resource resource = (Resource) i.next();
50  
51                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
52              }
53  
54              for ( Iterator i = build.getTestResources().iterator(); i.hasNext(); )
55              {
56                  Resource resource = (Resource) i.next();
57  
58                  resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
59              }
60  
61              if ( build.getFilters() != null )
62              {
63                  List filters = new ArrayList();
64                  for ( Iterator i = build.getFilters().iterator(); i.hasNext(); )
65                  {
66                      String filter = (String) i.next();
67  
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  
79      public String alignToBaseDirectory( String path, File basedir )
80      {
81          if ( path == null )
82          {
83              return null;
84          }
85  
86          String s = stripBasedirToken( path );
87  
88          File file = new File( s );
89          if ( file.isAbsolute() )
90          {
91              // path was already absolute, just normalize file separator and we're done
92              s = file.getPath();
93          }
94          else if ( file.getPath().startsWith( File.separator ) )
95          {
96              // drive-relative Windows path, don't align with project directory but with drive root
97              s = file.getAbsolutePath();
98          }
99          else
100         {
101             // an ordinary relative path, align with project directory
102             s = new File( new File( basedir, s ).toURI().normalize() ).getAbsolutePath();
103         }
104 
105         return s;
106     }
107 
108     private String stripBasedirToken( String s )
109     {
110         String basedirExpr = "${basedir}";
111 
112         if ( s != null )
113         {
114             s = s.trim();
115 
116             if ( s.startsWith( basedirExpr ) )
117             {
118                 if ( s.length() > basedirExpr.length() )
119                 {
120                     // Take out ${basedir} and the leading slash
121                     s = s.substring( basedirExpr.length() + 1 );
122                 }
123                 else
124                 {
125                     s = ".";
126                 }
127             }
128         }
129 
130         return s;
131     }
132 
133     public void unalignFromBaseDirectory( Model model, File basedir )
134     {
135         Build build = model.getBuild();
136 
137         if ( build != null )
138         {
139             build.setDirectory( unalignFromBaseDirectory( build.getDirectory(), basedir ) );
140 
141             build.setSourceDirectory( unalignFromBaseDirectory( build.getSourceDirectory(), basedir ) );
142 
143             build.setTestSourceDirectory( unalignFromBaseDirectory( build.getTestSourceDirectory(), basedir ) );
144 
145             for ( Iterator i = build.getResources().iterator(); i.hasNext(); )
146             {
147                 Resource resource = (Resource) i.next();
148 
149                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
150             }
151 
152             for ( Iterator i = build.getTestResources().iterator(); i.hasNext(); )
153             {
154                 Resource resource = (Resource) i.next();
155 
156                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
157             }
158 
159             if ( build.getFilters() != null )
160             {
161                 List filters = new ArrayList();
162                 for ( Iterator i = build.getFilters().iterator(); i.hasNext(); )
163                 {
164                     String filter = (String) i.next();
165 
166                     filters.add( unalignFromBaseDirectory( filter, basedir ) );
167                 }
168                 build.setFilters( filters );
169             }
170 
171             build.setOutputDirectory( unalignFromBaseDirectory( build.getOutputDirectory(), basedir ) );
172 
173             build.setTestOutputDirectory( unalignFromBaseDirectory( build.getTestOutputDirectory(), basedir ) );
174         }
175     }
176 
177     public String unalignFromBaseDirectory( String directory, File basedir )
178     {
179         String path = basedir.getPath();
180         if ( directory.startsWith( path ) )
181         {
182             directory = directory.substring( path.length() + 1 ).replace( '\\', '/' );
183         }
184         return directory;
185     }
186 
187 }
188