Coverage Report - org.apache.maven.plugin.reactor.RelativePather
 
Classes in this File Line Coverage Branch Coverage Complexity
RelativePather
95%
39/41
96%
23/24
3.333
RelativePather$DifferentRootsException
25%
2/8
N/A
3.333
 
 1  
 package org.apache.maven.plugin.reactor;
 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 java.io.File;
 23  
 import java.util.Iterator;
 24  
 import java.util.LinkedList;
 25  
 
 26  
 /**
 27  
  * Calculates relative paths
 28  
  * @author <a href="mailto:dfabulich@apache.org">Dan Fabulich</a>
 29  
  *
 30  
  */
 31  0
 class RelativePather {
 32  
     /**
 33  
      * Calculates a relative path
 34  
      * @param context the "current" context directory
 35  
      * @param dest the directory to be described by a relative path
 36  
      * @return a relative path from the context directory to the dest directory
 37  
      */
 38  
     public static String getRelativePath(File context, File dest) {
 39  20
         LinkedList contextChunks = getPathChunks(context);
 40  20
         LinkedList destChunks = getPathChunks(dest);
 41  20
         if (!contextChunks.getFirst().equals(destChunks.getFirst())) throw new DifferentRootsException("Roots differ");
 42  19
         int count = 0;
 43  19
         Iterator contextChunker = contextChunks.iterator();
 44  19
         Iterator destChunker = destChunks.iterator();
 45  19
         String contextChunk = (String) contextChunker.next();
 46  19
         String destChunk = (String) destChunker.next();
 47  19
         boolean pathsDiffer = false;
 48  
         while (true) {
 49  82
             count++;
 50  82
             if (!contextChunker.hasNext()) break;
 51  66
             if (!destChunker.hasNext()) break;
 52  65
             contextChunk = (String) contextChunker.next();
 53  65
             destChunk = (String) destChunker.next();
 54  65
             if (!contextChunk.equals(destChunk)) {
 55  2
                 pathsDiffer = true;
 56  2
                 break;
 57  
             }
 58  
         }
 59  
         
 60  
         // the paths agree for the first N chunks
 61  
         
 62  19
         StringBuffer relativePath = new StringBuffer();
 63  
         
 64  19
         if (count < contextChunks.size()) {
 65  3
             int dotDotCount = contextChunks.size() - count;
 66  7
             for (int i = 0; i < dotDotCount; i++) {
 67  4
                 relativePath.append("..");
 68  
                 // omit trailing slash
 69  4
                 if (i < dotDotCount -1) {
 70  1
                     relativePath.append(File.separatorChar);
 71  
                 }
 72  
             }
 73  
         }
 74  19
         if (pathsDiffer) {
 75  2
             if (relativePath.length() > 0) {
 76  2
                 relativePath.append(File.separatorChar);
 77  
             }
 78  2
             relativePath.append(destChunk);
 79  
         }
 80  44
         while (destChunker.hasNext()) {
 81  25
             if (relativePath.length() > 0) {
 82  12
                 relativePath.append(File.separatorChar);
 83  
             }
 84  25
             relativePath.append(destChunker.next());
 85  
         }
 86  
         
 87  19
         return relativePath.toString();
 88  
     }
 89  
     
 90  
     private static LinkedList getPathChunks(File f) {
 91  40
         LinkedList l = new LinkedList();
 92  197
         while (f.getParentFile() != null) {
 93  157
             l.addFirst(f.getName());
 94  157
             f = f.getParentFile();
 95  
         }
 96  40
         l.addFirst(f.getAbsolutePath());
 97  40
         return l;
 98  
     }
 99  
     
 100  0
     static class DifferentRootsException extends RuntimeException {
 101  
         private static final long serialVersionUID = 1L;
 102  
 
 103  
         public DifferentRootsException() {
 104  0
             super();
 105  0
         }
 106  
 
 107  
         public DifferentRootsException(String message, Throwable cause) {
 108  0
             super(message, cause);
 109  0
         }
 110  
 
 111  
         public DifferentRootsException(String message) {
 112  1
             super(message);
 113  1
         }
 114  
 
 115  
         public DifferentRootsException(Throwable cause) {
 116  0
             super(cause);
 117  0
         }
 118  
         
 119  
     }
 120  
 }