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: DependendJava.java,v 1.1 2002/05/10 08:59:12 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.Java;
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: public class DependendJava extends Java {
027:
028: //
029: // Data
030: //
031:
032: /** the filename of the target */
033: private String _target = null;
034: /** the filename or directory the target depends on */
035: private String _source = null;
036:
037: /**
038: * Check if the target is older than the source file or not exists.
039: * @param dst the target
040: * @param src the source file
041: */
042: private boolean isOlder(String source, String target)
043: throws BuildException {
044: File f_src = new File(source);
045:
046: if (!(f_src.exists() && (f_src.isFile() || f_src.isDirectory())))
047: throw new BuildException("Source [[" + source
048: + "]] doesn't exists or isn't a regular entry");
049:
050: File f_dst = new File(target);
051:
052: if (!(f_dst.exists()))
053: return true; // non existing target is ok
054:
055: if (f_dst.isDirectory()) {
056: target = target + File.separator + ".depend";
057: f_dst = new File(target);
058: if (!(f_dst.exists()))
059: return true; // again, non existing (modified) target is ok
060: }
061:
062: if (!(f_dst.isFile()))
063: throw new BuildException("Target [[" + target
064: + "]] isn't a regular entry");
065:
066: return f_src.lastModified() > f_dst.lastModified();
067: }
068:
069: /**
070: * Touch the target file.
071: * @param target the target to touch.
072: */
073: private void touch(String target) throws BuildException {
074: File f_dst = new File(target);
075:
076: // if the target is a directory, modify the target to point to a hidden file
077: if (f_dst.exists()) {
078: if (f_dst.isDirectory()) {
079: target = target + File.separator + ".depend";
080: f_dst = new File(target);
081: }
082: } else
083: return; // do nothing if target does not exist
084:
085: if (f_dst.exists()) {
086: f_dst.setLastModified(System.currentTimeMillis());
087: } else {
088: try {
089: // JDK 1.2 dependency :-(
090: f_dst.createNewFile();
091: } catch (IOException e) {
092: // simply ignore this exception
093: }
094:
095: }
096: }
097:
098: /**
099: * Execute the task. this function will be called by the ANT build system.
100: * The work will be made by the ANT internal 'Java' task. We create one,
101: * give them the needed parameters and execute them.
102: */
103: public void execute() throws BuildException {
104: if (isOlder(_source, _target)) {
105: super .execute();
106: touch(_target);
107: } else {
108: System.out
109: .println("Nothing to be done for this dependency: "
110: + _source + Project.MSG_INFO);
111: //log ("Nothing to be done for this dependency: " + _source, Project.MSG_INFO);
112: }
113: }
114:
115: /**
116: * @param name The target name.
117: */
118: public void setTarget(String s) {
119: _target = s;
120: }
121:
122: /**
123: * @param name The file name of the source file
124: */
125: public void setDependsOn(String s) {
126: _source = s;
127: }
128:
129: }
|