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

import java.net.URL;
import java.util.List;

import org.eclipse.birt.report.model.api.ModuleHandle;
import org.eclipse.birt.report.model.core.BundleHelper;

import com.ibm.icu.util.ULocale;

/**
 * Utility methods to find the resource file for the report design/library.
 * 
 */

public class MessageFileUtil
{

	/**
	 * Looks up a message file for the given locale, the search uses a reduced
	 * form of Java locale-driven search algorithm: Language&Country, language,
	 * default.
	 * 
	 * @param moduleHandle
	 *            the module
	 * @param fileName
	 *            the message file name.
	 * @param locale
	 *            the user-defined locale
	 * @return the URL of matched file.
	 */

	public static URL findMessageFile( ModuleHandle moduleHandle,
			String fileName, ULocale locale )
	{
		List possibleFiles = BundleHelper.getHelper( moduleHandle.getModule( ),
				fileName ).getMessageFilenames( locale );

		for ( int i = 0; i < possibleFiles.size( ); i++ )
		{
			String filename = (String) possibleFiles.get( i );
			URL url = moduleHandle.findResource( filename, 0 );
			if ( url != null )
				return url;
		}

		return null;
	}
}
