View Javadoc

1   /*
2    * Copyright (c) 2001-2003 The XDoclet team
3    * All rights reserved.
4    */
5   package xjavadoc.ant;
6   
7   import org.apache.tools.ant.Task;
8   import org.apache.tools.ant.BuildException;
9   import org.apache.tools.ant.DirectoryScanner;
10  import org.apache.tools.ant.AntClassLoader;
11  import org.apache.tools.ant.types.Path;
12  import org.apache.tools.ant.types.FileSet;
13  
14  import java.util.LinkedList;
15  import java.io.File;
16  
17  import xjavadoc.XJavaDoc;
18  import xjavadoc.DefaultXTag;
19  import xjavadoc.filesystem.FileSourceSet;
20  
21  /***
22   * This class should be subclassed to be used for XDocletImpl, revXDoclet etc.
23   *
24   * @author    Aslak Hellesøy
25   * @author    Ara Abrahamian
26   * @created   26. februar 2003
27   */
28  public abstract class XJavadocTask extends Task
29  {
30      private final XJavaDoc _xJavaDoc = new XJavaDoc();
31  	private final LinkedList _fileSets = new LinkedList();
32  
33      protected XJavaDoc getXJavaDoc() {
34          return _xJavaDoc;
35      }
36  
37  	/***
38  	 * Sets the tags to ignore if validation is true. The value should be a
39  	 * comma-separated list of tag names (without the tag name)
40  	 *
41  	 * @param tags  tags that should be ignored when doing validation.
42  	 */
43  	public void setIgnoredtags( String tags )
44  	{
45  		_xJavaDoc.getTagFactory().setIgnoredTags( tags );
46  	}
47  
48  	/***
49  	 * Sets whether or not tags will be validated.
50  	 *
51  	 * @param flag validate?
52  	 */
53  	public void setValidating( boolean flag )
54  	{
55  		_xJavaDoc.getTagFactory().setValidating( flag );
56  	}
57  
58  	/***
59  	 * set source file charset
60           *
61  	 * @param enc the encoding
62  	 */
63  	public void setEncoding(String enc)
64  	{
65  		_xJavaDoc.setEncoding(enc);
66  	}
67  
68  	/***
69  	 * set generated file charset
70           *
71  	 * @param enc the encoding
72   	 */
73  	public void setDocencoding(String enc)
74  	{
75  		_xJavaDoc.setDocEncoding(enc);
76  	}
77  
78  	/***
79  	 * Implementation of Ant's {@link Task#execute()}.
80  	 *
81  	 * @exception BuildException  Ant's way of reporting build exception
82  	 */
83  	public final void execute() throws BuildException
84  	{
85  		_xJavaDoc.reset( true );
86  		_xJavaDoc.setPropertyMap( project.getProperties() );
87  		try
88  		{
89  			validateOptions();
90  
91  			FileSourceSet[] sourceSets = new FileSourceSet[_fileSets.size()];
92  
93  			for( int i = 0; i < _fileSets.size(); i++ )
94  			{
95  				FileSet fs = ( FileSet ) _fileSets.get( i );
96  				File dir = fs.getDir( project );
97  
98  				DirectoryScanner ds = fs.getDirectoryScanner( project );
99  				String[] files = ds.getIncludedFiles();
100 
101 				sourceSets[i] = new FileSourceSet( dir, files );
102 				_xJavaDoc.addSourceSet( sourceSets[i] );
103 			}
104 
105 			start();
106 		}
107 		catch( OutOfMemoryError e )
108 		{
109 			System.err.println( e.getMessage() );
110 			XJavaDoc.printMemoryStatus();
111 			System.err.println( "Try to increase heap size. Can be done by defining ANT_OPTS=-Xmx640m" );
112 			System.err.println( "See the JDK tooldocs." );
113 			throw new BuildException( e.getMessage(), e, location );
114 		}
115 		catch( Throwable t )
116 		{
117 			t.printStackTrace();
118 			throw new BuildException( "Unexpected error", t, location );
119 		}
120 		finally
121 		{
122 			//XJavaDoc.printMemoryStatus();
123 
124 			_xJavaDoc.printLogMessages( System.out, XJavaDoc.NO_IMPORTED_PACKAGES );
125 			_xJavaDoc.printLogMessages( System.out, XJavaDoc.ONE_OR_MORE_IMPORTED_PACKAGES );
126 			_xJavaDoc.reset(true);
127 			System.gc();
128 		}
129 	}
130 
131 	/***
132 	 * Ignores one tag
133 	 *
134 	 * @return
135 	 */
136 	public Object createIgnoredtag()
137 	{
138 		return
139 			new Object()
140 			{
141 				public void addText( String text )
142 				{
143 					_xJavaDoc.getTagFactory().registerTagClass( text, DefaultXTag.class );
144 				}
145 			};
146 	}
147 
148 	/***
149 	 * Ant's &lt;fileset&gt; definition. To define the files to parse.
150 	 *
151 	 * @param set  a fileset to add
152 	 */
153 	public void addFileset( FileSet set )
154 	{
155 		_fileSets.add( set );
156 	}
157 
158 	/***
159 	 * Returns the classpath
160 	 *
161 	 * @return   the classpath
162 	 */
163 	protected String getClasspath()
164 	{
165 		return ( ( AntClassLoader ) getClass().getClassLoader() ).getClasspath();
166 	}
167 
168 	/***
169 	 * Implement this method and play with _xJavaDoc
170 	 *
171 	 * @exception BuildException  Ant's way of reporting exception
172 	 */
173 	protected abstract void start() throws BuildException;
174 
175 	/***
176 	 * Validate a Xdoclet task before running it.
177 	 *
178 	 * @exception BuildException  in case the validation fails.
179 	 */
180 	protected void validateOptions() throws BuildException
181 	{
182 		if( _fileSets.size() == 0 )
183 		{
184 			throw new BuildException( "At least one fileset must be specified", location );
185 		}
186 	}
187 }