Package org.apache.imperius.spl.parser.compiler.symboltable

Source Code of org.apache.imperius.spl.parser.compiler.symboltable.SPLSymbolTable

/*
* 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.compiler.symboltable;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.imperius.spl.core.DataCollector;
import org.apache.imperius.spl.core.InstanceInfo;
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.exceptions.SymbolAlreadyDefinedException;
import org.apache.imperius.spl.parser.statements.impl.MacroDefinition;
import org.apache.imperius.spl.parser.util.DataCollectorFactory;
import org.apache.imperius.spl.parser.util.TypeInfoImpl;
import org.apache.imperius.util.Messages;
import org.apache.imperius.util.SPLLogger;


public class SPLSymbolTable
{
  public static final int COLLECT = 1;
  public static final int MACRO = 2;
  public static final int DEFAULT = 3;
  public static final int BASICCOLLECT = 4;
  public static final int POLICYGROUP = 5;
  public static final int UNDEFINED = -1;
  private AnchorData _anchorData = null;
  private static Logger logger = SPLLogger.getSPLLogger().getLogger();
  private static final String sourceClass = "SPLSymbolTable";
  private Map _symbolMap = null;
  private List _subSymbolTables = null;
  private SPLSymbolTable _parentTable = null;
  private int _symbolTableType = UNDEFINED;
  private DataCollector _dataCollector = null;

  private String _defaultQualifier = null;
 
  public void addAnchor(String className,
      String qualifier,
      List instanceInfoList) throws SPLException
  {
    if(_anchorData == null)
    {
      _anchorData = new AnchorData();
    }

    if(instanceInfoList.size()==0)
    {
      InstanceInfo iI=new InstanceInfo(className,null);
            instanceInfoList.add(iI);
    }
    _anchorData.addInstanceInfoListForClass(className, instanceInfoList);
    _anchorData.addQualifierForClass(className, qualifier);
    if(qualifier!=null)
    {
      _defaultQualifier = qualifier;
     
    }
    _populateAnchorClassSymbols(className, qualifier, instanceInfoList);
  }
 
  public AnchorData getAnchorData()
  {
    return _anchorData;
  }
 
  public void setAnchorData(AnchorData anchorData)
  {
    _anchorData = anchorData;
  }
 
 
  public SPLSymbolTable(SPLSymbolTable pt)
  {
    this(pt,DEFAULT);
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "SPLSymbolTable");

    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "SPLSymbolTable");
  }
 
 
  public SPLSymbolTable(SPLSymbolTable pt,int type)
  {
    this();
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "SPLSymbolTable");
   
    this._symbolTableType = type;
    if(_symbolTableType!= COLLECT && _symbolTableType!= POLICYGROUP)
    { 
      this._anchorData = pt.getAnchorData();
   
    this._dataCollector = DataCollectorFactory.getDataCollector();
    this._defaultQualifier=pt.getDefaultQualifier();
    this.setParentSymbolTable(pt);

    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "SPLSymbolTable");
  }
 
  public SPLSymbolTable()
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "SPLSymbolTable");

    _subSymbolTables = new ArrayList();
    _symbolMap = new LinkedHashMap();

    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "SPLSymbolTable");
  }
 
  public void addChildSymbolTable(SPLSymbolTable c)
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "addChildSymbolTable");

    _subSymbolTables.add(c);
    c._parentTable=this;
   
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "addChildSymbolTable");
  }
 
  public SPLSymbolTable getParentSymbolTable()
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "getParentSymbolTable");
   
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "getParentSymbolTable");
   
    return _parentTable;
  }
 
  public void insertVariableSymbol(String name,
      TypeInfo type,
      boolean isKey,
      boolean isClassProp) throws SPLException
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "insertVariableSymbol");

    if(!_symbolMap.containsKey(name))
    { 
      Symbol cimSym = new PropertySymbol(name,type,isKey,isClassProp);
      _symbolMap.put(name, cimSym);
    }
    else
    {
      logger.log(Level.SEVERE, Messages.SPLOF0009E, new Object[]{name});
      throw new SymbolAlreadyDefinedException(Messages.getString("SPL_SYMBOL_ALREADY_EXISTS_MSG") + name);
    }
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "insertVariableSymbol");
  }
 
  public void insertVariableSymbol(String name,
      int type,
      String referenceTypeName,
      boolean isArray,
      boolean isKey,
      boolean isClassProp) throws SPLException
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "insertVariableSymbol");

    if(!_symbolMap.containsKey(name))
    { 
      TypeInfo tp = new TypeInfoImpl(type,referenceTypeName,isArray);
      Symbol cimSym = new PropertySymbol(name,tp,isKey,isClassProp);
      _symbolMap.put(name, cimSym);
    }
    else
    {
      logger.log(Level.SEVERE, Messages.SPLOF0009E, new Object[]{name});
      throw new SymbolAlreadyDefinedException(Messages.getString("SPL_SYMBOL_ALREADY_EXISTS_MSG") + name);
    }
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "insertVariableSymbol");
  }
 
  public void insertMethodSymbol(String name,
      TypeInfo retType,
      boolean isReturnArray,
      String returnReferenceName,
      List argTypeList,
      SPLSymbolTable argsSymbolTable) throws SymbolAlreadyDefinedException
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "insertMethodSymbol");
   
    if(!_symbolMap.containsKey(name))
    { 
      Symbol cimSym = new MethodSymbol(name,
          retType,
          argTypeList,
          argsSymbolTable);
      _symbolMap.put(name, cimSym);
    }
    else
    {
      logger.log(Level.SEVERE, Messages.SPLOF0009E, new Object[]{name});
      throw new SymbolAlreadyDefinedException(Messages.getString("SPL_SYMBOL_ALREADY_EXISTS_MSG") + name);
    }
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "insertMethodSymbol");
  }
 
  public void insertMacroSymbol(String name,
      String retType,
      boolean isArray,
      ArrayList argTypeList,
      MacroDefinition macroDef) throws SPLException
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "insertMacroSymbol");

    int type = TypeConstants.getActualType(retType);
    TypeInfo returnType = new TypeInfoImpl(type,null,isArray);
    if(!_symbolMap.containsKey(name))
    { 
      Symbol cimSym = new MacroSymbol(name,returnType,argTypeList,macroDef);
      _symbolMap.put(name, cimSym);
    }
    else
    {
      logger.log(Level.SEVERE, Messages.SPLOF0009E, new Object[]{name});
      throw new SymbolAlreadyDefinedException(Messages.getString("SPL_SYMBOL_ALREADY_EXISTS_MSG") + name);
    }
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "insertMacroSymbol");
  }
 
//  public Symbol getSymbol(String name) throws SPLException
//  {
//    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "getSymbol");
//    name=name.trim();
//    //System.out.println("getSymbol "+name);
//    Symbol retSym = null;
//    if(_symbolMap.containsKey(name))
//    { 
//      //System.out.println("_symbolMap contains "+name);
//      retSym = (Symbol)_symbolMap.get(name);
//    }
//    else
//    {
//      //System.out.println("_symbolMap does not contains "+name);
//      if(_parentTable != null)
//      {
//        //System.out.println("trying parent symboltable");
//        retSym = (Symbol)_parentTable.getSymbol(name);
//      }
//    }
//    if (retSym==null)
//    {
//      throw new SPLException("Symbol does not exist " + name);
//    }
//    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "getSymbol");
//    return retSym;
//  }

  // xiping's modification to support java overloading, 06/19/09 
  public Object getSymbol(String name) throws SPLException
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "getSymbol");
    name = name.trim();
    //System.out.println("getSymbol "+name);
//    Symbol retSym = null;
   
    // xiping 09-16-2009
    // obj can a collection of MethodSymbols that have the same name or
    // a PropertySymbol
    Object obj = null;
    if(_symbolMap.containsKey(name))
    { 
      //System.out.println("_symbolMap contains "+name);
//      retSym = (Symbol)_symbolMap.get(name);
      obj = _symbolMap.get(name);
    }
    else
    {
      //System.out.println("_symbolMap does not contains "+name);
      if(_parentTable != null)
      {
        //System.out.println("trying parent symboltable");
//        retSym = (Symbol)_parentTable.getSymbol(name);
        obj = _parentTable.getSymbol(name);
      }
    }

//    if (retSym==null)
   
    if (obj == null)
    {
      throw new SPLException(Messages.getString("SPL_SYMBOL_DOES_NOT_EXISTS_MSG") + name);
    }
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "getSymbol");
 
//    return retSym;
   
    return obj;
  }
 
  public boolean symbolExists(String name, boolean recurse)
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "symbolExists");

    boolean res = false;
    if(_symbolMap.containsKey(name))
    { 
      res = true;
    }
    else
    {
      if(_parentTable != null && recurse)
      {
        res = _parentTable.symbolExists(name, recurse);
      }
    }
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "symbolExists");
    return res;
  }
 
 
  private void _populateAnchorClassSymbols(String className,
      String qualifier,
      List instanceInfoList)
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" " +
        "populateAnchorElementSymbols");
   
    Map sMap = null;
    try
    {
      //System.out.println("populateAnchorElementSymbols ::
      //_anchorElement,nameSpace :"+_anchorElement+"  "+nameSpace);
         
      if(_dataCollector == null)
      {
        _dataCollector = DataCollectorFactory.getDataCollector();
      }
       
      //System.out.println("trying to get class "+className+" "+qualifier);
      //System.out.println("_dataCollector "+_dataCollector);
      sMap = _dataCollector.getSymbolsForClass(className,qualifier);
      //System.out.println("sMap "+sMap);
      Iterator it = sMap.keySet().iterator();

      // xiping 09-16-2009
      // at this point we have the methods (MethodSymbol) w/ the same name stored in a vector
      // which is stored in Map and field members (PropertySymbol) stored directly in Map.
      while(it.hasNext())
      {
        String key = (String)it.next();
//        Symbol s = (Symbol)sMap.get(key);

        // xiping 06-19-2009
        Object obj = sMap.get(key);
        //
       
        Iterator instanceInfoListIt = instanceInfoList.iterator();
        if(instanceInfoList != null && !instanceInfoList.isEmpty())
        { 
          while(instanceInfoListIt.hasNext())
          { 
            InstanceInfo instanceInfo = (InstanceInfo)instanceInfoListIt.next();
            String instanceName = instanceInfo.getInstanceName();
//            _symbolMap.put(instanceName + "." + key,s);

//            _symbolMap.put(instanceName + "." + key, v);

            // xiping's test 06/19/09
            _symbolMap.put(instanceName + "." + key, obj);
//
            //System.out.println("Symbol added to symbolmap : "+instanceName + "." + key);
         
         
        }
        else
        {
//          _symbolMap.put(className + "." + key,s);

          // xiping's test 06/19/09
          _symbolMap.put(className + "." + key, obj);
          //
         
          //System.out.println("Symbol added to symbolmap : "+className + "." + key);
         
        }
      }
       
    }
    catch (SPLException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
   
   
  /*  insertVariableSymbol(_anchorElement + ".intprop",TypeConstants.intType,false);
    insertVariableSymbol(_anchorElement + ".intprop2",TypeConstants.intType,false);
    insertVariableSymbol(_anchorElement + ".doubleprop",TypeConstants.doubleType,false);
    insertVariableSymbol(_anchorElement + ".stringprop3",TypeConstants.stringType,false);*/
   
   
    // TODO Auto-generated method stub
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "populateAnchorElementSymbols");
   
  }
 

  /*private void _populateCollectAnchorClassSymbols(String className,
      String qualifier,
      List instanceInfoList)
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" " +
        "populateAnchorElementSymbols");
    Map sMap = null;
    try
    {
      if(_dataCollector == null)
      {
        _dataCollector = DataCollectorFactory.getDataCollector();
      }
      sMap = _dataCollector.getSymbolsForClass(className,qualifier);
      Iterator it = sMap.keySet().iterator();
      while(it.hasNext())
      {
        String key = (String)it.next();
        Symbol s = (Symbol)sMap.get(key);
        Iterator instanceInfoListIt = instanceInfoList.iterator();
        if(instanceInfoList != null && !instanceInfoList.isEmpty())
        { 
          while(instanceInfoListIt.hasNext())
          { 
            InstanceInfo instanceInfo = (InstanceInfo)instanceInfoListIt.next();
            String instanceName = instanceInfo.getInstanceName();
            _symbolMap.put(instanceName + "." + key,s);
          } 
        }
        else
        {
          _symbolMap.put(className + "." + key,s);
        }
      }
    }
    catch (SPLException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
   
  logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "populateAnchorElementSymbols");
   
  }*/
 
  public void populateInstanceValuesForInstance(
      String className,
      String instanceName,
      Object instance,
      Map currentInstanceSymbols) throws SPLException
  {
    logger.entering(sourceClass,Thread.currentThread().getName()
        +" "+ "populateInstanceValuesForInstance");

    resetSymbolValues();
    Iterator keyIt = currentInstanceSymbols.keySet().iterator();
    while(keyIt.hasNext())
    {
      String currentKey = (String)keyIt.next();
      String qualifiedKey = currentKey;
      qualifiedKey = instanceName + "." + currentKey;
            PropertySymbol sym = (PropertySymbol)getSymbol(qualifiedKey);
      Object value = (Object)currentInstanceSymbols.get(currentKey);
      sym.setValue(value);
      if(logger.isLoggable(Level.FINE))
              logger.fine(Thread.currentThread().getName() + " SPLSymbolTable setting symbol:" +sym.getName()+" from:"+sym.getValue()+" to:"+value);
    }
    _insertInstanceObject(className, instanceName, instance);
 
 
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "populateSymbolTableWithInstanceValues");
  }

 
  private void _insertInstanceObject(String className,
      String instanceName,
      Object instance) throws SPLException
  {
    _anchorData.insertInstanceObject(className, instanceName, instance);
     
  }


  public void resetSymbolValues()
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "resetSymbolValues");
    //TBD
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "resetSymbolValues");
  }

 
  public void setParentSymbolTable(SPLSymbolTable table) {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "setParentSymbolTable");
   
    this._parentTable = table;
    table._subSymbolTables.add(this);
   
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "setParentSymbolTable");
   
  }
  public List getSubSymbolTables() {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "getSubSymbolTables");
   
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "getSubSymbolTables");
   
    return _subSymbolTables;
  }
  public void setSubSymbolTables(List symbolTables) {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "setSubSymbolTables");
   
    _subSymbolTables = symbolTables;
   
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "setSubSymbolTables");
   
  }
  public Map getSymbolMap() {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "getSymbolMap");
   
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "getSymbolMap");
   
    return _symbolMap;
  }

  public void setSymbolMap(Map map) {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "setSymbolMap");
 
    _symbolMap = map;
   
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "setSymbolMap");
  }
 
  public int getSymbolTableType() {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "getSymbolTableType");
   
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "getSymbolTableType");
   
    return _symbolTableType;
  }
 
  public void setSymbolTableType(int tp) {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "setSymbolTableType");
 
    this._symbolTableType = tp;
   
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "setSymbolTableType");
  }
 
  public void setDataCollector(DataCollector dataCollector)
  {
    logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "setDataCollector");
 
    if(dataCollector == null)
    {
      this._dataCollector = DataCollectorFactory.getDataCollector();
    }
    else
    {
      this._dataCollector = dataCollector;
    }
   
    logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "setDataCollector");
  }
 
  public List getAnchorClassList()
  {
    List anchorClassList = _anchorData.getAnchorClassList();
    return anchorClassList;
  }
 
  public String getQualifierForClass(String className)
  {
    String qualifier = _anchorData.getQualifierForClass(className);
    return qualifier;
  }

  public List getInstanceInfoList(String className)
  {
    List instanceInfoList = _anchorData.getInstanceInfoList(className);
    return instanceInfoList;
  }

  public Object getInstance(String instanceName)
  {
    Object instance = null;
    instance = _anchorData.getInstance(instanceName);
    return instance;
  }

  public String getDefaultQualifier()
  {
    return _defaultQualifier;
  }

  public void setDefaultQualifier(String qualifier)
  {
    _defaultQualifier = qualifier;
  }
 
/*
  public void populateInstanceValuesForCollect(String className,
      String instanceName, Object instance, Map currentInstanceSymbols)
      throws SPLException {
    logger.entering(sourceClass, Thread.currentThread().getName() + " "
        + "populateSymbolTableWithInstanceValues");
    // System.out.println("SPLSymbolTable::populateSymbolTableWithInstanceValues");
    resetSymbolValues();

    Iterator keyIt = currentInstanceSymbols.keySet().iterator();
    while (keyIt.hasNext()) {

      String currentKey = (String) keyIt.next();
      String qualifiedKey = currentKey;
      qualifiedKey = instanceName + "." + currentKey;

      // System.out.println("currentKey "+currentKey);
      PropertySymbol sym = (PropertySymbol) getSymbol(qualifiedKey);

      Object value = (Object) currentInstanceSymbols.get(currentKey);
      sym.setValue(value);
      if (logger.isLoggable(Level.FINE)) {
        logger.fine(Thread.currentThread().getName()
            + " SPLSymbolTable setting symbol:" + sym.getName()
            + " from:" + sym.getValue() + " to:" + value);
      }

    }

    logger.exiting(sourceClass, Thread.currentThread().getName() + " "
        + "populateSymbolTableWithInstanceValues");
  }

  private void _populateCollectAnchorClassSymbols(String className,
      String qualifier, List instanceInfoList) {
    logger.entering(sourceClass, Thread.currentThread().getName() + " "
        + "populateAnchorElementSymbols");
    Map sMap = null;
    try {
      // System.out.println("populateAnchorElementSymbols ::
      // _anchorElement,nameSpace :"+_anchorElement+" "+nameSpace);

      if (_dataCollector == null) {
        _dataCollector = DataCollectorFactory.getDataCollector();
      }

      sMap = _dataCollector.getSymbolsForClass(className, qualifier);
      Iterator it = sMap.keySet().iterator();

      while (it.hasNext()) {
        String key = (String) it.next();
        Symbol s = (Symbol) sMap.get(key);
        Iterator instanceInfoListIt = instanceInfoList.iterator();
        if (instanceInfoList != null && !instanceInfoList.isEmpty()) {
          while (instanceInfoListIt.hasNext()) {
            InstanceInfo instanceInfo = (InstanceInfo) instanceInfoListIt
                .next();
            String instanceName = instanceInfo.getInstanceName();
            _symbolMap.put(instanceName + "." + key, s);
            // System.out.println("Symbol added to symbolmap :
            // "+instanceName + "." + key);
          }

        } else {
          _symbolMap.put(className + "." + key, s);
          // System.out.println("Symbol added to symbolmap :
          // "+className + "." + key);

        }
      }

    } catch (SPLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    logger.exiting(sourceClass, Thread.currentThread().getName() + " "
        + "populateAnchorElementSymbols");

  }

  public DataCollector getDataCollector() {
    logger.entering(sourceClass, Thread.currentThread().getName() + " "
        + "getDataCollector");
    logger.exiting(sourceClass, Thread.currentThread().getName() + " "
        + "getDataCollector");

    return _dataCollector;

  }
*/ 

}
 
TOP

Related Classes of org.apache.imperius.spl.parser.compiler.symboltable.SPLSymbolTable

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.