You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Patrick Martin <an...@gmail.com> on 2010/05/01 15:17:51 UTC

loop replace mapper

Hello,

I wrote a small mapper which loops on a string list and replaces a
token in the input file name.
it can be used (since ant 1.8.1) like this:
		<copy todir="test/data/destdir" enablemultiplemappings="true">
			<fileset dir="test/data/srcdir" includes="**" />
			<loopreplacemapper token="@dirname@" list="dir1,dir2" />
		</copy>
		<zip destfile="test/data/destzip.zip">
			<mappedresources enablemultiplemappings="true">
				<fileset dir="test/data/srcdir" includes="**" />
				<loopreplacemapper token="@dirname@" list="dir1,dir2" />
			</mappedresources>
               </zip>

Would this fit in the next standard ant version ?

Here is the source code:

public class LoopReplaceMapper implements FileNameMapper {
	private String token;
	private ArrayList<String> list;

	public void setToken(String token) {
		this.token = token;
	}

	public String getToken() {
		return token;
	}

	public void setList(String _list) {
		StringTokenizer st = new StringTokenizer(_list, "	, ");
		this.list = new ArrayList<String>(st.countTokens());
		while (st.hasMoreTokens()) {
			list.add(st.nextToken());
		}
	}

	public String getList() {
		return list.toString();
	}

	public String[] mapFileName(String sourceFileName) {
		String[] mappedFileNames = null;
		if (sourceFileName.contains(getToken())) {
			mappedFileNames = new String[list.size()];
			int index = 0;
			for (String replacement : list) {
				mappedFileNames[index++] = sourceFileName.replace(getToken(), replacement);
			}
		} else {
			mappedFileNames = new String[1];
			mappedFileNames[0] = sourceFileName;
		}
		return mappedFileNames;
	}

	public void setFrom(String from) {
	}

	public void setTo(String to) {
	}
}

 Rgds,

Patrick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: Fwd: loop replace mapper

Posted by Antoine Levy-Lambert <an...@gmx.de>.
Hello Patrick,

it is likely that it is too late for ant 1.8.1, unless the build of Friday does not pass the vote.

The other questions are ? What is the use case for this mapper ? Is it generic enough to be added to ant ?

If it interesting we would need also patches of documentation and unit tests.

Regards,

Antoine
-------- Original-Nachricht --------
> Datum: Sat, 1 May 2010 15:31:20 +0200
> Von: Patrick Martin <an...@gmail.com>
> An: Ant Dev <de...@ant.apache.org>
> Betreff: Fwd: loop replace mapper

> I guess I should have posted this mail on ant dev mailing list.
> Is this request too late for ant 1.8.1 ?
> 
> 
> ---------- Forwarded message ----------
> From: Patrick Martin <an...@gmail.com>
> Date: Sat, May 1, 2010 at 3:17 PM
> Subject: loop replace mapper
> To: Ant User <us...@ant.apache.org>
> 
> 
> Hello,
> 
> I wrote a small mapper which loops on a string list and replaces a
> token in the input file name.
> it can be used (since ant 1.8.1) like this:
>                <copy todir="test/data/destdir"
> enablemultiplemappings="true">
>                        <fileset dir="test/data/srcdir"
> includes="**" />
>                        <loopreplacemapper token="@dirname@"
> list="dir1,dir2" />
>                </copy>
>                <zip destfile="test/data/destzip.zip">
>                        <mappedresources
> enablemultiplemappings="true">
>                                <fileset
> dir="test/data/srcdir" includes="**" />
>                                <loopreplacemapper
> token="@dirname@"
> list="dir1,dir2" />
>                        </mappedresources>
>               </zip>
> 
> Would this fit in the next standard ant version ?
> 
> Here is the source code:
> 
> public class LoopReplaceMapper implements FileNameMapper {
>        private String token;
>        private ArrayList<String> list;
> 
>        public void setToken(String token) {
>                this.token = token;
>        }
> 
>        public String getToken() {
>                return token;
>        }
> 
>        public void setList(String _list) {
>                StringTokenizer st = new StringTokenizer(_list, "
>       , ");
>                this.list = new
> ArrayList<String>(st.countTokens());
>                while (st.hasMoreTokens()) {
>                        list.add(st.nextToken());
>                }
>        }
> 
>        public String getList() {
>                return list.toString();
>        }
> 
>        public String[] mapFileName(String sourceFileName) {
>                String[] mappedFileNames = null;
>                if (sourceFileName.contains(getToken())) {
>                        mappedFileNames = new
> String[list.size()];
>                        int index = 0;
>                        for (String replacement : list) {
>                                mappedFileNames[index++] =
> sourceFileName.replace(getToken(), replacement);
>                        }
>                } else {
>                        mappedFileNames = new String[1];
>                        mappedFileNames[0] = sourceFileName;
>                }
>                return mappedFileNames;
>        }
> 
>        public void setFrom(String from) {
>        }
> 
>        public void setTo(String to) {
>        }
> }
> 
>  Rgds,
> 
> Patrick
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Fwd: loop replace mapper

Posted by Patrick Martin <an...@gmail.com>.
I guess I should have posted this mail on ant dev mailing list.
Is this request too late for ant 1.8.1 ?


---------- Forwarded message ----------
From: Patrick Martin <an...@gmail.com>
Date: Sat, May 1, 2010 at 3:17 PM
Subject: loop replace mapper
To: Ant User <us...@ant.apache.org>


Hello,

I wrote a small mapper which loops on a string list and replaces a
token in the input file name.
it can be used (since ant 1.8.1) like this:
               <copy todir="test/data/destdir" enablemultiplemappings="true">
                       <fileset dir="test/data/srcdir" includes="**" />
                       <loopreplacemapper token="@dirname@" list="dir1,dir2" />
               </copy>
               <zip destfile="test/data/destzip.zip">
                       <mappedresources enablemultiplemappings="true">
                               <fileset dir="test/data/srcdir" includes="**" />
                               <loopreplacemapper token="@dirname@"
list="dir1,dir2" />
                       </mappedresources>
              </zip>

Would this fit in the next standard ant version ?

Here is the source code:

public class LoopReplaceMapper implements FileNameMapper {
       private String token;
       private ArrayList<String> list;

       public void setToken(String token) {
               this.token = token;
       }

       public String getToken() {
               return token;
       }

       public void setList(String _list) {
               StringTokenizer st = new StringTokenizer(_list, "       , ");
               this.list = new ArrayList<String>(st.countTokens());
               while (st.hasMoreTokens()) {
                       list.add(st.nextToken());
               }
       }

       public String getList() {
               return list.toString();
       }

       public String[] mapFileName(String sourceFileName) {
               String[] mappedFileNames = null;
               if (sourceFileName.contains(getToken())) {
                       mappedFileNames = new String[list.size()];
                       int index = 0;
                       for (String replacement : list) {
                               mappedFileNames[index++] =
sourceFileName.replace(getToken(), replacement);
                       }
               } else {
                       mappedFileNames = new String[1];
                       mappedFileNames[0] = sourceFileName;
               }
               return mappedFileNames;
       }

       public void setFrom(String from) {
       }

       public void setTo(String to) {
       }
}

 Rgds,

Patrick

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org