/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
//
/**
* @author Prashant Baliga <prabalig@in.ibm.com>
*
*/
package org.apache.imperius.spl.parser.expressions.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import org.apache.imperius.spl.core.Expression;
import org.apache.imperius.spl.core.TypeConstants;
import org.apache.imperius.spl.core.TypeInfo;
import org.apache.imperius.spl.parser.exceptions.IllegalParameterTypeException;
import org.apache.imperius.spl.parser.exceptions.SPLException;
import org.apache.imperius.spl.parser.expressions.TripleArgumentExpression;
import org.apache.imperius.spl.parser.util.ExpressionUtility;
import org.apache.imperius.spl.parser.util.TypeResolver;
import org.apache.imperius.util.Messages;
import org.apache.imperius.util.SPLLogger;
public class AllInCollection extends TripleArgumentExpression implements
Expression
{
public String className = InCollection.class.toString();
public List expressionList = null;
public String operationString = null;
private static Logger logger = SPLLogger.getSPLLogger().getLogger();
private static final String sourceClass="AllInCollection";
public static final String LOR = "OR";
public static final String LAND = "AND";
public static final String BXOR = "XOR";
public static final String NOT_EQUAL = "NOT_EQUAL";
public static final String EQUAL = "EQUAL";
public static final String LT = "LESS";
public static final String GT = "GREATER";
public static final String LE = "LESS_OR_EQUAL";
public static final String GE = "GREATER_OR_EQUAL";
public static final String PLUS = "PLUS";
public static final String MINUS = "MINUS";
public static final String STAR = "MULTIPLY";
public static final String DIV = "DIVIDE";
public AllInCollection(List exprList, boolean evaluateExpression)
throws SPLException
{
super(exprList);
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
//System.out.println("AllInCollection");
if (evaluateExpression)
{
//System.out.println("evaluateExpression " + evaluateExpression);
if (!validate())
{
logger.severe(
"validation error: wrong data type passed in "
+ this._dataType);
throw new SPLException(Messages.getString(
"SPL_VALIDATION_ERROR_MSG", new Object[] { className }));
}
}
_dataType.setType(TypeConstants.booleanType);
this._dataType.setIsArray(false);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
}
public Object evaluate() throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "evaluate");
//System.out.println("AllInCollection:evaluate");
Expression lhsExpression = (Expression) this._lhsExp;
Expression rhsExpression = (Expression) this._rhsExp;
if (rhsExpression.isArray())
{
//System.out.println("rhs is of type Basic Collection");
Object rhsResult = rhsExpression.evaluate();
//System.out.println("rhsResult , class " + rhsResult + " "+ rhsResult.getClass());
if (!(rhsResult instanceof java.util.List))
{
logger.severe(
"rhsResult is not of type List");
throw new SPLException(Messages.getString(
"SPL_PASSED_EXPRESSION_TYPE_ERROR_MSG",
new Object[] { "RHS","is not", "List" }));
}
Boolean result = Boolean.FALSE;
ArrayList rhsResultArray = (ArrayList) rhsResult;
//System.out.println("resultArray size " + rhsResultArray.size()+ " to string " + rhsResultArray.toString());
//System.out.println("LHS expression is of type "+ lhsExpression.getType());
if ((rhsResultArray != null) && (!rhsResultArray.isEmpty()))
{
Object lhsResult = (Object) lhsExpression.evaluate();
//System.out.println("lhsResult " + lhsResult.toString());
Iterator rhsResultIt = rhsResultArray.iterator();
while (rhsResultIt.hasNext())
{
Object value = (Object) rhsResultIt.next();
//System.out.println("rhsResult element " + value.toString());
//System.out.println("operationString " + operationString);
if (operationString.equalsIgnoreCase(LAND))
{
if ((!(value instanceof Boolean))
|| (!(lhsResult instanceof Boolean)))
{
logger.severe(
"LHS or RHS result is not of type Boolean");
throw new SPLException(Messages.getString(
"SPL_NOT_REQUIRED_EXP_EXCEPTION_MSG",
new Object[] { "boolean" }));
}
else
{
Boolean Value1 = (Boolean) lhsResult;
Boolean Value2 = (Boolean) value;
boolean res = Value1.booleanValue()
&& Value2.booleanValue();
if (!res)
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
return new Boolean(!res);
}
}
}
else if (operationString.equalsIgnoreCase(LOR))
{
if ((!(value instanceof Boolean))
|| (!(lhsResult instanceof Boolean)))
{
logger.severe(
"LHS or RHS result is not of type Boolean");
throw new SPLException(Messages.getString(
"SPL_NOT_REQUIRED_EXP_EXCEPTION_MSG",
new Object[] { "boolean" }));
}
else
{
Boolean Value1 = (Boolean) lhsResult;
Boolean Value2 = (Boolean) value;
boolean res = Value1.booleanValue()
|| Value2.booleanValue();
if (!res)
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
return new Boolean(!res);
}
}
}
else if (operationString.equalsIgnoreCase(BXOR))
{
if ((!(value instanceof Boolean))
|| (!(lhsResult instanceof Boolean)))
{
logger.severe(
"LHS or RHS result is not of type Boolean");
throw new SPLException(Messages.getString(
"SPL_NOT_REQUIRED_EXP_EXCEPTION_MSG",
new Object[] { "boolean" }));
}
else
{
Boolean Value1 = (Boolean) lhsResult;
Boolean Value2 = (Boolean) value;
boolean res = (Value1.booleanValue() && !Value2
.booleanValue())
|| (!Value1.booleanValue() && Value2
.booleanValue());
if (!res)
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
return new Boolean(!res);
}
}
}
else if (operationString.equalsIgnoreCase(EQUAL))
{
Object Value1 = lhsResult;
Object Value2 = value;
if (!(ExpressionUtility.compare(Value2, Value1) == 0))
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
return Boolean.FALSE;
}
}
else if (operationString.equalsIgnoreCase(NOT_EQUAL))
{
Object Value1 = lhsResult;
Object Value2 = value;
if (!(ExpressionUtility.compare(Value2, Value1) != 0))
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
return Boolean.FALSE;
}
}
else if (operationString.equalsIgnoreCase(GT))
{
Object Value1 = lhsResult;
Object Value2 = value;
// //System.out.println("Value1 GT Value2 "+Value1+"
// "+Value2);
if (!(ExpressionUtility.compare(Value2, Value1) > 0))
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
return Boolean.FALSE;
}
}
else if (operationString.equalsIgnoreCase(GE))
{
Object Value1 = lhsResult;
Object Value2 = value;
if (!(ExpressionUtility.compare(Value2, Value1) >= 0))
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
return Boolean.FALSE;
}
}
else if (operationString.equalsIgnoreCase(LT))
{
Object Value1 = lhsResult;
Object Value2 = value;
// //System.out.println("Value1 LT Value2 "+Value1+"
// "+Value2);
if (!(ExpressionUtility.compare(Value2, Value1) < 0))
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
return Boolean.FALSE;
}
}
else if (operationString.equalsIgnoreCase(LE))
{
Object Value1 = lhsResult;
Object Value2 = value;
if (!(ExpressionUtility.compare(Value2, Value1) <= 0))
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
return Boolean.FALSE;
}
}
else
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
//System.out.println("did not match");
return Boolean.FALSE;
}
}
//System.out.println("result=true");
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "AllInCollection");
result = Boolean.TRUE;
return result;
}
else
{
logger.severe(
"the RHS of AllInCollection expression is null BasicCollection");
throw new IllegalParameterTypeException(Messages.getString(
"SPL_PASSED_EXPRESSION_TYPE_ERROR_MSG", new Object[] {
"Right", "is null", "" }));
}
}
else
{
logger.severe(
"the RHS of AllInCollection expression should be of BasicCollection type");
throw new IllegalParameterTypeException(Messages.getString(
"SPL_PASSED_EXPRESSION_TYPE_ERROR_MSG", new Object[] {
"Right", "should be", "BasicCollection" }));
}
}
public boolean validate() throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "validate");
//System.out.println("AllInCollection : validate ");
Expression expression1 = (Expression) this._lhsExp;
Expression expression2 = (Expression) this._midExp;
Expression expression3 = (Expression) this._rhsExp;
// expression3.validate();
TypeInfo leftType = expression1.getType();
TypeInfo middleType = expression2.getType();
TypeInfo rightType = expression3.getType();
if (!rightType.getIsArray())
{
logger.severe(
"Last Expression should be a collection");
throw new SPLException(Messages.getString(
"SPL_PASSED_EXPRESSION_TYPE_ERROR_MSG", new Object[] {
"right", "is not", "collection" }));
}
if (leftType.getIsArray())
{
logger.severe(
"First Expression cannot be a collection");
throw new SPLException(Messages.getString(
"SPL_PASSED_EXPRESSION_TYPE_ERROR_MSG", new Object[] {
"left", "can not", "collection" }));
}
if (!TypeResolver.isString(middleType))
{
logger.severe(
"Middle Expression should be a string describing the Operation");
throw new SPLException(Messages.getString(
"SPL_PASSED_EXPRESSION_TYPE_ERROR_MSG", new Object[] {
"middle", "should", "string" }));
}
this.operationString = (String) expression2.evaluate();
if (this.operationString == null)
{
logger.severe(
"Operation string is null");
throw new SPLException(Messages.getString(
"SPL_PASSED_EXPRESSION_ERROR_MSG", new Object[] {
"middle" }));
}
// if (this.operationString == null)
// {
// logger.severe(
// "Operation type is "
// + this.operationString);
//
// throw new SPLException("Operation type is "
// + this.operationString);
// }
if (operationString.equalsIgnoreCase(LAND)
|| operationString.equalsIgnoreCase(LOR)
|| operationString.equalsIgnoreCase(BXOR))
{
if (TypeResolver.isBoolean(leftType))
{
if (TypeResolver.isBoolean(rightType))
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
return true;
}
else
{
logger.severe(
"LHS is of type Boolean but RHS is not of type Boolean");
throw new SPLException(Messages.getString(
"SPL_TYPE_NOT_COMPATIBLE_EXCEPTION_MSG",
new Object[] { leftType, rightType }));
}
}
else
{
logger.severe(
"Operation is of type Boolean but LHS is not of type boolean");
throw new SPLException(Messages.getString(
"SPL_PASSED_EXPRESSION_TYPE_ERROR_MSG", new Object[] {
"left", "is not", "boolean" }));
}
}
else if (operationString.equalsIgnoreCase(EQUAL)
|| operationString.equalsIgnoreCase(NOT_EQUAL)
|| operationString.equalsIgnoreCase(GT)
|| operationString.equalsIgnoreCase(GE)
|| operationString.equalsIgnoreCase(LT)
|| operationString.equalsIgnoreCase(LE))
{
if (TypeResolver.isTypeAssignableForEquality(leftType, rightType))
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
return true;
}
else if (TypeResolver.isTypeAssignableForRelation(leftType,
rightType))
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
return true;
}
else
{
logger.severe(
"types are not assignable for Relation or Equality");
throw new SPLException(Messages.getString(
"SPL_TYPE_NOT_COMPATIBLE_EXCEPTION_MSG",
new Object[] { leftType, rightType }));
}
}
else
{
logger.severe(
"operationString is not supported by AllInCollection");
throw new SPLException(Messages.getString("SPL_OPERATION_NOT_SUPPORTED_EXCEPTION_MSG"));
}
}
public String getReferenceTypeName() throws SPLException {
// TODO Auto-generated method stub
return null;
}
public String toString()
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "toString");
String str = "AllInCollection("+ this._lhsExp.toString() + "," + this._midExp.toString() + "," + this._rhsExp.toString() + ")";
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "toString");
return str;
}
}