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

import com.sun.identity.saml2.assertion.SubjectLocality;
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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class SubjectLocalityImpl implements SubjectLocality {
   private String address;
   private String dnsName;
   private boolean mutable = true;

   private void parseElement(Element element) throws SAML2Exception {
      if (element == null) {
         if (SAML2SDKUtils.DEBUG.messageEnabled()) {
            SAML2SDKUtils.DEBUG.message("SubjectLocalityImpl.parseElement: Input element is null.");
         }

         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("nullInput"));
      } else {
         String tag = null;
         tag = element.getLocalName();
         if (tag != null && tag.equals("SubjectLocality")) {
            NamedNodeMap atts = element.getAttributes();
            if (atts != null) {
               Node att = atts.getNamedItem("Address");
               if (att != null) {
                  this.address = ((Attr)att).getValue().trim();
               }

               att = atts.getNamedItem("DNSName");
               if (att != null) {
                  this.dnsName = ((Attr)att).getValue().trim();
               }
            }

            this.mutable = false;
         } else {
            if (SAML2SDKUtils.DEBUG.messageEnabled()) {
               SAML2SDKUtils.DEBUG.message("SubjectLocalityImpl.parseElement: input is not SubjectLocality");
            }

            throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("wrongInput"));
         }
      }
   }

   public SubjectLocalityImpl() {
   }

   public SubjectLocalityImpl(Element element) throws SAML2Exception {
      this.parseElement(element);
   }

   public SubjectLocalityImpl(String xmlString) throws SAML2Exception {
      Document doc = XMLUtils.toDOMDocument(xmlString, SAML2SDKUtils.DEBUG);
      if (doc == null) {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("errorObtainingElement"));
      }

      this.parseElement(doc.getDocumentElement());
   }

   @Override
   public void makeImmutable() {
      this.mutable = false;
   }

   @Override
   public boolean isMutable() {
      return this.mutable;
   }

   @Override
   public String getDNSName() {
      return this.dnsName;
   }

   @Override
   public void setDNSName(String value) throws SAML2Exception {
      if (!this.mutable) {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }

      this.dnsName = value;
   }

   @Override
   public String getAddress() {
      return this.address;
   }

   @Override
   public void setAddress(String value) throws SAML2Exception {
      if (!this.mutable) {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }

      this.address = value;
   }

   @Override
   public String toXMLString() {
      return this.toXMLString(true, false);
   }

   @Override
   public String toXMLString(boolean includeNS, boolean declareNS) {
      StringBuilder result = new StringBuilder(1000);
      String prefix = "";
      String uri = "";
      if (includeNS) {
         prefix = "saml:";
      }

      if (declareNS) {
         uri = " xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\"";
      }

      result.append("<").append(prefix).append("SubjectLocality").append(uri);
      if (this.address != null && !this.address.trim().isEmpty()) {
         result.append(" Address=\"").append(this.address).append("\"");
      }

      if (this.dnsName != null && !this.dnsName.trim().isEmpty()) {
         result.append(" DNSName=\"").append(this.dnsName).append("\"");
      }

      result.append("></").append(prefix).append("SubjectLocality>");
      return result.toString();
   }
}
