View Javadoc

1   /*
2    * Copyright (C) 2000 - 2008 Grid Systems, S.A.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License, version 2, as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11   * GNU General Public License for more details.
12   *
13   * You should have received a copy of the GNU General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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   * VersionParser type.
27   *
28   * @author Rodrigo Ruiz
29   */
30  public class VersionParser {
31  
32    /**
33     * <p>Patterns used for recognising versions.</p>
34     *
35     * <p>It may look like some of these patterns can be merged. However, the
36     * parser requires these patterns not to have optional or nested groups.</p>
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     * Suffixes used for each of the version number properties.
47     */
48    private static final String[] SUFFIXES = {
49      null, ".major", ".minor", ".release", ".build"
50    };
51  
52    /**
53     * Formatter used for creating the "build" version number.
54     */
55    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMdd");
56  
57    /**
58     * Parses a tag version string.
59     *
60     * @param props  Destination of all obtained information
61     * @param s      Tag to parse
62     * @param prefix Prefix used for generated properties
63     * @return <tt>true</tt> if s could be parsed
64     */
65    public boolean parse(Properties props, String s, String prefix) {
66      if (props == null || s == null) {
67        return false;
68      } else {
69        // Determine the proper prefix
70        String fPrefix = (prefix == null) ? "tag.version" : prefix.trim();
71  
72        // This array will hold the different version numbers
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  }