1 49 50 package org.apache.avalon.meta.info.builder.tags; 51 52 53 import java.util.ArrayList ; 54 import java.util.List ; 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 69 public class AbstractTag 70 { 71 74 public static final String TYPE_PARAM = "type"; 75 76 79 public static final String VERSION_PARAM = "version"; 80 81 82 private JavaClass m_class; 83 84 88 public AbstractTag( final JavaClass clazz ) 89 { 90 m_class = clazz; 91 } 92 93 97 protected JavaClass getJavaClass() 98 { 99 return m_class; 100 } 101 102 110 public String getNS() throws IllegalArgumentException 111 { 112 return Tags.NAMESPACE; 113 } 114 115 125 public String getNSD() throws IllegalArgumentException 126 { 127 return getNS() + Tags.DELIMITER; 128 } 129 130 139 protected String getNamedParameter( final DocletTag tag, 140 final String name, 141 final String defaultValue ) 142 { 143 String 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 168 protected String getNamedParameter( final DocletTag tag, final String name ) 169 { 170 final String value = getNamedParameter( tag, name, null ); 171 if( null == value ) 172 { 173 final String message = 174 "Malformed tag '" + tag.getName() + "'. " 175 + "Missing required parameter '" + name + "'"; 176 throw new IllegalArgumentException ( message ); 177 } 178 return value; 179 } 180 181 188 protected Version resolveVersion( final String version, final String 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 207 protected Version resolveVersion( final String type ) 208 { 209 if( type.indexOf(":") > -1 ) 210 { 211 return Version.getVersion( type.substring( type.indexOf(":") + 1 ) ); 212 } 213 return null; 214 } 215 216 224 protected String resolveType( final String type ) 225 { 226 final String 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 message = 240 "Unable to find type " + type 241 + " in class " + getJavaClass().getFullyQualifiedName(); 242 throw new RuntimeException ( message ); 243 } 244 else 245 { 246 return resolvedType; 247 } 248 } 249 250 private String doResolveType( final JavaClass clazz, final String type ) 251 { 252 final String 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 276 protected JavaMethod[] getLifecycleMethods( final String methodName, 277 final String parameterType ) 278 { 279 List result = new ArrayList (); 280 findLifecycleMethod( result, getJavaClass(), methodName, parameterType ); 281 return (JavaMethod[]) result.toArray( new JavaMethod[ result.size() ] ); 282 } 283 284 private void findLifecycleMethod( 285 final List result, 286 final JavaClass clazz, 287 final String methodName, 288 final String 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 key ) 314 { 315 ArrayList list = new ArrayList (); 316 return findTaggedMethods( clazz, key, list ); 317 } 318 319 private JavaMethod[] findTaggedMethods( final JavaClass clazz, String key, List 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 |