View Javadoc

    1   /*
    2    * Copyright (c) 2001-2003 The XDoclet team
    3    * All rights reserved.
    4    */
    5   package xjavadoc.ant;
    6
    7   import java.io.File;
    8   import java.util.Iterator;
    9
    10  import org.apache.tools.ant.BuildException;
    11  import org.apache.tools.ant.types.Parameter;
    12  import org.apache.tools.ant.types.selectors.BaseExtendSelector;
    13
    14  import xjavadoc.XClass;
    15  import xjavadoc.XJavaDoc;
    16  import xjavadoc.filesystem.FileSourceSet;
    17
    18  /***
    19   * Custom file filter for Ant based on XJavadoc. Filters java sources according
    20   * to some Java specific features. <br/>
    21   * Usage:<br/>
    22   * <pre>
    23   *&lt;copy todir="filtered-src"&gt;
    24   *   &lt;fileset dir="src"&gt;
    25   *      &lt;or&gt;
    26   *         &lt;custom classname="xjavadoc.XJavadocFilter" classpathref="lib.jars"&gt;
    27   *            &lt;parameter name="implements" value="javax.ejb.EntityBean" /&gt;
    28   *         &lt;/custom&gt;
    29   *         &lt;custom classname="xjavadoc.XJavadocFilter" classpathref="lib.jars"&gt;
    30   *            &lt;parameter name="implements" value="javax.ejb.SessionBean" /&gt;
    31   *         &lt;/custom&gt;
    32   *      &lt;/or&gt;
    33   *   &lt;/fileset&gt;
    34   *&lt;/copy&gt;
    35   *</pre> Valid parameters are:<br/>
    36   *
    37   * <dl>
    38   *   <dt> <strong>implements</strong> </dt>
    39   *   <dd> full qualified name of the class or interface to implement</dd>
    40   *   <dt> <strong>contains-tag</strong> </dt>
    41   *   <dd> javadoc tag to contain</dd>
    42   * </dl>
    43   *
    44   *
    45   * @author    Ludovic Claude
    46   * @created   02 November 2002
    47   * @version   $Revision: 1.6 $
    48   */
    49  public class XJavadocFilter extends BaseExtendSelector
    50  {
    51      XJavaDoc _xJavaDoc = new XJavaDoc();
    52
    53  	/***
    54  	 * Constructor for XJavadocFilter.
    55  	 */
    56  	public XJavadocFilter()
    57  	{
    58  		super();
    59  	}
    60
    61  	/***
    62  	 * @param basedir
    63  	 * @param filename
    64  	 * @param file
    65  	 * @return
    66  	 * @exception BuildException
    67  	 * @see                       org.apache.tools.ant.types.selectors.FileSelector#isSelected(File,
    68  	 *      String, File)
    69  	 */
    70  	public boolean isSelected( File basedir, String filename, File file )
    71  		 throws BuildException
    72  	{
    73
    74  		if( !filename.endsWith( ".java" ) )
    75  			return false;
    76
    77  		_xJavaDoc.reset( true );
    78  		try
    79  		{
    80  			//validateOptions();
    81
    82  			_xJavaDoc.addSourceSet( new FileSourceSet( basedir, new String[]{filename} ) );
    83
    84  			for( Iterator i =  _xJavaDoc.getSourceClasses().iterator() ; i.hasNext();  )
    85  			{
    86  				XClass clazz = (XClass) i.next();
    87  				Parameter[] params = getParameters();
    88
    89  				for( int j = 0; j < params.length; j++ )
    90  				{
    91  					Parameter param = params[j];
    92
    93  					if( param.getName().equals( "implements" ) )
    94  					{
    95  						String mandatoryClass = param.getValue();
    96
    97  						if( !clazz.isA( mandatoryClass ) )
    98  							return false;
    99  					}
    100 					else if( param.getName().equals( "contains-tag" ) )
    101 					{
    102 						String mandatoryTag = param.getValue();
    103
    104 						if( !clazz.getDoc().hasTag( mandatoryTag ) )
    105 							return false;
    106 					}
    107 				}
    108 			}
    109 		}
    110 		catch( OutOfMemoryError e )
    111 		{
    112 			System.err.println( e.getMessage() );
    113 			XJavaDoc.printMemoryStatus();
    114 			System.err.println( "Try to increase heap size. Can be done by defining ANT_OPTS=-Xmx640m" );
    115 			System.err.println( "See the JDK tooldocs." );
    116 			throw new BuildException( e.getMessage(), e );
    117 		}
    118 		catch( Throwable t )
    119 		{
    120 			t.printStackTrace();
    121 			throw new BuildException( "Unexpected error", t );
    122 		}
    123 		finally
    124 		{
    125 			//XJavaDoc.printMemoryStatus();
    126
    127 			_xJavaDoc.printLogMessages( System.out, XJavaDoc.NO_IMPORTED_PACKAGES );
    128 			_xJavaDoc.printLogMessages( System.out, XJavaDoc.ONE_OR_MORE_IMPORTED_PACKAGES );
    129 			_xJavaDoc.reset( true );
    130 			System.gc();
    131 		}
    132 		return true;
    133 	}
    134
    135 }