summaryrefslogtreecommitdiffstats
path: root/etc/src/com/mbien/ant/HeaderFileDownloader.java
blob: 64e193d4a68b96557394b0e50032a245d429bb0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.jogamp.ant;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 * Keeps downloaded headers up2date.
 * @author Michael Bien
 */
public class HeaderFileDownloader extends Task {

    private String filePath;
    private String urlStr;

    /*example: $Revision: 9283 $ on $Date: 2009-10-14 10:18:57 -0700 (Wed, 14 Oct 2009) $ */
    private final Pattern revisionPattern = Pattern.compile("\\$Revision:\\s(\\d+)");

    @Override
    public void execute() throws BuildException {

        if(filePath == null)
            throw new IllegalArgumentException("file must be set");
        if(urlStr == null)
            throw new IllegalArgumentException("update url must be set");

        try {
            URL url = new URL(urlStr);
            int remoteRevision = readRevision(url.openStream());
            int localRevision = readRevision(new FileInputStream(new File(filePath)));

            if(remoteRevision != localRevision) {

                System.out.println("updating header: "+filePath);
                System.out.println("from revision "+localRevision +" to revision "+remoteRevision);

                BufferedInputStream in = new BufferedInputStream(url.openStream());
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filePath));

                int val;
                while((val=in.read()) != -1) {
                    out.write(val);
                }
                in.close();

                out.flush();
                out.close();
            }else{
                System.out.println("header "+filePath+" is up to date");
            }

        } catch (IOException ex) {
            throw new BuildException(ex);
        }finally{
        }

    }

    private int readRevision(InputStream is) throws IOException  {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                Matcher matcher = revisionPattern.matcher(line);
                if(matcher.find()) {
                    System.out.println(line);
                    return Integer.parseInt(matcher.group(1));
                }
            }
        } finally {
            reader.close();
        }

        return 0;
    }


    public void setURL(String url) {
        this.urlStr = url;
    }

    public void setHeader(String filePath) {
        this.filePath = filePath;
    }


}