package daruma.storage_manager.type_definition.types;

import daruma.storage_manager.type_definition.AbstractSingleTypeDefinition;
import daruma.storage_manager.type_definition.TypeException;
import daruma.sql.SQLDataType;

import daruma.xml.util.WhitespaceProceccer;


public class BooleanTypeDefinition extends AbstractSingleTypeDefinition
{
	public	BooleanTypeDefinition()
	{
		super();
	}

	@Override
	public	SQLDataType	getSingleSQLDataType()
	{
		return( new SQLDataType( "boolean" , Boolean.class ) );
	}

	@Override
	public	String	parseValue( String  value ) throws TypeException
	{
		// In XMLSchema, boolean value's `whiteSpace' type
		// is `collapse', drop outside whitespaces.
		//
		// all valid value doesn't contain whitespace,
		// so omit replacing whitespaces to one whitespace.

		String	v = WhitespaceProceccer.cutoffEdgeWhitespaces( value );

		if ( ! (v.equals( "true" )
			|| v.equals( "false" )
			|| v.equals( "0" )
			|| v.equals( "1" )) )
		{
			if ( v.equalsIgnoreCase( "TRUE" )
			  || v.equalsIgnoreCase( "FALSE" ) )
			{
				throw new TypeException
				( "\"" + v + "\" is not a valid boolean value,"
				  + " use lowercase word"
				  + " \"true\" or \"false\"" );
			}

			throw new TypeException
			  ( "\"" + v + "\" is not a valid boolean value" );
		}

		return( v );
	}
}
