001    /*
002     * Copyright (c) 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.tools;
020    
021    import java.io.IOException;
022    import java.util.ArrayList;
023    import java.util.List;
024    import java.beans.IntrospectionException;
025    
026    import net.dpml.metro.data.ContextDirective;
027    import net.dpml.metro.info.PartReference;
028    import net.dpml.metro.info.Type;
029    
030    import org.apache.tools.ant.Task;
031    
032    /**
033     * A context directive class.
034     *
035     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
036     * @version 1.2.0
037     */
038    public class ContextDataType
039    {
040        private String m_class;
041        private List m_builders = new ArrayList();
042    
043       /**
044        * Declare a custom context implementation classname.
045        * @param classname the classname of an optional context implementation class
046        */
047        public void setClass( final String classname )
048        {
049            m_class = classname;
050        }
051    
052       /**
053        * Create a new constructed part builder.
054        * @return a part builder
055        */
056        public EntryDataType createEntry()
057        {
058            final EntryDataType builder = new EntryDataType();
059            m_builders.add( builder );
060            return builder;
061        }
062    
063       /**
064        * Create a name component builder.
065        * @return a new component builder
066        */
067        public ComponentBuilderTask createComponent()
068        {
069            ComponentBuilderTask builder = new ComponentBuilderTask();
070            m_builders.add( builder );
071            return builder;
072        }
073    
074       /**
075        * Return all of the part reference builders.
076        * @return the set of part reference builders
077        */
078        PartReferenceBuilder[] getBuilders()
079        {
080            return (PartReferenceBuilder[]) m_builders.toArray( new PartReferenceBuilder[0] );
081        }
082    
083       /**
084        * Return the optional context implementation classname.
085        * @return the classname
086        */
087        String getClassname()
088        {
089            return m_class;
090        }
091    
092       /**
093        * Create a new context directive.
094        * @param classloader the classloader to use
095        * @param type the underlying component type
096        * @return the context directive
097        * @exception IntrospectionException if a class introspection error occurs
098        * @exception IOException if an I/O error occurs
099        * @exception ClassNotFoundException if a context class was not found
100        */
101        ContextDirective getContextDirective( ClassLoader classloader, Type type ) 
102          throws IntrospectionException, IOException, ClassNotFoundException
103        {
104            String classname = getClassname();
105            PartReferenceBuilder[] builders = getBuilders();
106            PartReference[] references = new PartReference[ builders.length ];
107            for( int i=0; i<builders.length; i++ )
108            {
109                PartReferenceBuilder builder = builders[i];
110                if( builder instanceof ComponentBuilderTask )
111                {
112                    Task task = (Task) builder;
113                    task.setTaskName( "component" );
114                }
115                PartReference reference = builder.buildPartReference( classloader, type );
116                references[i] = reference;
117            }
118            return new ContextDirective( classname, references );
119        }
120    }
121