summaryrefslogtreecommitdiff
path: root/java/ant/Slice2FreezeJTask.java
blob: c1fb1e8d986d7dc67e3781e52572a9a9e676c4c4 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

//package Ice.Ant;

import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.taskdefs.ExecTask;
import org.apache.tools.ant.types.Commandline.Argument;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;

import java.io.File;
import java.io.FileOutputStream;

/**
 * An ant task for slice2freezej. The task minimizes regeneration by
 * the use of tag files.
 *
 * Attributes:
 *
 *   translator - The pathname of the translator (default: "slice2java").
 *   tagdir - The directory in which tag files are located (default: ".").
 *   outputdir - The value for the --output-dir translator option.
 *
 * Nested elements:
 *
 *   includepath - The directories in which to search for Slice files.
 *   These are converted into -I directives for the translator.
 *   fileset - The set of Slice files which contain relevent types.
 *   dict - contains the NAME KEY & VALUE of the freeze type.
 *
 * Example:
 *
 *    <project ...>
 *        <taskdef name="slice2freezej" classname="Slice2FreezeJTask" />
 *        <property name="slice.dir" value="../include/slice"/>
 *        <target name="generate">
 *            <mkdir dir="tags" />
 *            <slice2freezej tagdir="tags" outputdir="out" output="CharIntMap">
 *                <includepath>
 *                    <pathelement path="${slice.dir}" />
 *                </includepath>
 *                <fileset dir="${slice.dir}">
 *                    <include name="*.ice" />
 *                </fileset>
 *                <dict name="CharIntMap" key="char" value="int"/>
 *            </slice2freezej>
 *        </target>
 *    </project>
 *
 * The <taskdef> element installs the slice2java task.
 */
public class Slice2FreezeJTask extends org.apache.tools.ant.Task
{
    public
    Slice2FreezeJTask()
    {
    }

    public void
    setTranslator(File prog)
    {
        _translator = prog;
    }

    public void
    setTagdir(File dir)
    {
        _tagDir = dir;
    }

    public void
    setOutputdir(File dir)
    {
        _outputDir = dir;
    }

    public Path
    createIncludePath()
    {
        if (_includePath == null) 
        {
            _includePath = new Path(project);
        }
        return _includePath.createPath();
    }

    public void
    setIncludePathRef(Reference ref)
    {
        createIncludePath().setRefid(ref);
    }

    public void
    setIncludePath(Path includePath)
    {
        if (_includePath == null)
        {
            _includePath = includePath;  
        }
        else
        {
            _includePath.append(includePath);
        }
    }

    public FileSet
    createFileset()
    {
        if (_fileSet == null) 
        {
            _fileSet = new FileSet();
        }
        return _fileSet;
    }

    public Dict
    createDict()
    {
	Dict d = new Dict();
	_dicts.add(d);
	return d;
    }

    public void
    execute()
        throws BuildException
    {
	if (_dicts.isEmpty())
	{
            throw new BuildException("No dictionaries specified");
	}

	//
	// Determine if the output file needs to be created.
	//
	DirectoryScanner scanner = _fileSet.getDirectoryScanner(project);
        String[] files = scanner.getIncludedFiles();

	java.util.List sliceFiles = new java.util.LinkedList();
	for (int i = 0; i < files.length; i++)
	{
	    File slice = new File(_fileSet.getDir(project), files[i]);
	    sliceFiles.add(slice);
	}

	java.util.List tagFiles = new java.util.LinkedList();

	boolean build = false;

	java.util.Iterator p = _dicts.iterator();
	while (p.hasNext())
	{
	    //
	    // Build the complete list of tag files - it's not
	    // possible to stop on the first missing or old file since
	    // if changed they all need to be touched.
	    //
	    File tag = new File(_tagDir, "." + ((Dict)p.next()).getName() + ".tag");
	    tagFiles.add(tag);

	    if (!build)
	    {
		if (!tag.exists())
		{
		    build = true;
		    continue;
		}
		java.util.Iterator q = sliceFiles.iterator();
		while (q.hasNext())
		{
		    File slice = (File)q.next();
		    if (slice.lastModified() > tag.lastModified())
		    {
			build = true;
			break;
		    }
		}
	    }
	}

	//
	// Add the --dict options.
	//
	p = _dicts.iterator();
	StringBuffer dictString = new StringBuffer();
	while (p.hasNext())
	{
	    Dict d = (Dict)p.next();

	    dictString.append(" --dict ");
	    dictString.append(d.getName() + "," + d.getKey() + "," + d.getValue());
	}

	if (!build)
	{
	    log("skipping" + dictString);
	    return;
	}

        //
        // Run the translator
        //
	StringBuffer cmd = new StringBuffer();

	//
	// Add --output-dir
	//
	if (_outputDir != null)
	{
	    cmd.append(" --output-dir ");
	    cmd.append(_outputDir.toString());
	}
	
	//
	// Add include directives
	//
	if (_includePath != null)
	{
	    String[] dirs = _includePath.list();
	    for (int i = 0; i < dirs.length; i++)
	    {
		cmd.append(" -I");
		cmd.append(dirs[i]);
	    }
	}

	//
	// Add the --dict options.
	//
	cmd.append(dictString);

	//
	// Add the slice files.
	//
	for (int i = 0; i < files.length; i++)
	{
	    cmd.append(" " + files[i]);
	}
	
	//
	// Execute.
	//
	log(_translator.toString() + " " + cmd);
	ExecTask task = (ExecTask)project.createTask("exec");
	task.setFailonerror(true);
	Argument arg = task.createArg();
	arg.setLine(cmd.toString());
	task.setExecutable(_translator.toString());
	task.execute();
	
	//
	// Touch the tag files.
	//
	p = tagFiles.iterator();
	while (p.hasNext())
	{
	    File tag = (File)p.next();
	    try
	    {
		FileOutputStream out = new FileOutputStream(tag);
		out.close();
	    }
	    catch(java.io.IOException ex)
	    {
		throw new BuildException("Unable to create tag file " +
					 tag + ": " + ex);
	    }
	}
    }

    private File _translator = new File("slice2freezej");
    private File _tagDir = new File(".");
    private File _outputDir = null;
    private Path _includePath = null;
    private FileSet _fileSet = null;

    public class Dict
    {
	private String _name;
	private String _key;
	private String _value;

	public void
	setName(String name)
	{
	    _name = name;
	}

	public String
	getName()
	{
	    return _name;
	}

	public void
	setKey(String key)
	{
	    _key = key;
	}

	public String
	getKey()
	{
	    return _key;
	}

	public void
	setValue(String value)
	{
	    _value = value;
	}

	public String
	getValue()
	{
	    return _value;
	}
    };
    private java.util.List _dicts = new java.util.LinkedList();
}