/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.engine.css.engine.value.css;

import org.eclipse.birt.report.engine.css.engine.CSSEngine;
import org.eclipse.birt.report.engine.css.engine.CSSStylableElement;
import org.eclipse.birt.report.engine.css.engine.ValueManager;
import org.eclipse.birt.report.engine.css.engine.value.AbstractLengthManager;
import org.eclipse.birt.report.engine.css.engine.value.StringMap;
import org.eclipse.birt.report.engine.css.engine.value.Value;
import org.w3c.css.sac.LexicalUnit;
import org.w3c.dom.DOMException;
import org.w3c.dom.css.CSSPrimitiveValue;
import org.w3c.dom.css.CSSValue;

public class SpacingManager extends AbstractLengthManager
{

	/**
	 * The identifier values.
	 */
	protected final static StringMap values = new StringMap( );
	static
	{
		values.put( CSSConstants.CSS_NORMAL_VALUE,
				CSSValueConstants.NORMAL_VALUE );
	}

	String propertyName;

	public SpacingManager( String propertyName )
	{
		this.propertyName = propertyName;
	}

	public String getPropertyName( )
	{
		return propertyName;
	}

	public boolean isInheritedProperty( )
	{
		return true;
	}

	public Value getDefaultValue( )
	{
		return CSSValueConstants.NORMAL_VALUE;
	}

	public Value createValue( LexicalUnit lu, CSSEngine engine )
			throws DOMException
	{
		switch ( lu.getLexicalUnitType( ) )
		{
			case LexicalUnit.SAC_IDENT :
				String s = lu.getStringValue( ).toLowerCase( ).intern( );
				Object v = values.get( s );
				if ( v == null )
				{
					throw createInvalidIdentifierDOMException( lu
							.getStringValue( ) );
				}
				return (Value) v;
			case LexicalUnit.SAC_PERCENTAGE :
				throw createInvalidLexicalUnitDOMException( lu
						.getLexicalUnitType( ) );
		}
		return super.createValue( lu, engine );
	}

	/**
	 * Implements {@link
	 * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
	 */
	public Value computeValue( CSSStylableElement elt, CSSEngine engine,
			int idx, Value value )
	{
		if ( value.getCssValueType( ) == CSSValue.CSS_PRIMITIVE_VALUE )
		{
			if ( value.getPrimitiveType( ) == CSSPrimitiveValue.CSS_IDENT )
			{
				return CSSValueConstants.NUMBER_0;
			}
		}
		return super.computeValue( elt, engine, idx, value );
	}

}
