KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > meta > info > builder > tags > ContextTag


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 2002-2003 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
26     "Apache Software Foundation" must not be used to endorse or promote
27     products derived from this software without prior written
28     permission. For written permission, please contact apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48 */

49
50 package org.apache.avalon.meta.info.builder.tags;
51
52 import java.util.ArrayList JavaDoc;
53 import java.util.HashSet JavaDoc;
54 import java.util.Set JavaDoc;
55 import java.util.Properties JavaDoc;
56
57 import com.thoughtworks.qdox.model.DocletTag;
58 import com.thoughtworks.qdox.model.JavaClass;
59 import com.thoughtworks.qdox.model.JavaMethod;
60 import org.apache.avalon.meta.info.ContextDescriptor;
61 import org.apache.avalon.meta.info.EntryDescriptor;
62 import org.apache.avalon.meta.info.ReferenceDescriptor;
63
64 /**
65  * A doclet tag handler for the 'extension' tag.
66  *
67  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
68  * @version $Revision: 1.6 $ $Date: 2003/08/19 03:24:02 $
69  */

70 public class ContextTag extends AbstractTag
71 {
72    /**
73     * The default context class.
74     */

75     protected static final String JavaDoc CONTEXT_CLASS =
76         "org.apache.avalon.framework.context.Context";
77
78    /**
79     * The javadoc key for the context tag.
80     */

81     public static final String JavaDoc KEY = "context";
82
83    /**
84     * The javadoc context context strategy parameter.
85     */

86     public static final String JavaDoc STRATEGY_PARAM = "strategy";
87
88    /**
89     * The javadoc context tag key parameter.
90     */

91     public static final String JavaDoc KEY_PARAM = "key";
92
93    /**
94     * The javadoc context tag volatile flag.
95     */

96     public static final String JavaDoc VOLATILE_PARAM = "volatile";
97
98    /**
99     * The javadoc context tag alias parameter.
100     */

101     public static final String JavaDoc ALIAS_PARAM = "alias";
102
103    /**
104     * The javadoc context tag optional parameter.
105     */

106     public static final String JavaDoc OPTIONAL_PARAM = "optional";
107
108    /**
109     * The javadoc context entry tag name.
110     */

111     public static final String JavaDoc ENTRY = "entry";
112
113    /**
114     * Context tag constructor.
115     * @param clazz the javadoc class descriptor
116     */

117     public ContextTag( final JavaClass clazz )
118     {
119         super( clazz );
120     }
121
122    /**
123     * Return an array of StageDescriptor instances based on declared 'stage' tags.
124     * @return the context descriptors
125     */

126     public ContextDescriptor getContext()
127     {
128         JavaMethod[] methods = findTaggedMethods(
129           getJavaClass(), getNS() + Tags.DELIMITER + ENTRY );
130         if( methods.length == 0 )
131         {
132             return new ContextDescriptor( new EntryDescriptor[0] );
133         }
134         else
135         {
136             
137             //
138
// collect the @avalon.entry tags from this class and
139
// all supertypes methods marked with @avalon.entry
140
//
141

142             final ArrayList JavaDoc list = new ArrayList JavaDoc();
143             final Set JavaDoc marked = new HashSet JavaDoc( 10 );
144             for( int j = 0; j < methods.length; j++ )
145             {
146                 final DocletTag[] tags =
147                   methods[j].getTagsByName( getNS() + Tags.DELIMITER + ENTRY );
148                 for( int i = 0; i < tags.length; i++ )
149                 {
150                     final String JavaDoc key = getNamedParameter( tags[i], KEY_PARAM );
151                     if( !marked.contains( key ) )
152                     {
153                         list.add( getEntry( tags[i] ) );
154                         marked.add( key );
155                     }
156                 }
157             }
158
159             final EntryDescriptor[] entries =
160               (EntryDescriptor[])list.toArray( new EntryDescriptor[ list.size() ] );
161
162             String JavaDoc type = null;
163             String JavaDoc strategy = null;
164             for( int j = 0; j < methods.length; j++ )
165             {
166                 JavaMethod method = methods[j];
167                 final DocletTag tag = method.getTagByName( getNS() + Tags.DELIMITER + KEY );
168                 if( tag != null )
169                 {
170                     type =
171                       resolveType( getNamedParameter( tag, TYPE_PARAM, CONTEXT_CLASS ) );
172                     strategy = getNamedParameter( tag, STRATEGY_PARAM, null );
173                     break;
174                 }
175             }
176
177             Properties JavaDoc properties = null;
178             if( strategy != null )
179             {
180                 properties = new Properties JavaDoc();
181                 properties.setProperty(
182                    ContextDescriptor.STRATEGY_KEY, strategy );
183             }
184
185             return new ContextDescriptor( type, entries, properties );
186         }
187     }
188
189     private EntryDescriptor getEntry( DocletTag tag )
190     {
191         final String JavaDoc key = getNamedParameter( tag, KEY_PARAM );
192         final String JavaDoc alias = getNamedParameter( tag, ALIAS_PARAM, null );
193         final String JavaDoc entryType = getNamedParameter( tag, TYPE_PARAM, "java.lang.String" );
194         final String JavaDoc optional = getNamedParameter( tag, OPTIONAL_PARAM, "false" );
195         final boolean isOptional = "true".equals( optional );
196         final String JavaDoc volatileValue = getNamedParameter( tag, VOLATILE_PARAM, "false" );
197         final boolean isVolatile = "true".equals( volatileValue );
198         return new EntryDescriptor( key, entryType, isOptional, isVolatile, alias );
199     }
200 }
201
Popular Tags