package cx2x.xcodeml.xnode;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

/**
 * XcodeML AST node.
 *
 * @author clementval
 */
public class Xnode {

  private final Element _baseElement;

  public Xnode(Element element){
    _baseElement = element;
  }

  /**
   * Get the element opcode.
   * @return Opcode.
   */
  public Xcode Opcode(){
    return Xcode.valueOf(_baseElement.getTagName());
  }

  /**
   * Check whether the element has the corresponding attribute.
   * @param attrCode Attribute code.
   * @return True if the element has the corresponding attribute.
   */
  public boolean hasAttribute(Xattr attrCode){
    return _baseElement != null
        && _baseElement.hasAttribute(attrCode.toString());
  }

  /**
   * Get the attribute's value.
   * @param attrCode Attribute code.
   * @return Attribute's value.
   */
  public String getAttribute(Xattr attrCode){
    return _baseElement.getAttribute(attrCode.toString());
  }

  /**
   * Get the element's value.
   * @return Element value.
   */
  public String getValue(){
    return _baseElement.getTextContent();
  }

  /**
   * Set attribute value.
   * @param attrCode Attribute code.
   * @param value Value of the attribute.
   */
  public void setAttribute(Xattr attrCode, String value){
    _baseElement.setAttribute(attrCode.toString(), value);
  }

  /**
   * Get the list of child elements.
   * @return List of children of the current element.
   */
  public List<Xnode> getChildren(){
    List<Xnode> nodes = new ArrayList<>();
    NodeList children = _baseElement.getChildNodes();
    for(int i = 0; i < children.getLength(); ++i){
      Node child = children.item(i);
      if(child.getNodeType() == Node.ELEMENT_NODE){
        nodes.add(new Xnode((Element)child));
      }
    }
    return nodes;
  }

}

@@ -1871,10 +1871,838 @@
 s;%0A  %7D%0A%0A
+  /**%0A   * Check whether the current element has a body element.%0A   * @return True if the element has a body. False otherwise.%0A   */%0A  public boolean hasBody()%7B%0A    switch (Opcode())%7B%0A      case FDOSTATEMENT:%0A      case FFUNCTIONDEFINITION:%0A        return true;%0A    %7D%0A    return false;%0A  %7D%0A%0A  /**%0A   * Get the inner body element.%0A   * @return The body element if found. Null otherwise.%0A   */%0A  public Xnode getBody()%7B%0A    return findNode(Xcode.BODY);%0A  %7D%0A%0A  /**%0A   * Find first child with the given opcode.%0A   * @param opcode Code of the element to be found.%0A   * @return The found element. Null if nothing found.%0A   */%0A  public Xnode findNode(Xcode opcode)%7B%0A    List%3CXnode%3E children = getChildren();%0A    for(Xnode child : children)%7B%0A      if(child.Opcode() == opcode)%7B%0A        return child;%0A      %7D%0A    %7D%0A    return null;%0A  %7D%0A%0A
 %7D%0A

Add helper method