| ConfigDataProcessor |

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

| ConfigDataProcessor |