1 package org.apache.maven.ant.tasks.support;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.tools.ant.util.FileNameMapper;
23 import org.codehaus.plexus.util.StringUtils;
24
25 import java.io.File;
26 import java.util.Arrays;
27 import java.util.Comparator;
28 import java.util.List;
29
30
31
32
33
34
35 public class VersionMapper
36 implements FileNameMapper, Comparator<String>
37 {
38 private List<String> versions;
39
40 private String to;
41
42
43 @Override
44 public String[] mapFileName( String sourceFileName )
45 {
46 String originalFileName = new File( sourceFileName ).getName();
47 for ( String version : versions )
48 {
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
71 @Override
72 public void setFrom( String from )
73 {
74 String[] split = StringUtils.split( from, File.pathSeparator );
75
76 Arrays.sort( split, this );
77 versions = Arrays.asList( split );
78 }
79
80
81
82
83
84 @Override
85 public void setTo( String to )
86 {
87 this.to = to;
88 }
89
90
91 @Override
92 public int compare( String s1, String s2 )
93 {
94 int lengthDiff = s2.length() - s1.length();
95 return ( lengthDiff != 0 ) ? lengthDiff : s1.compareTo( s2 );
96 }
97 }