/*
 * This file is released under terms of BSD license
 * See LICENSE file for more information
 */

package cx2x.xcodeml.xelement;

import org.w3c.dom.Element;
import cx2x.xcodeml.helper.*;


/**
 * The XifStatement represents the FifStatement (6.4) element in XcodeML
 * intermediate representation.
 *
 * Elements: (condition, then, else?)
 * - Required:
 *   - condition (Xcondition)
 *   - then (Xthen)
 * - Optional:
 *   - else (Xelse)
 * Attributes:
 * - Optional: construct_name (text)
 *
 * Can have lineno and file attribtues
 *
 * @author clementval
 */

public class XifStatement extends XenhancedElement {

  private Xcondition _cond = null;
  private Xthen _then = null;
  private Xelse _else = null;

  // attributes
  private String _construct_name = null;

  /**
   * Xelement standard ctor. Pass the base element to the base class and read
   * inner information (elements and attributes).
   * @param baseElement The root element of the Xelement
   */
  public XifStatement(Element baseElement){
    super(baseElement);
    _cond = XelementHelper.findCondition(this, false);
    _then = XelementHelper.findThen(this, false);
    _else = XelementHelper.findElse(this, false);


    // read optional attributes
    _construct_name = XelementHelper.getAttributeValue(this,
      XelementName.ATTR_CONSTRUCT_NAME);
  }


  /**
   * Check whether the element has a construct name attribute defined.
   * @return True the attribute is defined. False otherwise.
   */
  public boolean hasConstructName(){
    return _construct_name != null;
  }

  /**
   * Get the construct name attribute value.
   * @return Construct name value. Null if the attribute is not defined.
   */
  public String getConstructName(){
    return _construct_name;
  }

  /**
   * Get the condition part of the if statement.
   * @return A Xcondition object.
   */
  public Xcondition getCondition(){
    return _cond;
  }

  /**
   * Get the then block of the if statement.
   * @return A Xthen object.
   */
  public Xthen getThen(){
    return _then;
  }

  /**
   * Get the else block of the if statement
   * @return A Xelse object. Null if there is no else block.
   */
  public Xelse getElse(){
    return _else;
  }
}

@@ -126,16 +126,78 @@
 ement;%0A%0A
+import cx2x.xcodeml.exception.IllegalTransformationException;%0A
 import o
@@ -216,16 +216,16 @@
 lement;%0A
-
 import c
@@ -1095,16 +1095,141 @@
 ement);%0A
+    readElementinformation();%0A  %7D%0A%0A  /**%0A   * Read inner element information.%0A   */%0A  private void readElementinformation()%7B%0A
     _con
@@ -1376,17 +1376,16 @@
 alse);%0A%0A
-%0A
     // r
@@ -1469,16 +1469,18 @@
 e(this,%0A
+  
       Xe
@@ -1516,14 +1516,15 @@
 E);%0A
-
   %7D%0A%0A%0A
+%0A
   /*
@@ -2368,24 +2368,24 @@
  getElse()%7B%0A
-
     return _
@@ -2393,11 +2393,1702 @@
 lse;%0A  %7D
+%0A%0A  /**%0A   * Create a new XifStatement object with minimal element (condition, then)%0A   * @param xcodeml XcodeML program.%0A   * @return A newly constructs XifStatement element with empty condition and%0A   * block.%0A   * @throws IllegalTransformationException can be thrown while constrcuting%0A   * empty elements.%0A   */%0A  public static XifStatement create(XcodeProgram xcodeml)%0A      throws IllegalTransformationException%0A  %7B%0A    XifStatement root = XelementHelper.createEmpty(XifStatement.class, xcodeml);%0A    Xcondition cond = XelementHelper.createEmpty(Xcondition.class, xcodeml);%0A    Xthen thenBlock = XelementHelper.createEmpty(Xthen.class, xcodeml);%0A    Xbody thenBody = XelementHelper.createEmpty(Xbody.class, xcodeml);%0A    thenBlock.appendToChildren(thenBody, false);%0A    root.appendToChildren(thenBlock, false);%0A    root.appendToChildren(cond, false);%0A    root.readElementinformation();%0A    return root;%0A  %7D%0A%0A  /**%0A   * Create a new XifStatement object with minimal blocks (condition, then,%0A   * else)%0A   * @param xcodeml XcodeML program.%0A   * @return A newly constructs XifStatement element with empty condition and%0A   * blocks.%0A   * @throws IllegalTransformationException can be thrown while constrcuting%0A   * empty elements.%0A   */%0A  public static XifStatement createWithElseBlock(XcodeProgram xcodeml)%0A      throws IllegalTransformationException%0A  %7B%0A    XifStatement root = create(xcodeml);%0A    Xelse elseBlock = XelementHelper.createEmpty(Xelse.class, xcodeml);%0A    Xbody elseBody = XelementHelper.createEmpty(Xbody.class, xcodeml);%0A    elseBlock.appendToChildren(elseBody, false);%0A    root.appendToChildren(elseBlock, false);%0A    root.readElementinformation();%0A    return root;%0A  %7D
 %0A%7D%0A

Add method to create empty if statement with/w/o else block