KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > info > PhoenixAttributeInterceptor


1 /*
2  * Copyright (C) The Loom Group. All rights reserved.
3  *
4  * This software is published under the terms of the Loom
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.loom.info;
9
10 import com.thoughtworks.qdox.model.DocletTag;
11 import com.thoughtworks.qdox.model.JavaClass;
12 import com.thoughtworks.qdox.model.JavaMethod;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Properties JavaDoc;
15 import org.codehaus.metaclass.model.Attribute;
16 import org.codehaus.metaclass.tools.qdox.DefaultQDoxAttributeInterceptor;
17 import org.codehaus.metaclass.tools.qdox.QDoxAttributeInterceptor;
18
19 /**
20  * This is an Attribute interceptor that invoked during construction of
21  * ClassDescriptors that will translate legacy Loom attributes into modern DNA
22  * and MX attributes.
23  *
24  * @author Peter Donald
25  * @version $Revision: 1.2 $ $Date: 2004/05/01 12:48:35 $
26  */

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

35     public Attribute processClassAttribute( final JavaClass clazz,
36                                             final Attribute attribute )
37     {
38         final String JavaDoc name = attribute.getName();
39         if( name.equals( "phoenix:block" ) )
40         {
41             return new Attribute( "dna.component" );
42         }
43         else if( name.equals( "phoenix:service" ) )
44         {
45             final Properties JavaDoc parameters = new Properties JavaDoc();
46             final String JavaDoc type = attribute.getParameter( "name", null );
47             setParameter( parameters, "type", type );
48             return new Attribute( "dna.service", parameters );
49         }
50         else if( name.equals( "phoenix:mx-topic" ) )
51         {
52             final String JavaDoc description = attribute.getParameter( "name", "" );
53             final Properties JavaDoc parameters = new Properties JavaDoc();
54             setParameter( parameters, "description", description );
55             return new Attribute( "mx.component", parameters );
56         }
57         else if( name.equals( "phoenix:mx-proxy" ) )
58         {
59             final Properties JavaDoc parameters = new Properties JavaDoc();
60             final String JavaDoc type = attribute.getParameter( "class", "" );
61             setParameter( parameters, "type", type );
62             return new Attribute( "mx.proxy", parameters );
63         }
64         else
65         {
66             return attribute;
67         }
68     }
69
70     /**
71      * @see QDoxAttributeInterceptor#processMethodAttribute(JavaMethod,
72         * Attribute)
73      */

74     public Attribute processMethodAttribute( final JavaMethod method,
75                                              final Attribute attribute )
76     {
77         final String JavaDoc name = attribute.getName();
78         if( name.equals( "phoenix:configuration-schema" ) )
79         {
80             final String JavaDoc type =
81                 attribute.getParameter( "type", null );
82             final Properties JavaDoc parameters = new Properties JavaDoc();
83             if( "relax-ng".equals( type ) )
84             {
85                 setParameter( parameters,
86                               "type",
87                               "http://relaxng.org/ns/structure/1.0" );
88             }
89             else
90             {
91                 setParameter( parameters, "type", type );
92             }
93             return new Attribute( "dna.configuration", parameters );
94         }
95         else if( name.equals( "phoenix:dependency" ) )
96         {
97             final Properties JavaDoc parameters = new Properties JavaDoc();
98             final String JavaDoc key = attribute.getParameter( "role", null );
99             final String JavaDoc type = attribute.getParameter( "name", null );
100             setParameter( parameters, "type", type );
101             setParameter( parameters, "key", key );
102             return new Attribute( "dna.dependency", parameters );
103         }
104         else if( name.equals( "phoenix:mx-operation" ) )
105         {
106             final DocletTag descriptionTag =
107                 method.getTagByName( "phoenix:mx-description" );
108             final String JavaDoc description;
109             if( null != descriptionTag )
110             {
111                 description = descriptionTag.getValue();
112             }
113             else
114             {
115                 description = method.getComment();
116             }
117             final Properties JavaDoc parameters = new Properties JavaDoc();
118             setParameter( parameters, "description", description );
119             return new Attribute( "mx.operation", parameters );
120         }
121         if( name.equals( "phoenix:mx-attribute" ) )
122         {
123             final DocletTag descriptionTag =
124                 method.getTagByName( "phoenix:mx-description" );
125             final String JavaDoc description;
126             if( null != descriptionTag )
127             {
128                 description = descriptionTag.getValue();
129             }
130             else
131             {
132                 description = method.getComment();
133             }
134             final Properties JavaDoc parameters = new Properties JavaDoc();
135             setParameter( parameters, "description", description );
136             return new Attribute( "mx.attribute", parameters );
137         }
138         else if( name.equals( "phoenix:mx-description" ) )
139         {
140             return null;
141         }
142         else
143         {
144             return attribute;
145         }
146     }
147
148     /**
149      * @see QDoxAttributeInterceptor#processClassAttributes(JavaClass,
150         * Attribute[])
151      */

152     public Attribute[] processClassAttributes( final JavaClass clazz,
153                                                final Attribute[] attributes )
154     {
155         final ArrayList JavaDoc result = new ArrayList JavaDoc();
156         for( int i = 0; i < attributes.length; i++ )
157         {
158             final Attribute attribute = attributes[ i ];
159             final String JavaDoc name = attribute.getName();
160             if( !name.equals( "phoenix:mx" ) )
161             {
162                 result.add( attribute );
163             }
164             else
165             {
166                 final Properties JavaDoc parameters = new Properties JavaDoc();
167                 final String JavaDoc type = attribute.getParameter( "name", "" );
168                 setParameter( parameters, "type", type );
169                 result.add( new Attribute( "dna.service", parameters ) );
170
171                 final Properties JavaDoc mxParameters = new Properties JavaDoc();
172                 setParameter( mxParameters, "type", type );
173                 final String JavaDoc topic = type.substring(
174                     type.lastIndexOf( '.' ) + 1 );
175                 setParameter( mxParameters, "topic", topic );
176
177                 result.add( new Attribute( "mx.interface", mxParameters ) );
178             }
179         }
180         return (Attribute[])result.toArray( new Attribute[ result.size() ] );
181     }
182
183     /**
184      * @see QDoxAttributeInterceptor#processMethodAttributes(JavaMethod,
185         * Attribute[])
186      */

187     public Attribute[] processMethodAttributes( final JavaMethod method,
188                                                 final Attribute[] attributes )
189     {
190         final ArrayList JavaDoc result = new ArrayList JavaDoc();
191
192         boolean isMxOperation = false;
193         for( int i = 0; i < attributes.length; i++ )
194         {
195             final Attribute attribute = attributes[ i ];
196             if( attribute.getName().equals( "mx.operation" ) )
197             {
198                 isMxOperation = true;
199             }
200             result.add( attribute );
201         }
202         if( isMxOperation )
203         {
204             for( int i = 0; i < attributes.length; i++ )
205             {
206                 final Attribute attribute = attributes[ i ];
207                 if( attribute.getName().equals( "param" ) )
208                 {
209                     final String JavaDoc value = attribute.getValue();
210                     final int index = value.indexOf( " " );
211                     if( -1 == index )
212                     {
213                         continue;
214                     }
215                     final String JavaDoc name = value.substring( 0, index ).trim();
216                     final String JavaDoc description = value.substring( index + 1 );
217                     final Properties JavaDoc parameters = new Properties JavaDoc();
218                     setParameter( parameters, "name", name );
219                     setParameter( parameters, "description", description );
220                     result.add( new Attribute( "mx.parameter", parameters ) );
221                 }
222             }
223         }
224         return (Attribute[])result.toArray( new Attribute[ result.size() ] );
225     }
226
227     /**
228      * Set parameter if value not null.
229      *
230      * @param parameters the parameters object
231      * @param key the key
232      * @param value the value
233      */

234     private void setParameter( final Properties JavaDoc parameters,
235                                final String JavaDoc key,
236                                final String JavaDoc value )
237     {
238         if( null != value )
239         {
240             parameters.setProperty( key, value );
241         }
242     }
243 }
244
Popular Tags