/**
   * Converts an XML element into an <code>EppCommandRenewXriName</code> object. The caller of this
   * method must make sure that the root node is of an EPP Command Renew entity for EPP XRI I-Name
   * object
   *
   * @param root root node for an <code>EppCommandRenewXriName</code> object in XML format
   * @return an <code>EppCommandRenewXriName</code> object, or null if the node is invalid
   */
  public static EppEntity fromXML(Node root) {
    EppCommandRenewXriName cmd = null;
    String iname = null;
    Calendar curExpDate = null;
    EppPeriod period = null;

    NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
      Node node = list.item(i);
      String name = node.getLocalName();
      if (name == null) {
        continue;
      }
      if (name.equals("iname")) {
        iname = EppUtil.getText(node);
      } else if (name.equals("curExpDate")) {
        curExpDate = EppUtil.getDate(node, true);
      } else if (name.equals("period")) {
        period = (EppPeriod) EppPeriod.fromXML(node);
      }
    }
    if (iname != null) {
      cmd = new EppCommandRenewXriName(iname, curExpDate, period, null);
    }

    return cmd;
  }
  /**
   * Converts an XML element into an <code>EppResponseDataRenewXriNumber</code> object. The caller
   * of this method must make sure that the root node is the resData element of EPP responseType for
   * creating an EPP XRI I-Number object
   *
   * @param root root node for an <code>EppResponseDataRenewXriNumber</code> object in XML format
   * @return an <code>EppResponseDataRenewXriNumber</code> object, or null if the node is invalid
   */
  public static EppEntity fromXML(Node root) {
    String i_number = null;
    Calendar exDate = null;
    NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
      Node node = list.item(i);
      String name = node.getLocalName();
      if (name == null) {
        continue;
      } else if (name.equals("inumber")) {
        i_number = EppUtil.getText(node);
      } else if (name.equals("exDate")) {
        exDate = EppUtil.getDate(node);
      }
    }

    return new EppResponseDataRenewXriNumber(i_number, exDate);
  }
  /**
   * Converts an <code>EppResponseDataRenewXriNumber</code> object into an XML element.
   *
   * @param doc the XML <code>Document</code> object
   * @param tag the tag/element name for the <code>EppResponseDataRenewXriNumber</code> object
   * @return an <code>Element</code> object
   */
  public Element toXML(Document doc, String tag) {
    Element elm;
    Element body = doc.createElement(tag);
    ElementNSImpl data = EppUtil.createElementNS(doc, "xriINU", "renData");
    body.appendChild(data);

    if (inumber != null) {
      elm = doc.createElement("inumber");
      elm.appendChild(doc.createTextNode(inumber));
      data.appendChild(elm);
    }
    if (exDate != null) {
      elm = doc.createElement("exDate");
      elm.appendChild(EppUtil.createTextNode(doc, exDate));
      data.appendChild(elm);
    }

    return body;
  }
  /**
   * Converts the <code>EppCommandRenewXriName</code> object into an XML element
   *
   * @param doc the XML <code>Document</code> object
   * @param tag the tag/element name for the <code>EppCommandRenewXriName</code> object
   * @return an <code>Element</code> object
   */
  public Element toXML(Document doc, String tag) {
    Element elm;
    Element body = EppUtil.createElementNS(doc, "xriINA", tag);

    if (iname != null) {
      elm = doc.createElement("iname");
      elm.appendChild(doc.createTextNode(iname));
      body.appendChild(elm);
    }
    if (curExpDate != null) {
      elm = doc.createElement("curExpDate");
      elm.appendChild(EppUtil.createTextNode(doc, curExpDate, true));
      body.appendChild(elm);
    }
    if (period != null) {
      body.appendChild(period.toXML(doc, "period"));
    }

    return toXMLCommon(doc, tag, body);
  }
  /**
   * Converts an XML element into an <code>EppXriAuthority</code> object. The caller of this method
   * must make sure that the root node is of the EPP Authority type.
   *
   * @param root root node for an <code>EppXriAuthority</code> object in XML format
   * @return an <code>EppXriAuthority</code> object, or null if the node is invalid
   */
  public static EppEntity fromXML(Node root) {
    EppXriAuthority authority = new EppXriAuthority();
    NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
      Node node = list.item(i);
      String name = node.getLocalName();
      if (name == null) {
        continue;
      }
      if (name.equals("authId")) {
        authority.setAuthorityId(EppUtil.getText(node));
      }
      if (name.equals("isEscrow")) {
        authority.setIsEscrow(EppUtil.getTextAsBool(node));
      }
      if (name.equals("isContact")) {
        authority.setIsContact(EppUtil.getTextAsBool(node));
      }
      if (name.equals("escrowAgent")) {
        EppXriTrustee agent = (EppXriTrustee) EppXriTrustee.fromXML(node);
        if (agent != null) authority.setEscrowAgent(agent);
      }
      if (name.equals("contactAgent")) {
        EppXriTrustee agent = (EppXriTrustee) EppXriTrustee.fromXML(node);
        if (agent != null) authority.setContactAgent(agent);
      } else if (name.equals("socialData")) {
        EppXriSocialData socialData = (EppXriSocialData) EppXriSocialData.fromXML(node);
        if (socialData != null) {
          authority.setSocialData(socialData);
        }
      } else if (name.equals("trustee")) {
        EppXriTrustee trustee = (EppXriTrustee) EppXriTrustee.fromXML(node);
        if (trustee != null) {
          authority.addTrustee(trustee);
        }
      } else if (name.equals("contactId")) {
        EppXriContactData con = (EppXriContactData) EppXriContactData.fromXML(node);
        if (con != null) {
          authority.addContactHandle(con);
        }
      } else if (name.equals("ref")) {
        EppXriRef ref = (EppXriRef) EppXriRef.fromXML(node);
        if (ref != null) {
          authority.addRef(ref);
        }
      } else if (name.equals("redirect")) {
        EppXriURI redirect = (EppXriURI) EppXriURI.fromXML(node);
        if (redirect != null) {
          authority.addRedirect(redirect);
        }
      } else if (name.equals("equivID")) {
        EppXriSynonym equivID = (EppXriSynonym) EppXriSynonym.fromXML(node);
        if (equivID != null) {
          authority.addEquivID(equivID);
        }
      }
      if (name.equals("canonicalEquivID")) {
        authority.setCanonicalEquivID(EppUtil.getText(node));
      } else if (name.equals("sep")) {
        EppXriServiceEndpoint sep = (EppXriServiceEndpoint) EppXriServiceEndpoint.fromXML(node);
        if (sep != null) {
          authority.addServiceEndpoint(sep);
        }
      } else if (name.equals("iname")) {
        authority.addIName(EppUtil.getText(node));
      } else if (name.equals("inumber")) {
        EppXriNumberAttribute xin = (EppXriNumberAttribute) EppXriNumberAttribute.fromXML(node);
        if (xin != null) {
          authority.addINumber(xin);
        }
      } else if (name.equals("iservice")) {
        authority.addIService(EppUtil.getText(node));
      } else if (name.equals("authInfo")) {
        EppAuthInfo authInfo = (EppAuthInfo) EppAuthInfo.fromXML(node);
        if (authInfo != null) {
          authority.setAuthInfo(authInfo);
        }
      } else if (name.equals("extension")) {
        authority.setExtension(EppUtil.getText(node));
      } else {
        authority.fromXMLCommon(node, name);
      }
    }

    return authority;
  }
  /**
   * Converts the <code>EppXriAuthority</code> object into an XML element
   *
   * @param doc the XML <code>Document</code> object
   * @param tag the tag/element name for the <code>EppXriAuthority</code> object
   * @return an <code>Element</code> object
   */
  public Element toXML(Document doc, String tag) {
    Element elm;
    Element body = EppUtil.createElementNS(doc, "xriAU", tag);
    boolean isCreate = tag.equals("create");

    // the order of the tags for create is:
    //
    // authId/isEscrow/isContact/escrowAgent/contactAgent/socialData/authInfo
    //
    // the order of the tags for info is
    //
    // authId/isEscrow/isContact/escrowAgent/contactAgent/
    // roid/status/socialData/trustee/ref/redirect/equivID/canonicalEquivID/
    // sep/iname/inumber/iservice/extension/
    // clID/crID/crDate/upDate/trDate/authInfo
    //

    if (authId != null) {
      elm = doc.createElement("authId");
      elm.appendChild(doc.createTextNode(authId));
      body.appendChild(elm);
    }

    if (isEscrow != null) {
      elm = doc.createElement("isEscrow");
      elm.appendChild(doc.createTextNode(isEscrow.toString()));
      body.appendChild(elm);
    }

    if (isContact != null) {
      elm = doc.createElement("isContact");
      elm.appendChild(doc.createTextNode(isContact.toString()));
      body.appendChild(elm);
    }

    if (escrowAgent != null) {
      body.appendChild(escrowAgent.toXML(doc, "escrowAgent"));
    }

    if (contactAgent != null) {
      body.appendChild(contactAgent.toXML(doc, "contactAgent"));
    }
    if (contactHands != null) {
      for (int i = 0; i < contactHands.size(); i++) {
        EppXriContactData t = (EppXriContactData) contactHands.elementAt(i);
        body.appendChild(t.toXML(doc, "contactId"));
      }
    }
    if (isCreate) {
      if (socialData != null) {
        body.appendChild(socialData.toXML(doc, "socialData"));
      }
      if (authInfo != null) {
        body.appendChild(authInfo.toXML(doc, "authInfo"));
      }

      return body;
    }
    if (roid != null) {
      elm = doc.createElement("roid");
      elm.appendChild(doc.createTextNode(roid));
      body.appendChild(elm);
    }
    if (status != null) {
      for (int i = 0; i < status.size(); i++) {
        EppStatus s = (EppStatus) status.elementAt(i);
        body.appendChild(s.toXML(doc, "status"));
      }
    }
    if (socialData != null) {
      body.appendChild(socialData.toXML(doc, "socialData"));
    }
    if (trustee != null) {
      for (int i = 0; i < trustee.size(); i++) {
        EppXriTrustee t = (EppXriTrustee) trustee.elementAt(i);
        body.appendChild(t.toXML(doc, "trustee"));
      }
    }
    if (ref != null) {
      for (int i = 0; i < ref.size(); i++) {
        EppXriRef t = (EppXriRef) ref.elementAt(i);
        body.appendChild(t.toXML(doc, "ref"));
      }
    }
    if (redirect != null) {
      for (int i = 0; i < redirect.size(); i++) {
        EppXriURI t = (EppXriURI) redirect.elementAt(i);
        body.appendChild(t.toXML(doc, "redirect"));
      }
    }
    if (equivIDs != null) {
      for (int i = 0; i < equivIDs.size(); i++) {
        EppXriSynonym id = (EppXriSynonym) equivIDs.elementAt(i);
        body.appendChild(id.toXML(doc, "equivID"));
      }
    }
    if (canonicalEquivID != null) {
      elm = doc.createElement("canonicalEquivID");
      elm.appendChild(doc.createTextNode(canonicalEquivID));
      body.appendChild(elm);
    }
    if (sep != null) {
      for (int i = 0; i < sep.size(); i++) {
        EppXriServiceEndpoint t = (EppXriServiceEndpoint) sep.elementAt(i);
        body.appendChild(t.toXML(doc, "sep"));
      }
    }
    if (iname != null) {
      for (int i = 0; i < iname.size(); i++) {
        String s = (String) iname.elementAt(i);
        elm = doc.createElement("iname");
        elm.appendChild(doc.createTextNode(s));
        body.appendChild(elm);
      }
    }
    if (inumber != null) {
      for (int i = 0; i < inumber.size(); i++) {
        EppXriNumberAttribute xin = (EppXriNumberAttribute) inumber.elementAt(i);
        body.appendChild(xin.toXML(doc, "inumber"));
      }
    }
    if (iservice != null) {
      for (int i = 0; i < iservice.size(); i++) {
        String s = (String) iservice.elementAt(i);
        elm = doc.createElement("iservice");
        elm.appendChild(doc.createTextNode(s));
        body.appendChild(elm);
      }
    }
    if (extension != null) {
      elm = doc.createElement("extension");
      elm.appendChild(doc.createTextNode(extension));
      body.appendChild(elm);
    }

    toXMLCommon(doc, body);

    return body;
  }