KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infozone > tools > ant > DependendExec


1 // You can redistribute this software and/or modify it under the terms of
2
// the Infozone Software License version 2 published by the Infozone Group
3
// (http://www.infozone-group.org).
4
//
5
// Copyright (C) @year@ by The Infozone Group. All rights reserved.
6
//
7
// $Id: DependendExec.java,v 1.1 2002/05/10 08:59:11 per_nyfelt Exp $
8

9 package org.infozone.tools.ant;
10
11 import org.apache.tools.ant.BuildException;
12 import org.apache.tools.ant.taskdefs.Exec;
13 import org.apache.tools.ant.Project;
14
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17
18 /**
19  * ANT cannot handle the dependency of source files from a definition file.
20  * This ability is needed if one will generate source files with castor.
21  * This module solves this problem.
22  *
23  * @version $Revision: 1.1 $
24  * @author andreas.kasparz@interface-projects.de
25  *
26  */

27 public class DependendExec extends Exec {
28
29     //
30
// Data
31
//
32

33     /** the filename of the target */
34     private String JavaDoc _target = null;
35     /** the filename the target depends on */
36     private String JavaDoc _source = null;
37
38     /**
39      * Check if the target is older than the source file or not exists.
40      * @param target the target
41      * @param source the source file
42      */

43     private boolean isOlder( String JavaDoc source, String JavaDoc target ) throws BuildException {
44         File JavaDoc f_src = new File JavaDoc( source );
45
46         if( !(f_src.exists() && (f_src.isFile() || f_src.isDirectory())) )
47             throw new BuildException("Source [[" + source + "]] doesn't exists or isn't a regular entry");
48     
49         File JavaDoc f_dst = new File JavaDoc( target );
50
51         if( !(f_dst.exists()))
52             return true; // non existing target is ok
53

54         if ( f_dst.isDirectory() ) {
55             target = target + File.separator + ".depend";
56             f_dst = new File JavaDoc (target);
57             if( !(f_dst.exists()))
58                 return true; // again, non existing (modified) target is ok
59
}
60
61         if( !(f_dst.isFile()) )
62             throw new BuildException("Target [[" + target + "]] isn't a regular entry");
63
64         return f_src.lastModified() > f_dst.lastModified();
65     }
66
67     /**
68      * Touch the target file.
69      * @param target the target
70      */

71     private void touch( String JavaDoc target ) throws BuildException {
72         File JavaDoc f_dst = new File JavaDoc( target );
73
74         // if the target is a directory, modify the target to point to a hidden file
75
if( f_dst.exists() ) {
76             if ( f_dst.isDirectory() ) {
77                 target = target + File.separator + ".depend";
78                 f_dst = new File JavaDoc (target);
79             }
80         }
81         else
82             return; // do nothing if target does not exist
83

84         if( f_dst.exists() ) {
85             f_dst.setLastModified( System.currentTimeMillis() );
86         }
87         else {
88             try {
89                 // JDK 1.2 dependency :-(
90
f_dst.createNewFile();
91             } catch (IOException JavaDoc e) {
92                 // simply ignore this exception
93
}
94             
95         }
96     }
97
98
99     /**
100      * Exectue the task. this function will be called by the ANT build system.
101      * The work will be made by the ANT internal 'Java' task. We create one,
102      * give them the needed parameters and execute them.
103      */

104     public void execute() throws BuildException {
105         if( isOlder( _source, _target ) ) {
106             super.execute();
107             touch( _target );
108         }
109         else {
110             System.out.println("Nothing to be done for this dependency: " + _source + Project.MSG_INFO);
111             //log ("Nothing to be done for this dependency: " + _source, Project.MSG_INFO);
112
}
113     }
114
115
116     /**
117      * @param s The target name.
118      */

119     public void setTarget( String JavaDoc s ) {
120         _target = s;
121     }
122
123
124     /**
125      * @param s The file name of the source file
126      */

127     public void setDependsOn( String JavaDoc s ) {
128         _source = s;
129     }
130 }
131
Popular Tags