package javakurs.musterloesung.uebung02.aufgabe1;

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


/**
 * {@link FeatValPair} is a data wrapper that represents a
 * feature-value-pair (FVP) of a typed features structure (TFS). The value of a
 * feature-value-pair is a {@link TypedFeatStruct}.
 *
 * @author Joerg Steffen, DFKI
 * @version $Id: FeatValPair.java,v 1.1 2010/11/11 16:51:25 steffen Exp $
 */
public class FeatValPair {

  /**
   * This contains the feature.
   */
  private String feature;

  /**
   * This contains the value of the feature.
   */
  private TypedFeatStruct value;


  /**
   * This creates a new instance of {@link FeatValPair} for the given feature
   * and value.
   *
   * @param feature
   *          the feature name
   * @param tfs
   *          the type feature structure that is the feature's value
   */
  public FeatValPair(String feature, TypedFeatStruct tfs) {

    this.feature = feature;
    this.value = tfs;
  }


  /**
   * This creates a new instance of {@link FeatValPair} for the given feature
   * and atomic value. The atomic value will be represented by a typed feature
   * structure without feature-value-pairs.
   *
   * @param feature
   *          the feature name
   * @param atomicValue
   *          the atomic value
   */
  public FeatValPair(String feature, String atomicValue) {

    this.feature = feature;
    this.value = new TypedFeatStruct(atomicValue);
  }


  /**
   * This returns the feature.
   *
   * @return the feature name
   */
  public String getFeature() {

    return this.feature;
  }


  /**
   * This returns the value.
   *
   * @return the value
   */
  public TypedFeatStruct getValue() {

    return this.value;
  }


  /**
   * This sets the value to the given parameter.
   *
   * @param value
   *          the type feature structure that is the value
   */
  void setValue(TypedFeatStruct value) {
    this.value = value;
  }


  /**
   * {@inheritDoc}
   */
  @Override
  public String toString() {

    return this.toString(0);
  }


  /**
   * This returns a string representation of this feature structure with the
   * given indentation string.
   *
   * @param indent
   *          the indentation to use
   * @return the string representation
   */
  String toString(int indent) {

    // init result
    StringBuilder strRep = new StringBuilder();

    strRep.append(this.getFeature()).append(" ");
    // increase the indentation by the length of the feature plus 1 for the
    // following blank
    indent = indent + this.getFeature().length() + 1;
    // add value string representation
    strRep.append(this.getValue().toString(indent));

    return strRep.toString();
  }

  // AUFGABE 2:

  /**
   * This returns a DOM representation of this feature-value-pair.
   *
   * @param doc
   *          an XML document to which the result element belongs
   * @return an XML element
   */
  public Element toDom(Document doc) {

    // create new element
    Element ele = doc.createElement(TfsHandler.F_ID);
    // set name attribute
    ele.setAttribute("name", this.getFeature());
    // add value as child
    ele.appendChild(this.getValue().toDom(doc));
    // return result
    return ele;
  }
}
