summaryrefslogtreecommitdiffstats
path: root/src/net/java/games/gluegen/ant/StaticGLGenTask.java
blob: 35ed2d504949447ece7e0f06332b1ce3cda3b34e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package net.java.games.gluegen.ant;

/*
 * StaticGLGenTask.java
 * Copyright (C) 2003 Rob Grzywinski (rgrzywinski@realityinteractive.com)
 *
 * Copying, distribution and use of this software in source and binary
 * forms, with or without modification, is permitted provided that the
 * following conditions are met:
 *
 * Distributions of source code must reproduce the copyright notice,
 * this list of conditions and the following disclaimer in the source
 * code header files; and Distributions of binary code must reproduce
 * the copyright notice, this list of conditions and the following
 * disclaimer in the documentation, Read me file, license file and/or
 * other materials provided with the software distribution.
 *
 * The names of Sun Microsystems, Inc. ("Sun") and/or the copyright
 * holder may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT A WARRANTY OF ANY
 * KIND. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, NON-INTERFERENCE, ACCURACY OF
 * INFORMATIONAL CONTENT OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. THE
 * COPYRIGHT HOLDER, SUN AND SUN'S LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL THE
 * COPYRIGHT HOLDER, SUN OR SUN'S LICENSORS BE LIABLE FOR ANY LOST
 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
 * INABILITY TO USE THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGES. YOU ACKNOWLEDGE THAT THIS SOFTWARE IS NOT
 * DESIGNED, LICENSED OR INTENDED FOR USE IN THE DESIGN, CONSTRUCTION,
 * OPERATION OR MAINTENANCE OF ANY NUCLEAR FACILITY. THE COPYRIGHT
 * HOLDER, SUN AND SUN'S LICENSORS DISCLAIM ANY EXPRESS OR IMPLIED
 * WARRANTY OF FITNESS FOR SUCH USES.
 */

import java.io.IOException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.LogStreamHandler;
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.PatternSet;
import org.apache.tools.ant.util.JavaEnvUtils;

/**
 * <p>An <a href="http://ant.apache.org">ANT</a> {@link org.apache.tools.ant.Task}
 * for using {@link net.java.games.gluegen.opengl.BuildStaticGLInfo}.</p>
 * 
 * <p>Usage:</p>
 * <pre>
    &lt;staticglgen package="[generated files package]" 
                    headers="[file pattern of GL headers]"
                    outputdir="[directory to output the generated files]" /&gt;
 * </pre> 
 *
 * @author Rob Grzywinski <a href="mailto:rgrzywinski@realityinteractive.com">rgrzywinski@yahoo.com</a>
 */
// FIXME:  blow out javadoc
public class StaticGLGenTask extends Task
{
    /**
     * <p>The {@link net.java.games.gluegen.opengl.BuildStaticGLInfo} classname.</p>
     */
    private static final String GL_GEN = "net.java.games.gluegen.opengl.BuildStaticGLInfo";
    
    // =========================================================================
    /**
     * <p>The {@link org.apache.tools.ant.types.CommandlineJava} that is used
     * to execute {@link net.java.games.gluegen.opengl.BuildStaticGLInfo}.</p>
     */
    private CommandlineJava glgenCommandline;

    // =========================================================================
    /**
     * <p>The package name for the generated files.</p>
     */
    private String packageName;

    /**
     * <p>The output directory.</p>
     */
    private String outputDirectory;
    
    /**
     * <p>The {@link org.apache.tools.ant.types.FileSet} of GL headers.</p>
     */
    private FileSet headerSet = new FileSet();
    
    // =========================================================================
    /**
     * <p>Create and add the VM and classname to {@link org.apache.tools.ant.types.CommandlineJava}.</p>
     */
    public StaticGLGenTask()
    {
        // create the CommandlineJava that will be used to call BuildStaticGLInfo
        glgenCommandline = new CommandlineJava();
        
        // set the VM and classname in the commandline
        glgenCommandline.setVm(JavaEnvUtils.getJreExecutable("java"));
        glgenCommandline.setClassname(GL_GEN);
    }

    // =========================================================================
    // ANT getters and setters
    /**
     * <p>Set the package name for the generated files.  This is called by ANT.</p>
     * 
     * @param  packageName the name of the package for the generated files
     */
    public void setPackage(String packageName)
    {
        log( ("Setting package name to: " + packageName), Project.MSG_VERBOSE);
        this.packageName = packageName;
    }

    /**
     * <p>Set the output directory.  This is called by ANT.</p>
     * 
     * @param  directory the output directory
     */
    public void setOutputDir(String directory)
    {
        log( ("Setting output directory to: " + directory), 
              Project.MSG_VERBOSE);
        this.outputDirectory = directory;
    }

    /**
     * <p>Add a header file to the list.  This is called by ANT for a nested
     * element.</p>
     * 
     * @return {@link org.apache.tools.ant.types.PatternSet.NameEntry}
     */
    public PatternSet.NameEntry createHeader()
    {
        return headerSet.createInclude();
    }

    /**
     * <p>Add a header file to the list.  This is called by ANT for a nested
     * element.</p>
     * 
     * @return {@link org.apache.tools.ant.types.PatternSet.NameEntry}
     */
    public PatternSet.NameEntry createHeadersFile()
    {
        return headerSet.createIncludesFile();
    }

    /**
     * <p>Set the set of header patterns.  Patterns may be separated by a comma
     * or a space.  This is called by ANT.</p>
     *
     * @param  headers the string containing the header patterns
     */
    public void setHeaders(String headers)
    {
        headerSet.setIncludes(headers);
    }

    /**
     * <p>Add an optional classpath that defines the location of {@link net.java.games.gluegen.opengl.BuildStaticGLInfo}
     * and <code>BuildStaticGLInfo</code>'s dependencies.</p>
     * 
     * @returns {@link org.apache.tools.ant.types.Path}
     */
     public Path createClasspath()
     {
         return glgenCommandline.createClasspath(project).createPath();
     }

    // =========================================================================
    /**
     * <p>Run the task.  This involves validating the set attributes, creating
     * the command line to be executed and finally executing the command.</p>
     * 
     * @see  org.apache.tools.ant.Task#execute()
     */
    public void execute() 
        throws BuildException 
    {
        // validate that all of the required attributes have been set
        validateAttributes();
        
        // TODO:  add logic to determine if the generated file needs to be
        //        regenerated
        
        // add the attributes to the CommandlineJava
        addAttributes();

        log(glgenCommandline.describeCommand(), Project.MSG_VERBOSE);
        
        // execute the command and throw on error
        final int error = execute(glgenCommandline.getCommandline());
        if(error == 1)
            throw new BuildException( ("BuildStaticGLInfo returned: " + error), location);
    }

    /**
     * <p>Ensure that the user specified all required arguments.</p>
     * 
     * @throws BuildException if there are required arguments that are not 
     *         present or not valid
     */
    private void validateAttributes() 
        throws BuildException
    {
        // validate that the package name is set
        if(!isValid(packageName))
            throw new BuildException("Invalid package name: " + packageName);

        // validate that the output directory is set
        // TODO:  switch to file and ensure that it exists
        if(!isValid(outputDirectory))
            throw new BuildException("Invalid output directory name: " + outputDirectory);
            
        // TODO:  validate that there are headers set
    }

    /**
     * <p>Is the specified string valid?  A valid string is non-<code>null</code>
     * and has a non-zero length.</p>
     * 
     * @param  string the string to be tested for validity
     * @return <code>true</code> if the string is valid.  <code>false</code>
     *         otherwise. 
     */
    private boolean isValid(String string)
    {
        // check for null
        if(string == null)
            return false;
            
        // ensure that the string has a non-zero length
        // NOTE:  must trim() to remove leading and trailing whitespace
        if(string.trim().length() < 1)
            return false;
            
        // the string is valid
        return true;
    }

    /**
     * <p>Add all of the attributes to the command line.  They have already
     * been validated.</p>
     */
    private void addAttributes()
    {
        // add the package name
        glgenCommandline.createArgument().setValue(packageName);
        
        // add the output directory name
        glgenCommandline.createArgument().setValue(outputDirectory);
        
        // add the header -files- from the FileSet
        headerSet.setDir(getProject().getBaseDir());
        DirectoryScanner directoryScanner = headerSet.getDirectoryScanner(getProject());
        String[] directoryFiles = directoryScanner.getIncludedFiles();
        for(int i=0; i<directoryFiles.length; i++)
        {
            glgenCommandline.createArgument().setValue(directoryFiles[i]);
        }
    }

    /** 
     * <p>Execute {@link net.java.games.gluegen.opengl.BuildStaticGLInfo} in a 
     * forked JVM.</p>
     * 
     * @throws BuildException
     */
    private int execute(String[] command) 
        throws BuildException
    {
        // create the object that will perform the command execution
        Execute execute = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
                                                           Project.MSG_WARN), 
                                      null);
                                      
        // set the project and command line
        execute.setAntRun(project);
        execute.setCommandline(command);
        
        // execute the command
        try
        {
            return execute.execute();
        } catch(IOException ioe)
        {
            throw new BuildException(ioe, location);
        }
    }
}