/**
   * 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;
  }