
package org.biomoby.shared.data;

import org.biomoby.registry.meta.Registry;
import org.biomoby.shared.MobyDataType;
import org.biomoby.shared.MobyNamespace;
import org.biomoby.shared.parser.MobyTags;

/**
 * A class representing a MOBY Boolean primitive. 
 *
 * Internally, the value is stored in a StringBuffer.  Because
 * getObject() will return a mutable StringBuffer, you can use
 * its methods to modify the underlying value of this MOBY object.
 */

public class MobyDataBoolean extends MobyDataObject{

    private Boolean value;

    /**
     * Construct the object using a DOM fragment.
     *
     * @throws IllegalArgumentException if the element is not a String tag
     */
    public MobyDataBoolean(org.w3c.dom.Element element) throws IllegalArgumentException{
	this(element, null);
    }

    public MobyDataBoolean(org.w3c.dom.Element element, Registry registry) throws IllegalArgumentException{
	this(getName(element), getTextContents(element), registry);
	setId(getId(element));
	addNamespace(getNamespace(element, registry));
    }
    
    /**
     * Constructor to build a MOBY base String object from a CharSequence
     * (i.e. String, StringBuffer, CharBuffer or StringBuilder).
     */
    public MobyDataBoolean(String articleName, Boolean b){
	this(articleName, b, null);
    }

    /** Every c-tor eventually winds up here */
    public MobyDataBoolean(String articleName, Boolean b, Registry registry){
	super(articleName, registry);
	setDataType(MobyDataType.getDataType(MobyTags.MOBYBOOLEAN, registry));
	value = b;
    }

    public MobyDataBoolean(String articleName, boolean b){
	this(articleName, Boolean.valueOf(b), null);
    }

    public MobyDataBoolean(String articleName, boolean b, Registry registry){
	this(articleName, Boolean.valueOf(b), registry);
    }

    /**
     * Uses Boolean.valueOf(String) to convert to a Boolean object.
     * i.e. the value is false unless the string is "true" irrespective of capitalization.
     * "Yes" and "1" are not acceptable.
     */
    public MobyDataBoolean(String articleName, String booleanString){
	this(articleName, Boolean.valueOf(booleanString.trim()), null);
    }

    public MobyDataBoolean(String articleName, String booleanString, Registry registry){
	this(articleName, Boolean.valueOf(booleanString.trim()), registry);
    }

    public MobyDataBoolean(Boolean b){
	this("", b);
    }

    public MobyDataBoolean(Boolean b, Registry r){
	this("", b, r);
    }

    public MobyDataBoolean(boolean b){
	this("", b);
    }

    public MobyDataBoolean(boolean b, Registry r){
	this("", b, r);
    }

    public MobyDataBoolean(String booleanString){
	this("", booleanString);
    }

    public MobyDataBoolean(String booleanString, Registry r){
	this("", booleanString, r);
    }

    public String toString(){
	return value.toString();
    }

    public MobyDataBoolean clone(){
	MobyDataBoolean copy = new MobyDataBoolean(getName(), value, getDataType().getRegistry());
	copy.setDataType(getDataType());
	copy.setId(getId());
	copy.setNamespaces(getNamespaces());
	return copy;
    }

    /**
     * Since the Boolean object is immutable, use the setValue(boolean) method instead 
     * to change the underlying value for this MOBYObject.
     *
     * @return an immutable Boolean
     */
    public Object getObject(){
	return value;
    }

    /**
     * A convenience method to get a Java primitive representing the underlying Boolean of this object
     */
    public boolean booleanValue(){
	return value.booleanValue();
    }

    public void setValue(boolean b){
	value = Boolean.valueOf(b);
    }

    public void setValue(Boolean b){
	value = b;
    }

    /**
     * Uses Boolean.valueOf(String) to convert to a Boolean object.
     * i.e. the value is false unless the string is "true" irrespective of capitalization.
     * "Yes" and "1" are not acceptable.
     */
    public void setValue(String booleanValue){
	value = Boolean.valueOf(booleanValue);
    }

    /**
     * @return "true" or "false"
     */
    public String getValue(){
	return value.toString();
    }

    public String toXML(){
	MobyNamespace[] ns = getNamespaces();
	if(xmlMode == MobyDataInstance.SERVICE_XML_MODE){
	    return "<"+MobyTags.MOBYBOOLEAN+" "+ getAttrXML() + ">" + value + "</"+MobyTags.MOBYBOOLEAN+">";
	}
	// Central mode, use default toXML provided by superclasses
	else{
	    return super.toXML();
	}
    }
}
