1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.maven.furabuild;
18
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21 import java.util.Properties;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25
26
27
28
29
30 public class VersionParser {
31
32
33
34
35
36
37
38 private static final Pattern[] PATTERNS = {
39 Pattern.compile("V_(\\d+)_(\\d+)_(\\d+)"),
40 Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)"),
41 Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)"),
42 Pattern.compile("(\\d+)\\.(\\d+)"),
43 };
44
45
46
47
48 private static final String[] SUFFIXES = {
49 null, ".major", ".minor", ".release", ".build"
50 };
51
52
53
54
55 private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMdd");
56
57
58
59
60
61
62
63
64
65 public boolean parse(Properties props, String s, String prefix) {
66 if (props == null || s == null) {
67 return false;
68 } else {
69
70 String fPrefix = (prefix == null) ? "tag.version" : prefix.trim();
71
72
73 String[] tokens = { null, null, null, null, SDF.format(new Date()) };
74
75 for (int i = 0; i < PATTERNS.length; i++) {
76 Matcher matcher = PATTERNS[i].matcher(s);
77 if (matcher.matches()) {
78 int count = matcher.groupCount();
79 for (int j = 1; j <= count; j++) {
80 tokens[j] = matcher.group(j);
81 }
82 break;
83 }
84 }
85
86 if (tokens[1] != null) {
87 StringBuffer sb = new StringBuffer();
88 for (int i = 1; i < tokens.length && tokens[i] != null; i++) {
89 props.setProperty(fPrefix + SUFFIXES[i], tokens[i]);
90 sb.append('.');
91 sb.append(tokens[i]);
92 }
93 props.setProperty(fPrefix, sb.toString().substring(1));
94 }
95 return (tokens[1] != null);
96 }
97 }
98
99 }