Browse Source

Use FileUtils instead of yet another touch method in <get>.

cosmetics.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272376 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
7820c64af8
6 changed files with 89 additions and 86 deletions
  1. +2
    -0
      src/main/org/apache/tools/ant/taskdefs/Exit.java
  2. +9
    -6
      src/main/org/apache/tools/ant/taskdefs/Filter.java
  3. +20
    -14
      src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
  4. +12
    -7
      src/main/org/apache/tools/ant/taskdefs/GenerateKey.java
  5. +46
    -58
      src/main/org/apache/tools/ant/taskdefs/Get.java
  6. +0
    -1
      src/main/org/apache/tools/ant/taskdefs/Jar.java

+ 2
- 0
src/main/org/apache/tools/ant/taskdefs/Exit.java View File

@@ -64,6 +64,8 @@ import org.apache.tools.ant.BuildException;
*
* @author <a href="mailto:nico@seessle.de">Nico Seessle</a>
*
* @since Ant 1.2
*
* @ant.task name="fail" category="control"
*/
public class Exit extends Task {


+ 9
- 6
src/main/org/apache/tools/ant/taskdefs/Filter.java View File

@@ -54,11 +54,8 @@

package org.apache.tools.ant.taskdefs;



import java.io.File;


import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
@@ -72,6 +69,8 @@ import org.apache.tools.ant.Project;
* @author Gero Vermaas <a href="mailto:gero@xs4all.nl">gero@xs4all.nl</a>
* @author <A href="gholam@xtra.co.nz">Michael McCallum</A>
*
* @since Ant 1.1
*
* @ant.task category="filesystem"
*/
public class Filter extends Task {
@@ -93,11 +92,15 @@ public class Filter extends Task {
}

public void execute() throws BuildException {
boolean isFiltersFromFile = filtersFile != null && token == null && value == null;
boolean isSingleFilter = filtersFile == null && token != null && value != null;
boolean isFiltersFromFile =
filtersFile != null && token == null && value == null;
boolean isSingleFilter =
filtersFile == null && token != null && value != null;
if (!isFiltersFromFile && !isSingleFilter) {
throw new BuildException("both token and value parameters, or only a filtersFile parameter is required", location);
throw new BuildException("both token and value parameters, or "
+ "only a filtersFile parameter is "
+ "required", location);
}
if (isSingleFilter) {


+ 20
- 14
src/main/org/apache/tools/ant/taskdefs/FixCRLF.java View File

@@ -117,6 +117,7 @@ import java.util.NoSuchElementException;
* @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
* @author <a href="mailto:pbwest@powerup.com.au">Peter B. West</a>
* @version $Revision$ $Name$
* @since Ant 1.1
*
* @ant.task category="filesystem"
*/
@@ -309,7 +310,7 @@ public class FixCRLF extends MatchingTask {
}

/**
* Specify how DOS EOF (control-z) charaters are to be handled
* Specify how DOS EOF (control-z) characters are to be handled
*
* @param option valid values:
* <ul>
@@ -405,7 +406,8 @@ public class FixCRLF extends MatchingTask {
try {
tmpFile = fileUtils.createTempFile("fixcrlf", "", destD);
Writer writer = (encoding == null) ? new FileWriter(tmpFile)
: new OutputStreamWriter(new FileOutputStream(tmpFile), encoding);
: new OutputStreamWriter(new FileOutputStream(tmpFile),
encoding);
outWriter = new BufferedWriter(writer);
} catch (IOException e) {
throw new BuildException(e);
@@ -469,11 +471,13 @@ public class FixCRLF extends MatchingTask {

case IN_CHAR_CONST:
case IN_STR_CONST:
// Got here from LOOKING by finding an opening "\'"
// next points to that quote character.
// Find the end of the constant. Watch out for
// backslashes. Literal tabs are left unchanged, and
// the column is adjusted accordingly.
// Got here from LOOKING by finding an
// opening "\'" next points to that quote
// character.
// Find the end of the constant. Watch
// out for backslashes. Literal tabs are
// left unchanged, and the column is
// adjusted accordingly.

int begin = line.getNext();
char terminator = (lines.getState() == IN_STR_CONST
@@ -482,10 +486,10 @@ public class FixCRLF extends MatchingTask {
endOfCharConst(line, terminator);
while (line.getNext() < line.getLookahead()) {
if (line.getNextCharInc() == '\t') {
line.setColumn(
line.getColumn() +
line.setColumn(line.getColumn() +
tablength -
line.getColumn() % tablength);
(line.getColumn()
% tablength));
}
else {
line.incColumn();
@@ -494,7 +498,8 @@ public class FixCRLF extends MatchingTask {

// Now output the substring
try {
outWriter.write(line.substring(begin, line.getNext()));
outWriter.write(line.substring(begin,
line.getNext()));
} catch (IOException e) {
throw new BuildException(e);
}
@@ -541,16 +546,17 @@ public class FixCRLF extends MatchingTask {
}


File destFile = new File(destD, file);

try {
lines.close();
lines = null;
}
catch (IOException e) {
throw new BuildException("Unable to close source file " + srcFile);
throw new BuildException("Unable to close source file "
+ srcFile);
}

File destFile = new File(destD, file);

if (destFile.exists()) {
// Compare the destination with the temp file
log("destFile exists", Project.MSG_DEBUG);


+ 12
- 7
src/main/org/apache/tools/ant/taskdefs/GenerateKey.java View File

@@ -65,6 +65,8 @@ import org.apache.tools.ant.types.Commandline;
*
* @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
*
* @since Ant 1.2
*
* @ant.task name="genkey" category="java"
*/
public class GenerateKey extends Task {
@@ -172,10 +174,12 @@ public class GenerateKey extends Task {

public DistinguishedName createDname() throws BuildException {
if( null != expandedDname ) {
throw new BuildException( "DName sub-element can only be specified once." );
throw new BuildException( "DName sub-element can only be "
+ "specified once." );
}
if( null != dname ) {
throw new BuildException( "It is not possible to specify dname both " +
throw new BuildException( "It is not possible to specify dname " +
" both " +
"as attribute and element." );
}
expandedDname = new DistinguishedName();
@@ -184,11 +188,12 @@ public class GenerateKey extends Task {
public void setDname(final String dname) {
if( null != expandedDname ) {
throw new BuildException( "It is not possible to specify dname both " +
throw new BuildException( "It is not possible to specify dname " +
" both " +
"as attribute and element." );
}
this.dname = dname;
}
}

public void setAlias(final String alias) {
this.alias = alias;
@@ -240,9 +245,9 @@ public class GenerateKey extends Task {

public void execute() throws BuildException {
if (Project.getJavaVersion().equals(Project.JAVA_1_1)) {
throw new BuildException( "The genkey task is only available on JDK" +
" versions 1.2 or greater" );
}
throw new BuildException( "The genkey task is only available on JDK"
+ " versions 1.2 or greater" );
}

if (null == alias) {
throw new BuildException( "alias attribute must be set" );


+ 46
- 58
src/main/org/apache/tools/ant/taskdefs/Get.java View File

@@ -62,9 +62,10 @@ import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.util.Date;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.util.FileUtils;

/**
* Get a particular file from a URL source.
@@ -75,6 +76,8 @@ import org.apache.tools.ant.Project;
* @author costin@dnt.ro
* @author gg@grtmail.com (Added Java 1.1 style HTTP basic auth)
*
* @since Ant 1.1
*
* @ant.task category="network"
*/
public class Get extends Task {
@@ -143,7 +146,8 @@ public class Get extends Task {
// check to see if sun's Base64 encoder is available.
try {
sun.misc.BASE64Encoder encoder =
(sun.misc.BASE64Encoder) Class.forName("sun.misc.BASE64Encoder").newInstance();
(sun.misc.BASE64Encoder)
Class.forName("sun.misc.BASE64Encoder").newInstance();
encoding = encoder.encode (up.getBytes());

}
@@ -151,7 +155,8 @@ public class Get extends Task {
Base64Converter encoder = new Base64Converter();
encoding = encoder.encode(up.getBytes());
}
connection.setRequestProperty ("Authorization", "Basic " + encoding);
connection.setRequestProperty ("Authorization",
"Basic " + encoding);
}

//connect to the remote site (may take some time)
@@ -159,24 +164,29 @@ public class Get extends Task {
//next test for a 304 result (HTTP only)
if(connection instanceof HttpURLConnection) {
HttpURLConnection httpConnection=(HttpURLConnection)connection;
if(httpConnection.getResponseCode()==HttpURLConnection.HTTP_NOT_MODIFIED) {
//not modified so no file download. just return instead
//and trace out something so the user doesn't think that the
//download happened when it didnt
if(httpConnection.getResponseCode()
== HttpURLConnection.HTTP_NOT_MODIFIED) {
//not modified so no file download. just return
//instead and trace out something so the user
//doesn't think that the download happened when it
//didnt
log("Not modified - so not downloaded");
return;
}
// test for 401 result (HTTP only)
if(httpConnection.getResponseCode()==HttpURLConnection.HTTP_UNAUTHORIZED) {
if(httpConnection.getResponseCode()
== HttpURLConnection.HTTP_UNAUTHORIZED) {
log("Not authorized - check " + dest + " for details");
return;
}

}

//REVISIT: at this point even non HTTP connections may support the if-modified-since
//behaviour -we just check the date of the content and skip the write if it is not
//newer. Some protocols (FTP) dont include dates, of course.
//REVISIT: at this point even non HTTP connections may
//support the if-modified-since behaviour -we just check
//the date of the content and skip the write if it is not
//newer. Some protocols (FTP) dont include dates, of
//course.

FileOutputStream fos = new FileOutputStream(dest);

@@ -204,26 +214,30 @@ public class Get extends Task {
while ((length = is.read(buffer)) >= 0) {
fos.write(buffer, 0, length);
if (verbose) {
System.out.print(".");
System.out.print(".");
}
}
if(verbose) {
System.out.println();
System.out.println();
}
fos.close();
is.close();

//if (and only if) the use file time option is set, then the
//saved file now has its timestamp set to that of the downloaded file
//if (and only if) the use file time option is set, then
//the saved file now has its timestamp set to that of the
//downloaded file
if(useTimestamp) {
long remoteTimestamp=connection.getLastModified();
if (verbose) {
Date t=new Date(remoteTimestamp);
log("last modified = "+t.toString()
+((remoteTimestamp==0)?" - using current time instead":""));
+((remoteTimestamp==0)
? " - using current time instead"
: ""));
}
if(remoteTimestamp!=0) {
touchFile(dest,remoteTimestamp);
FileUtils.newFileUtils()
.setFileLastModified(dest, remoteTimestamp);
}
}
} catch (IOException ioe) {
@@ -235,34 +249,6 @@ public class Get extends Task {
}
}

/**
* set the timestamp of a named file to a specified time.
*
* @param filename
* @param time in milliseconds since the start of the era
* @return true if it succeeded. False means that this is a
* java1.1 system and that file times can not be set
*@exception BuildException Thrown in unrecoverable error. Likely
*this comes from file access failures.
*/
protected boolean touchFile(File file, long timemillis)
throws BuildException {

if (Project.getJavaVersion() != Project.JAVA_1_1) {
Touch touch = (Touch) project.createTask("touch");
touch.setOwningTarget(target);
touch.setTaskName(getTaskName());
touch.setLocation(getLocation());
touch.setFile(file);
touch.setMillis(timemillis);
touch.touch();
return true;

} else {
return false;
}
}

/**
* Set the URL.
*
@@ -302,16 +288,18 @@ public class Get extends Task {
/**
* Use timestamps, if set to "<CODE>true</CODE>".
*
* <p>In this situation, the if-modified-since header is set so that the file is
* only fetched if it is newer than the local file (or there is no local file)
* This flag is only valid on HTTP connections, it is ignored in other cases.
* When the flag is set, the local copy of the downloaded file will also
* have its timestamp set to the remote file time.
* <br>
* Note that remote files of date 1/1/1970 (GMT) are treated as 'no timestamp', and
* web servers often serve files with a timestamp in the future by replacing their timestamp
* with that of the current time. Also, inter-computer clock differences can cause no end of
* grief.
* <p>In this situation, the if-modified-since header is set so
* that the file is only fetched if it is newer than the local
* file (or there is no local file) This flag is only valid on
* HTTP connections, it is ignored in other cases. When the flag
* is set, the local copy of the downloaded file will also have
* its timestamp set to the remote file time.</p>
*
* <p>Note that remote files of date 1/1/1970 (GMT) are treated as
* 'no timestamp', and web servers often serve files with a
* timestamp in the future by replacing their timestamp with that
* of the current time. Also, inter-computer clock differences can
* cause no end of grief.</p>
* @param v "true" to enable file time fetching
*/
public void setUseTimestamp(boolean v) {
@@ -327,7 +315,7 @@ public class Get extends Task {
* @param u username for authentication
*/
public void setUsername(String u) {
this.uname = u;
this.uname = u;
}

/**
@@ -336,7 +324,7 @@ public class Get extends Task {
* @param p password for authentication
*/
public void setPassword(String p) {
this.pword = p;
this.pword = p;
}

/*********************************************************************
@@ -346,7 +334,7 @@ public class Get extends Task {
*
* @author
* Unknown
* @author
* @author
* <a HREF="gg@grtmail.com">Gautam Guliani</a>
*********************************************************************/



+ 0
- 1
src/main/org/apache/tools/ant/taskdefs/Jar.java View File

@@ -59,7 +59,6 @@ import org.apache.tools.ant.FileScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.ZipFileSet;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.util.StringUtils;
import org.apache.tools.zip.ZipOutputStream;

import java.io.IOException;


Loading…
Cancel
Save