Package anvil.core.naming

Source Code of anvil.core.naming.AnyName

/*
* $Id: AnyName.java,v 1.7 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.naming;

import javax.naming.*;

import anvil.core.Any;
import anvil.core.AnyString;
import anvil.core.AnyRange;
import anvil.core.AnyAbstractClass;
import anvil.core.ArrayUtils;
import anvil.core.IndexedEnumeration;
import anvil.java.util.BindingEnumeration;
import anvil.script.Context;

/// @class Name

/**
* class AnyName
*
* @author: Simo Tuokko
* @author: Jani Lehtim�ki
*/
public class AnyName extends AnyAbstractClass
{

  public static final anvil.script.compiler.NativeClass __class__ =
    new anvil.script.compiler.NativeClass("Name", AnyName.class,
    //DOC{{
    ""+
      " @class Name\n" +
      " @method add\n" +
      " @synopsis Name add(String comp)\n" +
      " @synopsis Name add(int index, String comp)\n" +
      " @synopsis Name add(Name name)\n" +
      " @synopsis Name add(int index, Name name)\n" +
      " @method startsWith\n" +
      " @synopsis boolean startsWith(Name prefix)\n" +
      " @method endsWith\n" +
      " @synopsis boolean endsWith(Name suffix)\n" +
      " @method get\n" +
      " @synopsis String get(int index)\n" +
      " @method getPrefix\n" +
      " @synopsis Name getPrefix(int endIndex)\n" +
      " @method getSuffix\n" +
      " @synopsis Name getSuffix(int startIndex)\n" +
      " @method isEmpty\n" +
      " @synopsis boolean isEmpty()\n" +
      " @method remove\n" +
      " @synopsis string remove(int index)\n" +
      " @method size\n" +
      " @synopsis int size()\n"
    //}}DOC
    );
  static {
    NamingModule.class.getName();
  }
 
  private Name _name;
 
 
  public AnyName(Name name)
  {
    _name = name;
  }
 
 
  public anvil.script.ClassType classOf()
  {
    return __class__;
  }


  public Object clone()
  {
    return new AnyName((Name)_name.clone());
  }


  public Any copy()
  {
    return new AnyName((Name)_name.clone());
  }


  public Object toObject()
  {
    return _name;
  }


  public boolean toBoolean()
  {
    return !_name.isEmpty();
  }
 

  public int sizeOf()
  {
    return _name.size();
  }


  public boolean equals(Object obj)
  {
    if (this == obj) {
      return true;
    }
    if (obj instanceof AnyName) {
      return _name.compareTo(((AnyName)obj)._name) == 0;
    }
    return false;
  }
 
 
  protected int compare(Any other)
  {
    if (other instanceof AnyName) {
      return _name.compareTo(((AnyName)other)._name);
    }
    return 1;
 
 
 
  public Any getReference(anvil.script.Context context, Any index)
  {
    Name name = _name;
    if (index.isRange()) {
      AnyRange r = index.toRange();
      int size   = name.size();
      Any a      = r.getLeft();
      int start  = a.isDefined() ? a.toInt() : 0;
      a          = r.getRight();
      int end    = a.isDefined() ? a.toInt() : size;
      long l     = ArrayUtils.adjust2(start, end, size);
      start      = (int)(l & 0xffffffff);
      int length = (int)(l >> 32);
      if (start == 0) {
        return new AnyName(name.getPrefix(length));
      } else if (start+length == size) {
        return new AnyName(name.getSuffix(start));
      } else {
        throw context.BadParameter("Only prefix and suffix slices can be returned from Name");
      }
     
    } else {
      int i = index.toInt();
      if (i>=0 && i<name.size()) {
        return Any.create(name.get(i));
      }
    }
    return UNDEFINED;
  }


  public boolean deleteReference(anvil.script.Context context, Any index)
  {
    try {
      Name name = _name;
      if (index.isRange()) {
        AnyRange r = index.toRange();
        int size   = _name.size();
        Any a      = r.getLeft();
        int start  = a.isDefined() ? a.toInt() : 0;
        a          = r.getRight();
        int end    = a.isDefined() ? a.toInt() : size;
        long l     = ArrayUtils.adjust2(start, end, size);
        start      = (int)(l & 0xffffffff);
        int length = (int)(l >> 32);
        while(length-- > 0) {
          name.remove(start);
        }
        return true;
      } else {
        int i = index.toInt();
        if (i>=0 && i<name.size()) {
          name.remove(i);
          return true;
        }
      }
     return false;
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }


  public Any checkReference(anvil.script.Context context, Any index)
  {
    return getReference(context, index);
  }


  public Any setReference(anvil.script.Context context, Any value)
  {
    try {
      if (value instanceof AnyName) {
        _name.addAll((Name)value.toObject());
      } else {
        _name.add(value.toString());
      }
    } catch (NamingException e) {
      throw context.exception(e);
    }
    return value;
  }


  public BindingEnumeration enumeration()
  {
    return new IndexedEnumeration(_name.getAll());
  }


  /// @method add
  /// @synopsis Name add(String comp)
  /// @synopsis Name add(int index, String comp)
  /// @synopsis Name add(Name name)
  /// @synopsis Name add(int index, Name name)
  public Any m_add(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "add");
    }
    try {
      if (parameters.length == 1) {
        Any param = parameters[0];
        if (param instanceof AnyName) {
          _name.addAll((Name)param.toObject());
        } else {
          _name.add(param.toString());
        }
      } else {
        int index = parameters[0].toInt();
        if (index>=0 && index<=_name.size()) {
          Any param = parameters[1];
          if (param instanceof AnyName) {
            _name.addAll(index, (Name)param.toObject());
          } else {
            _name.add(index, param.toString());
          }
        }
      }
    } catch (Exception e) {
      throw context.exception(e);
    }
    return this;
  }


  /// @method startsWith
  /// @synopsis boolean startsWith(Name prefix)
  public Any m_startsWith(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "startsWith");
    }
    try {
      Any param = parameters[0];
      if (!(param instanceof AnyName)) {
        throw context.BadParameter("Name expected");
      }
      return Any.create(_name.startsWith((Name)param.toObject()));
    } catch (Exception e) {
      throw context.exception(e);
    }
  }


  /// @method endsWith
  /// @synopsis boolean endsWith(Name suffix)
  public Any m_endsWith(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "endsWith");
    }
    try {
      Any param = parameters[0];
      if (!(param instanceof AnyName)) {
        throw context.BadParameter("Name expected");
      }
      return Any.create(_name.endsWith((Name)param.toObject()));
    } catch (Exception e) {
      throw context.exception(e);
    }
  }


  /// @method get
  /// @synopsis String get(int index)
  public Any m_get(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "get");
    }
    int index = parameters[0].toInt();
    if (index>=0 && index<=_name.size()) {
      return Any.create(_name.get(index));
    } else {
      return UNDEFINED;
    }
  }


  /// @method getPrefix
  /// @synopsis Name getPrefix(int endIndex)
  public Any m_getPrefix(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "getPrefix");
    }
    try {
      int index = parameters[0].toInt();
      if (index>=0 && index<=_name.size()) {
        return new AnyName(_name.getPrefix(index));
      } else {
        return UNDEFINED;
      }
    } catch (Exception e) {
      throw context.exception(e);
    }
  }


  /// @method getSuffix
  /// @synopsis Name getSuffix(int startIndex)
  public Any m_getSuffix(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "getSuffix");
    }
    int index = parameters[0].toInt();
    if (index>=0 && index<=_name.size()) {
      return new AnyName(_name.getSuffix(index));
    } else {
      return UNDEFINED;
    }
  }


  /// @method isEmpty
  /// @synopsis boolean isEmpty()
  public Any m_isEmpty(anvil.script.Context context, Any[] parameters)
  {
    return Any.create(_name.isEmpty());
  }


  /// @method remove
  /// @synopsis string remove(int index)
  public Any m_remove(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "remove");
    }
    try {
      int index = parameters[0].toInt();
      if (index>=0 && index<=_name.size()) {
        return Any.create(_name.remove(index));
      }
      return UNDEFINED;
    } catch(NamingException e) {
      throw context.exception(e);
    }
  }


  /// @method size
  /// @synopsis int size()
  public Any m_size(anvil.script.Context context, Any[] parameters)
  {
    return Any.create(_name.size());
  }



}
TOP

Related Classes of anvil.core.naming.AnyName

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.