001: // You can redistribute this software and/or modify it under the terms of
002: // the Infozone Software License version 2 published by the Infozone Group
003: // (http://www.infozone-group.org).
004: //
005: // Copyright (C) @year@ by The Infozone Group. All rights reserved.
006: //
007: // $Id: DependendExec.java,v 1.1 2002/05/10 08:59:11 per_nyfelt Exp $
008:
009: package org.infozone.tools.ant;
010:
011: import org.apache.tools.ant.BuildException;
012: import org.apache.tools.ant.taskdefs.Exec;
013: import org.apache.tools.ant.Project;
014:
015: import java.io.File;
016: import java.io.IOException;
017:
018: /**
019: * ANT cannot handle the dependency of source files from a definition file.
020: * This ability is needed if one will generate source files with castor.
021: * This module solves this problem.
022: *
023: * @version $Revision: 1.1 $
024: * @author andreas.kasparz@interface-projects.de
025: *
026: */
027: public class DependendExec extends Exec {
028:
029: //
030: // Data
031: //
032:
033: /** the filename of the target */
034: private String _target = null;
035: /** the filename the target depends on */
036: private String _source = null;
037:
038: /**
039: * Check if the target is older than the source file or not exists.
040: * @param dst the target
041: * @param src the source file
042: */
043: private boolean isOlder(String source, String target)
044: throws BuildException {
045: File f_src = new File(source);
046:
047: if (!(f_src.exists() && (f_src.isFile() || f_src.isDirectory())))
048: throw new BuildException("Source [[" + source
049: + "]] doesn't exists or isn't a regular entry");
050:
051: File f_dst = new File(target);
052:
053: if (!(f_dst.exists()))
054: return true; // non existing target is ok
055:
056: if (f_dst.isDirectory()) {
057: target = target + File.separator + ".depend";
058: f_dst = new File(target);
059: if (!(f_dst.exists()))
060: return true; // again, non existing (modified) target is ok
061: }
062:
063: if (!(f_dst.isFile()))
064: throw new BuildException("Target [[" + target
065: + "]] isn't a regular entry");
066:
067: return f_src.lastModified() > f_dst.lastModified();
068: }
069:
070: /**
071: * Touch the target file.
072: * @param dst the target
073: */
074: private void touch(String target) throws BuildException {
075: File f_dst = new File(target);
076:
077: // if the target is a directory, modify the target to point to a hidden file
078: if (f_dst.exists()) {
079: if (f_dst.isDirectory()) {
080: target = target + File.separator + ".depend";
081: f_dst = new File(target);
082: }
083: } else
084: return; // do nothing if target does not exist
085:
086: if (f_dst.exists()) {
087: f_dst.setLastModified(System.currentTimeMillis());
088: } else {
089: try {
090: // JDK 1.2 dependency :-(
091: f_dst.createNewFile();
092: } catch (IOException e) {
093: // simply ignore this exception
094: }
095:
096: }
097: }
098:
099: /**
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: } else {
109: System.out
110: .println("Nothing to be done for this dependency: "
111: + _source + Project.MSG_INFO);
112: //log ("Nothing to be done for this dependency: " + _source, Project.MSG_INFO);
113: }
114: }
115:
116: /**
117: * @param name The target name.
118: */
119: public void setTarget(String s) {
120: _target = s;
121: }
122:
123: /**
124: * @param name The file name of the source file
125: */
126: public void setDependsOn(String s) {
127: _source = s;
128: }
129: }
|