0

I'm using Ant to create a zip file. I want to also replace a token in the zipped files with the line number where the token is. I have tried to use a FilterChain, but I don't know how to engage with the zipfileset. Any suggestions?


I'm adding an example here. Let's say I have a file test.js like this:

function start(){
   console.debug("Start");
}

function end(){
   console.debug("End");
}

function processLine(line){
   console.debug("processLine"/*--location--*/);
}

I want to replace /*--location-- */ with ,"test.js:10". I've managed to create a FilterReader that writes the line number

@Override
public int read() throws IOException {
    if (!getInitialized()) {
        initialize();
        setInitialized(true);
    }

    int ch = -1;

    if (line != null) {
        ch = line.charAt(0);
        if (line.length() == 1) {
            line = null;
        } else {
            line = line.substring(1);
        }
    } else {
        lineNumber++;
        line = readLine();
        System.out.println("line " + line);
        if (line != null) {
            line = line.replace("/*--location--*/", ",\"pagina:"+lineNumber+"\"");
            return read();
        }
    }
    return ch;
}

And I can invoke it using a copy with a filterchain

<copy file="${src}/ant-util/test/resources/box.js" tofile="${installation}/box.js">
  <filterchain>
    <filterreader classname="com.xxx.util.ant.AddLineNumberReaderFilter"/>
  </filterchain>
</copy>         

But I don't know how to get the file name. And then I would like to use this filter when zipping files, instead of doing a copy.

Jose L Martinez-Avial
  • 2,191
  • 4
  • 28
  • 42
  • Can you give us a clearer example? You probably need to use `copy` to copy the file with the line number tokens in order to use FilterChain with that. Then archive the results with ``. – David W. Apr 18 '14 at 21:09
  • I think the filename is problematic - see: http://stackoverflow.com/questions/4376795/getting-file-name-inside-ant-copy-task-filter – martin clayton Apr 28 '14 at 21:41

2 Answers2

1
<copy file="${src}/ant-util/test/resources/box.js" tofile="${installation}/box.js">
<filterchain>
 <filterreader classname="com.xxx.util.ant.AddLineNumberReaderFilter">
  <param name="name" value="value"/>
 </filterreader>
</filterchain>
</copy>  

And in your Ant Reader class, create setter/getter methods

public void setName(String filename)
public String getName()
herau
  • 1,466
  • 2
  • 18
  • 36
0

try calling this method in the constructor of the FilterReader

private String determineFilepath() {
    String result = null;
    Object obj = lock;
    while(obj != null && !(obj instanceof FileInputStream)) {
        obj = findField(obj, "lock");
    }
    if(obj instanceof FileInputStream) {
        result = (String)findField(obj, "path");
    }
    return result;
}

private Object findField(Object obj, String fieldName) {
    Object result = null;
    if(obj != null) {
        Class<? extends Object> clazz = obj.getClass();
        Field field = null;
        while(clazz != null) {
            try {
                field = clazz.getDeclaredField(fieldName);
            }
            catch(NoSuchFieldException e) {}

            if(field != null) {
                break;
            }
            else {
                clazz = clazz.getSuperclass();
            }
        }
        if(field != null) {
            field.setAccessible(true);
            try {
                result = field.get(obj);
            }
            catch(IllegalArgumentException | IllegalAccessException e) {}
        }
    }

    return result;
}
Vimil Saju
  • 345
  • 3
  • 7