KickJava   Java API By Example, From Geeks To Geeks.

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


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: DependendJava.java,v 1.1 2002/05/10 08:59:12 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.Java;
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 public class DependendJava extends Java {
27
28     //
29
// Data
30
//
31

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

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

53         if ( f_dst.isDirectory() ) {
54             target = target + File.separator + ".depend";
55             f_dst = new File JavaDoc (target);
56             if( !(f_dst.exists()))
57                 return true; // again, non existing (modified) target is ok
58
}
59
60         if( !(f_dst.isFile()) )
61             throw new BuildException("Target [[" + target + "]] isn't a regular entry");
62
63         return f_src.lastModified() > f_dst.lastModified();
64     }
65
66
67     /**
68      * Touch the target file.
69      * @param target the target to touch.
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      * Execute 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      * @param s The file name of the source file
125      */

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