View Javadoc

1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.rule;
4   
5   import java.util.HashMap;
6   import java.util.Map;
7   import org.xml.sax.SAXException;
8   import org.xml.sax.helpers.DefaultHandler;
9   
10  
11  /***
12   * DefaultHandler for VirtualMock Rules.
13   *
14   * @author Property Systems Development
15   * @version $Revision: 1.4 $
16   */
17  public class RuleHandler extends DefaultHandler {
18      private Map ruleEnforcementLevels = new HashMap();
19      private String characters = null;
20      private String key = null;
21  
22      /***
23       * Returns a Map representing the enforcement levels for all Rules defined
24       * in the configuration file.  In the Map, the key is a String, and the
25       * enforcementLevel is an Integer.
26       *
27       * @return a Map representing the enforcement levels for all Rules defined
28       *         in the configuration file
29       */
30      public Map getRuleEnforcementLevels() {
31          return ruleEnforcementLevels;
32      }
33  
34      /***
35       * Overridden SAX event.
36       *
37       * @param buffer the buffer
38       * @param offset the offset
39       * @param len the length
40       *
41       * @throws SAXException Any SAXException
42       */
43      public void characters(char[] buffer, int offset, int len)
44          throws SAXException {
45          characters = new String(buffer, offset, len);
46      }
47  
48      /***
49       * Overridden SAX event.
50       *
51       * @param uri the uri
52       * @param localName the local name
53       * @param name the name
54       *
55       * @throws RuntimeException If invalid data is found
56       */
57      public void endElement(String uri, String localName, String name) {
58          Integer enforcementLevel = null;
59  
60          if (name.equals("key")) {
61              key = characters;
62          } else if (name.equals("enforcementLevel")) {
63              String enforcementLevelString = characters;
64  
65              if ("IGNORE".equalsIgnoreCase(enforcementLevelString)) {
66                  enforcementLevel = new Integer(0);
67              } else if ("WARN".equalsIgnoreCase(enforcementLevelString)) {
68                  enforcementLevel = new Integer(1);
69              } else if ("FAIL".equalsIgnoreCase(enforcementLevelString)) {
70                  enforcementLevel = new Integer(2);
71              } else {
72                  throw new RuntimeException(
73                      "Invalid Rule enforcement level specified: "
74                      + enforcementLevelString);
75              }
76          } else if (name.equals("rule")) {
77              ruleEnforcementLevels.put(key, enforcementLevel);
78          }
79      }
80  }