source: src/main/java/weka/core/RevisionUtils.java @ 9

Last change on this file since 9 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 4.2 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * RevisionUtils.java
19 * Copyright (C) 2008 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.core;
23
24/**
25 * Contains utility functions for handling revisions.
26 *
27 * @author  fracpete (fracpete at waikato dot ac dot nz)
28 * @version $Revision: 5953 $
29 */
30public class RevisionUtils {
31 
32  /**
33   * Enumeration of source control types.
34   *
35   * @author  fracpete (fracpete at waikato dot ac dot nz)
36   * @version $Revision: 5953 $
37   */
38  public enum Type {
39    /** unknown source control revision. */
40    UNKNOWN,
41    /** CVS. */
42    CVS,
43    /** Subversion. */
44    SUBVERSION;
45  }
46 
47  /**
48   * Extracts the revision string returned by the RevisionHandler.
49   *
50   * @param handler     the RevisionHandler to get the revision for
51   * @return            the actual revision string
52   */
53  public static String extract(RevisionHandler handler) {
54    return extract(handler.getRevision());
55  }
56 
57  /**
58   * Extracts the revision string.
59   *
60   * @param s           the string to get the revision string from
61   * @return            the actual revision string
62   */
63  public static String extract(String s) {
64    String      result;
65   
66    result = s;
67    result = result.replaceAll("\\$Revision:", "");
68    result = result.replaceAll("\\$", "");
69    result = result.replaceAll(" ", "");
70   
71    return result;
72  }
73 
74  /**
75   * Determines the type of a (sanitized) revision string returned by the
76   * RevisionHandler.
77   *
78   * @param handler     the RevisionHandler to determine the type for
79   * @return            the type, UNKNOWN if it cannot be determined
80   */
81  public static Type getType(RevisionHandler handler) {
82    return getType(extract(handler));
83  }
84 
85  /**
86   * Determines the type of a (sanitized) revision string. Use extract(String)
87   * method to extract the revision first before calling this method.
88   *
89   * @param revision    the revision to get the type for
90   * @return            the type, UNKNOWN if it cannot be determined
91   * @see #extract(String)
92   */
93  public static Type getType(String revision) {
94    Type        result;
95    String[]    parts;
96    int         i;
97   
98    result = Type.UNKNOWN;
99   
100    // subversion?
101    try {
102      Integer.parseInt(revision);
103      result = Type.SUBVERSION;
104    }
105    catch (Exception e) {
106      // ignored
107    }
108   
109    // CVS?
110    if (result == Type.UNKNOWN) {
111      try {
112        // must contain at least ONE dot
113        if (revision.indexOf('.') == -1)
114          throw new Exception("invalid CVS revision - not dots!");
115       
116        parts = revision.split("\\.");
117
118        // must consist of at least TWO parts/integers
119        if (parts.length < 2)
120          throw new Exception("invalid CVS revision - not enough parts separated by dots!");
121
122        // try parsing parts of revision string - must be ALL integers
123        for (i = 0; i < parts.length; i++)
124          Integer.parseInt(parts[i]);
125       
126        result = Type.CVS;
127      }
128      catch (Exception e) {
129        // ignored
130      }
131    }
132   
133    return result;
134  }
135 
136  /**
137   * For testing only. The first parameter must be a classname of a
138   * class implementing the weka.core.RevisionHandler interface.
139   *
140   * @param args        the commandline arguments
141   * @throws Exception  if something goes wrong
142   */
143  public static void main(String[] args) throws Exception {
144    if (args.length != 1) {
145      System.err.println("\nUsage: " + RevisionUtils.class.getName() + " <classname>\n");
146      System.exit(1);
147    }
148   
149    RevisionHandler handler = (RevisionHandler) Class.forName(args[0]).newInstance();
150    System.out.println("Type: " + getType(handler));
151    System.out.println("Revision: " + extract(handler));
152  }
153}
Note: See TracBrowser for help on using the repository browser.