1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.javadpkg.control.impl;
20
21 import net.sourceforge.javadpkg.Context;
22 import net.sourceforge.javadpkg.ParseException;
23 import net.sourceforge.javadpkg.control.PackageVersion;
24 import net.sourceforge.javadpkg.control.PackageVersionParser;
25
26
27
28
29
30
31
32
33
34
35 public class PackageVersionParserImpl implements PackageVersionParser {
36
37
38
39 private static final String REGEXP_UPSTREAM_VERSION = "([0-9]){1}([A-Za-z0-9.+:~])*";
40
41 private static final String REGEXP_DEBIAN_REVISION = "([A-Za-z0-9.+~-])+";
42
43
44
45
46
47
48
49
50 public PackageVersionParserImpl() {
51 super();
52 }
53
54
55 @Override
56 public PackageVersion parsePackageVersion(String value, Context context) throws ParseException {
57 PackageVersion packageVersion;
58 String rest, epoch = null, upstreamVersion, debianRevision = null;
59 int index, intValue;
60
61 if (value == null)
62 throw new IllegalArgumentException("Argument value is null.");
63 if (context == null)
64 throw new IllegalArgumentException("Argument context is null.");
65
66 rest = value;
67
68
69 index = rest.indexOf(':');
70 if (index != -1) {
71 epoch = rest.substring(0, index);
72 rest = rest.substring(index + 1);
73 if (epoch.startsWith("0") && !epoch.equals("0")) {
74 throw new ParseException("Version |" + value + "| contains an invalid epoch |" + epoch
75 + "|: An epoch can't start with 0 except it is 0.");
76 }
77 try {
78 intValue = Integer.parseInt(epoch);
79 } catch (NumberFormatException e) {
80 throw new ParseException(
81 "Version |" + value + "| contains an invalid epoch |" + epoch + "|: The epoch is not a valid integer.");
82 }
83 if (intValue < 0)
84 throw new ParseException(
85 "Version |" + value + "| contains an invalid epoch |" + epoch + "|: The epoch is negative.");
86 }
87
88
89 index = rest.indexOf('-');
90 if (index == -1) {
91 upstreamVersion = rest;
92 rest = "";
93 } else {
94 upstreamVersion = rest.substring(0, index);
95 rest = rest.substring(index + 1);
96 }
97 if (!upstreamVersion.matches(REGEXP_UPSTREAM_VERSION))
98 throw new ParseException("Version |" + value + "| contains an invalid upstream version |" + upstreamVersion + "|");
99
100
101 if (!rest.isEmpty()) {
102 debianRevision = rest;
103 if (!debianRevision.matches(REGEXP_DEBIAN_REVISION))
104 throw new ParseException(
105 "Version |" + value + "| contains an invalid Debian revision |" + debianRevision + "|");
106 }
107
108 packageVersion = new PackageVersionImpl(epoch, upstreamVersion, debianRevision);
109 return packageVersion;
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 private class PackageVersionImpl implements PackageVersion {
130
131
132
133 private String epoch;
134
135 private String upstreamVersion;
136
137 private String debianRevision;
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 public PackageVersionImpl(String epoch, String upstreamVersion, String debianRevision) {
153 super();
154
155 this.epoch = epoch;
156 this.upstreamVersion = upstreamVersion;
157 this.debianRevision = debianRevision;
158 }
159
160
161 @Override
162 public String getEpoch() {
163 return this.epoch;
164 }
165
166
167 @Override
168 public String getUpstreamVersion() {
169 return this.upstreamVersion;
170 }
171
172
173 @Override
174 public String getDebianRevision() {
175 return this.debianRevision;
176 }
177
178
179 @Override
180 public String getText() {
181 StringBuilder sb;
182
183
184 sb = new StringBuilder();
185 if (this.epoch != null) {
186 sb.append(this.epoch);
187 sb.append(':');
188 }
189 sb.append(this.upstreamVersion);
190 if (this.debianRevision != null) {
191 sb.append('-');
192 sb.append(this.debianRevision);
193 }
194 return sb.toString();
195 }
196
197
198 }
199
200
201 }