1 package org.apache.maven.resolver.internal.ant.tasks;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }