Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create a subclass of FilterReader

Status
Not open for further replies.

BugBlatter

Programmer
Jul 29, 2003
6
AU
I want to use Ant to replace a token @PACKAGE@ with a String stored in a property called ${package}. But the value of the package property would be in the format "com/company/package" and needs to be converted to the format com.company.package. I need to create a subclass of FilterReader in order to do this but I don't know how to override the read() method. It only returns a character at a time so how do I read in enough characters to realise I've found my token to replace and then return the replacement string?

Cheers
 
>>> in the format "com/company/package" and needs to be converted to the format com.company.package

Why can you not use the java.util.regex. package and use the classes Pattern and Matcher - which can replace reguslar expressions ?
 
try coverting your String to a character array, and looping, replacing "/" with "." as you go ...
 
I don't know whether this is what you are looking for, it seems too simple. StringTokenizer exists since jdk1.0. There is also StreamTokenizer and BreakIterator, CharacterIterator, StringCharacterIterator ...
(this program is by no means intended to be nice, fast or good style, .... just demonstrating StringTokenizer)
The output of this pgm :
Before : com/company/package
After : com.company.package
Code:
import java.util.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: ECC</p>
 * @author Ward
 * @version 1.0
 */

public class StringExample2 {

  public StringExample2() {
  }

  public void doIt() {
    String theString = &quot;com/company/package&quot;;
    String convertedString = convertString(theString, &quot;/&quot;, &quot;.&quot;);
    System.out.println(&quot;Before : &quot; + theString);
    System.out.println(&quot;After : &quot; + convertedString);
  }

  public String convertString(String fromString, String from, String to) {
    String toString = &quot;&quot;;
    StringTokenizer tokenizer = new StringTokenizer(fromString, from);
    if (tokenizer.hasMoreTokens())
      toString = tokenizer.nextToken();
    while (tokenizer.hasMoreTokens()) {
        String word = tokenizer.nextToken();
        toString = toString + to + word;
    }
    return toString;
  }

  public static void main(String args[]) {
    StringExample2 stringExample=new StringExample2();
    stringExample.doIt();
  }

}
 
Thanks for your input guys. Converting the String is not a problem. The following code does the trick nicely:

String newString = oldString.replace('/', '.');

The problem is that I have to create a subclass of FilterReader so that Ant can use it to read a text file and replace the token @PACKAGE@ with the string com.company.... I'm assuming that Ant will call the read method on my FilterReader and it expects to see a char returned.

After giving it some thought I guess what I have to do is read a char from the underlying stream and if its a '@' then I have to mark my place in the stream and read the next 8 chars into an array to see if its the token I'm looking for. If so, then I don't need to return to my mark, when Ant calls my read method I just return one char at a time from the char array until its empty then I carry on reading from the underlying stream.

I'll give that a try and post another reply to let you know how it went.

Thanks again for your replies so far.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top