/*******************************************************************************
 * 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.api.validators;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.birt.report.model.api.IResourceLocator;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.SemanticError;
import org.eclipse.birt.report.model.api.util.URIUtil;
import org.eclipse.birt.report.model.core.DesignElement;
import org.eclipse.birt.report.model.core.Module;
import org.eclipse.birt.report.model.elements.ImageItem;
import org.eclipse.birt.report.model.elements.ReportDesign;
import org.eclipse.birt.report.model.validators.AbstractElementValidator;

/**
 * Validates the existance of the image file on the file system.
 * 
 * <h3>Rule</h3>
 * The rule is that the file should be on the file system.
 * 
 * <h3>Applicability</h3>
 * This validator is only applied to <code>ImageItem</code>.
 * 
 */

public class ImageFileExistingValidator extends AbstractElementValidator
{

	private final static ImageFileExistingValidator instance = new ImageFileExistingValidator( );

	/**
	 * Returns the singleton validator instance.
	 * 
	 * @return the validator instance
	 */

	public static ImageFileExistingValidator getInstance( )
	{
		return instance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.validators.core.AbstractElementValidator#validate(org.eclipse.birt.report.model.elements.ReportDesign,
	 *      org.eclipse.birt.report.model.core.DesignElement)
	 */

	public List validate( Module module, DesignElement element )
	{
		if ( !( element instanceof ImageItem ) )
			return Collections.EMPTY_LIST;

		return doValidate( module, (ImageItem) element );
	}

	private List doValidate( Module module, ImageItem toValidate )
	{
		String source = toValidate.getStringProperty( module,
				ImageItem.SOURCE_PROP );

		if ( !DesignChoiceConstants.IMAGE_REF_TYPE_FILE
				.equalsIgnoreCase( source ) )
			return Collections.EMPTY_LIST;

		if ( !( module instanceof ReportDesign ) )
			return Collections.EMPTY_LIST;

		String uri = toValidate.getStringProperty( module,
				ImageItem.URI_PROP );

		String filePath = URIUtil.getLocalPath( uri );
		if ( ( filePath != null ) && !( (ReportDesign) module ).isFileExist( filePath, IResourceLocator.IMAGE ) )
		{
			toValidate.setValid( false );

			List list = new ArrayList( );
			list.add( new SemanticError( toValidate, new String[]{filePath},
					SemanticError.DESIGN_EXCEPTION_IMAGE_FILE_NOT_EXIST,
					SemanticError.WARNING ) );

			return list;
		}
		return Collections.EMPTY_LIST;
	}
}