Package anvil.core.naming

Source Code of anvil.core.naming.AnyAttributes$AttributesEnumeration

/*
* $Id: AnyAttributes.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.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import java.util.Enumeration;
import anvil.core.Any;
import anvil.core.AnyMap;
import anvil.core.AnyAbstractClass;
import anvil.core.AnyBindingEnumeration;
import anvil.core.IndexedEnumeration;
import anvil.script.Context;
import anvil.java.util.BindingEnumeration;

/// @class Attributes
/// This class represents a collection of attributes.
/// @see javax.naming.directory.Attributes
///
/// @attribute attrId
/// @synopsis Attribute <b>Attributes</b>.<i>attrId</i> ;
/// Retrieves given attribute.
/// @synopsis <b>delete Attributes</b>.<i>attrId</i> ;
/// Removes given attribute.
/// @param attrId ID of attribute to retrieve
/// @return Attribute or <code>undefined</code> if no such attribute
///         exist.

/// @reference attrId
/// @synopsis Attribute <b>Attributes</b>[<i>attrId</i>] ;
/// Retrieves given attribute.
/// @synopsis <b>delete Attributes</b>[<i>attrId</i>] ;
/// Removes given attribute.
/// @param attrId ID of attribute to retrieve
/// @return Attribute or <code>undefined</code> if no such attribute
///         exist.

/// @operator append
/// Adds a new value to this attribute.
/// @synopsis <b>Attributes</b>[] = element
/// @param element Element to add, either instance of Attribute or
///        <code>id=>element</code> map.


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


  /// @constructor Attributes
  /// @synopsis Attributes(attribute, ...)
  /// @synopsis Attributes(boolean ignoreCase, attribute, ...)
  /// @param attributes Map (id=>value) or instance of Attribute
  public static final Any newInstance(Context context, Any[] values)
  {
    boolean ignoreCase = false;
    BasicAttributes attrs = new BasicAttributes(ignoreCase);
    int n = values.length;
    int i =0;
    if (n > 0) {
      if (values[0].isBoolean()) {
        ignoreCase = values[0].toBoolean();
        i++;
      }
    }
    for(; i<n; i++) {
      Any value = values[i];
      if (value.isMap()) {
        AnyMap map = value.toMap();
        attrs.put(map.getLeft().toString(), map.getRight().toObject());
      } else if (value instanceof AnyAttribute) {
        attrs.put((Attribute)value.toObject());
      }
    }
    return new AnyAttributes(attrs);
  }
 


  private Attributes _attributes;
 
 
  public AnyAttributes(Attributes attributes)
  {
    _attributes = attributes;
  }


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

  public Object toObject()
  {
    return _attributes;
  }


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


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



  public Any getAttribute(anvil.script.Context context, String attribute)
  {
    Attribute attr = _attributes.get(attribute);
    return (attr != null) ? new AnyAttribute(attr) : UNDEFINED;
  }


  public Any checkAttribute(anvil.script.Context context, String attribute)
  {
    return getAttribute(context, attribute);
  }


  public boolean deleteAttribute(anvil.script.Context context, String attribute)
  {
    return (_attributes.remove(attribute) != null);
  }


  public Any setAttribute(anvil.script.Context context, String attribute, Any value)
  {
    _attributes.put(attribute, value);
    return value;
  }


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


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


  public boolean deleteReference(anvil.script.Context context, Any index)
  {
    return deleteAttribute(context, index.toString());
  }


  public Any setReference(anvil.script.Context context, Any index, Any value)
  {
    return setAttribute(context, index.toString(), value);
  }
 

  public Any setReference(anvil.script.Context context, Any value)
  {
    if (value instanceof AnyAttribute) {
      _attributes.put((Attribute)value.toObject());
    } else if (value.isMap()) {
      AnyMap map = value.toMap();
      _attributes.put(map.getLeft().toString(), map.getRight().toObject());
    } else {
      throw context.BadParameter("Attribute or map expected");
    }
    return value;
  }


  public boolean contains(Any value)
  {
    if (value instanceof AnyAttribute) {
      Attribute attr = (Attribute)value.toObject();
      return (_attributes.get(attr.getID()) != null);
    }
    return false;
  }


  public BindingEnumeration enumeration()
  {
    return new AttributesEnumeration(_attributes.getAll());
  }

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


  /// @method get
  /// Returns the attribute given id
  /// @synopsis Attribute get(String id)
  /// @return Attribute or <code>undefined</code>
  public static final Object[] p_get = { null, "id" };
  public Any m_get(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "get");
    }
    Attribute attr = _attributes.get(parameters[0].toString());
    return (attr != null) ? new AnyAttribute(attr) : UNDEFINED;
  }


  /// @method getAll
  /// Returns the enumeration of Attributes in this set.
  /// @synopsis enumeration getAll()
  public Any m_getAll(anvil.script.Context context, Any[] parameters)
  {
    return new AnyBindingEnumeration(
      new IndexedEnumeration(_attributes.getAll()));
  }


  /// @method getIDs
  /// Returns the numeration of ID's from this set.
  /// @synopsis enumeration getIDs()
  public Any m_getIDs(anvil.script.Context context, Any[] parameters)
  {
    return new AnyBindingEnumeration(
      new IndexedEnumeration(_attributes.getIDs()));
  }


  /// @method isCaseIgnored
  /// Determines whether the attribute set ignores the case of attribute
  /// identifiers when retrieving or adding attributes.
  /// @synopsis boolean isCaseIgnored()
  public Any m_isCaseIgnored(anvil.script.Context context, Any[] parameters)
  {
    return Any.create(_attributes.isCaseIgnored());
  }


  /// @method put
  /// Puts a new attribute to this set.
  /// @synopsis Attribute put(String id, Any value)
  /// @synopsis Attribute put(Attribute attr)
  /// @param id Id of attribute
  /// @param value Value of attribute
  /// @param attr Atttribut eto add
  /// @return Attribute that was replaced, or <code>undefined</code>
  public Any m_put(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "put");
    }
    Attribute attr;
    if (parameters.length == 1) {
      if (!(parameters[0] instanceof AnyAttribute)) {
      throw context.BadParameter("Attribute expected");
      }
      attr = _attributes.put((Attribute)parameters[0].toObject());
    } else {
      attr = _attributes.put(parameters[0].toString(), parameters[1].toObject());
    }
    return (attr != null) ? new AnyAttribute(attr) : UNDEFINED;
  }


  /// @method remove
  /// Removes attribute with give id.
  /// @synopsis Attribute remove(String id)
  /// @return Removed Attribute or <code>undefined</code>
  public Any m_remove(anvil.script.Context context, Any[] parameters)
  {
    if (parameters.length < 1) {
      throw parametersMissing(context, "remove");
    }
    Attribute attr = _attributes.remove(parameters[0].toString());
    return (attr != null) ? new AnyAttribute(attr) : UNDEFINED;
  }


  /// @method size
  /// Return the number attributes in this set.
  /// @synopsis int size()
  public Any m_size(anvil.script.Context context, Any[] parameters)
  {
    return Any.create(_attributes.size());
  }


  public class AttributesEnumeration implements BindingEnumeration
  {
    public Enumeration _enum;
    public Attribute _attr;

    public AttributesEnumeration(Enumeration e)
    {
      _enum = e;
    }

    public Object nextKey()
    {
      _attr = (Attribute)_enum.nextElement();
      return Any.create(_attr.getID());
    }

    public Object nextElement()
    {
      if (_attr != null) {
        Attribute a = _attr;
        _attr = null;
        return new AnyAttribute(a);
      } else {
        return new AnyAttribute((Attribute)_enum.nextElement());
      }
    }

    public boolean hasMoreElements()
    {
      return _enum.hasMoreElements();
    }

  }


  public static final anvil.script.compiler.NativeClass __class__ =
    new anvil.script.compiler.NativeClass("Attributes", AnyAttributes.class,
    //DOC{{
    ""+
      " @class Attributes\n" +
      " This class represents a collection of attributes. \n" +
      " @see javax.naming.directory.Attributes\n" +
      "\n" +
      " @attribute attrId\n" +
      " @synopsis Attribute <b>Attributes</b>.<i>attrId</i> ;\n" +
      " Retrieves given attribute.\n" +
      " @synopsis <b>delete Attributes</b>.<i>attrId</i> ;\n" +
      " Removes given attribute.\n" +
      " @param attrId ID of attribute to retrieve\n" +
      " @return Attribute or <code>undefined</code> if no such attribute\n" +
      "         exist.\n" +
      " @reference attrId\n" +
      " @synopsis Attribute <b>Attributes</b>[<i>attrId</i>] ;\n" +
      " Retrieves given attribute.\n" +
      " @synopsis <b>delete Attributes</b>[<i>attrId</i>] ;\n" +
      " Removes given attribute.\n" +
      " @param attrId ID of attribute to retrieve\n" +
      " @return Attribute or <code>undefined</code> if no such attribute\n" +
      "         exist.\n" +
      " @operator append\n" +
      " Adds a new value to this attribute.\n" +
      " @synopsis <b>Attributes</b>[] = element\n" +
      " @param element Element to add, either instance of Attribute or\n" +
      "        <code>id=>element</code> map.\n" +
      " @constructor Attributes\n" +
      " @synopsis Attributes(attribute, ...)\n" +
      " @synopsis Attributes(boolean ignoreCase, attribute, ...)\n" +
      " @param attributes Map (id=>value) or instance of Attribute\n" +
      " @method get\n" +
      " Returns the attribute given id\n" +
      " @synopsis Attribute get(String id)\n" +
      " @return Attribute or <code>undefined</code>\n" +
      " @method getAll\n" +
      " Returns the enumeration of Attributes in this set.\n" +
      " @synopsis enumeration getAll()\n" +
      " @method getIDs\n" +
      " Returns the numeration of ID's from this set.\n" +
      " @synopsis enumeration getIDs()\n" +
      " @method isCaseIgnored\n" +
      " Determines whether the attribute set ignores the case of attribute \n" +
      " identifiers when retrieving or adding attributes.\n" +
      " @synopsis boolean isCaseIgnored()\n" +
      " @method put\n" +
      " Puts a new attribute to this set.\n" +
      " @synopsis Attribute put(String id, Any value)\n" +
      " @synopsis Attribute put(Attribute attr)\n" +
      " @param id Id of attribute\n" +
      " @param value Value of attribute\n" +
      " @param attr Atttribut eto add\n" +
      " @return Attribute that was replaced, or <code>undefined</code>\n" +
      " @method remove\n" +
      " Removes attribute with give id.\n" +
      " @synopsis Attribute remove(String id)\n" +
      " @return Removed Attribute or <code>undefined</code>\n" +
      " @method size\n" +
      " Return the number attributes in this set.\n" +
      " @synopsis int size()\n"
    //}}DOC
    );
  static {
    NamingModule.class.getName();
  }


}
TOP

Related Classes of anvil.core.naming.AnyAttributes$AttributesEnumeration

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.