Subversion Repositories DevTools

Rev

Rev 1615 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1615 Rev 1619
Line 7... Line 7...
7
import org.apache.tools.ant.taskdefs.*;
7
import org.apache.tools.ant.taskdefs.*;
8
import org.apache.tools.ant.types.*;
8
import org.apache.tools.ant.types.*;
9
 
9
 
10
 
10
 
11
/**
11
/**
12
 * Custom ANT task - given a code JAR and a source base, build a JAR  
12
 * Custom ANT task - given a code JAR and a source base, build a JAR
13
 * containing the corresponding source code.
13
 * containing the corresponding source code.
14
 * 
14
 *
15
 * @ant.examples Here are examples
15
 * @ant.examples Here are examples
16
 * 
16
 *
17
 */
17
 */
18
public class JarSrc extends Task
18
public class JarSrc extends Task
19
{
19
{
20
	/**
20
	/**
21
	 * The binary source file; the src jar will be constructed with a
21
	 * The binary source file; the src jar will be constructed with a
Line 23... Line 23...
23
	 */
23
	 */
24
	private String binary;
24
	private String binary;
25
 
25
 
26
	/**
26
	/**
27
	 * The location that the source file will be created in.
27
	 * The location that the source file will be created in.
28
	 */	
28
	 */
29
	private String toDir;
29
	private String toDir;
30
	
30
 
31
	/**
31
	/**
32
	 * The FileSet indicating which corresponding source files should be
32
	 * The FileSet indicating which corresponding source files should be
33
	 * obtained (if they exist) from the source path.
33
	 * obtained (if they exist) from the source path.
34
	 */
34
	 */
35
	private FileSet correspondingFileSet = null;
35
	private FileSet correspondingFileSet = null;
36
	
36
 
37
	/**
37
	/**
38
	 * The set of directories under which the source code is located in
38
	 * The set of directories under which the source code is located in
39
	 * its' expected location.
39
	 * its' expected location.
40
	 */
40
	 */
41
	private ArrayList dirsets = new ArrayList(); 
41
	private ArrayList dirsets = new ArrayList();
42
	
42
 
43
	/**
43
	/**
44
	 * Set the filename of the source file. This file is used as a basis
44
	 * Set the filename of the source file. This file is used as a basis
45
	 * for emulation, by constructing a src package to match.
45
	 * for emulation, by constructing a src package to match.
46
	 * 
46
	 *
47
	 * @param value the filename.
47
	 * @param value the filename.
48
	 */
48
	 */
49
	public void setBinary( String value )
49
	public void setBinary( String value )
50
	{
50
	{
51
		binary = value;
51
		binary = value;
52
	}
52
	}
53
 
53
 
54
	/**
54
	/**
55
	 * Set the 'todir', being the directory location in which the source
55
	 * Set the 'todir', being the directory location in which the source
56
	 * file will be constructed.
56
	 * file will be constructed.
57
	 * 
57
	 *
58
	 * @param value the directory.
58
	 * @param value the directory.
59
	 */
59
	 */
60
	public void setToDir( String value )
60
	public void setToDir( String value )
61
	{
61
	{
62
		toDir = value;
62
		toDir = value;
63
	}
63
	}
64
	
64
 
65
	/**
65
	/**
66
	 * Nested element - this tag supports nested 'DirSet' elements.
66
	 * Nested element - this tag supports nested 'DirSet' elements.
67
	 */
67
	 */
68
	public DirSet createDirSet()
68
	public DirSet createDirSet()
69
	{
69
	{
70
		DirSet dirset = new DirSet();
70
		DirSet dirset = new DirSet();
71
		dirsets.add( dirset );
71
		dirsets.add( dirset );
72
		return dirset;
72
		return dirset;
73
	}
73
	}
74
	
74
 
75
	/**
75
	/**
76
	 * 
76
	 *
77
	 */
77
	 */
78
	public void execute() throws BuildException
78
	public void execute() throws BuildException
79
	{
79
	{
80
		if ( dirsets.size() == 0 ) 
80
		if ( dirsets.size() == 0 )
81
		{
81
		{
82
			throw new BuildException("At least one DirSet must be provided");	
82
			throw new BuildException("At least one DirSet must be provided");
83
		}
83
		}
84
 
84
 
85
		for ( Iterator i=dirsets.iterator(); i.hasNext(); )
85
		for ( Iterator i=dirsets.iterator(); i.hasNext(); )
86
		{
86
		{
87
			DirSet dirset = (DirSet)i.next();
87
			DirSet dirset = (DirSet)i.next();
Line 93... Line 93...
93
				File srcDirectory = new File( basedir, directories[j] );
93
				File srcDirectory = new File( basedir, directories[j] );
94
				packageSource( srcDirectory );
94
				packageSource( srcDirectory );
95
			}
95
			}
96
		}
96
		}
97
	}
97
	}
98
	
98
 
99
	/**
99
	/**
100
	 * 
100
	 *
101
	 */
101
	 */
102
	private void packageSource( File srcDir ) throws BuildException
102
	private void packageSource( File srcDir ) throws BuildException
103
	{
103
	{
104
		log("Packaging source from '" + srcDir + "'", Project.MSG_VERBOSE );		
104
		log("Packaging source from '" + srcDir + "'", Project.MSG_VERBOSE );
105
		FileSet corresponding = null;
105
		FileSet corresponding = null;
106
		
106
 
107
		try
107
		try
108
		{
108
		{
109
			corresponding = getCorrespondingNames();
109
			corresponding = getCorrespondingNames();
110
			corresponding.setDir( srcDir );
110
			corresponding.setDir( srcDir );
111
		}
111
		}
112
		catch (IOException x)
112
		catch (IOException x)
113
		{
113
		{
114
			throw new BuildException( "Could not read binary '" + binary + "'", x);
114
			throw new BuildException( "Could not read binary '" + binary + "'", x);
115
		}
115
		}
116
		
116
 
117
		File binarySource = new File( srcDir, binary );
117
		File binarySource = new File( srcDir, binary );
118
		String binaryName = binarySource.getName();
118
		String binaryName = binarySource.getName();
119
		String destName = binaryName.substring(0, binaryName.lastIndexOf("."));
119
		String destName = binaryName.substring(0, binaryName.lastIndexOf("."));
120
		destName += "-src.jar";
120
		destName += "-src.jar";
121
		File destDir = new File( toDir );
121
		File destDir = new File( toDir );
122
		File destFile = new File( toDir, destName );
122
		File destFile = new File( toDir, destName );
123
		
123
 
124
		// Check that the corresponding src JAR exists; if not, create one.
124
		// Check that the corresponding src JAR exists; if not, create one.
125
		Jar jarTask = new Jar();
125
		Jar jarTask = new Jar();
126
		jarTask.setTaskName(this.getTaskName());
126
		jarTask.setTaskName(this.getTaskName());
127
		jarTask.setProject( getProject() );
127
		jarTask.setProject( getProject() );
128
		jarTask.setDestFile( destFile );
128
		jarTask.setDestFile( destFile );
129
		jarTask.setUpdate(true);
129
		jarTask.setUpdate(true);
130
		
130
 
131
		jarTask.addFileset( corresponding );
131
		jarTask.addFileset( corresponding );
132
		jarTask.execute();
132
		jarTask.execute();
133
	}
133
	}
134
	
134
 
135
	/**
135
	/**
136
	 * Get the corresponding java source names (as a FileSet), by
136
	 * Get the corresponding java source names (as a FileSet), by
137
	 * using the configured 'binary'.
137
	 * using the configured 'binary'.
138
	 * 
138
	 *
139
	 * @return a FileSet containing a list of the expected source
139
	 * @return a FileSet containing a list of the expected source
140
	 *         files.
140
	 *         files.
141
	 * 
141
	 *
142
	 * @throws IOException if the binary could not be read.
142
	 * @throws IOException if the binary could not be read.
143
	 */
143
	 */
144
	private FileSet getCorrespondingNames() throws IOException
144
	private FileSet getCorrespondingNames() throws IOException
145
	{
145
	{
146
		if ( correspondingFileSet == null )
146
		if ( correspondingFileSet == null )
147
		{
147
		{
148
			FileSet corresponding = new FileSet();
148
			FileSet corresponding = new FileSet();
149
			corresponding.setProject( getProject() );
149
			corresponding.setProject( getProject() );
150
			
150
 
151
			ZipFile zf = null;
151
			ZipFile zf = null;
152
			try 
152
			try
153
			{
153
			{
154
				log("Loading from source binary '" + binary + "'", Project.MSG_VERBOSE );
154
				log("Loading from source binary '" + binary + "'", Project.MSG_VERBOSE );
155
				zf = new ZipFile(binary);
155
				zf = new ZipFile(binary);
156
				Enumeration enum = zf.entries();
156
				Enumeration l_enum = zf.entries();
157
				while (enum.hasMoreElements()) 
157
				while (l_enum.hasMoreElements())
158
				{
158
				{
159
					ZipEntry ze = (ZipEntry)enum.nextElement();
159
					ZipEntry ze = (ZipEntry)l_enum.nextElement();
160
					String entryName = ze.getName();
160
					String entryName = ze.getName();
161
					if ( entryName.endsWith( ".class" ) )
161
					if ( entryName.endsWith( ".class" ) )
162
					{
162
					{
163
						String correspondingName = entryName.substring(0, entryName.indexOf(".class") );
163
						String correspondingName = entryName.substring(0, entryName.indexOf(".class") );
164
						correspondingName += ".java";
164
						correspondingName += ".java";
165
						log("Adding '" + correspondingName + "' to FileSet", Project.MSG_VERBOSE );						
165
						log("Adding '" + correspondingName + "' to FileSet", Project.MSG_VERBOSE );
166
						corresponding.setIncludes( correspondingName );
166
						corresponding.setIncludes( correspondingName );
167
					}
167
					}
168
				}
168
				}
169
			} 
169
			}
170
			finally 
170
			finally
171
			{
171
			{
172
				if (zf != null) 
172
				if (zf != null)
173
				{
173
				{
174
					try 
174
					try
175
					{
175
					{
176
						zf.close();
176
						zf.close();
177
					} 
177
					}
178
					catch (IOException e) 
178
					catch (IOException e)
179
					{
179
					{
180
					}
180
					}
181
				}
181
				}
182
			}
182
			}
183
			correspondingFileSet = corresponding;
183
			correspondingFileSet = corresponding;
184
		}		
184
		}
185
		return correspondingFileSet;
185
		return correspondingFileSet;
186
	}
186
	}
187
}
187
}