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