<commit_before>/*******************************************************************************
 * 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.data.engine.impl;

import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.birt.core.data.DataTypeUtil;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IFilterDefinition;
import org.eclipse.birt.data.engine.api.IScriptExpression;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.odi.IResultObject;
import org.eclipse.birt.data.engine.odi.IResultObjectEvent;
import org.eclipse.birt.data.engine.script.JSRowObject;
import org.eclipse.birt.data.engine.script.ScriptEvalUtil;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

/**
 * Implementation of IFilter, which will do filtering on row data.
 */
public class FilterByRow implements IResultObjectEvent
{

	protected List filters;
	protected Scriptable scope;
	protected JSRowObject scriptObj;
	
	protected static Logger logger = Logger.getLogger( FilterByRow.class.getName( ) );
	
	FilterByRow( List filters, Scriptable scope, JSRowObject scriptObj )
	{
		this.filters = filters;
		this.scope = scope;
		this.scriptObj = scriptObj;
		logger.log( Level.FINER, "FilterByRow starts up" );
	}
	
	/*
	 * @see org.eclipse.birt.data.engine.odi.IResultObjectEvent#process(org.eclipse.birt.data.engine.odi.IResultObject)<commit_msg>Fix bugzilla bug:110566 Wrong data set result returned when set an invalid filter expression row[0]<commit_after>/*******************************************************************************
 * 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.data.engine.impl;

import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.birt.core.data.DataTypeUtil;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.data.engine.api.IBaseExpression;
import org.eclipse.birt.data.engine.api.IConditionalExpression;
import org.eclipse.birt.data.engine.api.IFilterDefinition;
import org.eclipse.birt.data.engine.api.IScriptExpression;
import org.eclipse.birt.data.engine.api.querydefn.ConditionalExpression;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
import org.eclipse.birt.data.engine.odi.IResultObject;
import org.eclipse.birt.data.engine.odi.IResultObjectEvent;
import org.eclipse.birt.data.engine.script.JSRowObject;
import org.eclipse.birt.data.engine.script.ScriptEvalUtil;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

/**
 * Implementation of IFilter, which will do filtering on row data.
 */
public class FilterByRow implements IResultObjectEvent
{

	protected List filters;
	protected Scriptable scope;
	protected JSRowObject scriptObj;
	
	protected static Logger logger = Logger.getLogger( FilterByRow.class.getName( ) );
	
	FilterByRow( List filters, Scriptable scope, JSRowObject scriptObj ) throws DataException
	{
		isLegal( filters );
		this.filters = filters;
		this.scope = scope;
		this.scriptObj = scriptObj;
		logger.log( Level.FINER, "FilterByRow starts up" );
	}
	
	/**
	 * whether the filter expression is valid. if not, throw the exception. 
	 * @param filters
	 * @throws DataException
	 */
	private void isLegal( List filters ) throws DataException
	{
		Iterator filterIt = filters.iterator( );
		while ( filterIt.hasNext( ) )
		{
			IFilterDefinition filter = (IFilterDefinition) filterIt.next( );
			IBaseExpression expr = filter.getExpression( );

			if ( expr instanceof IConditionalExpression )
			{
				String expr4Exception = ( (ConditionalExpression) expr ).getExpression( )
						.getText( );
				try
				{
					new FilterExpressionParser( null, null ).compileFilterExpression( expr4Exception );
				}
				catch ( DataException e )
				{
					throw new DataException( ResourceConstants.INVALID_EXPRESSION_IN_FILTER,
							new Object[]{
								expr4Exception
							} );
				}
			}
		}
	}
	
	/*
	 * @see org.eclipse.birt.data.engine.odi.IResultObjectEvent#process(org.eclipse.birt.data.engine.odi.IResultObject)
{'fraction_non_alphanumeric': 0.15449330783938814, 'fraction_numerical': 0.0057361376673040155, 'mean_word_length': 4.870931537598204, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2565097', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1464, 'n_words': 340}