1 package org.apache.maven.artifact.ant;
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.Arrays;
24 import java.util.Comparator;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import org.apache.tools.ant.util.FileNameMapper;
29 import org.codehaus.plexus.util.StringUtils;
30
31
32
33
34
35
36
37 public class VersionMapper implements FileNameMapper, Comparator
38 {
39 private List versions;
40
41 private String to;
42
43 public String[] mapFileName( String sourceFileName )
44 {
45 String originalFileName = new java.io.File( sourceFileName ).getName();
46 for ( Iterator iter = versions.iterator(); iter.hasNext(); )
47 {
48 String version = (String) iter.next();
49 int index = originalFileName.indexOf( version );
50 if ( index >= 0 )
51 {
52
53 String baseFilename = originalFileName.substring( 0, index - 1 );
54 String extension = originalFileName.substring( index + version.length() );
55 String path = sourceFileName.substring( 0, sourceFileName.length() - originalFileName.length() );
56 if ( "flatten".equals( to ) )
57 {
58 path = "";
59 }
60 return new String[] { path + baseFilename + extension };
61 }
62 }
63 return new String[] { sourceFileName };
64 }
65
66
67
68
69
70 public void setFrom( String from )
71 {
72 String[] split = StringUtils.split( from, File.pathSeparator );
73
74 Arrays.sort( split, this );
75 versions = Arrays.asList( split );
76 }
77
78
79
80
81
82 public void setTo( String to )
83 {
84 this.to = to;
85 }
86
87 public int compare( Object o1, Object o2 )
88 {
89 String s1 = (String) o1;
90 String s2 = (String) o2;
91 int lengthDiff = s2.length() - s1.length();
92 return ( lengthDiff != 0 ) ? lengthDiff : s1.compareTo( s2 );
93 }
94 }