1 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 ; 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 26 public class DNAAttributeInterceptor 27 extends DefaultQDoxAttributeInterceptor 28 implements QDoxAttributeInterceptor 29 { 30 33 public Attribute processClassAttribute( final JavaClass clazz, 34 final Attribute attribute ) 35 { 36 final String name = attribute.getName(); 37 if( name.equals( "dna.service" ) ) 38 { 39 final Properties parameters = new Properties (); 40 final String type = attribute.getParameter( "type", null ); 41 if( null == type ) 42 { 43 final String message = 44 "dna.service attribute on class " + 45 clazz.getFullyQualifiedName() + 46 " must specify the parameter 'type'"; 47 throw new IllegalArgumentException ( message ); 48 } 49 final String 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 62 public Attribute processMethodAttribute( final JavaMethod method, 63 final Attribute attribute ) 64 { 65 final JavaClass clazz = method.getParentClass(); 66 final String 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 parameters = new Properties (); 75 final String type = attribute.getParameter( "type", null ); 76 if( null == type ) 77 { 78 final String message = 79 "dna.dependency attribute on class " + 80 clazz.getFullyQualifiedName() + 81 " must specify the parameter 'type'"; 82 throw new IllegalArgumentException ( message ); 83 } 84 final String resolvedType = resolveType( clazz, type ); 85 parameters.setProperty( "type", resolvedType ); 86 87 final String optional = attribute.getParameter( "optional", "false" ); 88 parameters.setProperty( "optional", optional ); 89 90 final String 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 108 String determineKey( final Attribute attribute, 109 final String resolvedType ) 110 { 111 final String key = attribute.getParameter( "key", null ); 112 final String 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 134 Attribute processConfigurationAttribute( final JavaClass clazz, 135 final Attribute attribute ) 136 { 137 final String type = 138 attribute.getParameter( "type", null ); 139 final String classname = clazz.getName(); 140 final String defaultLocation = getSchemaLocationFor( classname ); 141 final String location = 142 attribute.getParameter( "location", defaultLocation ); 143 final Properties parameters = new Properties (); 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 159 String getSchemaLocationFor( final String classname ) 160 { 161 final int index = classname.lastIndexOf( "." ); 162 String location = classname; 163 if( -1 != index ) 164 { 165 location = classname.substring( index + 1 ); 166 } 167 location += "-schema.xml"; 168 return location; 169 } 170 171 180 String resolveType( final JavaClass javaClass, 181 final String type ) 182 { 183 String baseType = type; 184 String 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 |