KickJava   Java API By Example, From Geeks To Geeks.

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


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
53 import java.util.ArrayList JavaDoc;
54 import java.util.List JavaDoc;
55
56 import com.thoughtworks.qdox.model.DocletTag;
57 import com.thoughtworks.qdox.model.JavaClass;
58 import com.thoughtworks.qdox.model.JavaMethod;
59 import com.thoughtworks.qdox.model.Type;
60
61 import org.apache.avalon.framework.Version;
62
63 /**
64  * A doclet tag representing the name of the Type.
65  *
66  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
67  * @version $Revision: 1.3 $ $Date: 2003/08/19 03:24:02 $
68  */

69 public class AbstractTag
70 {
71    /**
72     * The dependency tag type parameter name.
73     */

74     public static final String JavaDoc TYPE_PARAM = "type";
75
76    /**
77     * The dependency tag version parameter name.
78     */

79     public static final String JavaDoc VERSION_PARAM = "version";
80
81
82     private JavaClass m_class;
83
84    /**
85     * Class constructor.
86     * @param clazz the javadoc class descriptor
87     */

88     public AbstractTag( final JavaClass clazz )
89     {
90         m_class = clazz;
91     }
92
93    /**
94     * Return the javadoc class descriptor.
95     * @return the javadoc class descriptor
96     */

97     protected JavaClass getJavaClass()
98     {
99         return m_class;
100     }
101
102    /**
103     * Return the user defined namespace for avalon tags. The value returned is
104     * established by the javadoc tag 'avalon.namespace [namespace-value]' where
105     * [namespace-value] is a string representing the namespace identifier.
106     * @return the namespace tag used to represent the avalon.meta tag space
107     * @exception IllegalArgumentException if the namespace tag is declared by does not
108     * contain a value
109     */

110     public String JavaDoc getNS() throws IllegalArgumentException JavaDoc
111     {
112         return Tags.NAMESPACE;
113     }
114
115    /**
116     * Return the user defined namespace for avalon tags including the
117     * standard namespace delimiter. The value returned is established
118     * by the javadoc tag 'avalon.namespace [namespace-value]' where
119     * [namespace-value] is a string representing the namespace identifier
120     * with the namespace delimiter appended.
121     * @return the namespace tag with delimeter
122     * @exception IllegalArgumentException if the namespace tag is declared by does not
123     * contain a value
124     */

125     public String JavaDoc getNSD() throws IllegalArgumentException JavaDoc
126     {
127         return getNS() + Tags.DELIMITER;
128     }
129
130     /**
131      * Retrieve specified named parameter from tag. If the parameter
132      * does not exist then return specified default value.
133      *
134      * @param tag the tag
135      * @param name the name of parameter
136      * @param defaultValue the default value
137      * @return the value of named parameter
138      */

139     protected String JavaDoc getNamedParameter( final DocletTag tag,
140                                         final String JavaDoc name,
141                                         final String JavaDoc defaultValue )
142     {
143         String JavaDoc value = tag.getNamedParameter( name );
144         if( null == value )
145         {
146             return defaultValue;
147         }
148         value = value.trim();
149         if( value.startsWith( "\"" ) || value.startsWith( "'" ) )
150         {
151             value = value.substring( 1 );
152         }
153         if( value.endsWith( "\"" ) || value.endsWith( "'" ) )
154         {
155             value = value.substring( 0, value.length() - 1 );
156         }
157         return value;
158     }
159
160     /**
161      * Retrieve specified named parameter from tag. If the parameter
162      * does not exist then throw an exception.
163      *
164      * @param tag the tag
165      * @param name the name of parameter
166      * @return the value of named parameter
167      */

168     protected String JavaDoc getNamedParameter( final DocletTag tag, final String JavaDoc name )
169     {
170         final String JavaDoc value = getNamedParameter( tag, name, null );
171         if( null == value )
172         {
173             final String JavaDoc message =
174                 "Malformed tag '" + tag.getName() + "'. "
175                 + "Missing required parameter '" + name + "'";
176             throw new IllegalArgumentException JavaDoc( message );
177         }
178         return value;
179     }
180
181     /**
182      * Resolve a version form the supplied string.
183      *
184      * @param version the explicit version
185      * @param type the unresolved type in the classname:version format
186      * @return the version or null if no version specified
187      */

188     protected Version resolveVersion( final String JavaDoc version, final String JavaDoc type )
189     {
190         if( version != null )
191         {
192             return Version.getVersion( version );
193         }
194         if( type.indexOf(":") > -1 )
195         {
196             return Version.getVersion( type.substring( type.indexOf(":") + 1 ) );
197         }
198         return null;
199     }
200
201     /**
202      * Resolve a version form the supplied string.
203      *
204      * @param type the unresolved type
205      * @return the version or null if no version specified
206      */

207     protected Version resolveVersion( final String JavaDoc type )
208     {
209         if( type.indexOf(":") > -1 )
210         {
211             return Version.getVersion( type.substring( type.indexOf(":") + 1 ) );
212         }
213         return null;
214     }
215
216     /**
217      * Resolve the specified type.
218      * Resolving essentially means finding the fully qualified name of
219      * a class from just it's short name.
220      *
221      * @param type the unresolved type classname
222      * @return the resolved type classname
223      */

224     protected String JavaDoc resolveType( final String JavaDoc type )
225     {
226         final String JavaDoc resolvedType;
227         if( type.indexOf(":") > -1 )
228         {
229             resolvedType =
230               this.doResolveType( getJavaClass(),
231               type.substring( 0, type.indexOf(":") ) );
232         }
233         else
234         {
235             resolvedType = doResolveType( getJavaClass(), type );
236         }
237         if( resolvedType == null )
238         {
239             final String JavaDoc message =
240               "Unable to find type " + type
241               + " in class " + getJavaClass().getFullyQualifiedName();
242             throw new RuntimeException JavaDoc( message );
243         }
244         else
245         {
246             return resolvedType;
247         }
248     }
249
250     private String JavaDoc doResolveType( final JavaClass clazz, final String JavaDoc type )
251     {
252         final String JavaDoc resolvedType = clazz.getParentSource().resolveType( type );
253         if( resolvedType != null )
254         {
255             return resolvedType;
256         }
257         else if( clazz.getSuperJavaClass() == null
258           || JavaClass.OBJECT.equals( clazz.getSuperClass() ) )
259         {
260             return null;
261         }
262         else
263         {
264             return doResolveType( clazz.getSuperJavaClass(), type );
265         }
266     }
267
268     /**
269      * Retrieves all methods in the inheritance graph with specified name and
270      * one parameter of specified type. The methods must also return void.
271      *
272      * @param methodName the name of the methods
273      * @param parameterType the class name of parameter
274      * @return an array of such methods
275      */

276     protected JavaMethod[] getLifecycleMethods( final String JavaDoc methodName,
277                             final String JavaDoc parameterType )
278     {
279     List JavaDoc result = new ArrayList JavaDoc();
280     findLifecycleMethod( result, getJavaClass(), methodName, parameterType );
281     return (JavaMethod[]) result.toArray( new JavaMethod[ result.size() ] );
282     }
283
284     private void findLifecycleMethod(
285             final List JavaDoc result,
286             final JavaClass clazz,
287             final String JavaDoc methodName,
288             final String JavaDoc parameterType )
289     {
290         final JavaMethod[] methods = clazz.getMethods();
291         for( int i = 0; i < methods.length; i++ )
292         {
293             final JavaMethod method = methods[ i ];
294             if( methodName.equals( method.getName() )
295                 && method.getReturns().equals( new Type( "void", 0 ) )
296                 && method.getParameters().length == 1
297                 && method.getParameters()[ 0 ].getType().getValue().equals( parameterType ) )
298             {
299                 result.add( method );
300             break;
301             }
302         }
303
304         if(
305             clazz.getSuperJavaClass() != null
306             && !JavaClass.OBJECT.equals( clazz.getSuperClass() ) )
307         {
308             this.findLifecycleMethod(
309               result, clazz.getSuperJavaClass(), methodName, parameterType );
310         }
311     }
312
313     JavaMethod[] findTaggedMethods( final JavaClass clazz, String JavaDoc key )
314     {
315         ArrayList JavaDoc list = new ArrayList JavaDoc();
316         return findTaggedMethods( clazz, key, list );
317     }
318
319     private JavaMethod[] findTaggedMethods( final JavaClass clazz, String JavaDoc key, List JavaDoc list )
320     {
321         final JavaMethod[] methods = clazz.getMethods();
322         for( int i=0; i<methods.length; i++ )
323         {
324             JavaMethod method = methods[i];
325             final DocletTag tag =
326               method.getTagByName( key );
327             if( tag != null ) list.add( method );
328         }
329
330         if(
331             clazz.getSuperJavaClass() != null
332             && !JavaClass.OBJECT.equals( clazz.getSuperClass() ) )
333         {
334             return this.findTaggedMethods( clazz.getSuperJavaClass(), key, list );
335         }
336
337         return (JavaMethod[]) list.toArray( new JavaMethod[0] );
338
339     }
340 }
341
Popular Tags