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.util.List;
022    import java.util.LinkedList;
023    
024    import net.dpml.metro.info.Priority;
025    import net.dpml.metro.data.CategoryDirective;
026    import net.dpml.metro.data.CategoriesDirective;
027    
028    import org.apache.tools.ant.BuildException;
029    
030    /**
031     * Build datatype used to construct a categories descriptor.
032     *
033     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
034     * @version 1.1.3
035     */
036    public class CategoriesDataType
037    {
038        private List m_list = new LinkedList();
039    
040       /**
041        * CategoryDataType creation function invoked by the ant builder
042        * for all nested 'category' elements.
043        * 
044        * @return a datatype used to construct a category descriptor
045        */
046        public CategoryDataType createCategory()
047        {
048            CategoryDataType category = new CategoryDataType();
049            m_list.add( category );
050            return category;
051        }
052    
053       /**
054        * Utility method used to construct a CategoriesDirective based on 
055        * build time features assigned by ant.
056        * @return the CategoriesDirective containing zero or more CategoryDirective instances
057        */
058        public CategoriesDirective getCategoriesDirective()
059        {
060            CategoryDataType[] types = (CategoryDataType[]) m_list.toArray( new CategoryDataType[0] );
061            CategoryDirective[] directives = new CategoryDirective[ types.length ];
062            for( int i=0; i<types.length; i++ )
063            {
064                CategoryDataType type = types[i];
065                directives[i] = type.getCategoryDirective();
066            }
067            return new CategoriesDirective( directives );
068        }
069    
070       /**
071        * Build datatype used to construct a categories descriptor.
072        */
073        public class CategoryDataType
074        {
075            private String m_name;
076            private String m_priority;
077            private String m_target;
078    
079           /**
080            * Set the category name.
081            * @param name the category name
082            */
083            public void setName( String name )
084            {
085                m_name = name;
086            }
087    
088           /**
089            * Set the category priority.
090            * @param priority the category priority
091            */
092            public void setPriority( String priority )
093            {
094                m_priority = priority;
095            }
096    
097           /**
098            * Set the category target.
099            * @param target the category target
100            */
101            public void setTarget( String target )
102            {
103                m_target = target;
104            }
105    
106           /**
107            * Return the category directive.
108            * @return the directive
109            */
110            public CategoryDirective getCategoryDirective()
111            {
112                if( null == m_name )
113                {
114                    throw new BuildException( "Missing category name." );
115                }
116                Priority priority = getPriority();
117                return new CategoryDirective( m_name, priority, m_target );
118            }
119            
120            private Priority getPriority()
121            {
122                if( null == m_priority )
123                {
124                    return null;
125                }
126                else
127                {
128                    return Priority.parse( m_priority );
129                }
130            }
131        } 
132    }
133