1
2
3 package org.virtualmock.configuration;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7 import org.apache.commons.digester.Digester;
8 import org.xml.sax.SAXException;
9
10
11 /***
12 * This class uses Jakarta Commons Digester to read the VirtualMock
13 * configuration file.
14 *
15 * @author Chad Woolley
16 * @version $Revision: 1.9 $
17 */
18 public class ConfigDigester {
19 private Digester digester = null;
20 private VMConfig vmConfig = null;
21
22 /***
23 * Creates a new ConfigDigester object, with validation enabled.
24 *
25 * @param inputStream the InputStream which contains the virtualmock xml
26 * config file.
27 */
28 public ConfigDigester(InputStream inputStream) {
29 digester = createDigester();
30 performDigest(inputStream, true);
31 }
32
33 /***
34 * Creates a new ConfigDigester object, with validation enabled, and using
35 * the specified Digester. This is to facilitate unit testing.
36 *
37 * @param inputStream the InputStream which contains the virtualmock xml
38 * config file.
39 * @param digester the Digester to use.
40 */
41 public ConfigDigester(InputStream inputStream, Digester digester) {
42 this.digester = digester;
43 performDigest(inputStream, true);
44 }
45
46 /***
47 * Creates a new ConfigDigester object, with validation enabled or disabled
48 * according to specified flag.
49 *
50 * @param inputStream the InputStream which contains the virtualmock xml
51 * config file.
52 * @param validate flag which indicates if the config xml should be
53 * validated.
54 */
55 public ConfigDigester(InputStream inputStream, boolean validate) {
56 digester = createDigester();
57 performDigest(inputStream, validate);
58 }
59
60 /***
61 * Returns the VMConfig that was parsed when this ConfigDigester was
62 * constructed.
63 *
64 * @return the VMConfig that was parsed when this ConfigDigester was
65 * constructed.
66 */
67 public VMConfig getVMConfig() {
68 return vmConfig;
69 }
70
71 /***
72 * Creates a new Digester.
73 *
74 * @return a new Digester
75 */
76 private Digester createDigester() {
77 return new Digester();
78 }
79
80 /***
81 * Performs the digestion.
82 *
83 * @param inputStream the InputStream which contains the virtualmock xml
84 * config file.
85 * @param validate flag which indicates if the config xml should be
86 * validated.
87 *
88 * @throws RuntimeException if exception occurs when digesting.
89 */
90 private void performDigest(InputStream inputStream, boolean validate) {
91 vmConfig = new VMConfig();
92
93 digester.push(vmConfig);
94 digester.setValidating(validate);
95
96 digester.addCallMethod("virtualmock/excluded-classes/exclusion-pattern",
97 "addClassExclusionPattern", 0);
98
99 digester.addObjectCreate("virtualmock/rules/rule", RuleConfig.class);
100 digester.addBeanPropertySetter("virtualmock/rules/rule/rule-type",
101 "ruleType");
102 digester.addSetNext("virtualmock/rules/rule", "addRuleConfig", "org.virtualmock.configuration.RuleConfig");
103
104 try {
105 digester.parse(inputStream);
106 } catch (SAXException saxException) {
107 throw new RuntimeException(
108 "SAXException occurred while parsing input stream with digester: "
109 + saxException.fillInStackTrace());
110 } catch (IOException ioException) {
111 throw new RuntimeException(
112 "IOException occurred while parsing input stream with digester: "
113 + ioException.fillInStackTrace());
114 }
115 }
116 }