Package anvil.core.naming

Source Code of anvil.core.naming.AnyAttribute

/*
* $Id: AnyAttribute.java,v 1.9 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.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.BasicAttribute;
import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.core.AnyBindingEnumeration;
import anvil.core.IndexedEnumeration;
import anvil.java.util.BindingEnumeration;
import anvil.script.Context;
import java.util.NoSuchElementException;

/// @class Attribute
/// This class represents an attribute associated with a named object.
/// @see javax.naming.directory.Attribute
///
/// @operator "(boolean)<b>Attribute</b>"
/// Returns true if this attribute has values.
///
/// @operator "<b>sizeof</b> <b>Attribute</b>"
/// Returns the number of values in this attribute.
///
/// @operator "object <b>Attribute</b>[int index]"
/// Retrieves the value at given index.
///
/// @operator "delete <b>Attribute</b>[int index]"
/// Deletes the value at given index.
///
/// @operator "value in <b>Attribute</b>"
/// Checks if given value is contained in this attribute.
///
/// @operator "enumeration *<b>Attribute</b>"
/// Returns enumeration of values in this attribute.

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


  /// @constructor Attribute
  /// @synopsis Attribute(string id, values..)
  /// @synopsis Attribute(string id, boolean ordered, values..)
  public static final Object[] newInstance = { "id", "values" };
  public static final Any newInstance(String id, Any[] values)
  {
    boolean ordered = true;
    int i = 0;
    int n = values.length;
    if (n > 0) {
      if (values[0].isBoolean()) {
        ordered = values[0].toBoolean();
        i++;
      }
    }
    BasicAttribute attr = new BasicAttribute(id, ordered);
    for(; i<n; i++) {
      attr.add(values[i].toObject());
    }
    return new AnyAttribute(attr);
  }


  private Attribute _attribute;
 
 
  public AnyAttribute(Attribute attribute)
  {
    _attribute = attribute;
  }
 

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


  public Object clone()
  {
    return new AnyAttribute((Attribute)_attribute.clone());
  }


  public Any copy()
  {
    return new AnyAttribute((Attribute)_attribute.clone());
  }


  public Object toObject()
  {
    return _attribute;
  }


  public boolean toBoolean()
  {
    return _attribute.size() > 0;
  }
 

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


  public Any getReference(anvil.script.Context context, Any index)
  {
    try {
      Attribute attr = _attribute;
      int i = index.toInt();
      if (i>=0 && i<attr.size()) {
        return Any.create(attr.get(i));
      }
      return UNDEFINED;
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }


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


  public boolean deleteReference(anvil.script.Context context, Any index)
  {
    Attribute attr = _attribute;
    int i = index.toInt();
    if (i>=0 && i<attr.size()) {
      attr.remove(i);
      return true;
    }
    return false;
  }


  public Any setReference(anvil.script.Context context, Any index, Any value)
  {
    Attribute attr = _attribute;
    int i = index.toInt();
    if (i>=0 && i<attr.size()) {
      Any.create(attr.set(i, value.toObject()));
    }
    return value;
  }


  public Any setReference(anvil.script.Context context, Any value)
  {
    _attribute.add(value.toObject());
    return value;
  }


  public boolean contains(Any value)
  {
    return _attribute.contains(value.toObject());
  }


  public BindingEnumeration enumeration()
  {
    try {
      return new IndexedEnumeration(_attribute.getAll());
    } catch (NamingException e) {
      throw Context.getInstance().exception(e);
    }
  }

 
  //--------------------------------------------------------

  /// @method add
  /// @synopsis Attribute add(object value) ;
  ///           Adds a new value to the attribute
  /// @synopsis Attribute add(int index, object value) ;
  ///           Adds an attribute value to the ordered list of attribute values
  /// @param index The index in the ordered list of attribute
  ///        values to add the new value. <code>0 <= ix <= size()</code>.
  /// @return this
  public static final Object [] p_add = { null, "indexOrValue", "*value", null };
  public Any m_add(anvil.script.Context context, Any p, Any q)
  {
    if (q == null) {
      //boolean add(Object attrVal)
      return Any.create(_attribute.add(p.toObject()));
    } else {
      //void add(int ix, Object attrVal)
      int index = p.toInt();
      if (index >= 0 && index < _attribute.size()) {
        _attribute.add(index, q.toObject());
      }
    }
    return this;
  }


  /// @method clear
  /// Removes all values from this attribute.
  /// @synopsis Attribute clear()
  /// @return this
  public Any m_clear()
  {
    _attribute.clear();
    return this;
  }


  /// @method contains
  /// Determines whether a value is in the attribute.
  /// @synopsis boolean contains(object value)
  /// @return <code>true</code> if value is one of this attribute's values;
  ///         <code>false</code> otherwise.
  public static final Object [] p_contains = { "value" };
  public Any m_contains(Any value)
  {
    return Any.create(_attribute.contains(value.toObject()));
  }


  /// @method get
  /// @synopsis object get() ;
  ///           Retrieves one of this attribute's values.
  /// @synopsis object get(int index) ;
  ///           Retrieves the attribute value from the ordered list of
  ///           attribute value
  /// @param index The index of the value in the ordered list of
  ///              attribute values. 0 <= index < size().
  public static final Object [] p_get = { null, "value" };
  public Any m_get(anvil.script.Context context, Any index_)
  {
    try {
      if (index_ == null) {
        return Any.create(_attribute.get());
       
      } else {
        Attribute attr = _attribute;
        int index = index_.toInt();
        if (index >= 0 && index < attr.size()) {
          return Any.create(attr.get(index));
        }
        return UNDEFINED;
      }
     
    } catch(NoSuchElementException e) {
      throw context.TypeError("Attribute has no value");
     
    } catch(NamingException e) {
      throw context.exception(e);
    }
  }


  /// @method getAll
  /// Retrieves an enumeration of the attribute's values.
  /// @synopsis enumeration getAll()
  public Any m_getAll(anvil.script.Context context)
  {
    try {
      return new AnyBindingEnumeration(
        new IndexedEnumeration(_attribute.getAll()));
    } catch (NamingException e) {
      throw context.exception(e);
    }
  }

 
  /// @method getAttributeDefinition
  /// Retrieves the attribute's schema definition. An attribute's schema definition
  /// contains information such as whether the attribute is multivalued or single-valued,
  /// the matching rules to use when comparing the attribiute's values. The information
  /// that you can retrieve from an attribute definition is directory-dependent.
  /// @synopsis NamingContext getAttributeDefinition()
  public Any method_getAttributeDefinition(anvil.script.Context context)
  {
    try {
      return new AnyNamingContext(_attribute.getAttributeDefinition());
    } catch(NamingException e) {
      throw context.exception(e);
    }
  }


  /// @method getAttributeSyntaxDefinition
  /// Retrieves the syntax definition associated with the attribute.
  /// An attribute's syntax definition specifies the format of the attribute's
  /// value(s).
  /// @synopsis NamingContext getAttributeSyntaxDefinition()
  public Any method_getAttributeSyntaxDefinition(anvil.script.Context context)
  {
    try {
      return new AnyNamingContext(_attribute.getAttributeSyntaxDefinition());
    } catch(NamingException e) {
      throw context.exception(e);
    }
  }


  /// @method getID
  /// Retrieves the id of this attribute.
  /// @synopsis string getID()
  public Any m_getID()
  {
    return Any.create(_attribute.getID());
  }


  /// @method isOrdered
  /// Determines whether this attribute's values are ordered.
  /// @synopsis boolean isOrdered()
  public Any m_isOrdered()
  {
    return Any.create(_attribute.isOrdered());
  }


  /// @method remove
  /// @synopsis object remove(int index) ;
  ///           Removes an attribute value from the ordered list of attribute values.
  /// @synopsis object remove(object element) ;
  ///           Removes a specified value from the attribute.
  /// @param index Index to remove
  /// @param element Element to remove
  /// @return Removed element, if any
 
  public static final Object [] p_remove = { null, "indexOrElement" };
  public Any m_remove(anvil.script.Context context, Any param)
  {
    Attribute attr = _attribute;
    if (param.isInt()) {
      int index = param.toInt();
      if (index>=0 && index<attr.size()) {
        return Any.create(attr.remove(index));
      }
      return UNDEFINED;
    } else {
      return Any.create(attr.remove(param.toObject()));
    }
  }


  /// @method set
  /// Sets an attribute value in the ordered list of attribute values.
  /// @synopsis object set(int index, object element)
  /// @param index The index of the value in the ordered list
  ///        of attribute values. 0 <= index < size().
  /// @param element Element to set
  /// @return Replaced element, if any
  public static final Object [] p_set = { null, "index", "value" };
  public Any m_set(anvil.script.Context context, int index, Any value)
  {
    Attribute attr = _attribute;
    if (index >= 0 && index < attr.size()) {
      return Any.create(attr.set(index, value.toObject()));
    }
    return UNDEFINED;
  }


  /// @method size
  /// Retrieves the number of values in this attribute.
  /// @synopsis int size()
  public Any m_size()
  {
    return Any.create(_attribute.size());
  }

  public static final anvil.script.compiler.NativeClass __class__ =
    new anvil.script.compiler.NativeClass("Attribute", AnyAttribute.class,
    //DOC{{
    ""+
      " @class Attribute\n" +
      " This class represents an attribute associated with a named object.\n" +
      " @see javax.naming.directory.Attribute\n" +
      "\n" +
      " @operator \"(boolean)<b>Attribute</b>\"\n" +
      " Returns true if this attribute has values.\n" +
      "\n" +
      " @operator \"<b>sizeof</b> <b>Attribute</b>\"\n" +
      " Returns the number of values in this attribute.\n" +
      "\n" +
      " @operator \"object <b>Attribute</b>[int index]\"\n" +
      " Retrieves the value at given index.\n" +
      "\n" +
      " @operator \"delete <b>Attribute</b>[int index]\"\n" +
      " Deletes the value at given index.\n" +
      "\n" +
      " @operator \"value in <b>Attribute</b>\"\n" +
      " Checks if given value is contained in this attribute.\n" +
      "\n" +
      " @operator \"enumeration *<b>Attribute</b>\"\n" +
      " Returns enumeration of values in this attribute.\n" +
      " @constructor Attribute\n" +
      " @synopsis Attribute(string id, values..)\n" +
      " @synopsis Attribute(string id, boolean ordered, values..)\n" +
      " @method add\n" +
      " @synopsis Attribute add(object value) ; \n" +
      "           Adds a new value to the attribute\n" +
      " @synopsis Attribute add(int index, object value) ; \n" +
      "           Adds an attribute value to the ordered list of attribute values\n" +
      " @param index The index in the ordered list of attribute \n" +
      "        values to add the new value. <code>0 <= ix <= size()</code>.\n" +
      " @return this\n" +
      " @method clear\n" +
      " Removes all values from this attribute.\n" +
      " @synopsis Attribute clear()\n" +
      " @return this\n" +
      " @method contains\n" +
      " Determines whether a value is in the attribute.\n" +
      " @synopsis boolean contains(object value)\n" +
      " @return <code>true</code> if value is one of this attribute's values; \n" +
      "         <code>false</code> otherwise.\n" +
      " @method get\n" +
      " @synopsis object get() ; \n" +
      "           Retrieves one of this attribute's values.\n" +
      " @synopsis object get(int index) ; \n" +
      "           Retrieves the attribute value from the ordered list of \n" +
      "           attribute value\n" +
      " @param index The index of the value in the ordered list of \n" +
      "              attribute values. 0 <= index < size().\n" +
      " @method getAll\n" +
      " Retrieves an enumeration of the attribute's values. \n" +
      " @synopsis enumeration getAll()\n" +
      " @method getAttributeDefinition\n" +
      " Retrieves the attribute's schema definition. An attribute's schema definition\n" +
      " contains information such as whether the attribute is multivalued or single-valued,\n" +
      " the matching rules to use when comparing the attribiute's values. The information\n" +
      " that you can retrieve from an attribute definition is directory-dependent. \n" +
      " @synopsis NamingContext getAttributeDefinition()\n" +
      " @method getAttributeSyntaxDefinition\n" +
      " Retrieves the syntax definition associated with the attribute. \n" +
      " An attribute's syntax definition specifies the format of the attribute's \n" +
      " value(s).\n" +
      " @synopsis NamingContext getAttributeSyntaxDefinition()\n" +
      " @method getID\n" +
      " Retrieves the id of this attribute.\n" +
      " @synopsis string getID()\n" +
      " @method isOrdered\n" +
      " Determines whether this attribute's values are ordered.\n" +
      " @synopsis boolean isOrdered()\n" +
      " @method remove\n" +
      " @synopsis object remove(int index) ; \n" +
      "           Removes an attribute value from the ordered list of attribute values.\n" +
      " @synopsis object remove(object element) ;\n" +
      "           Removes a specified value from the attribute.\n" +
      " @param index Index to remove\n" +
      " @param element Element to remove\n" +
      " @return Removed element, if any\n" +
      " @method set\n" +
      " Sets an attribute value in the ordered list of attribute values. \n" +
      " @synopsis object set(int index, object element)\n" +
      " @param index The index of the value in the ordered list \n" +
      "        of attribute values. 0 <= index < size().\n" +
      " @param element Element to set\n" +
      " @return Replaced element, if any\n" +
      " @method size\n" +
      " Retrieves the number of values in this attribute.\n" +
      " @synopsis int size()\n"
    //}}DOC
    );
  static {
    NamingModule.class.getName();
  }



}
TOP

Related Classes of anvil.core.naming.AnyAttribute

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.