alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Ant example source code file (tokenfilter.xml)

This example Ant source code file (tokenfilter.xml) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Ant tags/keywords

capitalize, cat, found, hello, hello, suite, sun, this, this, whitespace, whitespace, world, world

The tokenfilter.xml source code

<?xml version="1.0"?>
<project default="cleanup" basedir=".">

  <target name="init">
    <mkdir dir="result" />
  </target>

  <target name="cleanup">
    <delete dir="result"/>
  </target>

  <target name="tokenfilter">
    <copy file="input/linecontains.test" tofile="result/file1">
      <filterchain>
        <tokenfilter/>
      </filterchain>
    </copy>
  </target>

  <target name="trimignore">
    <concat destfile="result/input">
      Hello
      
      World
    </concat>
    <copy file="result/input" tofile="result/output" overwrite="yes">
      <filterchain>
        <tokenfilter delimoutput="-">
          <trim/>
          <ignoreblank/>
        </tokenfilter>
      </filterchain>
    </copy>
    <concat>
      <filelist dir="." files="result/output"/>
    </concat>
  </target>

  <target name="trimfile">
    <concat destfile="result/trimfile">
      This is the contents of the trimmed file.
      This is the second line.
      <filterchain>
        <trim byline="no"/>
      </filterchain>
    </concat>
  </target>

  <target name="trimfilebyline">
    <concat destfile="result/trimfilebyline">
      This is the contents of the trimmed file.
      This is the second line.
      <filterchain>
        <trim/>
        <tokenfilter delimoutput="\n"/>
      </filterchain>
    </concat>
  </target>

  <target name="filterreplacestring">
    <concat destfile="result/filterreplacestring">
      This is foo bar
      <filterchain>
        <replacestring from="foo" to="the"/>
        <replacestring from="bar" to="moon"/>
      </filterchain>
    </concat>
  </target>

  <target name="filterreplacestrings">
    <concat>
      foo foo foo
      <filterchain>
        <replacestring from="foo" to="bar"/>
      </filterchain>
    </concat>
  </target>

  <target name="stringtokenizer">
    <concat destfile="result/input">
      This is a number
      of words
    </concat>
    <copy file="result/input" tofile="result/output" overwrite="yes">
      <filterchain>
        <tokenfilter delimoutput="#">
          <stringtokenizer/>
        </tokenfilter>
      </filterchain>
    </copy>
    <concat>
      <filelist dir="." files="result/output"/>
    </concat>
  </target>

  <target name="unixlineoutput">
    <concat destfile="result/unixlineoutput">
      This is a number
      of words
      <filterchain>
        <tokenfilter delimoutput="\n">
          <stringtokenizer/>
        </tokenfilter>
      </filterchain>
    </concat>
  </target>

  <target name="doslineoutput">
    <concat destfile="result/doslineoutput">
      This is a number
      of words
      <filterchain>
        <tokenfilter delimoutput="\r\n">
          <stringtokenizer/>
        </tokenfilter>
      </filterchain>
    </concat>
  </target>

  <target name="filetokenizer">
    <concat destfile="result/input">
      This is a number
      of words
    </concat>
    <copy file="result/input" tofile="result/filetokenizer">
      <filterchain>
        <tokenfilter>
          <filetokenizer/>
          <trim/>
        </tokenfilter>
      </filterchain>
    </copy>
  </target>

  <target name="replacestring">
    <concat destfile="result/replacestring">
      this is the sun
      <filterchain>
        <tokenfilter>
            <replacestring from="sun" to="moon"/>
        </tokenfilter>
      </filterchain>
    </concat>
  </target>

  <target name="replacestrings">
    <concat>
      foo foo foo
      <filterchain>
        <tokenfilter>
          <replacestring from="foo" to="bar"/>
        </tokenfilter>
      </filterchain>
    </concat>
  </target>

  <target name="containsstring">
    <concat destfile="result/input">
      this is a line contains foo
      this line does not
    </concat>
    <copy file="result/input" tofile="result/containsstring">
      <filterchain>
        <tokenfilter>
          <containsstring contains="foo"/>
        </tokenfilter>
      </filterchain>
    </copy>
  </target>

  <!-- need to check for existance of regex -->
  <target name="replaceregex">
    <concat destfile="result/input">
      hello Hello HELLO hello
      cat Cat cat
      Sun Sun Sun
      WhiteSpace 	tab
      This is a line with digits - 1234 -- there
    </concat>
    <copy file="result/input" tofile="result/replaceregex">
      <filterchain>
        <tokenfilter>
          <replaceregex pattern="hello" replace="world" flags="gi"/>
          <replaceregex pattern="cat" replace="dog" flags="g"/>
          <replaceregex pattern="sun" replace="moon" flags="i"/>
          <replaceregex pattern="WhiteSpace[ \t]+tab"
                        replace="found WhiteSpace"/>
          <replaceregex pattern="This is a line with dig.* ([0-9]+).*"
                        replace="Found digits [\1]"/>
        </tokenfilter>
      </filterchain>
    </copy>
  </target>

  <target name="filterreplaceregex">
    <concat destfile="result/filterreplaceregex">
      hello Hello HELLO hello
      <filterchain>
        <replaceregex pattern="hello" replace="world" flags="gi"/>
      </filterchain>
    </concat>
  </target>

  <target name="dollermatch">
    <concat>
      @hello@
      <filterchain>
        <replaceregex pattern="@([^@]*)@" replace="${\1}"/>
      </filterchain>
    </concat>
  </target>

  <!-- need to check for existance of regex -->
  <target name="containsregex">
    <concat destfile="result/input">
      hello world
      this is the moon
      World here
    </concat>
    <copy file="result/input" tofile="result/containsregex">
      <filterchain>
        <tokenfilter>
          <containsregex pattern="(hello|world)" flags="i"/>
        </tokenfilter>
      </filterchain>
    </copy>
  </target>

  <target name="filtercontainsregex">
    <concat destfile="result/filtercontainsregex">
      hello world
      this is the moon
      World here
      <filterchain>
        <tokenfilter>
          <containsregex pattern="(hello|world)" flags="i"/>
        </tokenfilter>
      </filterchain>
    </concat>
  </target>


  <!-- need to check for existance of regex -->
  <target name="containsregex2">
    <concat destfile="result/input">
      SUITE(TestSuite, bits);
      here
    </concat>
    <copy file="result/input" tofile="result/containsregex2">
      <filterchain>
        <tokenfilter>
          <containsregex
            pattern="^ *SUITE\(.*,\s*(.*)\s*\).*"
            replace="void register_\1();"/>
        </tokenfilter>
      </filterchain>
    </copy>
  </target>

  <target name="deletecharacters">
    <concat destfile="result/deletechars">
      This is some ### s
      some ****
      <filterchain>
        <tokenfilter>
          <deletecharacters chars="#"/>
        </tokenfilter>
        <deletecharacters chars="*"/>
      </filterchain>
    </concat>
  </target>

  <target name="scriptfilter">
    <concat destfile="result/input">
      hello world
    </concat>
    <copy file="result/input" tofile="result/scriptfilter">
      <filterchain>
        <tokenfilter>
          <scriptfilter language="javascript">
            self.setToken(self.getToken().toUpperCase());
          </scriptfilter>
       </tokenfilter>
      </filterchain>
    </copy>
  </target>

  <target name="scriptfilter2">
    <concat destfile="result/input">
      hello moon
    </concat>
    <copy file="result/input" tofile="result/scriptfilter2">
      <filterchain>
        <scriptfilter language="javascript">
          self.setToken(self.getToken().toUpperCase());
        </scriptfilter>
      </filterchain>
    </copy>
  </target>

  <target name="customtokenfilter">
    <path id="test-classes">
      <pathelement location="../../../../build/testcases" />
      <pathelement path="${java.class.path}" />
    </path>


    <typedef 
      name="capitalize" 
      classname="org.apache.tools.ant.filters.TokenFilterTest$Capitalize">
      <classpath refid="test-classes"/>
    </typedef>
    
    <concat destfile="result/input">
      hello world
    </concat>

    <copy file="result/input" tofile="result/custom">
      <filterchain>
        <tokenfilter>
          <stringtokenizer/>
          <capitalize/>
       </tokenfilter>
      </filterchain>
    </copy>
  </target>

  <target name="hasscript">
    <script language="javascript">
      i = 1;
    </script>
  </target>
  
  <target name="hasregex">
    <concat destfile="result/replaceregexp">
      hello world
    </concat>
    <replaceregexp file="result/replaceregexp"
                  match="hello( )world"
                  replace="bye\1world"/>
  </target>

</project>

Other Ant examples (source code examples)

Here is a short list of links related to this Ant tokenfilter.xml source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.