1   /*
2    *  ConfigDataProcessor.java
3    *
4    *  Copyright (c) 1998-2005, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Hamish Cunningham, 9/Nov/2000
12   *
13   *  $Id: ConfigDataProcessor.java,v 1.9 2005/01/11 13:51:31 ian Exp $
14   */
15  
16  package gate.config;
17  
18  import java.io.*;
19  import java.net.URL;
20  
21  import javax.xml.parsers.*;
22  
23  import org.xml.sax.SAXException;
24  import org.xml.sax.helpers.DefaultHandler;
25  
26  import gate.util.*;
27  
28  
29  /** This class parses <TT>gate.xml</TT> configuration data files.
30    */
31  public class ConfigDataProcessor
32  {
33    /** Debug flag */
34    protected static final boolean DEBUG = false;
35  
36    /** The parser for the CREOLE directory files */
37    protected SAXParser parser = null;
38  
39    /** Default constructor. Sets up config files parser. */
40    public ConfigDataProcessor() throws GateException {
41  
42      // construct a SAX parser for parsing the config files
43      try {
44        // Get a parser factory.
45        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
46  
47        // Set up the factory to create the appropriate type of parser:
48        // non validating one
49        saxParserFactory.setValidating(false);
50        // non namespace aware one
51        saxParserFactory.setNamespaceAware(true);
52  
53        // create the parser
54        parser = saxParserFactory.newSAXParser();
55  
56      } catch (SAXException e) {
57        if(DEBUG) Out.println(e);
58        throw(new GateException(e));
59      } catch (ParserConfigurationException e) {
60        if(DEBUG) Out.println(e);
61        throw(new GateException(e));
62      }
63  
64    } // default constructor
65  
66    /** Parse a config file (represented as an open stream).
67      */
68    public void parseConfigFile(InputStream configStream, URL configUrl)
69    throws GateException
70    {
71      String nl = Strings.getNl();
72  
73      // create a handler for the config file and parse it
74      String configString = null; // for debug messages
75      try {
76        if(DEBUG) {
77          File configFile = new File(configUrl.getFile());
78          if(configFile.exists())
79            configString = Files.getString(configUrl.getFile());
80          else
81            configString = configUrl.toString();
82        }
83        DefaultHandler handler = new ConfigXmlHandler(configUrl);
84        parser.parse(configStream, handler);
85        if(DEBUG) {
86          Out.prln(
87            "done parsing " +
88            ((configUrl == null) ? "null" : configUrl.toString())
89          );
90        }
91      } catch (IOException e) {
92        Out.prln("conf file:"+nl+configString+nl);
93        throw(new GateException("Config data error 1 on "+configUrl+": "+nl+e));
94      } catch (SAXException e) {
95        Out.prln("conf file:"+nl+configString+nl);
96        throw(new GateException("Config data error 2 on "+configUrl+": "+nl+e));
97      }
98  
99    } // parseConfigFile
100 
101 } // class ConfigDataProcessor
102