View Javadoc

1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.rule;
4   
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.util.Map;
8   import javax.xml.parsers.FactoryConfigurationError;
9   import javax.xml.parsers.ParserConfigurationException;
10  import javax.xml.parsers.SAXParser;
11  import javax.xml.parsers.SAXParserFactory;
12  import org.xml.sax.SAXException;
13  
14  
15  /***
16   * Parses VirtualMock configuration file.
17   *
18   * @author Property Systems Development
19   * @version $Revision: 1.13 $
20   */
21  public class RuleParser {
22      /*** constant for rules filename. */
23      public static final String RULES_CONFIG_PATH = "/virtualmock_rules.xml";
24      private static RuleHandler handler = new RuleHandler();
25  
26      /***
27       * Get a Map of ruleEnforcementLevels.
28       *
29       * @return a Map of ruleEnforcementLevels
30       */
31      public Map getRuleEnforcementLevels() {
32          parse();
33  
34          return handler.getRuleEnforcementLevels();
35      }
36  
37      /***
38       * The parse method of this Parser.
39       *
40       * @throws RuntimeException if unable to obtain the input stream for file
41       */
42      protected void parse() {
43          InputStream inputStream = null;
44  
45          try {
46              inputStream =
47                  this.getClass().getResourceAsStream(RULES_CONFIG_PATH);
48          } catch (Exception exception) {
49              exception.printStackTrace();
50          }
51  
52          if (inputStream == null) {
53              throw new RuntimeException(
54                  "Unable to obtain input stream for file: " + RULES_CONFIG_PATH);
55          }
56  
57          try {
58              performParse(inputStream);
59          } catch (SAXException saxException) {
60              saxException.printStackTrace();
61          } catch (ParserConfigurationException parserConfigurationException) {
62              parserConfigurationException.printStackTrace();
63          } catch (IOException ioException) {
64              ioException.printStackTrace();
65          }
66      }
67  
68      /***
69       * Perform the actual parsing.
70       *
71       * @param inputStream the input stream
72       *
73       * @throws FactoryConfigurationError parsing exception
74       * @throws ParserConfigurationException parsing exception
75       * @throws SAXException parsing exception
76       * @throws IOException parsing exception
77       */
78      protected void performParse(InputStream inputStream)
79          throws FactoryConfigurationError, 
80              ParserConfigurationException, 
81              SAXException, 
82              IOException {
83          SAXParserFactory factory = SAXParserFactory.newInstance();
84          SAXParser saxParser = factory.newSAXParser();
85          saxParser.parse(inputStream, handler);
86      }
87  }