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