java/ant/src/org/apidesign/infra/ant/GrepCopy.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 06 Jul 2008 00:12:56 +0200
changeset 268 fb9bf90251e3
child 271 a1c057f1f756
permissions -rw-r--r--
Tool to generate code snippets from sources
jtulach@268
     1
package org.apidesign.infra.ant;
jtulach@268
     2
jtulach@268
     3
import java.io.File;
jtulach@268
     4
import java.io.FileWriter;
jtulach@268
     5
import java.io.IOException;
jtulach@268
     6
import java.util.Map;
jtulach@268
     7
import java.util.Map.Entry;
jtulach@268
     8
import org.apache.tools.ant.BuildException;
jtulach@268
     9
import org.apache.tools.ant.Task;
jtulach@268
    10
import org.apache.tools.ant.types.FileSet;
jtulach@268
    11
import org.apache.tools.ant.types.FilterSet;
jtulach@268
    12
import org.apache.tools.ant.util.FileUtils;
jtulach@268
    13
import org.openide.util.Exceptions;
jtulach@268
    14
jtulach@268
    15
public class GrepCopy extends Task {
jtulach@268
    16
    private GrepFilter filter = new GrepFilter();
jtulach@268
    17
    private File dir;
jtulach@268
    18
    
jtulach@268
    19
    public GrepCopy() {
jtulach@268
    20
    }
jtulach@268
    21
jtulach@268
    22
    public FileSet createFileSet() {
jtulach@268
    23
        return filter.createFileSet();
jtulach@268
    24
    }
jtulach@268
    25
jtulach@268
    26
    public void setTarget(File dir) {
jtulach@268
    27
        this.dir = dir;
jtulach@268
    28
    }
jtulach@268
    29
jtulach@268
    30
    @Override
jtulach@268
    31
    public void execute() throws BuildException {
jtulach@268
    32
        filter.setProject(getProject());
jtulach@268
    33
jtulach@268
    34
        FilterSet set = filter.createFilterSet();
jtulach@268
    35
jtulach@268
    36
        for (Object object : set.getFilterHash().entrySet()) {
jtulach@268
    37
jtulach@268
    38
            FileWriter w = null;
jtulach@268
    39
            try {
jtulach@268
    40
                Entry en = (Entry) object;
jtulach@268
    41
                File to = new File(dir, (String) en.getKey());
jtulach@268
    42
                to.getParentFile().mkdirs();
jtulach@268
    43
                w = new FileWriter(to);
jtulach@268
    44
                w.write((String) en.getValue());
jtulach@268
    45
                w.close();
jtulach@268
    46
            } catch (IOException ex) {
jtulach@268
    47
                throw new BuildException(ex);
jtulach@268
    48
            }
jtulach@268
    49
        }
jtulach@268
    50
    }
jtulach@268
    51
}