Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
VersionMapper |
|
| 2.25;2.25 |
1 | package org.apache.maven.artifact.ant; | |
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.Arrays; | |
24 | import java.util.Comparator; | |
25 | import java.util.List; | |
26 | ||
27 | import org.apache.tools.ant.util.FileNameMapper; | |
28 | import org.codehaus.plexus.util.StringUtils; | |
29 | ||
30 | /** | |
31 | * Ant filename mapper to remove version info from filename when copying dependencies. | |
32 | * | |
33 | * @author <a href="mailto:hboutemy@apache.org">Herve Boutemy</a> | |
34 | * @version $Id: VersionMapper.java 928113 2010-03-27 00:33:45Z hboutemy $ | |
35 | */ | |
36 | 0 | public class VersionMapper implements FileNameMapper, Comparator<String> |
37 | { | |
38 | private List<String> versions; | |
39 | ||
40 | private String to; | |
41 | ||
42 | public String[] mapFileName( String sourceFileName ) | |
43 | { | |
44 | 0 | String originalFileName = new java.io.File( sourceFileName ).getName(); |
45 | 0 | for ( String version : versions ) |
46 | { | |
47 | 0 | int index = originalFileName.indexOf( version ); |
48 | 0 | if ( index >= 0 ) |
49 | { | |
50 | // remove version in artifactId-version(-classifier).type | |
51 | 0 | String baseFilename = originalFileName.substring( 0, index - 1 ); |
52 | 0 | String extension = originalFileName.substring( index + version.length() ); |
53 | 0 | String path = sourceFileName.substring( 0, sourceFileName.length() - originalFileName.length() ); |
54 | 0 | if ( "flatten".equals( to ) ) |
55 | { | |
56 | 0 | path = ""; |
57 | } | |
58 | 0 | return new String[] { path + baseFilename + extension }; |
59 | } | |
60 | 0 | } |
61 | 0 | return new String[] { sourceFileName }; |
62 | } | |
63 | ||
64 | /** | |
65 | * Set the versions identifiers that this mapper can remove from filenames. The separator value used is path | |
66 | * separator, as used by dependencies task when setting <code>versionsId</code> property value. | |
67 | */ | |
68 | public void setFrom( String from ) | |
69 | { | |
70 | 0 | String[] split = StringUtils.split( from, File.pathSeparator ); |
71 | // sort, from lengthiest to smallest | |
72 | 0 | Arrays.sort( split, this ); |
73 | 0 | versions = Arrays.asList( split ); |
74 | 0 | } |
75 | ||
76 | /** | |
77 | * By default, only filename is changed, but if this attribute is set to <code>flatten</code>, directory is | |
78 | * removed. | |
79 | */ | |
80 | public void setTo( String to ) | |
81 | { | |
82 | 0 | this.to = to; |
83 | 0 | } |
84 | ||
85 | public int compare( String s1, String s2 ) | |
86 | { | |
87 | 0 | int lengthDiff = s2.length() - s1.length(); |
88 | 0 | return ( lengthDiff != 0 ) ? lengthDiff : s1.compareTo( s2 ); |
89 | } | |
90 | } |