Package org.apache.imperius.spl.parser.expressions.impl

Source Code of org.apache.imperius.spl.parser.expressions.impl.Substring

/*
* 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.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.SPLException;
import org.apache.imperius.spl.parser.expressions.StringExpression;
import org.apache.imperius.spl.parser.util.TypeInfoImpl;
import org.apache.imperius.spl.parser.util.TypeResolver;
import org.apache.imperius.util.Messages;
import org.apache.imperius.util.SPLLogger;


public class Substring implements StringExpression
{
   
    public static final String className = Substring.class.getName();
   
    private Expression _lhsExp;
   
    private Expression _midExp;
   
    private Expression _rhsExp;
   
    private TypeInfo _dataType= new TypeInfoImpl();;
   
    private boolean _isArray = false;
   
    private static Logger logger = SPLLogger.getSPLLogger().getLogger();
    private static final String sourceClass="Substring";
   
   
   
    public boolean isArray()
    {

        logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "isArray");

        logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "isArray");
    
        return _isArray;
    }
   
    public Substring(List exprList, boolean validateExpression)
            throws SPLException
    {
        logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "Substring");

        if (exprList != null)
        {
            if (exprList.size() == 3)
            {
                Expression l = (Expression) exprList.get(0);
                Expression m = (Expression) exprList.get(1);
                Expression r = (Expression) exprList.get(2);
                if (l == null)
                {
                    logger.severe(
                    "left hand side expression passed in is null.");
                    throw new IllegalArgumentException(Messages.getString("SPL_PASSED_EXPRESSION_ERROR_MSG", new Object[]{"left hand"}));

                }
               
                if (m == null)
                {
                    logger.severe(
                    "middle expression passed in is null.");
                    throw new IllegalArgumentException(Messages.getString("SPL_PASSED_EXPRESSION_ERROR_MSG", new Object[]{"middle"}));                }
               
                if (r == null)
                {
                    logger.severe(
                "right hand side expression passed in is null.");
                    throw new IllegalArgumentException(Messages.getString("SPL_PASSED_EXPRESSION_ERROR_MSG", new Object[]{"right hand"}));                }
               
                this._lhsExp = l;
                this._midExp = m;
                this._rhsExp = r;
               
            }
            else if (exprList.size() == 2)
            {
                Expression l = (Expression) exprList.get(0);
                Expression m = (Expression) exprList.get(1);
               
                if (l == null)
                {
                    logger.severe(
                "left hand side expression passed in is null.");
                    throw new IllegalArgumentException(Messages.getString("SPL_PASSED_EXPRESSION_ERROR_MSG", new Object[]{"left hand"}));                }
               
                if (m == null)
                {
                    logger.severe(
                    "middle expression passed in is null.");
                    throw new IllegalArgumentException(Messages.getString("SPL_PASSED_EXPRESSION_ERROR_MSG", new Object[]{"middle"}));                }
               
                this._lhsExp = l;
                this._midExp = m;
                this._rhsExp = null;
               
            }
            else
            {
                logger.severe(
                "number of parameters passed is not 3.");
                throw new IllegalArgumentException(Messages.getString("SPL_NO_OF_ARGUMENTS_PASSED_ERROR_MSG", new Object[]{"3", ""+exprList.size()}));

            }
        }
        else
        {
            logger.severe(Thread.currentThread().getName()+" "+"parameters passed are null.");
            throw new IllegalArgumentException(Messages.getString("SPL_NO_OF_ARGUMENTS_PASSED_ERROR_MSG", new Object[]{"3", "null"}));
        }
        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()+" "+ "Substring");
       
    }
   
    public Object evaluate() throws SPLException
    {
        logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "evaluate");

        try
        {
          String result = "";
            String o1 = (String) _lhsExp.evaluate();
            Number o2 = (Number) _midExp.evaluate();
            if (_rhsExp != null)
            {
                Number o3 = (Number) _rhsExp.evaluate();
                result = o1.substring(((Integer) o2).intValue(), ((Integer) o3)
                        .intValue());
                logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "evaluate" + result);
               
                return result;
            }
            else
            {
              result = o1.substring(((Integer) o2).intValue());
                logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "evaluate" + result);
               
                return 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 lType = _lhsExp.getType();
        TypeInfo mType = _midExp.getType();
       
        _dataType.setType(TypeConstants.stringType);
        if(!lType.getIsArray() && !mType.getIsArray())
        { 
          if (_rhsExp != null)
          {
            TypeInfo rType = _rhsExp.getType();
              if (!rType.getIsArray() && TypeResolver.isString(lType) &&
                  TypeResolver.isNumeric(mType) &&
                  TypeResolver.isNumeric(rType))
              {
                  logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
                 
                  return true;
              }
          }
          else
          {
              if (TypeResolver.isString(lType) && TypeResolver.isNumeric(mType))
              {
                  logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
                 
                  return true;
              }
          }
        }   

        logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
    
        return false;
    }
   
    public TypeInfo getType() throws SPLException
    {
        logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "getType");

        logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "getType");
    
       
        return _dataType;
    }
   
    public String toString()
    {
        logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "toString");
       
        String className=this.getClass().getName();
       
        String str=className.substring(className.lastIndexOf(".")+1, className.length())+"( ";
        if(this._lhsExp!=null){
            str+=this._lhsExp.toString();
        }
        if(this._midExp!=null){
            str+=this._midExp.toString();
        }
        if(this._rhsExp!=null){
            str+=this._rhsExp.toString();
        }
        str+=" )";
       
        logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "toString");
      
        return str;
    }

  public String getReferenceTypeName() throws SPLException {
    // TODO Auto-generated method stub
    return null;
  }
   
}
TOP

Related Classes of org.apache.imperius.spl.parser.expressions.impl.Substring

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.