1 package org.apache.continuum.purge.repository.utils;
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 public class FilenameParser
31 {
32 private String name;
33
34 private String extension;
35
36 private int offset;
37
38 private static final Pattern mavenPluginPattern = Pattern.compile( "(maven-.*-plugin)|(.*-maven-plugin)" );
39
40 private static final Pattern extensionPattern = Pattern.compile(
41 "(\\.tar\\.gz$)|(\\.tar\\.bz2$)|(\\.[\\-a-z0-9]*$)", Pattern.CASE_INSENSITIVE );
42
43 private static final Pattern section = Pattern.compile( "([^-]*)" );
44
45 private final Matcher matcher;
46
47 public FilenameParser( String filename )
48 {
49 this.name = filename;
50
51 Matcher mat = extensionPattern.matcher( name );
52 if ( mat.find() )
53 {
54 extension = filename.substring( mat.start() + 1 );
55 name = name.substring( 0, name.length() - extension.length() - 1 );
56 }
57
58 matcher = section.matcher( name );
59
60 reset();
61 }
62
63 public void reset()
64 {
65 offset = 0;
66 }
67
68 public String next()
69 {
70
71 if ( offset > name.length() )
72 {
73 return null;
74 }
75
76
77 if ( matcher.find( offset ) )
78 {
79
80 offset = matcher.end() + 1;
81 return matcher.group();
82 }
83
84
85 return null;
86 }
87
88 protected String remaining()
89 {
90 if ( offset >= name.length() )
91 {
92 return null;
93 }
94
95 String end = name.substring( offset );
96 offset = name.length();
97 return end;
98 }
99
100 protected String nextNonVersion()
101 {
102 boolean done = false;
103
104 StringBuffer ver = new StringBuffer();
105
106
107 Matcher specialMat = mavenPluginPattern.matcher( name );
108 if ( specialMat.find() )
109 {
110 ver.append( name.substring( offset, specialMat.end() ) );
111 offset = specialMat.end() + 1;
112 }
113
114 while ( !done )
115 {
116 int initialOffset = offset;
117 String section = next();
118 if ( section == null )
119 {
120 done = true;
121 }
122 else if ( !VersionUtil.isVersion( section ) )
123 {
124 if ( ver.length() > 0 )
125 {
126 ver.append( '-' );
127 }
128 ver.append( section );
129 }
130 else
131 {
132 offset = initialOffset;
133 done = true;
134 }
135 }
136
137 return ver.toString();
138 }
139
140 public String getExtension()
141 {
142 return extension;
143 }
144 }