View Javadoc
1   package org.apache.maven.resolver.internal.ant.tasks;
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.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.TreeSet;
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  import org.apache.tools.ant.BuildException;
32  import org.eclipse.aether.artifact.Artifact;
33  
34  /**
35   */
36  class Layout
37  {
38  
39      public static final String GID = "{groupId}";
40  
41      public static final String GID_DIRS = "{groupIdDirs}";
42  
43      public static final String AID = "{artifactId}";
44  
45      public static final String VER = "{version}";
46  
47      public static final String BVER = "{baseVersion}";
48  
49      public static final String EXT = "{extension}";
50  
51      public static final String CLS = "{classifier}";
52  
53      private String[] tokens;
54  
55      Layout( String layout )
56          throws BuildException
57      {
58          Collection<String> valid = new HashSet<String>( Arrays.asList( GID, GID_DIRS, AID, VER, BVER, EXT, CLS ) );
59          List<String> tokens = new ArrayList<String>();
60          Matcher m = Pattern.compile( "(\\{[^}]*\\})|([^{]+)" ).matcher( layout );
61          while ( m.find() )
62          {
63              if ( m.group( 1 ) != null && !valid.contains( m.group( 1 ) ) )
64              {
65                  throw new BuildException( "Invalid variable '" + m.group() + "' in layout, supported variables are "
66                      + new TreeSet<String>( valid ) );
67              }
68              tokens.add( m.group() );
69          }
70          this.tokens = tokens.toArray( new String[tokens.size()] );
71      }
72  
73      public String getPath( Artifact artifact )
74      {
75          StringBuilder buffer = new StringBuilder( 128 );
76  
77          for ( int i = 0; i < tokens.length; i++ )
78          {
79              String token = tokens[i];
80              if ( GID.equals( token ) )
81              {
82                  buffer.append( artifact.getGroupId() );
83              }
84              else if ( GID_DIRS.equals( token ) )
85              {
86                  buffer.append( artifact.getGroupId().replace( '.', '/' ) );
87              }
88              else if ( AID.equals( token ) )
89              {
90                  buffer.append( artifact.getArtifactId() );
91              }
92              else if ( VER.equals( token ) )
93              {
94                  buffer.append( artifact.getVersion() );
95              }
96              else if ( BVER.equals( token ) )
97              {
98                  buffer.append( artifact.getBaseVersion() );
99              }
100             else if ( CLS.equals( token ) )
101             {
102                 if ( artifact.getClassifier().length() <= 0 )
103                 {
104                     if ( i > 0 )
105                     {
106                         String lt = tokens[i - 1];
107                         if ( lt.length() > 0 && "-_".indexOf( lt.charAt( lt.length() - 1 ) ) >= 0 )
108                         {
109                             buffer.setLength( buffer.length() - 1 );
110                         }
111                     }
112                 }
113                 else
114                 {
115                     buffer.append( artifact.getClassifier() );
116                 }
117             }
118             else if ( EXT.equals( token ) )
119             {
120                 buffer.append( artifact.getExtension() );
121             }
122             else
123             {
124                 buffer.append( token );
125             }
126         }
127 
128         return buffer.toString();
129     }
130 
131 }