Package anvil.core.runtime

Source Code of anvil.core.runtime.AnyScope$VariableFilter

/*
* $Id: AnyScope.java,v 1.4 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.runtime;

import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
import anvil.java.util.BindingEnumeration;
import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.core.Register;
import anvil.core.Serializer;
import anvil.core.Unserializer;
import anvil.core.UnserializationException;
import anvil.script.Context;
import anvil.script.Type;
import anvil.script.CompilableFunction;
import anvil.script.Function;
import anvil.script.FunctionDispatcher;
import anvil.script.Scope;
import anvil.script.Module;
import anvil.script.VariableType;

/// @class Scope
/// Scope is a wrapper for modules and namespaces.
/// It provides the contained entities with
/// attributes or references.

/**
* class AnyScope
*
* @author: Jani Lehtim�ki
*/
public class AnyScope extends AnyAbstractClass
{

  public static final anvil.script.compiler.NativeClass __class__ =
    new anvil.script.compiler.NativeClass("Scope", AnyScope.class,
    //DOC{{
    ""+
      " @class Scope\n" +
      " Scope is a wrapper for modules and namespaces.\n" +
      " It provides the contained entities with\n" +
      " attributes or references.\n"
    //}}DOC
  );
  static {
    RuntimeModule.class.getName();
  }
 
 
  private Scope _scope;

 
  public AnyScope(Scope scope)
  {
    _scope = scope;
  }


  public final anvil.script.ClassType classOf()
  {
    return __class__;
  }


  public final Type type()
  {
    return _scope;
  }

 
  public String toString()
  {
    return _scope.toString();
  }
 
   
  public Writer toAnvil(Writer writer) throws IOException
  {
    writer.write(_scope.getName());
    return writer;
  }

 
  public Object toObject()
  {
    return _scope;
  }

  private VariableType getVariable(String name)
  {
    Type type = _scope.lookupDeclaration(name);
    if (type instanceof VariableType) {
      return (VariableType)type;
    }
    return null;
  }


  public Any getAttribute(Context context, String attribute)
  {
    VariableType var = getVariable(attribute);
    if (var != null) {
      return var.getValue();
    }
    return UNDEFINED;
  }
 
 
  public Any setAttribute(Context context, String attribute, Any value)
  {
    VariableType var = getVariable(attribute);
    if (var != null) {
      var.setValue(value);
    }
    return value;
  }
   
 
  public Any checkAttribute(Context context, String attribute)
  {
    VariableType var = getVariable(attribute);
    if (var != null) {
      return var.getValue();
    }
    return UNDEFINED; 
  }
 

  public boolean deleteAttribute(Context context, String attribute)
  {
    return false;
  }


  public Any getReference(Context context, Any index)
  {
    return getAttribute(context, index.toString());
  }
 
 
  public Any setReference(Context context, Any index, Any value)
  {
    return setAttribute(context, index.toString(), value);
  }
 
  public Any setReference(Context context, Any value)
  {
    return this;
  }


  public Any checkReference(Context context, Any index)
  {
    return checkAttribute(context, index.toString());
  }
 

  public boolean deleteReference(Context context, Any index)
  {
    return false;
  }

 
  public BindingEnumeration enumeration()
  {
    return new VariableFilter(_scope.getDeclarations());
  }


  public static final class VariableFilter implements BindingEnumeration
  {
    private Enumeration _enum;
    private boolean _hasNext;
    private VariableType _nextElement;
   
    public VariableFilter(Enumeration enum)
    {
      _enum = enum;
      forward();
    }
   
    private void forward()
    {
      Enumeration enum = _enum;
      while(enum.hasMoreElements()) {
        Object elem = enum.nextElement();
        if (elem instanceof VariableType) {
          _hasNext = true;
          _nextElement = (VariableType)elem;
          return;
        }
      }
      _hasNext = false;
    }
   
    public boolean hasMoreElements()
    {
      return _hasNext;
    }

    public Object nextKey()
    {
      return _nextElement.getName();
    }

    public Object nextElement()
    {
      Any value = _nextElement.getValue();
      forward();
      return value;
    }
 
  }


  public final void serialize(Serializer serializer) throws IOException
  {
    if (serializer.register(this)) {
      return;
    }
    serializer.write('M');
    AnyType.serializeType(serializer, _scope);
  }


  public static final Any unserialize(Unserializer unserializer) throws UnserializationException
  {
    Type type = AnyType.unserializeType(unserializer);
    if (type instanceof Scope) {
      AnyScope scope = new AnyScope((Scope)type);
      unserializer.register(scope);
      return scope;
    }
    throw new UnserializationException();
  }


  public boolean has(String methodName)
  {
    Type type = _scope.lookupDeclaration(methodName);
    return ((type != null) && (type.getType() == Type.FUNCTION));
  }


  protected FunctionDispatcher getDispatcherFor(Context context, String name)
  {
    Type type = _scope.lookupDeclaration(name);
    if ((type != null) && (type.getType() == Type.FUNCTION)) {
      return ((CompilableFunction)type).getDispatcher(context);
    } else {
      throw context.NoSuchMethod(_scope.getName() + '.' + name);
    }   
  }

  protected FunctionDispatcher getDispatcherFor(Context context, int index)
  {
    String name = Register.getNameOf(index);
    Type type = _scope.lookupDeclaration(name);
    if ((type != null) && (type.getType() == Type.FUNCTION)) {
      return ((CompilableFunction)type).getDispatcher(context);
    } else {
      throw context.NoSuchMethod(_scope.getName() + '.' + name);
    }   
  }


  public Any invoke(Context context, int methodIndex, Any[] parameters)
  {
    return getDispatcherFor(context, methodIndex).execute(context, null, parameters);
  }

  public Any invoke(Context context, int methodIndex)
  {
    return getDispatcherFor(context, methodIndex).execute(context, null);
  }

  public Any invoke(Context context, int methodIndex, Any param1)
  {
    return getDispatcherFor(context, methodIndex).execute(context, null, param1);
  }

  public Any invoke(Context context, int methodIndex, Any param1, Any param2)
  {
    return getDispatcherFor(context, methodIndex).execute(context, null, param1, param2);
  }

  public Any invoke(Context context, int methodIndex, Any param1, Any param2, Any param3)
  {
    return getDispatcherFor(context, methodIndex).execute(context, null, param1, param2, param3);
  }

  public Any invoke(Context context, int methodIndex, Any param1, Any param2, Any param3, Any param4)
  {
    return getDispatcherFor(context, methodIndex).execute(context, null, param1, param2, param3, param4);
  }


  public Any invoke(Context context, String methodName, Any[] parameters)
  {
    return getDispatcherFor(context, methodName).execute(context, null, parameters);
  }
 
  public Any invoke(Context context, String methodName)
  {
    return getDispatcherFor(context, methodName).execute(context, null);
  }

  public Any invoke(Context context, String methodName, Any param1)
  {
    return getDispatcherFor(context, methodName).execute(context, null, param1);
  }

  public Any invoke(Context context, String methodName, Any param1, Any param2)
  {
    return getDispatcherFor(context, methodName).execute(context, null, param1, param2);
  }

  public Any invoke(Context context, String methodName, Any param1, Any param2, Any param3)
  {
    return getDispatcherFor(context, methodName).execute(context, null, param1, param2, param3);
  }

  public Any invoke(Context context, String methodName, Any param1, Any param2, Any param3, Any param4)
  {
    return getDispatcherFor(context, methodName).execute(context, null, param1, param2, param3, param4);
  }


 

}
TOP

Related Classes of anvil.core.runtime.AnyScope$VariableFilter

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.