1 package org.apache.maven.index.artifact;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.codehaus.plexus.component.annotations.Component;
26
27
28
29
30
31
32
33 @Component( role = GavCalculator.class, hint = "maven1" )
34 public class M1GavCalculator
35 implements GavCalculator
36 {
37
38 private static final Pattern pat1 = Pattern.compile( "^([^0-9]+)-([0-9].+)\\.([^0-9]+)(\\.md5|\\.sha1){0,1}$" );
39
40 private static final Pattern pat2 = Pattern.compile( "^([a-z0-9-_]+)-([0-9-].+)\\.([^0-9]+)(\\.md5|\\.sha1){0,1}$" );
41
42 public Gav pathToGav( String str )
43 {
44 try
45 {
46 String s = str.startsWith( "/" ) ? str.substring( 1 ) : str;
47
48 int n1 = s.lastIndexOf( '/' );
49
50 if ( n1 == -1 )
51 {
52 return null;
53 }
54
55 int n2 = s.lastIndexOf( '/', n1 - 1 );
56
57 if ( n2 == -1 )
58 {
59 return null;
60 }
61
62 String g = s.substring( 0, n2 ).replace( '/', '.' );
63 String middle = s.substring( n2 + 1, n1 );
64 String n = s.substring( n1 + 1 );
65
66 String classifier = null;
67 if ( "java-sources".equals( middle ) )
68 {
69 classifier = "sources";
70 }
71 else if ( "javadocs".equals( middle ) )
72 {
73 classifier = "javadoc";
74 }
75 else if ( "ejbs".equals( middle )
76 && ( n.endsWith( "client.jar" ) || n.endsWith( "client.jar.sha1" ) || n.endsWith( "client.jar.md5" ) ) )
77 {
78 classifier = "client";
79 }
80
81 boolean checksum = false;
82 Gav.HashType checksumType = null;
83 if ( s.endsWith( ".md5" ) )
84 {
85 checksum = true;
86 checksumType = Gav.HashType.md5;
87 s = s.substring( 0, s.length() - 4 );
88 }
89 else if ( s.endsWith( ".sha1" ) )
90 {
91 checksum = true;
92 checksumType = Gav.HashType.sha1;
93 s = s.substring( 0, s.length() - 5 );
94 }
95
96 if ( s.endsWith( "maven-metadata.xml" ) )
97 {
98 return null;
99 }
100
101 String ext = s.substring( s.lastIndexOf( '.' ) + 1 );
102
103 Matcher m = pat1.matcher( n );
104 if ( m.matches() )
105 {
106 String a = m.group( 1 );
107 String version = m.group( 2 );
108 if ( classifier != null )
109 {
110 version = version.substring( 0, version.length() - ( classifier.length() + 1 ) );
111 }
112
113 return new Gav( g, a, version, classifier, ext, null, null, n, checksum, checksumType, false,
114 null );
115 }
116 else
117 {
118 m = pat2.matcher( n );
119 if ( m.matches() )
120 {
121 String a = m.group( 1 );
122 String version = m.group( 2 );
123 if ( classifier != null )
124 {
125 version = version.substring( 0, version.length() - ( classifier.length() + 1 ) );
126 }
127
128 return new Gav( g, a, version, classifier, ext, null, null, n, checksum, checksumType,
129 false, null );
130 }
131 else
132 {
133 return null;
134 }
135 }
136 }
137 catch ( StringIndexOutOfBoundsException e )
138 {
139 return null;
140 }
141 catch ( IndexOutOfBoundsException e )
142 {
143 return null;
144 }
145 }
146
147
148
149
150
151
152
153 public String gavToPath( Gav gav )
154 {
155 StringBuilder path = new StringBuilder( "/" );
156
157 path.append( gav.getGroupId() );
158
159 path.append( "/" );
160
161 if ( gav.getClassifier() == null )
162 {
163 path.append( gav.getExtension() );
164
165 path.append( "s" );
166 }
167 else
168 {
169 if ( gav.getClassifier().startsWith( "source" ) )
170 {
171 path.append( "java-source" );
172 }
173 else if ( gav.getClassifier().startsWith( "client" ) )
174 {
175 path.append( "ejb" );
176 }
177 else
178 {
179 path.append( gav.getClassifier() );
180 }
181 path.append( "s" );
182 }
183
184 path.append( "/" );
185
186 path.append( gav.getArtifactId() );
187
188 path.append( "-" );
189
190 path.append( gav.getVersion() );
191
192 if ( gav.getClassifier() != null )
193 {
194 path.append( "-" );
195
196 path.append( gav.getClassifier() );
197 }
198
199 path.append( "." );
200
201 path.append( gav.getExtension() );
202
203 if ( gav.isHash() )
204 {
205 path.append( "." );
206
207 path.append( gav.getHashType().toString() );
208 }
209
210 return path.toString();
211 }
212
213 }