001 /*
002 * ConfigDataProcessor.java
003 *
004 * Copyright (c) 1995-2010, The University of Sheffield. See the file
005 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006 *
007 * This file is part of GATE (see http://gate.ac.uk/), and is free
008 * software, licenced under the GNU Library General Public License,
009 * Version 2, June 1991 (in the distribution as file licence.html,
010 * and also available at http://gate.ac.uk/gate/licence.html).
011 *
012 * Hamish Cunningham, 9/Nov/2000
013 *
014 * $Id: ConfigDataProcessor.java 12722 2010-06-03 19:53:33Z thomas_heitz $
015 */
016
017 package gate.config;
018
019 import java.io.*;
020 import java.net.URL;
021
022 import javax.xml.parsers.*;
023
024 import org.xml.sax.SAXException;
025 import org.xml.sax.helpers.DefaultHandler;
026
027 import gate.util.*;
028
029
030 /** This class parses <TT>gate.xml</TT> configuration data files.
031 */
032 public class ConfigDataProcessor
033 {
034 /** Debug flag */
035 protected static final boolean DEBUG = false;
036
037 /** The parser for the CREOLE directory files */
038 protected SAXParser parser = null;
039
040 /** Default constructor. Sets up config files parser. */
041 public ConfigDataProcessor() throws GateException {
042
043 // construct a SAX parser for parsing the config files
044 try {
045 // Get a parser factory.
046 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
047
048 // Set up the factory to create the appropriate type of parser:
049 // non validating one
050 saxParserFactory.setValidating(false);
051 // non namespace aware one
052 saxParserFactory.setNamespaceAware(true);
053
054 // create the parser
055 parser = saxParserFactory.newSAXParser();
056
057 } catch (SAXException e) {
058 if(DEBUG) Out.println(e);
059 throw(new GateException(e));
060 } catch (ParserConfigurationException e) {
061 if(DEBUG) Out.println(e);
062 throw(new GateException(e));
063 }
064
065 } // default constructor
066
067 /** Parse a config file (represented as an open stream).
068 */
069 public void parseConfigFile(InputStream configStream, URL configUrl)
070 throws GateException
071 {
072 String nl = Strings.getNl();
073
074 // create a handler for the config file and parse it
075 String configString = null; // for debug messages
076 try {
077 if(DEBUG) {
078 File configFile = Files.fileFromURL(configUrl);
079 if(configFile.exists())
080 configString = Files.getString(configUrl.getFile());
081 else
082 configString = configUrl.toString();
083 }
084 DefaultHandler handler = new ConfigXmlHandler(configUrl);
085 parser.parse(configStream, handler);
086 if(DEBUG) {
087 Out.prln(
088 "done parsing " +
089 ((configUrl == null) ? "null" : configUrl.toString())
090 );
091 }
092 } catch (IOException e) {
093 Out.prln("conf file:"+nl+configString+nl);
094 throw(new GateException("Config data error 1 on "+configUrl+": "+nl+e));
095 } catch (SAXException e) {
096 Out.prln("conf file:"+nl+configString+nl);
097 throw(new GateException("Config data error 2 on "+configUrl+": "+nl+e));
098 }
099
100 } // parseConfigFile
101
102 } // class ConfigDataProcessor
|