package javakurs.musterloesung.uebung02.aufgabe1;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * {@link TfsParser} provides methods to parse TFS from strings and files.
 *
 * @author Joerg Steffen, DFKI
 * @version $Id: TfsParser.java,v 1.1 2010/11/11 16:51:25 steffen Exp $
 */
public class TfsParser {

  /**
   * This contains the SAX parser.
   */
  private SAXParser parser;

  /**
   * This contains the TFS handler.
   */
  private TfsHandler handler;


  /**
   * This creates a new instance of {@link TfsParser}.
   *
   * @throws ParserConfigurationException
   *           if there is a problem with the parser factory
   * @throws SAXException
   *           if there is a problem when creating the parser
   */
  public TfsParser()
      throws ParserConfigurationException, SAXException {

    // init parser factory
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    // we want the factory to produce parsers that do a validation and are
    // not namespace aware
    parserFactory.setValidating(true);
    parserFactory.setNamespaceAware(false);
    // create parser
    this.parser = parserFactory.newSAXParser();

    // create TFS handler
    this.handler = new TfsHandler();
  }


  /**
   * This parses an XML document with typed feature structures from the given
   * input source and creates corresponding instances of type feature
   * structures.
   *
   * @param inputSource
   *          a source to read the XML document from
   * @return a list of typed feature structures
   * @throws IOException
   *           if there is a problem when reading the input source
   * @throws SAXException
   *           if there is a problem when parsing the input source
   */
  private List<TypedFeatStruct> parseTfs(InputSource inputSource)
      throws IOException, SAXException  {

    // parse input source
    this.parser.parse(inputSource, this.handler);
    // return result
    return this.handler.getResultTfsList();
  }


  /**
   * This parses an XML document with typed feature structures from the given
   * file and creates corresponding instances of typed feature structures.
   *
   * @param fileName
   *          the file name to read the XML document from
   * @return a list of type feature structures
   * @throws IOException
   *           if the is a problem when reading the file
   * @throws SAXException
   *           if there is a problem when parsing the XML document
   */
  public List<TypedFeatStruct> parseTfsFromFile(String fileName)
    throws IOException, SAXException {

    FileInputStream is = new FileInputStream(new File(fileName));
    return this.parseTfs(new InputSource(is));
  }


  /**
   * This parses an XML document with typed feature structures from the given
   * string and creates corresponding instances of type feature structures.
   *
   * @param xmlString
   *          the XML document as string
   * @return a list of type feature structures
   * @throws IOException
   *           if the is a problem when reading the string
   * @throws SAXException
   *           if there is a problem when parsing the XML document
   */
  public List<TypedFeatStruct> parseTfsFromString(String xmlString)
      throws IOException, SAXException {

    StringReader sr = new StringReader(xmlString);
    return this.parseTfs(new InputSource(sr));
  }
}
