001    /*
002     * Copyright 2004-2005 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.metro.data;
020    
021    import java.net.URI;
022    import java.net.URISyntaxException;
023    
024    import net.dpml.component.Directive;
025    
026    import net.dpml.lang.Value;
027    import net.dpml.lang.Construct;
028    
029    /**
030     * A <code>ValueDirective</code> represents a single constructed argument value. The directive
031     * holds a classname (default value of <code>java.lang.String</code>) and possible sub-directives.
032     * The directives value is established by creating a new instance using the classname
033     * together with the values directived from the sub-sidiary directives as constructor arguments.
034     *
035     * <p><b>XML</b></p>
036     * <p>A value is a nested structure containing a string value or contructor parameter arguments.</p>
037     * <pre>
038     *    <font color="gray">&lt;-- Simple string param declaration --&gt;</font>
039     *
040     *    &lt;value&gt;<font color="darkred">London</font>&lt;/value&gt;
041     *
042     *    <font color="gray">&lt;-- Typed value declaration --&gt;</font>
043     *
044     *    &lt;value class="<font color="darkred">java.io.File</font>"&gt;<font color="darkred">./home</font>&lt;/value&gt;
045     *
046     *    <font color="gray">&lt;-- Multi-argument parameter declaration --&gt;</font>
047     *
048     *    &lt;value class="<font color="darkred">MyClass</font>"&gt;
049     *       &lt;value class="<font color="darkred">java.io.File</font>"><font color="darkred">./home</font>&lt;/value&gt;
050     *       &lt;value&gt;<font color="darkred">London</font>&lt;/value&gt;
051     *    &lt;/value&gt;
052     * </pre>
053     *
054     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
055     * @version 1.0.4
056     */
057    public final class ValueDirective extends Construct implements Directive
058    {
059        //--------------------------------------------------------------------------
060        // static
061        //--------------------------------------------------------------------------
062    
063       /**
064        * Serial version identifier.
065        */
066        static final long serialVersionUID = 1L;
067        
068        //--------------------------------------------------------------------------
069        // constructors
070        //--------------------------------------------------------------------------
071    
072       /**
073        * Create a new construct using the default java.lang.String class as the base type.
074        * @param value the construct value
075        */
076        public ValueDirective( String value )
077        {
078            super( value );
079        }
080    
081       /**
082        * Create a new construct using a supplied target defintion.  The target argument 
083        * may be either a classname or a symbolic reference in the form ${[key]}.  If the 
084        * argument is symbolic it resolved relative to a context map supplied by the 
085        * application resolving construct values.
086        *
087        * @param target a classname or symbolic reference
088        * @param value the construct value
089        */
090        public ValueDirective( String target, String value )
091        {
092            super( target, value );
093        }
094    
095       /**
096        * Create a new construct using a supplied target defintion.  The target argument 
097        * may be either a classname or a symbolic reference in the form ${[key]}.  If the 
098        * argument is symbolic it is resolved relative to a context map supplied by the 
099        * application resolving construct values.  If the construct value is symbolic
100        * the implementation will attempt to expand the reference relative to a context
101        * map (if supplied) otherwise the implementation will attempt to expand the value 
102        * using system properties.
103        *
104        * @param target a classname or symbolic reference
105        * @param method the method to invoke on the target
106        * @param value the construct value
107        */
108        public ValueDirective( String target, String method, String value )
109        {
110            super( target, method, value );
111        }
112    
113       /**
114        * Create a new construct using a supplied target defintion.  The target argument 
115        * may be either a classname or a symbolic reference in the form ${[key]}.  If the 
116        * argument is symbolic it is resolved relative to a context map supplied by the 
117        * application resolving construct values. Instance values resolved from the 
118        * supplied Value[] will be used as constructor arguments when resolving the target.
119        *
120        * @param target the construct classname
121        * @param args an array of unresolved parameter values
122        */
123        public ValueDirective( String target, Value[] args )
124        {
125            super( target, args );
126        }
127        
128       /**
129        * Create a new construct using a supplied target defintion.  The target argument 
130        * may be either a classname or a symbolic reference in the form ${[key]}.  If the 
131        * argument is symbolic it is resolved relative to a context map supplied by the 
132        * application resolving construct values. Instance values resolved from the 
133        * supplied Value[] will be used as method arguments when resolving the target.
134        *
135        * @param target the construct classname
136        * @param method the method to invoke on the target
137        * @param args an array of unresolved parameter values
138        */
139        public ValueDirective( String target, String method, Value[] args )
140        {
141            super( target, method, args );
142        }
143        
144        //--------------------------------------------------------------------------
145        // ValueDirective
146        //--------------------------------------------------------------------------
147    
148       /**
149        * Test if the supplied object is equal to this object.
150        * @param other the object to compare with this instance
151        * @return TRUE if the supplied object is equal to this object
152        */
153        public boolean equals( Object other )
154        {
155            boolean equals = super.equals( other );
156            if( null == other )
157            {
158                return false;
159            }
160            if( !( other instanceof ValueDirective ) )
161            {
162                return false;
163            }
164            else
165            {
166                ValueDirective directive = (ValueDirective) other;
167                if( !getPartHandlerURI().equals( directive.getPartHandlerURI() ) )
168                {
169                    return false;
170                }
171                return super.equals( other );
172            }
173        }
174        
175       /**
176        * Return the hashcode for the instance.
177        * @return the instance hashcode
178        */
179        public int hashCode()
180        {
181            int hash = super.hashCode();
182            hash ^= getPartHandlerURI().hashCode();
183            return hash;
184        }
185        
186        //--------------------------------------------------------------------------
187        // Part
188        //--------------------------------------------------------------------------
189    
190       /**
191        * Return the part handler uri.
192        * @return the uri of the part handler
193        */
194        public URI getPartHandlerURI()
195        {
196            return PART_HANDLER_URI;
197        }
198    
199        private static final URI PART_HANDLER_URI = setupURI( "artifact:part:dpml/metro/dpml-metro-runtime#1.0.4" );
200    
201       /**
202        * Internal utility to create a static uri.
203        * @param spec the uri spec
204        * @return the uri
205        */
206        protected static URI setupURI( String spec )
207        {
208            try
209            {
210                return new URI( spec );
211            }
212            catch( URISyntaxException ioe )
213            {
214                return null;
215            }
216        }
217    }