1 package xdoclet.sdk.xgg;
2
3 import java.util.Arrays;
4 import java.util.HashMap;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7
8 /***
9 * Simple pojo (plain old java object) class that serves as a base class for
10 * XGG generated beans.
11 * This class contains logic to check that :
12 * - certain methods are supported for a certain version
13 * - value set matches with regular expression
14 *
15 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Hellesøy</a>
16 * @author <a href="mailto:letiemble at users.sourceforge.net">Laurent Etiemble</a>
17 * @created 7 mai 2003
18 * @version $Revision: 1.6 $
19 */
20 public class XGGPojo {
21 private static HashMap _patterns = new HashMap();
22 private static String _version;
23
24
25 /***
26 * Checks that the value match the pattern
27 *
28 * @param value Value to check
29 * @param pattern Regular expression pattern
30 * @exception IllegalStateException If value doesn't match the pattern
31 */
32 protected final void checkValue(String value, String pattern) throws IllegalStateException {
33 if( value != null ) {
34 // Performs some cache optimization
35 Pattern p = (Pattern) _patterns.get(pattern);
36 if (p == null) {
37 p = Pattern.compile(pattern);
38 _patterns.put(pattern, p);
39 }
40
41 // Do some regexp validation
42 Matcher m = p.matcher(value);
43 if (!m.find()) {
44 throw new IllegalStateException("The value doesn't match");
45 }
46 }
47 }
48
49
50 /***
51 * Checks that the currently (static) version is among the
52 * supportedVersions.
53 *
54 * @param supportedVersions supported versions
55 * @throws IllegalStateException if supportedVersions is not among the currently
56 * set version.
57 * @see #setVersion
58 */
59 protected final void checkVersion(String[] supportedVersions) throws IllegalStateException {
60 if (_version == null) {
61 throw new IllegalStateException("The version isn't set. I need to know that.");
62 }
63
64 boolean versionOk = false;
65 for (int i = supportedVersions.length - 1; i >= 0; i--) {
66 if (_version.equals(supportedVersions[i])) {
67 versionOk = true;
68 break;
69 }
70 }
71
72 if (!versionOk) {
73 throw new IllegalStateException("This is only supported in versions " +
74 Arrays.asList(supportedVersions) + ". The currently active version is " +
75 _version + ". This is a bug in the plugin.");
76 }
77 }
78
79
80 /***
81 * Sets the version of the XGG POJO
82 *
83 * @param version The new version
84 */
85 public final static void setVersion(String version) {
86 _version = version;
87 }
88 }
This page was automatically generated by Maven