/*******************************************************************************
 * 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.test.performance.parser;

import java.io.File;
import java.util.Locale;

import org.w3c.dom.Element;

public class Utility
{

	public static String getFirstChildValue( Element parent, String tag )
	{
		Element element = (Element) parent.getElementsByTagName( tag ).item( 0 );
		if ( element != null )
			return element.getFirstChild( ).getNodeValue( );
		return null;
	}

	// remove output folder
	public static void removeFolder( File folder )
	{
		if ( folder == null || !folder.exists( ) )
			return;

		File[] files = folder.listFiles( );
		for ( int i = 0; i < files.length; i++ )
		{
			if ( files[i].isDirectory( ) )
				removeFolder( files[i]);
			else
				files[i].delete( );
		}
		
		folder.delete( );
	}

	// get locale
	public static Locale getLocale( String locale )
	{

		int i = locale.indexOf( '_' );
		if ( i != -1 )
		{

			// e.g., en_US
			String language = locale.substring( 0, i );
			String country = locale.substring( i + 1 );
			return new Locale( language, country );
		}
		// e.g., en
		return new Locale( locale );
	}

	// return "Corporation" for a given file "Corporation.rptdesign"

	public static String getReportName( String fileName )
	{
		if ( fileName == null || fileName.length( ) == 0 )
			return null;

		int dot = fileName.lastIndexOf( '.' );
		if ( dot == -1 )
			return fileName;

		return fileName.substring( 0, dot );
	}
}
