View Javadoc

1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.configuration;
4   
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.util.List;
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 ExcludedClasses out of the VirtualMock config file.
17   *
18   * @author Chad Woolley
19   * @version $Revision: 1.1 $
20   * 
21   * @todo This should be refactored into a generic reusable parser, now that all dependencies are passed in via constructor.
22   */
23  public class ExcludedClassesParser {
24      ExcludedClassesHandler handler = null;
25      InputStream inputStream = null;
26      SAXParserFactory factory = null;
27      
28      
29      /***
30       * Creates a new ExcludedClassesParser object.
31       *
32       * @param inputStream the InputStream which contains the virtualmock xml
33       *        config file.
34       * @param handler the SAX Handler for this parser
35       */
36      public ExcludedClassesParser(InputStream inputStream,
37          ExcludedClassesHandler handler, SAXParserFactory factory) {
38          this.handler = handler;
39          this.inputStream = inputStream;
40          this.factory = factory;
41      }
42  
43      /***
44       * Returns a List representing the classExclusionPatterns defined in the
45       * configuration file.
46       *
47       * @return Returns a List representing the classExclusionPatterns defined
48       *         in the configuration file.
49       */
50      public List getClassExclusionPatterns() {
51          parse();
52  
53          return handler.getClassExclusionPatterns();
54      }
55  
56      /***
57       * The parse method of this Parser.
58       *
59       * @throws RuntimeException if unable to obtain the input stream for file
60       */
61      protected void parse() {
62          try {
63              performParse();
64          } catch (SAXException saxException) {
65              throw new RuntimeException(saxException);
66          } catch (ParserConfigurationException parserConfigurationException) {
67              throw new RuntimeException(parserConfigurationException);
68          } catch (IOException ioException) {
69              throw new RuntimeException(ioException);
70          }
71      }
72  
73      /***
74       * Perform the actual parsing.
75       *
76       * @throws FactoryConfigurationError parsing exception
77       * @throws ParserConfigurationException parsing exception
78       * @throws SAXException parsing exception
79       * @throws IOException parsing exception
80       */
81      protected void performParse()
82          throws FactoryConfigurationError, 
83              ParserConfigurationException, 
84              SAXException, 
85              IOException {
86          SAXParser saxParser = factory.newSAXParser();
87          saxParser.parse(inputStream, handler);
88      }
89  }