KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > actionflow > config > RegexLink


1 /*
2  * Copyright (c) 2002, Inversoft Corporation, All Rights Reserved
3  */

4 package com.inversoft.verge.mvc.controller.actionflow.config;
5
6
7 import java.util.regex.Matcher JavaDoc;
8 import java.util.regex.Pattern JavaDoc;
9
10 import com.inversoft.util.StringTools;
11
12
13 /**
14  * This class describes a link between two Nodes that is
15  * are connected by a regular expression. This regex is used
16  * to match action names. The Node objects are responsible
17  * for implementing how these Links are stored and searched,
18  * but the matching logic is contained in this class. Currently,
19  * this class uses only Java 1.4 regex package for simplicity
20  * and future support, because we all know what happens to OS
21  * projects when Sun adds something new to the API :)
22  *
23  * @author Brian Pontarelli
24  * @since 2.0
25  * @version 2.0
26  */

27 public class RegexLink extends BaseLink {
28
29     private Pattern JavaDoc regex;
30
31
32     /**
33      * Constructs a new RegexLink with the given regex, origin Node
34      * and destination Node.
35      */

36     public RegexLink(String JavaDoc regex, Node origin, Node destination) {
37
38         super(origin, destination);
39         if (StringTools.isEmpty(regex)) {
40             throw new IllegalArgumentException JavaDoc("The regex is empty or null");
41         }
42
43         this.regex = Pattern.compile(regex);
44     }
45
46
47     /**
48      * Gets the regular expression pattern of this link
49      *
50      * @return Returns the pattern of this link
51      */

52     public Pattern JavaDoc getRegex() {
53         return regex;
54     }
55     
56     /**
57      * Returns the value of the link which is the regex pattern.
58      *
59      * @return The value of the link
60      */

61     public String JavaDoc getValue() {
62         return regex.pattern();
63     }
64     
65     /**
66      * Returns whether this Link accepts the given action. This returns true if
67      * the given action is equal to the action of this Link using the equals
68      * method of the String class.
69      *
70      * @param action The action to check for acceptance
71      * @return True if the given action equals the action of this Link
72      */

73     public boolean acceptAction(Object JavaDoc action) {
74         assert (action instanceof String JavaDoc) : "action must be a String";
75         assert (!StringTools.isEmpty((String JavaDoc) action)) : "action can not be null or empty";
76
77         Matcher JavaDoc matcher = regex.matcher((String JavaDoc) action);
78         return matcher.matches();
79     }
80     
81     /**
82      * Returns a String representation of this link suitable for debugging
83      */

84     public String JavaDoc toString() {
85         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
86         buf.append(getOrigin().getName()).append(" --(").append(regex);
87         buf.append(")--> ").append(getDestination().getName());
88         return buf.toString();
89     }
90 }
91
92
Popular Tags