KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > dna > tools > metaclass > DNAAttributeInterceptor


1 /*
2  * Copyright (C) The DNA Group. All rights reserved.
3  *
4  * This software is published under the terms of the DNA
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.dna.tools.metaclass;
9
10 import com.thoughtworks.qdox.model.JavaClass;
11 import com.thoughtworks.qdox.model.JavaMethod;
12 import java.util.Properties JavaDoc;
13 import org.codehaus.metaclass.model.Attribute;
14 import org.codehaus.metaclass.tools.qdox.DefaultQDoxAttributeInterceptor;
15 import org.codehaus.metaclass.tools.qdox.QDoxAttributeInterceptor;
16
17 /**
18  * This is an Attribute interceptor that invoked during construction
19  * of ClassDescriptors that will process DNA metadata. The processing
20  * involves setting default values for required attributes and resolving
21  * types in type-based attributes.
22  *
23  * @author Peter Donald
24  * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
25  */

26 public class DNAAttributeInterceptor
27     extends DefaultQDoxAttributeInterceptor
28     implements QDoxAttributeInterceptor
29 {
30     /**
31      * @see QDoxAttributeInterceptor#processClassAttribute(JavaClass, Attribute)
32      */

33     public Attribute processClassAttribute( final JavaClass clazz,
34                                             final Attribute attribute )
35     {
36         final String JavaDoc name = attribute.getName();
37         if( name.equals( "dna.service" ) )
38         {
39             final Properties JavaDoc parameters = new Properties JavaDoc();
40             final String JavaDoc type = attribute.getParameter( "type", null );
41             if( null == type )
42             {
43                 final String JavaDoc message =
44                     "dna.service attribute on class " +
45                     clazz.getFullyQualifiedName() +
46                     " must specify the parameter 'type'";
47                 throw new IllegalArgumentException JavaDoc( message );
48             }
49             final String JavaDoc resolvedType = resolveType( clazz, type );
50             parameters.setProperty( "type", resolvedType );
51             return new Attribute( "dna.service", parameters );
52         }
53         else
54         {
55             return attribute;
56         }
57     }
58
59     /**
60      * @see QDoxAttributeInterceptor#processMethodAttribute(JavaMethod, Attribute)
61      */

62     public Attribute processMethodAttribute( final JavaMethod method,
63                                              final Attribute attribute )
64     {
65         final JavaClass clazz = method.getParentClass();
66         final String JavaDoc name = attribute.getName();
67         if( name.equals( "dna.configuration" ) ||
68             name.equals( "dna.parameters" ) )
69         {
70             return processConfigurationAttribute( clazz, attribute );
71         }
72         else if( name.equals( "dna.dependency" ) )
73         {
74             final Properties JavaDoc parameters = new Properties JavaDoc();
75             final String JavaDoc type = attribute.getParameter( "type", null );
76             if( null == type )
77             {
78                 final String JavaDoc message =
79                     "dna.dependency attribute on class " +
80                     clazz.getFullyQualifiedName() +
81                     " must specify the parameter 'type'";
82                 throw new IllegalArgumentException JavaDoc( message );
83             }
84             final String JavaDoc resolvedType = resolveType( clazz, type );
85             parameters.setProperty( "type", resolvedType );
86
87             final String JavaDoc optional = attribute.getParameter( "optional", "false" );
88             parameters.setProperty( "optional", optional );
89
90             final String JavaDoc actualKey = determineKey( attribute, resolvedType );
91             parameters.setProperty( "key", actualKey );
92
93             return new Attribute( "dna.dependency", parameters );
94         }
95         else
96         {
97             return attribute;
98         }
99     }
100
101     /**
102      * Calculate key used to lookup dependency.
103      *
104      * @param attribute the attribute
105      * @param resolvedType the type of dependency
106      * @return the key
107      */

108     String JavaDoc determineKey( final Attribute attribute,
109                          final String JavaDoc resolvedType )
110     {
111         final String JavaDoc key = attribute.getParameter( "key", null );
112         final String JavaDoc qualifier = attribute.getParameter( "qualifier", null );
113         if( null != key )
114         {
115             return key;
116         }
117         else if( null != qualifier )
118         {
119             return resolvedType + "/" + qualifier;
120         }
121         else
122         {
123             return resolvedType;
124         }
125     }
126
127     /**
128      * Process a Configuration or Parameters Attribute.
129      *
130      * @param clazz the associated JavaClass
131      * @param attribute the attribute
132      * @return the resultant attribute
133      */

134     Attribute processConfigurationAttribute( final JavaClass clazz,
135                                              final Attribute attribute )
136     {
137         final String JavaDoc type =
138             attribute.getParameter( "type", null );
139         final String JavaDoc classname = clazz.getName();
140         final String JavaDoc defaultLocation = getSchemaLocationFor( classname );
141         final String JavaDoc location =
142             attribute.getParameter( "location", defaultLocation );
143         final Properties JavaDoc parameters = new Properties JavaDoc();
144         if( null != type )
145         {
146             parameters.setProperty( "type", type );
147         }
148         parameters.setProperty( "location", location );
149         return new Attribute( attribute.getName(), parameters );
150     }
151
152     /**
153      * Get the location of the schema. By default it is "Foo-schema.xml"
154      * for the com.biz.Foo component.
155      *
156      * @param classname the classname of component
157      * @return the location of the schema
158      */

159     String JavaDoc getSchemaLocationFor( final String JavaDoc classname )
160     {
161         final int index = classname.lastIndexOf( "." );
162         String JavaDoc location = classname;
163         if( -1 != index )
164         {
165             location = classname.substring( index + 1 );
166         }
167         location += "-schema.xml";
168         return location;
169     }
170
171     /**
172      * Resolve the specified type.
173      * Resolving essentially means finding the fully qualified name of
174      * a class from just it's short name.
175      *
176      * @param javaClass the java class relative to which the type must be resolved
177      * @param type the unresolved type
178      * @return the resolved type
179      */

180     String JavaDoc resolveType( final JavaClass javaClass,
181                         final String JavaDoc type )
182     {
183         String JavaDoc baseType = type;
184         String JavaDoc postFix = "";
185         if( type.endsWith( "[]" ) || type.endsWith( "{}" ) )
186         {
187             final int index = type.length() - 2;
188             baseType = type.substring( 0, index );
189             postFix = type.substring( index );
190         }
191         return javaClass.getParentSource().resolveType( baseType ) + postFix;
192     }
193 }
194
Popular Tags