Coverage Report - org.apache.maven.project.path.DefaultPathTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultPathTranslator
64 %
63/98
52 %
31/60
6,833
 
 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.Reporting;
 25  
 import org.apache.maven.model.Resource;
 26  
 
 27  
 import java.io.File;
 28  
 import java.util.ArrayList;
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 
 32  
 /**
 33  
  * Default implementation of {@link PathTranslator}.
 34  
  */
 35  62
 public class DefaultPathTranslator
 36  
     implements PathTranslator
 37  
 {
 38  1
     private static final String[] BASEDIR_EXPRESSIONS = {
 39  
         "${basedir}",
 40  
         "${pom.basedir}",
 41  
         "${project.basedir}"
 42  
     };
 43  
 
 44  
     public void alignToBaseDirectory( Model model, File basedir )
 45  
     {
 46  15
         if ( basedir == null )
 47  
         {
 48  0
             return;
 49  
         }
 50  
 
 51  15
         Build build = model.getBuild();
 52  
 
 53  15
         if ( build != null )
 54  
         {
 55  15
             build.setDirectory( alignToBaseDirectory( build.getDirectory(), basedir ) );
 56  
 
 57  15
             build.setSourceDirectory( alignToBaseDirectory( build.getSourceDirectory(), basedir ) );
 58  
 
 59  15
             build.setTestSourceDirectory( alignToBaseDirectory( build.getTestSourceDirectory(), basedir ) );
 60  
 
 61  
             // TODO: MNG-3731
 62  
 //            build.setScriptSourceDirectory( alignToBaseDirectory( build.getScriptSourceDirectory(), basedir ) );
 63  
 
 64  15
             for ( Iterator i = build.getResources().iterator(); i.hasNext(); )
 65  
             {
 66  15
                 Resource resource = (Resource) i.next();
 67  
 
 68  15
                 resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
 69  15
             }
 70  
 
 71  15
             for ( Iterator i = build.getTestResources().iterator(); i.hasNext(); )
 72  
             {
 73  16
                 Resource resource = (Resource) i.next();
 74  
 
 75  16
                 resource.setDirectory( alignToBaseDirectory( resource.getDirectory(), basedir ) );
 76  16
             }
 77  
 
 78  15
             if ( build.getFilters() != null )
 79  
             {
 80  15
                 List filters = new ArrayList();
 81  15
                 for ( Iterator i = build.getFilters().iterator(); i.hasNext(); )
 82  
                 {
 83  9
                     String filter = (String) i.next();
 84  
 
 85  9
                     filters.add( alignToBaseDirectory( filter, basedir ) );
 86  9
                 }
 87  15
                 build.setFilters( filters );
 88  
             }
 89  
 
 90  15
             build.setOutputDirectory( alignToBaseDirectory( build.getOutputDirectory(), basedir ) );
 91  
 
 92  15
             build.setTestOutputDirectory( alignToBaseDirectory( build.getTestOutputDirectory(), basedir ) );
 93  
         }
 94  
 
 95  15
         Reporting reporting = model.getReporting();
 96  
 
 97  15
         if ( reporting != null )
 98  
         {
 99  15
             reporting.setOutputDirectory( alignToBaseDirectory( reporting.getOutputDirectory(), basedir ) );
 100  
         }
 101  15
     }
 102  
 
 103  
     /**
 104  
      * {@inheritDoc}
 105  
      */
 106  
     public String alignToBaseDirectory( String path, File basedir )
 107  
     {
 108  611
         if ( basedir == null )
 109  
         {
 110  0
             return path;
 111  
         }
 112  
 
 113  611
         if ( path == null )
 114  
         {
 115  0
             return null;
 116  
         }
 117  
 
 118  611
         String s = stripBasedirToken( path );
 119  
 
 120  611
         File file = new File( s );
 121  611
         if ( file.isAbsolute() )
 122  
         {
 123  
             // path was already absolute, just normalize file separator and we're done
 124  453
             s = file.getPath();
 125  
         }
 126  158
         else if ( file.getPath().startsWith( File.separator ) )
 127  
         {
 128  
             // drive-relative Windows path, don't align with project directory but with drive root
 129  0
             s = file.getAbsolutePath();
 130  
         }
 131  
         else
 132  
         {
 133  
             // an ordinary relative path, align with project directory
 134  158
             s = new File( new File( basedir, s ).toURI().normalize() ).getAbsolutePath();
 135  
         }
 136  
 
 137  611
         return s;
 138  
     }
 139  
 
 140  
     private String stripBasedirToken( String s )
 141  
     {
 142  611
         if ( s != null )
 143  
         {
 144  611
             String basedirExpr = null;
 145  2438
             for ( int i = 0; i < BASEDIR_EXPRESSIONS.length; i++ )
 146  
             {
 147  1829
                 basedirExpr = BASEDIR_EXPRESSIONS[i];
 148  1829
                 if ( s.startsWith( basedirExpr ) )
 149  
                 {
 150  2
                     break;
 151  
                 }
 152  
                 else
 153  
                 {
 154  1827
                     basedirExpr = null;
 155  
                 }
 156  
             }
 157  
 
 158  611
             if ( basedirExpr != null )
 159  
             {
 160  2
                 if ( s.length() > basedirExpr.length() )
 161  
                 {
 162  
                     // Take out basedir expression and the leading slash
 163  1
                     s = chopLeadingFileSeparator( s.substring( basedirExpr.length() ) );
 164  
                 }
 165  
                 else
 166  
                 {
 167  1
                     s = ".";
 168  
                 }
 169  
             }
 170  
         }
 171  
 
 172  611
         return s;
 173  
     }
 174  
 
 175  
     /**
 176  
      * Removes the leading directory separator from the specified filesystem path (if any). For platform-independent
 177  
      * behavior, this method accepts both the forward slash and the backward slash as separator.
 178  
      *
 179  
      * @param path The filesystem path, may be <code>null</code>.
 180  
      * @return The altered filesystem path or <code>null</code> if the input path was <code>null</code>.
 181  
      */
 182  
     private String chopLeadingFileSeparator( String path )
 183  
     {
 184  108
         if ( path != null )
 185  
         {
 186  108
             if ( path.startsWith( "/" ) || path.startsWith( "\\" ) )
 187  
             {
 188  108
                 path = path.substring( 1 );
 189  
             }
 190  
         }
 191  108
         return path;
 192  
     }
 193  
 
 194  
     /**
 195  
      * {@inheritDoc}
 196  
      */
 197  
     public void unalignFromBaseDirectory( Model model, File basedir )
 198  
     {
 199  0
         if ( basedir == null )
 200  
         {
 201  0
             return;
 202  
         }
 203  
 
 204  0
         Build build = model.getBuild();
 205  
 
 206  0
         if ( build != null )
 207  
         {
 208  0
             build.setDirectory( unalignFromBaseDirectory( build.getDirectory(), basedir ) );
 209  
 
 210  0
             build.setSourceDirectory( unalignFromBaseDirectory( build.getSourceDirectory(), basedir ) );
 211  
 
 212  0
             build.setTestSourceDirectory( unalignFromBaseDirectory( build.getTestSourceDirectory(), basedir ) );
 213  
 
 214  0
             build.setScriptSourceDirectory( unalignFromBaseDirectory( build.getScriptSourceDirectory(), basedir ) );
 215  
 
 216  0
             for ( Iterator i = build.getResources().iterator(); i.hasNext(); )
 217  
             {
 218  0
                 Resource resource = (Resource) i.next();
 219  
 
 220  0
                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
 221  0
             }
 222  
 
 223  0
             for ( Iterator i = build.getTestResources().iterator(); i.hasNext(); )
 224  
             {
 225  0
                 Resource resource = (Resource) i.next();
 226  
 
 227  0
                 resource.setDirectory( unalignFromBaseDirectory( resource.getDirectory(), basedir ) );
 228  0
             }
 229  
 
 230  0
             if ( build.getFilters() != null )
 231  
             {
 232  0
                 List filters = new ArrayList();
 233  0
                 for ( Iterator i = build.getFilters().iterator(); i.hasNext(); )
 234  
                 {
 235  0
                     String filter = (String) i.next();
 236  
 
 237  0
                     filters.add( unalignFromBaseDirectory( filter, basedir ) );
 238  0
                 }
 239  0
                 build.setFilters( filters );
 240  
             }
 241  
 
 242  0
             build.setOutputDirectory( unalignFromBaseDirectory( build.getOutputDirectory(), basedir ) );
 243  
 
 244  0
             build.setTestOutputDirectory( unalignFromBaseDirectory( build.getTestOutputDirectory(), basedir ) );
 245  
         }
 246  
 
 247  0
         Reporting reporting = model.getReporting();
 248  
 
 249  0
         if ( reporting != null )
 250  
         {
 251  0
             reporting.setOutputDirectory( unalignFromBaseDirectory( reporting.getOutputDirectory(), basedir ) );
 252  
         }
 253  0
     }
 254  
 
 255  
     /**
 256  
      * {@inheritDoc}
 257  
      */
 258  
     public String unalignFromBaseDirectory( String path, File basedir )
 259  
     {
 260  147
         if ( basedir == null )
 261  
         {
 262  0
             return path;
 263  
         }
 264  
 
 265  147
         if ( path == null )
 266  
         {
 267  0
             return null;
 268  
         }
 269  
 
 270  147
         path = path.trim();
 271  
 
 272  147
         String base = basedir.getAbsolutePath();
 273  147
         if ( path.startsWith( base ) )
 274  
         {
 275  107
             path = chopLeadingFileSeparator( path.substring( base.length() ) );
 276  
         }
 277  
 
 278  147
         if ( !new File( path ).isAbsolute() )
 279  
         {
 280  147
             path = path.replace( '\\', '/' );
 281  
         }
 282  
 
 283  147
         return path;
 284  
     }
 285  
 
 286  
 }
 287