package com.sun.identity.saml2.assertion.impl;

import com.sun.identity.saml2.assertion.BaseID;
import com.sun.identity.saml2.common.SAML2Exception;
import com.sun.identity.saml2.common.SAML2SDKUtils;
import com.sun.identity.shared.xml.XMLUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class BaseIDImpl extends BaseIDAbstractImpl implements BaseID {
   private static final String BASE_ID_ELEMENT = "BaseID";
   private static final String NAME_QUALIFIER_ATTR = "NameQualifier";
   private static final String SP_NAME_QUALIFIER_ATTR = "SPNameQualifier";

   public BaseIDImpl() {
   }

   public BaseIDImpl(String xml) throws SAML2Exception {
      Document document = XMLUtils.toDOMDocument(xml, SAML2SDKUtils.DEBUG);
      if (document != null) {
         Element rootElement = document.getDocumentElement();
         this.processElement(rootElement);
         this.makeImmutable();
      } else {
         SAML2SDKUtils.DEBUG.error("BaseIDImpl.processElement(): invalid XML input");
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("errorObtainingElement"));
      }
   }

   public BaseIDImpl(Element element) throws SAML2Exception {
      this.processElement(element);
      this.makeImmutable();
   }

   private void processElement(Element element) throws SAML2Exception {
      if (element == null) {
         SAML2SDKUtils.DEBUG.error("BaseIDImpl.processElement(): invalid root element");
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("invalid_assertion_element"));
      }

      String elemName = element.getLocalName();
      if (elemName == null) {
         SAML2SDKUtils.DEBUG.error("BaseIDImpl.processElement(): local name missing");
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("missing_local_name"));
      }

      if (!elemName.equals("BaseID")) {
         SAML2SDKUtils.DEBUG.error("BaseIDImpl.processElement(): invalid local name " + elemName);
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("invalid_local_name"));
      }

      String attrValue = element.getAttribute("NameQualifier");
      if (attrValue != null) {
         this.setNameQualifier(attrValue);
      }

      attrValue = element.getAttribute("SPNameQualifier");
      if (attrValue != null) {
         this.setSPNameQualifier(attrValue);
      }
   }

   @Override
   public String toXMLString(boolean includeNSPrefix, boolean declareNS) throws SAML2Exception {
      StringBuilder sb = new StringBuilder(2000);
      String ns = "";
      String appendNS = "";
      if (declareNS) {
         ns = " xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\"";
      }

      if (includeNSPrefix) {
         appendNS = "saml:";
      }

      sb.append("<").append(appendNS).append("BaseID").append(ns);
      String nq = this.getNameQualifier();
      if (nq != null) {
         sb.append(" ").append("NameQualifier").append("=\"").append(nq).append("\"");
      }

      String spnq = this.getSPNameQualifier();
      if (spnq != null) {
         sb.append(" ").append("SPNameQualifier").append("=\"").append(spnq).append("\"");
      }

      sb.append(" />");
      return sb.toString();
   }
}
