/*******************************************************************************
 * Copyright (c) 2006, 2007 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.chart.viewer.internal.util;

import java.io.File;
import java.io.FileInputStream;
import java.net.MalformedURLException;

import javax.servlet.ServletContext;

import org.eclipse.birt.chart.exception.ChartException;
import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.model.ChartWithAxes;
import org.eclipse.birt.chart.model.ChartWithoutAxes;
import org.eclipse.birt.chart.model.Serializer;
import org.eclipse.birt.chart.model.component.Axis;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
import org.eclipse.birt.chart.model.impl.SerializerImpl;
import org.eclipse.birt.chart.plugin.ChartEnginePlugin;
import org.eclipse.birt.chart.util.ChartUtil;
import org.eclipse.emf.common.util.EList;

/**
 * Utility class for web component
 */

public class ChartWebHelper
{

	/**
	 * Parses a xml file to chart model instance
	 * 
	 * @param strPath
	 *            chart xml file path
	 * @return
	 * @throws ChartException
	 */
	public static Chart parseChart( String strPath ) throws ChartException
	{
		if ( strPath == null )
		{
			return null;
		}
		Chart chartModel = null;
		final File chartFile = new File( strPath );
		// Reads the chart model
		try
		{
			if ( chartFile.exists( ) )
			{
				Serializer serializer = SerializerImpl.instance( );
				chartModel = serializer.read( new FileInputStream( chartFile ) );
			}
		}
		catch ( Exception e )
		{
			throw new ChartException( ChartEnginePlugin.ID,
					ChartException.NOT_FOUND,
					e );
		}
		return chartModel;
	}

	/**
	 * Checks if the output type is supported
	 * 
	 * @param type
	 *            output type
	 * @return
	 */
	public static boolean checkOutputType( String type )
	{
		try
		{
			return ChartUtil.isOutputFormatSupport( type );
		}
		catch ( ChartException e )
		{
			return false;
		}
	}

	/**
	 * Checks if current chart has runtime datasets.
	 * 
	 * @param cm
	 *            chart model
	 * @return
	 */
	public static boolean isChartInRuntime( Chart cm )
	{
		if ( cm instanceof ChartWithAxes )
		{
			Axis bAxis = (Axis) ( (ChartWithAxes) cm ).getAxes( ).get( 0 );
			EList oAxes = bAxis.getAssociatedAxes( );
			for ( int i = 0; i < oAxes.size( ); i++ )
			{
				Axis oAxis = (Axis) oAxes.get( i );
				EList oSeries = oAxis.getSeriesDefinitions( );
				for ( int j = 0; j < oSeries.size( ); j++ )
				{
					SeriesDefinition sd = (SeriesDefinition) oSeries.get( j );
					if ( sd.getRunTimeSeries( ).size( ) > 0 )
					{
						return true;
					}
				}
			}
		}
		else if ( cm instanceof ChartWithoutAxes )
		{
			SeriesDefinition bsd = (SeriesDefinition) ( (ChartWithoutAxes) cm ).getSeriesDefinitions( )
					.get( 0 );
			EList osds = bsd.getSeriesDefinitions( );
			for ( int i = 0; i < osds.size( ); i++ )
			{
				SeriesDefinition osd = (SeriesDefinition) osds.get( i );
				if ( osd.getRunTimeSeries( ).size( ) > 0 )
				{
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * Returns the real path of the file in the web folder
	 * 
	 * @param context
	 *            servlet context
	 * @param fileName
	 *            the relative path of the file
	 */
	public static String getRealPath( ServletContext context, String fileName )
	{
		String path = context.getRealPath( "/" ); //$NON-NLS-1$

		if ( path == null )
		{
			// resources are in a .war (JBoss, WebLogic)
			java.net.URL url;
			try
			{
				url = context.getResource( "/" ); //$NON-NLS-1$
				path = url.getPath( );
			}
			catch ( MalformedURLException e )
			{
				e.printStackTrace( );
			}
		}
		return path + fileName;
	}
}

@@ -4041,32 +4041,274 @@
 ce( );%0A%09%09%09%7D%0A%09%09%7D%0A
+%09%09// In WebSphere, path may not end with separator%0A%09%09if ( path != null && path.length( ) %3E 0 )%0A%09%09%7B%0A%09%09%09if ( path.charAt( path.length( ) - 1 ) != '%5C%5C'%0A%09%09%09%09%09%7C%7C path.charAt( path.length( ) - 1 ) != '/' )%0A%09%09%09%7B%0A%09%09%09%09path += File.separator;%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09return path + 

Fix #200294:Fail to deploy chart runtime in WebSphere6.1[13][0601]