Class MapTag

  • All Implemented Interfaces:
    org.apache.commons.jelly.Tag
    Direct Known Subclasses:
    PropertiesTag

    public class MapTag
    extends AbstractBeanTag

    A specialized BeanTag implementation for creating maps.

    This tag handler class can be placed either in the body of an arbitrary tag derived from DependencyTag. It will then create a map and pass it to the enclosing DependencyTag. Alternatively, the name attribute can be specified, then the map bean is directly stored in the corresponding BeanStore. The map can be populated by <entry> tags in the body of this tag. The type of the key and the value of the map entries can be specified as attributes. A typical example of using this tag could look as follows:

       <constructor>
         <param>
           <map keyClassName="java.lang.String"
             valueClassName="java.lang.Integer">
             <entry key="k1" value="1"/>
             <entry key="k2" value="2"/>
             <entry key="k3" value="3"/>
           </map>
         </param>
       </constructor>
     
    Here a map is created, populated with some key value pairs and passed as argument to a constructor.

    MapTag supports the following attributes:

    Attribute Description Optional
    name Using this attribute a name for the map bean can be specified. The bean can then be queried from the current bean store by this name. If no name is provided, an anonymous bean is created; in this case the tag must be nested in the body of a DependencyTag. yes
    keyClass Here the class of the keys of the map entries can be specified. When elements are added, the tag will try to convert the key objects to this class if necessary. If an EntryTag in the body of this tag defines a key class, this class will be used thus overriding the key class specified at the map level. If neither the map tag nor the entry tag specifies a key class, no type conversion will be performed. yes
    keyClassName This attribute has the same effect as keyClass, but the name of the key class is specified rather than the class object itself. yes
    keyClassLoader If the name of the key class is specified, with this attribute a class loader for resolving the class can be defined. The name specified here will be passed to the current ClassLoaderProvider for obtaining the desired class loader. yes
    valueClass With this attribute the class of the map values can be specified. This is analogous to the keyClass attribute, but defines a default class for the values stored in the map. yes
    valueClassName Analogous to valueClass, but the name of the value class is specified. yes
    valueClassLoader Defines the class loader to be used for resolving the value class when only the name is specified. This attribute has the same meaning as the keyClassLoader attribute for the value class. yes
    ordered Per default this tag will create a java.util.HashMap object that does not maintain a specific order of its elements. If this attribute is set to true, an instance of java.util.LinkedHashMap will be created instead. Then the map will remember the order in which its elements have been added. yes

    Version:
    $Id: MapTag.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • MapTag

        public MapTag()
        Creates a new instance of MapTag.
    • Method Detail

      • getName

        public String getName()
        Returns the name under which the map bean is to be stored in the bean store.
        Overrides:
        getName in class AbstractBeanTag
        Returns:
        the name of the map bean
      • setName

        public void setName​(String name)
        Set method of the name attribute.
        Parameters:
        name - the attribute's value
      • getKeyClassData

        public ClassDescData getKeyClassData()
        Returns the ClassDescData object for the key class. This object is populated by corresponding set methods for the key class or its name.
        Returns:
        the data object for defining the key class
      • setKeyClass

        public void setKeyClass​(Class<?> cls)
        Sets the class of the keys of this map.
        Parameters:
        cls - the key class
      • setKeyClassName

        public void setKeyClassName​(String clsName)
        Sets the name of the class for the keys of this map.
        Parameters:
        clsName - the name of the key class
      • setKeyClassLoader

        public void setKeyClassLoader​(String loaderName)
        Specifies the class loader to be used for the resolving the key class.
        Parameters:
        loaderName - the name of the class loader
      • getKeyClassDesc

        public ClassDescription getKeyClassDesc()
        Returns the class description for the key class. This object is initialized from the ClassDescData for the key class before the tag's body is executed. It may be null if no value class is specified.
        Returns:
        the class description for the key class
      • getValueClassData

        public ClassDescData getValueClassData()
        Returns the ClassDescData object for the value class.
        Returns:
        the data object for defining the value class
      • setValueClass

        public void setValueClass​(Class<?> cls)
        Sets the class of the values stored in this map.
        Parameters:
        cls - the value class
      • setValueClassName

        public void setValueClassName​(String clsName)
        Sets the name of the class of the values stored in this map.
        Parameters:
        clsName - the name of the value class
      • setValueClassLoader

        public void setValueClassLoader​(String loaderName)
        Specifies the class loader to be used for the resolving the value class.
        Parameters:
        loaderName - the name of the class loader
      • getValueClassDesc

        public ClassDescription getValueClassDesc()
        Returns the class description for the value class. This object is initialized before the tag's body gets executed. It may be null if no value class is specified.
        Returns:
        the class description for the value class
      • isOrdered

        public boolean isOrdered()
        Returns the value of the ordered attribute.
        Returns:
        the ordered attribute
      • setOrdered

        public void setOrdered​(boolean ordered)
        Sets the value of the ordered attribute. This attribute determines the type of the map created by this tag: if set to true, a linked map is created; the default value of false will cause a plain hash map to be created.
        Parameters:
        ordered - the value of the ordered attribute
      • addEntry

        public void addEntry​(Dependency depKey,
                             Dependency depValue)
        Adds a new entry to this map. This method is intended to be called by tags in the body of this tag. Key and value are passed in as dependencies. Their actual values are resolved when the map bean is created.
        Parameters:
        depKey - the dependency for the key (must not be null)
        depValue - the dependency for the value (must not be null)
        Throws:
        IllegalArgumentException - if the key or the value dependency is null
      • getKeyDependencies

        protected Collection<Dependency> getKeyDependencies()
        Returns the collection with the key dependencies.
        Returns:
        the key dependencies
      • getValueDependencies

        protected Collection<Dependency> getValueDependencies()
        Returns the collection with the value dependencies.
        Returns:
        the value dependencies
      • processBeforeBody

        protected void processBeforeBody()
                                  throws org.apache.commons.jelly.JellyTagException
        Performs some processing before the body gets executed. This implementation will initialize the class descriptions for the key and value class.
        Overrides:
        processBeforeBody in class AbstractBeanTag
        Throws:
        org.apache.commons.jelly.JellyTagException - if the tag is incorrectly used
      • createBeanProvider

        protected BeanProvider createBeanProvider()
                                           throws org.apache.commons.jelly.JellyTagException
        Creates the BeanProvider produced by this tag. This implementation creates a MapBeanProvider and initialize it with the dependencies for the keys and values that have been added so far.
        Specified by:
        createBeanProvider in class AbstractBeanTag
        Returns:
        the BeanProvider produced by this tag
        Throws:
        org.apache.commons.jelly.JellyTagException - if an error occurs