donaldp 02/04/20 06:27:51 Modified: api/src/java/org/apache/myrmidon/api/metadata ModelElement.java Log: Flesh out the ModelElement class a bit Revision Changes Path 1.2 +230 -6 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.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ModelElement.java 21 Mar 2002 10:22:48 -0000 1.1 +++ ModelElement.java 20 Apr 2002 13:27:51 -0000 1.2 @@ -7,6 +7,10 @@ */ package org.apache.myrmidon.api.metadata; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Set; + /** * A ModelElement represents the data necessary to configure * the task or sub-object. It usually represents an XML element in a @@ -17,10 +21,65 @@ * sub-elements or text content (one or the other - not both).</p> * * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a> - * @version $Revision: 1.1 $ $Date: 2002/03/21 10:22:48 $ + * @version $Revision: 1.2 $ $Date: 2002/04/20 13:27:51 $ */ -public class ModelElement +public final class ModelElement { + private static final String[] EMPTY_STRING_ARRAY = new String[ 0 ]; + private static final ModelElement[] EMPTY_MODEL_ELEMENT_ARRAY = + new ModelElement[ 0 ]; + + private final String m_name; + private final String m_location; + private HashMap m_attributes; + private ArrayList m_children; + private String m_content; + private boolean m_readOnly; + + /** + * Construct a ModelElement with the specified name + * and location. The name should be a valid XML name. + * The format of location string is unspecified but it + * is suggested that the format will be in the form + * "resourceLocation:row:column". This will make it easier + * to integrate into error reporting capabilities of most + * editors. + * + * @param name the name of the model element + * @param location the location of model element (ie "build.xml:20:4") + */ + public ModelElement( final String name, final String location ) + { + m_name = name; + m_location = location; + } + + /** + * Return the name of the <code>ModelElement</code>. + * The name should be a well formed XML name. + * + * @return the name of the <code>ModelElement</code>. + */ + public String getName() + { + return m_name; + } + + /** + * Return the location of the <code>ModelElement</code>. + * The format of location string is unspecified but it + * is suggested that the format will be in the form + * "resourceLocation:row:column". This will make it easier + * to integrate into error reporting capabilities of most + * editors. (ie "build.xml:20:4") + * + * @return the location of the <code>ModelElement</code>. + */ + public String getLocation() + { + return m_location; + } + /** * Return an array containing all the child <code>ModelElement</code>s * that are contained within this <code>ModelElement</code>. If this method @@ -34,7 +93,30 @@ */ public ModelElement[] getChildren() { - return null; + if( null == m_children ) + { + return EMPTY_MODEL_ELEMENT_ARRAY; + } + else + { + return (ModelElement[])m_children. + toArray( new ModelElement[ m_children.size() ] ); + } + } + + /** + * Return the number of Child ModelElement objects. + * + * @return an <code>int</code> value + */ + public int getChildCount() + { + if( null == m_children ) + { + return 0; + } + + return m_children.size(); } /** @@ -48,7 +130,15 @@ */ public String[] getAttributeNames() { - return null; + if( null == m_attributes ) + { + return EMPTY_STRING_ARRAY; + } + else + { + final Set keys = m_attributes.keySet(); + return (String[])keys.toArray( new String[ keys.size() ] ); + } } /** @@ -61,7 +151,14 @@ */ public String getAttribute( final String name ) { - return null; + if( null == m_attributes ) + { + return null; + } + else + { + return (String)m_attributes.get( name ); + } } /** @@ -75,6 +172,133 @@ */ public String getContent() { - return null; + return m_content; + } + + /** + * Make this <code>ModelElement</code> read-only. + * This means that all mutators from now on will throw + * a (@link IllegalStateException) if they are called. + * + */ + public void makeReadOnly() + { + m_readOnly = true; + } + + /** + * Set the content of this <code>ModelElement</code> object + * to the specified string. + * + * @param content a <code>String</code> content + * @throws IllegalArgumentException if the element is read-only + */ + public void setContent( final String content ) + throws IllegalArgumentException + { + checkWriteable(); + m_content = content; + } + + /** + * Set the value of the specified attribute to the specified string. + * + * @param name name of the attribute to set + * @param value a <code>String</code> value + * @throws IllegalArgumentException if the element is read-only + */ + public void setAttribute( final String name, final String value ) + throws IllegalArgumentException + { + checkWriteable(); + + if( null == name ) + { + throw new NullPointerException( "name" ); + } + + if( null == m_attributes ) + { + if( value == null ) + { + return; + } + else + { + m_attributes = new HashMap(); + } + } + + if( null == value ) + { + m_attributes.remove( name ); + } + else + { + m_attributes.put( name, value ); + } + } + + /** + * Add a child <code>ModelElement</code> to this ModelElement element. + * + * @param modelElement a <code>ModelElement</code> child + * @throws IllegalArgumentException if the element is read-only + */ + public void addChild( final ModelElement modelElement ) + throws IllegalArgumentException + { + checkWriteable(); + + if( null == modelElement ) + { + throw new NullPointerException( "modelElement" ); + } + + if( null == m_children ) + { + m_children = new ArrayList(); + } + + m_children.add( modelElement ); + } + + /** + * Remove a child <code>ModelElement</code>. + * + * @param modelElement a <code>ModelElement</code> + * @throws IllegalArgumentException if the element is read-only + */ + public void removeChild( final ModelElement modelElement ) + throws IllegalArgumentException + { + checkWriteable(); + + if( null == modelElement ) + { + throw new NullPointerException( "modelElement" ); + } + + if( null == m_children ) + { + return; + } + + m_children.remove( modelElement ); + } + + /** + * Check if this <code>ModelElement</code> is writeable. + * + * @throws IllegalStateException if this ModelElement s read-only + */ + private void checkWriteable() + throws IllegalStateException + { + if( m_readOnly ) + { + throw new IllegalStateException + ( "ModelElement is read only and can not be modified" ); + } } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>