package x2x.translator.xcodeml.xelement;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Hashtable;
import java.util.Map;

/**
 * The XargumentsTable represents the arguments (7.5.3) element in XcodeML
 * intermediate representation.
 *
 * Elements:
 * - Optional:
 *   - exprModel
 */

public class XargumentsTable extends XbaseElement {
  // TODO exprModel. For the moment only read var
  private Hashtable<String, XbaseElement> _table;

  public XargumentsTable(Element arguments){
    super(arguments);
    _table = new Hashtable<String, XbaseElement>();
    readTable();
  }

  private void readTable(){
    // Read Var element
    NodeList elements = baseElement.getElementsByTagName(XelementName.VAR);
    for (int i = 0; i < elements.getLength(); i++) {
      Node n = elements.item(i);
      if (n.getNodeType() == Node.ELEMENT_NODE) {
        Element el = (Element) n;
        Xvar var = new Xvar(el);
        _table.put(var.getValue(), var);
      }
    }
  }

  public XbaseElement findArgument(String name){
     return _table.get(name);
  }

  public void add(XarrayRef arrayRef){
    baseElement.appendChild(arrayRef.clone());
    _table.put(arrayRef.getVar().getValue(), arrayRef);
  }

  public void add(Xvar var){
    baseElement.appendChild(var.clone());
    _table.put(var.getValue(), var);
  }

  public XbaseElement get(String key){
    return _table.get(key);
  }
}

@@ -1093,32 +1093,280 @@
 get(name);%0A  %7D%0A%0A
+  public void replace(Xvar var, XarrayRef arrayRef)%7B%0A    if(var != null)%7B%0A      XelementHelper.insertAfter(var.getBaseElement(),%0A        arrayRef.getBaseElement());%0A      var.delete();%0A    %7D else %7B%0A      // TODO trigger a critical error%0A    %7D%0A  %7D%0A%0A
   public void ad
@@ -1431,32 +1431,34 @@
 f.clone());%0A    
+//
 _table.put(array
@@ -1492,16 +1492,21 @@
 rayRef);
+ TODO
 %0A  %7D%0A%0A  

Add method to replace an arguments in the table