001package org.apache.maven.project.path;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.maven.model.Build;
027import org.apache.maven.model.Model;
028import org.apache.maven.model.Reporting;
029import org.apache.maven.model.Resource;
030import org.codehaus.plexus.component.annotations.Component;
031
032@Deprecated
033@Component( role = PathTranslator.class )
034public class DefaultPathTranslator
035    implements PathTranslator
036{
037    private static final String[] BASEDIR_EXPRESSIONS = {"${basedir}", "${pom.basedir}", "${project.basedir}"};
038
039    public void alignToBaseDirectory( Model model, File basedir )
040    {
041        if ( basedir == null )
042        {
043            return;
044        }
045
046        Build build = model.getBuild();
047
048        if ( build != null )
049        {
050            build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
051
052            build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
053
054            build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
055
056            for ( Resource resource : build.getResources() )
057            {
058                resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
059            }
060
061            for ( Resource resource : build.getTestResources() )
062            {
063                resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
064            }
065
066            if ( build.getFilters() != null )
067            {
068                List<String> filters = new ArrayList<String>();
069                for ( String filter : build.getFilters() )
070                {
071                    filters.add( alignToBaseDirectory( filter, basedir ) );
072                }
073                build.setFilters( filters );
074            }
075
076            build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
077
078            build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
079        }
080
081        Reporting reporting = model.getReporting();
082
083        if ( reporting != null )
084        {
085            reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
086        }
087    }
088
089    public String alignToBaseDirectory( String path, File basedir )
090    {
091        if ( basedir == null )
092        {
093            return path;
094        }
095
096        if ( path == null )
097        {
098            return null;
099        }
100
101        String s = stripBasedirToken( path );
102
103        File file = new File( s );
104        if ( file.isAbsolute() )
105        {
106            // path was already absolute, just normalize file separator and we're done
107            s = file.getPath();
108        }
109        else if ( file.getPath().startsWith( File.separator ) )
110        {
111            // drive-relative Windows path, don't align with project directory but with drive root
112            s = file.getAbsolutePath();
113        }
114        else
115        {
116            // an ordinary relative path, align with project directory
117            s = new File( new File( basedir, s ).toURI().normalize() ).getAbsolutePath();
118        }
119
120        return s;
121    }
122
123    private String stripBasedirToken( String s )
124    {
125        if ( s != null )
126        {
127            String basedirExpr = null;
128            for ( String BASEDIR_EXPRESSION : BASEDIR_EXPRESSIONS )
129            {
130                basedirExpr = BASEDIR_EXPRESSION;
131                if ( s.startsWith( basedirExpr ) )
132                {
133                    break;
134                }
135                else
136                {
137                    basedirExpr = null;
138                }
139            }
140
141            if ( basedirExpr != null )
142            {
143                if ( s.length() > basedirExpr.length() )
144                {
145                    // Take out basedir expression and the leading slash
146                    s = chopLeadingFileSeparator( s.substring( basedirExpr.length() ) );
147                }
148                else
149                {
150                    s = ".";
151                }
152            }
153        }
154
155        return s;
156    }
157
158    /**
159     * Removes the leading directory separator from the specified filesystem path (if any). For platform-independent
160     * behavior, this method accepts both the forward slash and the backward slash as separator.
161     *
162     * @param path The filesystem path, may be <code>null</code>.
163     * @return The altered filesystem path or <code>null</code> if the input path was <code>null</code>.
164     */
165    private String chopLeadingFileSeparator( String path )
166    {
167        if ( path != null )
168        {
169            if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
170            {
171                path = path.substring( 1 );
172            }
173        }
174        return path;
175    }
176
177    public void unalignFromBaseDirectory( Model model, File basedir )
178    {
179        if ( basedir == null )
180        {
181            return;
182        }
183
184        Build build = model.getBuild();
185
186        if ( build != null )
187        {
188            build.setDirectory( unalignFromBaseDirectory( build.getDirectory(), basedir ) );
189
190            build.setSourceDirectory( unalignFromBaseDirectory( build.getSourceDirectory(), basedir ) );
191
192            build.setTestSourceDirectory( unalignFromBaseDirectory( build.getTestSourceDirectory(), basedir ) );
193
194            for ( Resource resource : build.getResources() )
195            {
196                resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
197            }
198
199            for ( Resource resource : build.getTestResources() )
200            {
201                resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
202            }
203
204            if ( build.getFilters() != null )
205            {
206                List<String> filters = new ArrayList<String>();
207                for ( String filter : build.getFilters() )
208                {
209                    filters.add( unalignFromBaseDirectory( filter, basedir ) );
210                }
211                build.setFilters( filters );
212            }
213
214            build.setOutputDirectory( unalignFromBaseDirectory( build.getOutputDirectory(), basedir ) );
215
216            build.setTestOutputDirectory( unalignFromBaseDirectory( build.getTestOutputDirectory(), basedir ) );
217        }
218
219        Reporting reporting = model.getReporting();
220
221        if ( reporting != null )
222        {
223            reporting.setOutputDirectory( unalignFromBaseDirectory( reporting.getOutputDirectory(), basedir ) );
224        }
225    }
226
227    public String unalignFromBaseDirectory( String path, File basedir )
228    {
229        if ( basedir == null )
230        {
231            return path;
232        }
233
234        if ( path == null )
235        {
236            return null;
237        }
238
239        path = path.trim();
240
241        String base = basedir.getAbsolutePath();
242        if ( path.startsWith( base ) )
243        {
244            path = chopLeadingFileSeparator( path.substring( base.length() ) );
245        }
246
247        if ( path.length() <= 0 )
248        {
249            path = ".";
250        }
251
252        if ( !new File( path ).isAbsolute() )
253        {
254            path = path.replace( '\\', '/' );
255        }
256
257        return path;
258    }
259
260}
261