View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.concepts;
25  
26  import java.util.*;
27  import zeus.util.*;
28  import gnu.regexp.*;
29  
30  
31  
32  /*** 
33      MessagePattern is used to set attribute value pairs for the MessageRule class: 
34      you specify as string array like so {"attribite1","reg_exp expression for a1", "attribute2", 
35      "reg_exp expression for a2", ...., "attribute<i>n</i>", "reg_exp expression for a<i>n</i>"} 
36      */
37  public class MessagePatternImpl implements MessagePattern {
38     protected Hashtable constraints = new Hashtable();
39  
40     public MessagePatternImpl() {
41     }
42     
43     
44     public MessagePatternImpl(String[] input) {
45        for(int i = 0; i < input.length; i += 2 )
46           setConstraint(input[i],input[i+1]);
47     }
48     
49     
50     public String[] listAttributes() {
51        String[] out = new String[constraints.size()];
52        Enumeration enum = constraints.keys();
53        for(int i = 0; enum.hasMoreElements(); i++ )
54           out[i] = (String)enum.nextElement();
55        return out;
56     }
57     
58     
59     public RE[] listValues() {
60        RE[] out = new RE[constraints.size()];
61        Enumeration enum = constraints.elements();
62        for(int i = 0; enum.hasMoreElements(); i++ )
63           out[i] = (RE)enum.nextElement();
64        return out;
65     }
66     
67     
68     public boolean setConstraint(String attribute, String regular_expr) {
69        try {
70           constraints.put(attribute, new RE(regular_expr,RE.REG_DOT_NEWLINE)); 
71           return true;
72        }
73        catch(REException rese) {
74          rese.printStackTrace();
75          return false;
76       }
77     }
78     
79     
80     public RE getConstraint(String attribute) {
81        return (RE)constraints.get(attribute);
82     }
83     
84     
85     public String toString() {
86        String s = "(";
87        RE regexp;
88        String key;
89        Enumeration enum = constraints.keys();
90        while( enum.hasMoreElements() ) {
91           key = (String)enum.nextElement();
92           regexp = (RE)constraints.get(key);
93           s += "(" + key + " " + regexp + ") ";
94        }
95        return s.trim() + ")";
96     }
97  }