View Javadoc

1   package org.apache.maven.ant.tasks.support;
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.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   * Ant filename mapper to remove version info from filename when copying dependencies.
33   *
34   * @author <a href="mailto:hboutemy@apache.org">Herve Boutemy</a>
35   * @version $Id: VersionMapper.java 947173 2010-05-21 21:04:32Z pgier $
36   */
37  public class VersionMapper
38      implements FileNameMapper, Comparator
39  {
40      private List versions;
41  
42      private String to;
43  
44      public String[] mapFileName( String sourceFileName )
45      {
46          String originalFileName = new File( sourceFileName ).getName();
47          for ( Iterator iter = versions.iterator(); iter.hasNext(); )
48          {
49              String version = (String) iter.next();
50              int index = originalFileName.indexOf( version );
51              if ( index >= 0 )
52              {
53                  // remove version in artifactId-version(-classifier).type
54                  String baseFilename = originalFileName.substring( 0, index - 1 );
55                  String extension = originalFileName.substring( index + version.length() );
56                  String path = sourceFileName.substring( 0, sourceFileName.length() - originalFileName.length() );
57                  if ( "flatten".equals( to ) )
58                  {
59                      path = "";
60                  }
61                  return new String[] { path + baseFilename + extension };
62              }
63          }
64          return new String[] { sourceFileName };
65      }
66  
67      /**
68       * Set the versions identifiers that this mapper can remove from filenames. The separator value used is path
69       * separator, as used by dependencies task when setting <code>versionsId</code> property value.
70       */
71      public void setFrom( String from )
72      {
73          String[] split = StringUtils.split( from, File.pathSeparator );
74          // sort, from lengthiest to smallest
75          Arrays.sort( split, this );
76          versions = Arrays.asList( split );
77      }
78  
79      /**
80       * By default, only filename is changed, but if this attribute is set to <code>flatten</code>, directory is removed.
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  }