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