package javakurs.musterloesung.uebung02.aufgabe2;

import java.util.List;

import javakurs.musterloesung.uebung02.aufgabe1.TfsHandler;
import javakurs.musterloesung.uebung02.aufgabe1.TypedFeatStruct;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * {@link TfsDomConvertor} provides methods to convert {@link TypedFeatStruct}s
 * to XML {@link Document}s
 *
 * @author Joerg Steffen, DFKI
 * @version $Id: TfsDomConvertor.java,v 1.1 2010/11/11 16:51:26 steffen Exp $
 */
public final class TfsDomConvertor {

  /**
   * This would create a new instance of {@link TfsDomConvertor}. Not to be
   * used.
   */
  private TfsDomConvertor() {

    // private constructor to enforce noninstantiability
  }


  /**
   * This converts the given list of typed feature structures to an XML
   * document.
   *
   * @param tfsList
   *          a list of typed feature structures
   * @return an XML document
   * @throws ParserConfigurationException
   *           if there is a problem when creating the result document
   */
  public static Document convert(List<TypedFeatStruct> tfsList)
      throws ParserConfigurationException {

    // init new XML document
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();

    // create the root node, FSLIST
    Element root = doc.createElement(TfsHandler.FSLIST_ID);
    doc.appendChild(root);

    // add typed feature structures
    for (TypedFeatStruct oneTfs : tfsList) {
      root.appendChild(oneTfs.toDom(doc));
    }

    // return result
    return doc;
  }
}
