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  	/***
34  	 * @param path
35  	 * @deprecated   This method will disappear soon
36  	 * @ant.ignore
37  	 */
38  	public void setSourcepath( Path path )
39  	{
40  		throw new BuildException( "The 'sourcepath' attribute can not be used anymore.  Use <fileset> to define the files to parse" );
41  	}
42  
43      protected XJavaDoc getXJavaDoc() {
44          return _xJavaDoc;
45      }
46  
47  	/***
48  	 * Sets the tags to ignore if validation is true. The value should be a
49  	 * comma-separated list of tag names (without the tag name)
50  	 *
51  	 * @param tags  tags that should be ignored when doing validation.
52  	 */
53  	public void setIgnoredtags( String tags )
54  	{
55  		_xJavaDoc.getTagFactory().setIgnoredTags( tags );
56  	}
57  
58  	/***
59  	 * Sets whether or not tags will be validated.
60  	 *
61  	 * @param flag
62  	 */
63  	public void setValidating( boolean flag )
64  	{
65  		_xJavaDoc.getTagFactory().setValidating( flag );
66  	}
67  
68  	/***
69  	 * set source file charset
70  	 */
71  	public void setEncoding(String enc)
72  	{
73  		_xJavaDoc.setEncoding(enc);
74  	}
75  
76  	/*
77  	 * set generated file charset
78   	*/	
79  	public void setDocencoding(String enc)
80  	{
81  		_xJavaDoc.setDocEncoding(enc);
82  	}
83  	/***
84  	 * Implementation of Ant's Task.execute().
85  	 *
86  	 * @exception BuildException  Ant's way or reporting build exception
87  	 */
88  	public final void execute() throws BuildException
89  	{
90  		_xJavaDoc.reset( true );
91  		_xJavaDoc.setPropertyMap( project.getProperties() );
92  		try
93  		{
94  			validateOptions();
95  
96  			FileSourceSet[] sourceSets = new FileSourceSet[_fileSets.size()];
97  
98  			for( int i = 0; i < _fileSets.size(); i++ )
99  			{
100 				FileSet fs = ( FileSet ) _fileSets.get( i );
101 				File dir = fs.getDir( project );
102 
103 				DirectoryScanner ds = fs.getDirectoryScanner( project );
104 				String[] files = ds.getIncludedFiles();
105 
106 				sourceSets[i] = new FileSourceSet( dir, files );
107 				_xJavaDoc.addSourceSet( sourceSets[i] );
108 			}
109 
110 			start();
111 		}
112 		catch( OutOfMemoryError e )
113 		{
114 			System.err.println( e.getMessage() );
115 			XJavaDoc.printMemoryStatus();
116 			System.err.println( "Try to increase heap size. Can be done by defining ANT_OPTS=-Xmx640m" );
117 			System.err.println( "See the JDK tooldocs." );
118 			throw new BuildException( e.getMessage(), e, location );
119 		}
120 		catch( Throwable t )
121 		{
122 			t.printStackTrace();
123 			throw new BuildException( "Unexpected error", t, location );
124 		}
125 		finally
126 		{
127 			//XJavaDoc.printMemoryStatus();
128 
129 			_xJavaDoc.printLogMessages( System.out, XJavaDoc.NO_IMPORTED_PACKAGES );
130 			_xJavaDoc.printLogMessages( System.out, XJavaDoc.ONE_OR_MORE_IMPORTED_PACKAGES );
131 			_xJavaDoc.reset(true);
132 			System.gc();
133 		}
134 	}
135 
136 	/***
137 	 * Ignores one tag
138 	 *
139 	 * @return
140 	 */
141 	public Object createIgnoredtag()
142 	{
143 		return
144 			new Object()
145 			{
146 				public void addText( String text )
147 				{
148 					_xJavaDoc.getTagFactory().registerTagClass( text, DefaultXTag.class );
149 				}
150 			};
151 	}
152 
153 	/***
154 	 * Ant's &lt;fileset&gt; definition. To define the files to parse.
155 	 *
156 	 * @param set  a fileset to add
157 	 */
158 	public void addFileset( FileSet set )
159 	{
160 		_fileSets.add( set );
161 	}
162 
163 	/***
164 	 * Returns the classpath
165 	 *
166 	 * @return   the classpath
167 	 */
168 	protected String getClasspath()
169 	{
170 		return ( ( AntClassLoader ) getClass().getClassLoader() ).getClasspath();
171 	}
172 
173 	/***
174 	 * Implement this method and play with _xJavaDoc
175 	 *
176 	 * @exception BuildException  Ant's way of reporting exception
177 	 */
178 	protected abstract void start() throws BuildException;
179 
180 	/***
181 	 * Validate a Xdoclet task before running it.
182 	 *
183 	 * @exception BuildException  in case the validation fails.
184 	 */
185 	protected void validateOptions() throws BuildException
186 	{
187 		if( _fileSets.size() == 0 )
188 		{
189 			throw new BuildException( "At least one fileset must be specified", location );
190 		}
191 	}
192 }
193