/*
* 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 Xiping Wang
* @modified Neeraj Joshi
*
*/
package org.apache.imperius.spl.parser.expressions.impl;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Logger;
import org.apache.imperius.spl.core.TypeConstants;
import org.apache.imperius.spl.core.TypeInfo;
import org.apache.imperius.spl.parser.exceptions.SPLException;
import org.apache.imperius.spl.parser.expressions.NumericExpression;
import org.apache.imperius.spl.parser.expressions.SingleArgumentExpression;
import org.apache.imperius.spl.parser.util.TypeResolver;
import org.apache.imperius.util.Messages;
import org.apache.imperius.util.SPLLogger;
public class GetMonth extends SingleArgumentExpression implements
NumericExpression
{
public static final String className = GetMonth.class.getName();
private static Logger logger = SPLLogger.getSPLLogger().getLogger();
private static final String sourceClass="GetMonth";
public GetMonth(List exprList, boolean validateExpression)
throws SPLException
{
super(exprList);
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "GetMonth");
if (validateExpression)
{
if (!validate())
{
logger.severe(Thread.currentThread().getName()+" "+"validation error: " + className
+ " has wrong data type passed in.");
throw new SPLException(Messages.getString(
"SPL_VALIDATION_ERROR_MSG", new Object[] { className }));
}
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "GetMonth");
}
public Object evaluate() throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "evaluate");
/*
* this method returns an integer number corresponding to the day of the
* month where the first day of the month has value 0. The parser and
* validator should ensure the argument is of type calendar type.
*/
try
{
Calendar calendar = (Calendar) _exp.evaluate();
// using translation to insulate from future JVM constant changes
int result = 0;
switch (calendar.get(Calendar.MONTH))
{
case Calendar.JANUARY:
result = 0;
break;
case Calendar.FEBRUARY:
result = 1;
break;
case Calendar.MARCH:
result = 2;
break;
case Calendar.APRIL:
result = 3;
break;
case Calendar.MAY:
result = 4;
break;
case Calendar.JUNE:
result = 5;
break;
case Calendar.JULY:
result = 6;
break;
case Calendar.AUGUST:
result = 7;
break;
case Calendar.SEPTEMBER:
result = 8;
break;
case Calendar.OCTOBER:
result = 9;
break;
case Calendar.NOVEMBER:
result = 10;
break;
case Calendar.DECEMBER:
result = 11;
break;
case Calendar.UNDECIMBER:
result = 12;
break;
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "evaluate");
return new Integer(result);
}
catch (Exception e)
{
logger.severe(Thread.currentThread().getName()+" "+"evaluation error: " + e.toString());
throw new SPLException(Messages.getString(
"SPL_EVALUATION_ERROR_MSG", new Object[] { e
.getLocalizedMessage() }));
}
}
public boolean validate() throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "validate");
TypeInfo eType = _exp.getType();
if (!eType.getIsArray() && TypeResolver.isCalendar(eType))
{
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
_dataType.setType(TypeConstants.intType);
return true;
}
else
{
throw new SPLException(Messages.getString(
"SPL_NOT_REQUIRED_EXP_EXCEPTION_MSG", new Object[] { eType }));
}
}
public String toString()
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "toString");
String str = "GetMonth("+this._exp.toString() +")";
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "toString");
return str;
}
}