You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by ad...@apache.org on 2002/05/17 09:30:58 UTC

cvs commit: jakarta-ant-myrmidon/api/src/java/org/apache/myrmidon/api/metadata ModelElement.java

adammurdoch    02/05/17 00:30:58

  Modified:    api/src/java/org/apache/myrmidon/api/metadata
                        ModelElement.java
  Log:
  Added some convenience methods.
  
  Revision  Changes    Path
  1.5       +117 -44   jakarta-ant-myrmidon/api/src/java/org/apache/myrmidon/api/metadata/ModelElement.java
  
  Index: ModelElement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant-myrmidon/api/src/java/org/apache/myrmidon/api/metadata/ModelElement.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ModelElement.java	21 Apr 2002 01:28:54 -0000	1.4
  +++ ModelElement.java	17 May 2002 07:30:58 -0000	1.5
  @@ -13,7 +13,7 @@
   
   /**
    * A ModelElement represents the data necessary to configure
  - * the task or sub-object. It usually represents an XML element in a
  + * a task or sub-object. It usually represents an XML element in a
    * build file and has similar features to XML elements.
    *
    * <p>It has a set of un-ordered attributes with each attribute mapping
  @@ -21,7 +21,7 @@
    * sub-elements or text content (one or the other - not both).</p>
    *
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
  - * @version $Revision: 1.4 $ $Date: 2002/04/21 01:28:54 $
  + * @version $Revision: 1.5 $ $Date: 2002/05/17 07:30:58 $
    * @see Modeller
    * @see ModelException
    */
  @@ -83,14 +83,13 @@
       }
   
       /**
  -     * Return an array containing all the child <code>ModelElement</code>s
  -     * that are contained within this <code>ModelElement</code>. If this method
  -     * returns an array containing 1 or more elements then it must return null
  -     * for getContent() method.
  +     * Return an array containing all the child elements that are contained
  +     * within this element.  If this method returns an array containing
  +     * 1 or more elements then {@link #getContent} must return null.
        *
        * @todo determine whether we return null or an empty array when no
        *       child elements.
  -     * @return all the child <code>ModelElement</code>s
  +     * @return The child elements.  Never returns null.
        * @see #getContent()
        */
       public ModelElement[] getChildren()
  @@ -107,12 +106,10 @@
       }
   
       /**
  -     * Return an array of child <code>ModelElement</code> objects
  -     * that have specified name.
  +     * Return an array of the child elements that have specified name.
        *
  -     * @param name The name of the child <code>ModelElement</code>
  -     *        objects to return.
  -     * @return an array of <code>ModelElement</code> objects
  +     * @param name The name of the child elements to return.
  +     * @return an array containing the matching elements.  Never returns null.
        */
       public ModelElement[] getChildren( final String name )
       {
  @@ -140,11 +137,10 @@
       }
   
       /**
  -     * Return the first child <code>ModelElement</code> with specified
  -     * name.
  +     * Returns the first child element with specified name.
        *
  -     * @param name the name of ModelElement to search for
  -     * @return the ModelElement or null if none
  +     * @param name the name of child element to search for
  +     * @return the matching element or null if none.
        */
       public ModelElement getChild( final String name )
       {
  @@ -165,7 +161,7 @@
       }
   
       /**
  -     * Return the number of Child ModelElement objects.
  +     * Return the number of child elements.
        *
        * @return an <code>int</code> value
        */
  @@ -180,12 +176,11 @@
       }
   
       /**
  -     * Return an array containing the names of all the attributes stored
  -     * in this <code>ModelElement</code>. The user can then pass these
  -     * parameters into the getAttribute() method of this class to get the
  -     * value of the attribute.
  +     * Return an array containing the names of the attributes of this element.
  +     * The user can then pass these names to the {@link #getAttribute(String)}
  +     * and {@link #getAttribute} methods to get the value of the attribute.
        *
  -     * @return an array of the attribute names
  +     * @return an array of the attribute names.  Never returns null.
        * @see #getAttribute(String)
        */
       public String[] getAttributeNames()
  @@ -202,29 +197,48 @@
       }
   
       /**
  -     * Get the value of the attribute passed in.
  -     * If no such attribute exists return null.
  +     * Get the value of an attribute, by name.  If no such attribute exists
  +     * null is returned.
        *
        * @param name the name of the attribute to retrieve value for
        * @return the value of the attribute with specified name or null
  -     *         if no such element.
  +     *         if no such attribute.
        */
       public String getAttribute( final String name )
       {
  -        if( null == m_attributes )
  +        return getAttribute( name, null );
  +    }
  +
  +    /**
  +     * Get the value of an attribute, by name,  If not such attribute exists,
  +     * a supplied default value is returned.
  +     *
  +     * @param name the name of the attribute to retrieve value for.
  +     * @param defaultVal the default value for the attribute.
  +     * @return the value of the attribute with specified name or the value
  +     *         of <code>defaultVal</code> if no such attribute.
  +     */
  +    public String getAttribute( final String name, final String defaultVal )
  +    {
  +        String value = null;
  +        if( m_attributes != null)
           {
  -            return null;
  +            value = (String)m_attributes.get( name );
  +        }
  +        if( value == null )
  +        {
  +            return defaultVal;
           }
           else
           {
  -            return (String)m_attributes.get( name );
  +            return value;
           }
       }
   
       /**
  -     * Retrieve the content of this element if any. Will return
  +     * Retrieve the content of this element, if any. Will return
        * null if no content available. Note it is invalid for this
  -     * method to return a non-null value and the getChildren()
  +     * method to return a non-null value and the {@link #getChildren()}
        * method to return an array of 1 or more child elements.
        *
        * @return the content value if any, else null
  @@ -236,10 +250,8 @@
       }
   
       /**
  -     * Make this <code>ModelElement</code> read-only.
  -     * This means that all mutators from now on will throw
  -     * a (@link IllegalStateException) if they are called.
  -     *
  +     * Make this element read-only.  This means that all mutators from now
  +     * on will throw an (@link IllegalStateException) if they are called.
        */
       public void makeReadOnly()
       {
  @@ -247,10 +259,9 @@
       }
   
       /**
  -     * Set the content of this <code>ModelElement</code> object
  -     * to the specified string.
  +     * Set the content of this element.
        *
  -     * @param content a <code>String</code> content
  +     * @param content the content for this element.
        * @throws IllegalArgumentException if the element is read-only
        */
       public void setContent( final String content )
  @@ -261,10 +272,11 @@
       }
   
       /**
  -     * Set the value of the specified attribute to the specified string.
  +     * Set the value of an attribute.
        *
        * @param name name of the attribute to set
  -     * @param value a <code>String</code> value
  +     * @param value value to set the attribute to.  Use null to remove the
  +     *        attribute.
        * @throws IllegalArgumentException if the element is read-only
        */
       public void setAttribute( final String name, final String value )
  @@ -300,9 +312,34 @@
       }
   
       /**
  -     * Add a child <code>ModelElement</code> to this ModelElement element.
  +     * Add a child element to this element.
  +     *
  +     * @param index The index to add the child element at.  Indexes start at 0.
  +     * @param modelElement the child element to add.
  +     * @throws IllegalArgumentException if the element is read-only
  +     */
  +    public void addChild( final int index, final ModelElement modelElement )
  +        throws IllegalArgumentException
  +    {
  +        checkWriteable();
  +
  +        if( null == modelElement )
  +        {
  +            throw new NullPointerException( "modelElement" );
  +        }
  +
  +        if( null == m_children )
  +        {
  +            m_children = new ArrayList();
  +        }
  +
  +        m_children.add( index, modelElement );
  +    }
  +
  +    /**
  +     * Add a child element to this element.
        *
  -     * @param modelElement a <code>ModelElement</code> child
  +     * @param modelElement The child element to add.
        * @throws IllegalArgumentException if the element is read-only
        */
       public void addChild( final ModelElement modelElement )
  @@ -324,9 +361,32 @@
       }
   
       /**
  -     * Remove a child <code>ModelElement</code>.
  +     * A utility method to add several child elements to this element.
  +     */
  +    public void addChildren( final ModelElement[] modelElements )
  +    {
  +        if( modelElements == null || modelElements.length == 0 )
  +        {
  +            return;
  +        }
  +
  +        checkWriteable();
  +
  +        if( m_children == null )
  +        {
  +            m_children = new ArrayList();
  +        }
  +
  +        for( int i = 0; i < modelElements.length; i++ )
  +        {
  +            m_children.add( modelElements[ i ] );
  +        }
  +    }
  +
  +    /**
  +     * Remove a child element.
        *
  -     * @param modelElement a <code>ModelElement</code>
  +     * @param modelElement the child element to remove
        * @throws IllegalArgumentException if the element is read-only
        */
       public void removeChild( final ModelElement modelElement )
  @@ -348,7 +408,20 @@
       }
   
       /**
  -     * Check if this <code>ModelElement</code> is writeable.
  +     * Remove all children of this element.
  +     */
  +    public void removeChildren()
  +    {
  +        checkWriteable();
  +
  +        if( m_children != null )
  +        {
  +            m_children.clear();
  +        }
  +    }
  +
  +    /**
  +     * Check if this element is writeable.
        *
        * @throws IllegalStateException if this ModelElement s read-only
        */
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>