1 | |
package org.apache.maven.archiva.repository.content; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.archiva.common.utils.VersionUtil; |
23 | |
|
24 | |
import java.util.regex.Matcher; |
25 | |
import java.util.regex.Pattern; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class FilenameParser |
33 | |
{ |
34 | |
private String name; |
35 | |
|
36 | |
private String extension; |
37 | |
|
38 | |
private int offset; |
39 | |
|
40 | 0 | private static final Pattern mavenPluginPattern = Pattern.compile( "(maven-.*-plugin)|(.*-maven-plugin)" ); |
41 | |
|
42 | 0 | private static final Pattern extensionPattern = |
43 | |
Pattern.compile( "(\\.tar\\.gz$)|(\\.tar\\.bz2$)|(\\.[\\-a-z0-9]*$)", Pattern.CASE_INSENSITIVE ); |
44 | |
|
45 | 0 | private static final Pattern SNAPSHOT_PATTERN = Pattern.compile( "^([0-9]{8}\\.[0-9]{6}-[0-9]+)(.*)$" ); |
46 | |
|
47 | 0 | private static final Pattern section = Pattern.compile( "([^-]*)" ); |
48 | |
|
49 | |
private Matcher matcher; |
50 | |
|
51 | |
protected FilenameParser( String filename ) |
52 | 0 | { |
53 | 0 | this.name = filename; |
54 | |
|
55 | 0 | Matcher mat = extensionPattern.matcher( name ); |
56 | 0 | if ( mat.find() ) |
57 | |
{ |
58 | 0 | extension = filename.substring( mat.start() + 1 ); |
59 | 0 | name = name.substring( 0, name.length() - extension.length() - 1 ); |
60 | |
} |
61 | |
|
62 | 0 | matcher = section.matcher( name ); |
63 | |
|
64 | 0 | reset(); |
65 | 0 | } |
66 | |
|
67 | |
protected void reset() |
68 | |
{ |
69 | 0 | offset = 0; |
70 | 0 | } |
71 | |
|
72 | |
protected String next() |
73 | |
{ |
74 | |
|
75 | 0 | if ( offset > name.length() ) |
76 | |
{ |
77 | 0 | return null; |
78 | |
} |
79 | |
|
80 | |
|
81 | 0 | if ( matcher.find( offset ) ) |
82 | |
{ |
83 | |
|
84 | 0 | offset = matcher.end() + 1; |
85 | 0 | return matcher.group(); |
86 | |
} |
87 | |
|
88 | |
|
89 | 0 | return null; |
90 | |
} |
91 | |
|
92 | |
protected String expect( String expected ) |
93 | |
{ |
94 | 0 | String value = null; |
95 | |
|
96 | 0 | if ( name.startsWith( expected, offset ) ) |
97 | |
{ |
98 | 0 | value = expected; |
99 | |
} |
100 | 0 | else if ( VersionUtil.isGenericSnapshot( expected ) ) |
101 | |
{ |
102 | 0 | String version = name.substring( offset ); |
103 | |
|
104 | |
|
105 | 0 | int leadingLength = expected.length() - 9; |
106 | 0 | if ( leadingLength > 0 && version.startsWith( expected.substring( 0, leadingLength ) ) && |
107 | |
version.length() > leadingLength ) |
108 | |
{ |
109 | |
|
110 | 0 | Matcher m = SNAPSHOT_PATTERN.matcher( version.substring( leadingLength + 1 ) ); |
111 | 0 | if ( m.matches() ) |
112 | |
{ |
113 | 0 | value = version.substring( 0, leadingLength + 1 ) + m.group( 1 ); |
114 | |
} |
115 | |
} |
116 | |
} |
117 | |
|
118 | 0 | if ( value != null ) |
119 | |
{ |
120 | |
|
121 | 0 | int seperatorOffset = offset + value.length(); |
122 | |
|
123 | |
|
124 | 0 | if ( seperatorOffset >= name.length() ) |
125 | |
{ |
126 | 0 | offset = name.length(); |
127 | 0 | return value; |
128 | |
} |
129 | |
|
130 | |
|
131 | 0 | char seperatorChar = name.charAt( seperatorOffset ); |
132 | 0 | if ( ( seperatorChar == '-' ) || ( seperatorChar == '.' ) ) |
133 | |
{ |
134 | 0 | offset = seperatorOffset + 1; |
135 | 0 | return value; |
136 | |
} |
137 | |
} |
138 | |
|
139 | 0 | return null; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
protected char seperator() |
148 | |
{ |
149 | |
|
150 | 0 | if ( offset >= name.length() ) |
151 | |
{ |
152 | 0 | return 0; |
153 | |
} |
154 | |
|
155 | |
|
156 | 0 | if ( offset <= 0 ) |
157 | |
{ |
158 | 0 | return 0; |
159 | |
} |
160 | |
|
161 | 0 | return name.charAt( offset - 1 ); |
162 | |
} |
163 | |
|
164 | |
protected String getName() |
165 | |
{ |
166 | 0 | return name; |
167 | |
} |
168 | |
|
169 | |
protected String getExtension() |
170 | |
{ |
171 | 0 | return extension; |
172 | |
} |
173 | |
|
174 | |
protected String remaining() |
175 | |
{ |
176 | 0 | if ( offset >= name.length() ) |
177 | |
{ |
178 | 0 | return null; |
179 | |
} |
180 | |
|
181 | 0 | String end = name.substring( offset ); |
182 | 0 | offset = name.length(); |
183 | 0 | return end; |
184 | |
} |
185 | |
|
186 | |
protected String nextNonVersion() |
187 | |
{ |
188 | 0 | boolean done = false; |
189 | |
|
190 | 0 | StringBuffer ver = new StringBuffer(); |
191 | |
|
192 | |
|
193 | 0 | Matcher specialMat = mavenPluginPattern.matcher( name ); |
194 | 0 | if ( specialMat.find() ) |
195 | |
{ |
196 | 0 | ver.append( name.substring( offset, specialMat.end() ) ); |
197 | 0 | offset = specialMat.end() + 1; |
198 | |
} |
199 | |
|
200 | 0 | while ( !done ) |
201 | |
{ |
202 | 0 | int initialOffset = offset; |
203 | 0 | String section = next(); |
204 | 0 | if ( section == null ) |
205 | |
{ |
206 | 0 | done = true; |
207 | |
} |
208 | 0 | else if ( !VersionUtil.isVersion( section ) ) |
209 | |
{ |
210 | 0 | if ( ver.length() > 0 ) |
211 | |
{ |
212 | 0 | ver.append( '-' ); |
213 | |
} |
214 | 0 | ver.append( section ); |
215 | |
} |
216 | |
else |
217 | |
{ |
218 | 0 | offset = initialOffset; |
219 | 0 | done = true; |
220 | |
} |
221 | 0 | } |
222 | |
|
223 | 0 | return ver.toString(); |
224 | |
} |
225 | |
|
226 | |
protected String nextVersion() |
227 | |
{ |
228 | 0 | boolean done = false; |
229 | |
|
230 | 0 | StringBuffer ver = new StringBuffer(); |
231 | |
|
232 | 0 | while ( !done ) |
233 | |
{ |
234 | 0 | int initialOffset = offset; |
235 | 0 | String section = next(); |
236 | 0 | if ( section == null ) |
237 | |
{ |
238 | 0 | done = true; |
239 | |
} |
240 | 0 | else if ( VersionUtil.isVersion( section ) ) |
241 | |
{ |
242 | 0 | if ( ver.length() > 0 ) |
243 | |
{ |
244 | 0 | ver.append( '-' ); |
245 | |
} |
246 | 0 | ver.append( section ); |
247 | |
} |
248 | |
else |
249 | |
{ |
250 | 0 | offset = initialOffset; |
251 | 0 | done = true; |
252 | |
} |
253 | 0 | } |
254 | |
|
255 | 0 | return ver.toString(); |
256 | |
} |
257 | |
|
258 | |
|
259 | |
} |