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

import com.sun.identity.saml2.common.SAML2Exception;
import com.sun.identity.saml2.protocol.RequesterID;
import com.sun.identity.shared.xml.XMLUtils;
import org.w3c.dom.Element;

public class RequesterIDImpl implements RequesterID {
   private String requesterIdURI;
   private boolean isMutable;

   public RequesterIDImpl(Element element) {
      this.parseElement(element);
   }

   @Override
   public String getValue() {
      return this.requesterIdURI;
   }

   @Override
   public void setValue(String value) throws SAML2Exception {
      if (this.isMutable) {
         this.requesterIdURI = value;
      } else {
         throw new SAML2Exception("objectImmutable");
      }
   }

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

   @Override
   public String toXMLString(boolean includeNSPrefix, boolean declareNS) throws SAML2Exception {
      String reqIDXMLString = null;
      if (this.requesterIdURI != null && !this.requesterIdURI.isEmpty()) {
         StringBuilder xmlString = new StringBuilder(100);
         xmlString.append("<");
         if (includeNSPrefix) {
            xmlString.append("samlp:");
         }

         xmlString.append("RequesterID");
         if (declareNS) {
            xmlString.append(" xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\"");
         }

         xmlString.append(">").append("\n").append(this.requesterIdURI).append("\n").append("</samlp:").append("RequesterID").append(">");
         reqIDXMLString = xmlString.toString();
      }

      return reqIDXMLString;
   }

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

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

   private void parseElement(Element element) {
      this.requesterIdURI = XMLUtils.getValueOfValueNode(element);
   }
}
