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