1 | |
package org.apache.maven.plugin.reactor; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.LinkedList; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | class RelativePather { |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
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 | |
|
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 | |
|
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 | |
} |