<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2005 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.data.engine.olap.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.birt.core.data.ExpressionUtil;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IBaseQueryResults;
import org.eclipse.birt.data.engine.api.IBinding;
import org.eclipse.birt.data.engine.api.IScriptExpression;
import org.eclipse.birt.data.engine.api.aggregation.AggregationManager;
import org.eclipse.birt.data.engine.api.aggregation.IAggrFunction;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.expression.ExpressionCompilerUtil;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.impl.IQueryService;
import org.eclipse.birt.data.engine.olap.api.ICubeQueryResults;
import org.eclipse.birt.data.engine.olap.data.api.DimLevel;
import org.eclipse.birt.data.engine.olap.query.view.CubeQueryDefinitionUtil;
import org.eclipse.birt.data.engine.olap.script.JSCubeBindingObject;
import org.eclipse.birt.data.engine.script.ScriptConstants;
import org.mozilla.javascript.Scriptable;

/**
 * 
 */

public class OlapExpressionUtil
{

	private static Logger logger = Logger.getLogger( OlapExpressionUtil.class.getName( ) );
	/**
	 * get the attribute reference name.
	 * 
	 * @param dimName
	 * @param levelName
	 * @param attrName
	 * @return
	 */
	public static String getAttrReference( String dimName, String levelName,
			String attrName )
	{
		return dimName + '/' + levelName + '/' + attrName;
	}

	/**
	 * 
	 * @param originalMeasureName
	 * @return
	 */
	public static String createMeasureCalculateMemeberName(
			String originalMeasureName )
	{
		return "_&$" + originalMeasureName + "$&_";
	}

	/**
	 * 
	 * @param expr
	 * @return
	 */
	public static boolean isReferenceToDimLevel( String expr )
	{
		if ( expr == null )
			return false;
		return expr.matches( "\\Qdimension[\"\\E.*\\Q\"][\"\\E.*\\Q\"]\\E" );
	}


	/**
	 * This method is used to get the dimension,level,attributes name that
	 * reference by a level&attribute reference expression of following format:
	 * dimension["dimensionName"]["levelName"]["attributeName"].
	 * 
	 * String[0] dimensionName; String[1] levelName; String[2] attributeName;
	 * 
	 * @param expr
	 * @return String[]
	 * @throws DataException
	 */
	private static String[] getTargetAttribute( String expr, List bindings )
			throws DataException
	{
		if ( expr == null )
			return null;
		if ( !( expr.matches( "\\Qdimension[\"\\E.*\\Q\"][\"\\E.*\\Q\"][\"\\E.*\\Q\"]\\E" ) || expr.matches( "\\Qdimension[\"\\E.*\\Q\"][\"\\E.*\\Q\"]\\E" ) ) )
		{
			String bindingName = getBindingName( expr );
			if ( bindingName != null )
			{
				for ( int i = 0; i < bindings.size( ); i++ )
				{
					IBinding binding = (IBinding) bindings.get( i );
					if ( bindingName.equals( binding.getBindingName( ) ) )
					{
						if ( binding.getExpression( ) instanceof IScriptExpression )
						{
							return getTargetAttribute( ( (IScriptExpression) binding.getExpression( ) ).getText( ),
									bindings );
						}
					}
				}
			}

			throw new DataException( ResourceConstants.INVALID_EXPRESSION, expr );
		}

		expr = expr.replaceFirst( "\\Qdimension\\E", "" );
		String[] result = new String[3];
		String[] candidateResult = expr.split( "\\Q\"][\"\\E" );
		if ( candidateResult.length == 2 )
		{
			result[0] = candidateResult[0].replaceAll( "\\Q[\"\\E", "" );
			result[1] = candidateResult[1].replaceAll( "\\Q\"]\\E", "" );
		}
		else
		{
			result[0] = candidateResult[0].replaceAll( "\\Q[\"\\E", "" );
			result[1] = candidateResult[1];
			result[2] = candidateResult[2].replaceAll( "\\Q\"]\\E", "" );
		}
		return result;
	}

	/**
	 * 
	 * @param expr
	 * @return
	 * @throws DataException
	 */
	public static DimLevel getTargetDimLevel( String expr )
			throws DataException
	{
		Set<DimLevel> s = OlapExpressionCompiler.getReferencedDimLevel( new ScriptExpression( expr ), 
				Collections.EMPTY_LIST );
		if ( s != null && s.size( ) == 1 )
		{
			return s.iterator( ).next( );
		}
		throw new DataException( ResourceConstants.LEVEL_NAME_NOT_FOUND,
					expr );
	}<commit_msg>Checkin:"2003 TOTAL" is shown as "2,003" for xtab sub total[26635]<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2005 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.data.engine.olap.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.birt.core.data.ExpressionUtil;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IBaseQueryResults;
import org.eclipse.birt.data.engine.api.IBinding;
import org.eclipse.birt.data.engine.api.IScriptExpression;
import org.eclipse.birt.data.engine.api.aggregation.AggregationManager;
import org.eclipse.birt.data.engine.api.aggregation.IAggrFunction;
import org.eclipse.birt.data.engine.api.querydefn.ScriptExpression;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.expression.ExpressionCompilerUtil;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.impl.IQueryService;
import org.eclipse.birt.data.engine.olap.api.ICubeQueryResults;
import org.eclipse.birt.data.engine.olap.data.api.DimLevel;
import org.eclipse.birt.data.engine.olap.query.view.CubeQueryDefinitionUtil;
import org.eclipse.birt.data.engine.olap.script.JSCubeBindingObject;
import org.eclipse.birt.data.engine.script.ScriptConstants;
import org.mozilla.javascript.Scriptable;

/**
 * 
 */

public class OlapExpressionUtil
{

	private static Logger logger = Logger.getLogger( OlapExpressionUtil.class.getName( ) );
	/**
	 * get the attribute reference name.
	 * 
	 * @param dimName
	 * @param levelName
	 * @param attrName
	 * @return
	 */
	public static String getAttrReference( String dimName, String levelName,
			String attrName )
	{
		return dimName + '/' + levelName + '/' + attrName;
	}

	/**
	 * 
	 * @param originalMeasureName
	 * @return
	 */
	public static String createMeasureCalculateMemeberName(
			String originalMeasureName )
	{
		return "_&$" + originalMeasureName + "$&_";
	}

	/**
	 * 
	 * @param expr
	 * @return
	 */
	public static boolean isReferenceToDimLevel( String expr )
	{
		if ( expr == null )
			return false;
		return expr.matches( "\\Qdimension[\"\\E.*\\Q\"][\"\\E.*\\Q\"]\\E" );
	}


	/**
	 * This method is used to get the dimension,level,attributes name that
	 * reference by a level&attribute reference expression of following format:
	 * dimension["dimensionName"]["levelName"]["attributeName"].
	 * 
	 * String[0] dimensionName; String[1] levelName; String[2] attributeName;
	 * 
	 * @param expr
	 * @return String[]
	 * @throws DataException
	 */
	private static String[] getTargetAttribute( String expr, List bindings )
			throws DataException
	{
		if ( expr == null )
			return null;
		if ( !( expr.matches( "\\Qdimension[\"\\E.*\\Q\"][\"\\E.*\\Q\"][\"\\E.*\\Q\"]\\E" ) || expr.matches( "\\Qdimension[\"\\E.*\\Q\"][\"\\E.*\\Q\"]\\E" ) ) )
		{
			String bindingName = getBindingName( expr );
			if ( bindingName != null )
			{
				for ( int i = 0; i < bindings.size( ); i++ )
				{
					IBinding binding = (IBinding) bindings.get( i );
					if ( bindingName.equals( binding.getBindingName( ) ) )
					{
						if ( binding.getExpression( ) instanceof IScriptExpression )
						{
							return getTargetAttribute( ( (IScriptExpression) binding.getExpression( ) ).getText( ),
									bindings );
						}
					}
				}
			}

			throw new DataException( ResourceConstants.INVALID_EXPRESSION, expr );
		}

		expr = expr.replaceFirst( "\\Qdimension\\E", "" );
		String[] result = new String[3];
		String[] candidateResult = expr.split( "\\Q\"][\"\\E" );
		if ( candidateResult.length == 2 )
		{
			result[0] = candidateResult[0].replaceAll( "\\Q[\"\\E", "" );
			result[1] = candidateResult[1].replaceAll( "\\Q\"]\\E", "" );
		}
		else
		{
			result[0] = candidateResult[0].replaceAll( "\\Q[\"\\E", "" );
			result[1] = candidateResult[1];
			result[2] = candidateResult[2].replaceAll( "\\Q\"]\\E", "" );
		}
		return result;
	}

	/**
	 * 
	 * @param expr
	 * @return
	 * @throws DataException
	 */
	public static DimLevel getTargetDimLevel( String expr )
			throws DataException
	{
		if ( expr != null && expr.matches( "\\Qdimension[\"\\E.*\\Q\"][\"\\E.*\\Q\"]\\E" ) )
		{
			Set<DimLevel> s = OlapExpressionCompiler.getReferencedDimLevel( new ScriptExpression( expr ), 
				Collections.EMPTY_LIST );
			if ( s != null && s.size( ) == 1 )
			{
				return s.iterator( ).next( );
			}
		}
		throw new DataException( ResourceConstants.LEVEL_NAME_NOT_FOUND,
					expr );
	}
{'fraction_non_alphanumeric': 0.17916461916461918, 'fraction_numerical': 0.007371007371007371, 'mean_word_length': 3.702402957486137, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2774919', 'n_tokens_mistral': 3567, 'n_tokens_neox': 3148, 'n_words': 646}