/*
* 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 Neeraj Joshi <jneeraj@us.ibm.com>
*
*/
package org.apache.imperius.spl.parser.expression.primary;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.apache.imperius.spl.core.Expression;
import org.apache.imperius.spl.core.TypeInfo;
import org.apache.imperius.spl.parser.compiler.symboltable.MacroSymbol;
import org.apache.imperius.spl.parser.compiler.symboltable.PropertySymbol;
import org.apache.imperius.spl.parser.compiler.symboltable.SPLSymbolTable;
import org.apache.imperius.spl.parser.compiler.symboltable.Symbol;
import org.apache.imperius.spl.parser.exceptions.SPLException;
import org.apache.imperius.spl.parser.statements.impl.MacroDefinition;
import org.apache.imperius.spl.parser.util.TypeResolver;
import org.apache.imperius.util.SPLLogger;
public class MacroExpression implements Expression
{
private TypeInfo _returnType;
private MacroDefinition _macroDefn;
private List _passedParams;
private static Logger logger = SPLLogger.getSPLLogger().getLogger();
private static final String sourceClass="MacroExpression";
private boolean _isArray = false;
private String _referenceTypeName = "";
public boolean isArray()
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "isArray");
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "isArray");
return _isArray;
}
public MacroExpression(MacroSymbol ms, ArrayList pList,
SPLSymbolTable symTab) throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "MacroExpression");
_returnType = ms.getType();
if(TypeResolver.isReference(_returnType))
{
_referenceTypeName = ms.getReferenceTypeName();
}
_macroDefn = ms.getMacroDefn();
_passedParams = pList;
validate();
if(_macroDefn.isArray())
{
_isArray = true;
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "MacroExpression");
}
public Object evaluate() throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "evaluate");
// TODO Auto-generated method stub
SPLSymbolTable macroSymbolTable = _macroDefn.getMacroSymTab();
populateMacroSymbolTable(macroSymbolTable, _passedParams);
Object result = _macroDefn.evaluate();
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "evaluate");
return result;
}
private void populateMacroSymbolTable(SPLSymbolTable macroSymbolTable,
List passedParams2) throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "populateMacroSymbolTable");
Map symbolMap = macroSymbolTable.getSymbolMap();
Iterator keySetIt = symbolMap.keySet().iterator();
Iterator passedParamIt = passedParams2.iterator();
while (keySetIt.hasNext() && passedParamIt.hasNext())
{
String symbolName = (String) keySetIt.next();
Symbol sym = (Symbol) symbolMap.get(symbolName);
Expression currentParam = (Expression) passedParamIt.next();
Object value = currentParam.evaluate();
//System.out.println("symbolName,value : "+symbolName+" "+ value);
((PropertySymbol) sym).setValue(value);
}
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "populateMacroSymbolTable");
}
public TypeInfo getType()
{
return _returnType;
}
public boolean validate() throws SPLException
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "validate");
List formalParams = _macroDefn.getArgList();
TypeResolver.validateActualParameterTypes(formalParams, _passedParams);
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
return true;
}
public String toString()
{
logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "toString");
String str = _macroDefn.getMacroName()+" ( "+_macroDefn.getMacroExpression().toString()+" ) ";
logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "toString");
return str;
}
public String getReferenceTypeName() throws SPLException
{
return _referenceTypeName;
}
}