/*******************************************************************************
 * 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.model.metadata;

import org.eclipse.birt.report.model.api.metadata.PropertyValueException;
import org.eclipse.birt.report.model.api.util.StringUtil;
import org.eclipse.birt.report.model.core.Module;

/**
 * Element name property type. Represents the name of an element.
 * 
 */

public class NamePropertyType extends TextualPropertyType
{

	/**
	 * Display name key.
	 */

	private static final String DISPLAY_NAME_KEY = "Property.name"; //$NON-NLS-1$

	/**
	 * Constructor.
	 */

	public NamePropertyType( )
	{
		super( DISPLAY_NAME_KEY );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.design.metadata.PropertyType#getTypeCode()
	 */

	public int getTypeCode( )
	{
		return NAME_TYPE;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.design.metadata.PropertyType#getXmlName()
	 */

	public String getName( )
	{
		return NAME_TYPE_NAME;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.metadata.PropertyType#validateValue(org.eclipse.birt.report.model.elements.ReportDesign,
	 *      org.eclipse.birt.report.model.metadata.PropertyDefn,
	 *      java.lang.Object)
	 */

	public Object validateValue( Module module, PropertyDefn defn, Object value )
			throws PropertyValueException
	{
		assert defn != null;
		if ( value == null )
		{
			if ( defn.isStructureMember( ) )
				throw new PropertyValueException( value,
						PropertyValueException.DESIGN_EXCEPTION_VALUE_REQUIRED,
						NAME_TYPE );
			return null;
		}
		if ( value instanceof String )
		{
			String stringValue = StringUtil.trimString( (String) value );
			if ( stringValue == null )
			{
				if ( defn.isStructureMember( ) )
					throw new PropertyValueException(
							value,
							PropertyValueException.DESIGN_EXCEPTION_INVALID_VALUE,
							NAME_TYPE );
				return null;
			}
			else if ( !isValidName( stringValue ) )
			{
				throw new PropertyValueException( value,
						PropertyValueException.DESIGN_EXCEPTION_INVALID_VALUE,
						NAME_TYPE );
			}

			return stringValue;
		}
		throw new PropertyValueException( value,
				PropertyValueException.DESIGN_EXCEPTION_INVALID_VALUE,
				NAME_TYPE );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.metadata.PropertyType#validateXml(org.eclipse.birt.report.model.elements.ReportDesign,
	 *      org.eclipse.birt.report.model.metadata.PropertyDefn,
	 *      java.lang.String)
	 */

	public Object validateXml( Module module, PropertyDefn defn, String value )
			throws PropertyValueException
	{
		if ( value == null )
			return null;
		return StringUtil.trimString( value );
	}

	/**
	 * 
	 * @param value
	 * @return
	 */
	private boolean isValidName( String value )
	{
		assert value != null;

		// can not contain: / \ . : ! ; ,
		if ( value.contains( "/" ) || value.contains( "\\" ) || value.contains( "." ) || value.contains( ":" ) || value.contains( "!" ) || value.contains( ";" ) || value.contains( "," ) ) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
			return false;
		return true;
	}
}
@@ -3260,17 +3260,19 @@
 = null;%0A
+%09%09
 %0A
-
 %09%09// can
@@ -3305,286 +3305,185 @@
 ,%0A%09%09
-if ( value.contains( %22/%22 ) %7C%7C value.contains( %22%5C%5C%22 ) %7C%7C value.contains( %22.%22 ) %7C%7C value.contains( %22:%22 ) %7C%7C value.contains( %22!%22 ) %7C%7C value.contains( %22;%22 ) %7C%7C value.contains( %22,%22 ) ) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$%0A
+for ( int i = 0; i %3C value.length( ); i++ )%0A%09%09%7B%0A%09%09%09char c = value.charAt( i );%0A%09%09%09if ( c == '/' %7C%7C c == '%5C%5C' %7C%7C c == '.' %7C%7C c == ':' %7C%7C c == '!'%0A%09%09%09%09%09%7C%7C c == ';' %7C%7C c == ',' )%0A%09
 %09%09%09r
@@ -3495,16 +3495,20 @@
  false;%0A
+%09%09%7D%0A
 %09%09return

Fix 180893 -- support local name scope like level in dimension.