ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From co...@apache.org
Subject cvs commit: ant/src/main/org/apache/tools/ant/filters ReplaceTokens.java
Date Wed, 19 Feb 2003 14:11:43 GMT
conor       2003/02/19 06:11:43

  Modified:    src/main/org/apache/tools/ant/filters ReplaceTokens.java
  Log:
  replaceTokens now finds tokens which overlap with other token candidates
  
  PR: 12134
  
  Revision  Changes    Path
  1.9       +48 -14    ant/src/main/org/apache/tools/ant/filters/ReplaceTokens.java
  
  Index: ReplaceTokens.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/filters/ReplaceTokens.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -w -u -r1.8 -r1.9
  --- ReplaceTokens.java	10 Feb 2003 14:13:32 -0000	1.8
  +++ ReplaceTokens.java	19 Feb 2003 14:11:42 -0000	1.9
  @@ -86,9 +86,18 @@
       /** Default "end token" character. */
       private static final char DEFAULT_END_TOKEN = '@';
   
  -    /** Data that must be read from, if not null. */
  +    /** Data to be used before reading from stream again */
       private String queuedData = null;
   
  +    /** replacement test from a token */
  +    private String replaceData = null;
  +
  +    /** Index into replacement data */
  +    private int replaceIndex = -1;
  +    
  +    /** Index into queue data */
  +    private int queueIndex = -1;
  +    
       /** Hashtable to hold the replacee-replacer pairs (String to String). */
       private Hashtable hash = new Hashtable();
   
  @@ -117,6 +126,18 @@
           super(in);
       }
   
  +    private int getNextChar() throws IOException {
  +        if (queueIndex != -1) {
  +            final int ch = queuedData.charAt(queueIndex++);
  +            if (queueIndex >= queuedData.length()) {
  +                queueIndex = -1;
  +            }
  +            return ch;
  +        }
  +        
  +        return in.read();
  +    }
  +    
       /**
        * Returns the next character in the filtered stream, replacing tokens
        * from the original stream.
  @@ -133,21 +154,20 @@
               setInitialized(true);
           }
   
  -        if (queuedData != null && queuedData.length() > 0) {
  -            final int ch = queuedData.charAt(0);
  -            if (queuedData.length() > 1) {
  -                queuedData = queuedData.substring(1);
  -            } else {
  -                queuedData = null;
  +        if (replaceIndex != -1) {
  +            final int ch = replaceData.charAt(replaceIndex++);
  +            if (replaceIndex >= replaceData.length()) {
  +                replaceIndex = -1;
               }
               return ch;
           }
   
  -        int ch = in.read();
  +        int ch = getNextChar();
  +
           if (ch == beginToken) {
               final StringBuffer key = new StringBuffer("");
               do  {
  -                ch = in.read();
  +                ch = getNextChar();
                   if (ch != -1) {
                       key.append((char) ch);
                   } else {
  @@ -156,17 +176,31 @@
               } while (ch != endToken);
   
               if (ch == -1) {
  -                queuedData = beginToken + key.toString();
  -                return read();
  +                if (queuedData == null || queueIndex == -1) {
  +                    queuedData = key.toString();
  +                } else {
  +                    queuedData 
  +                        = key.toString() + queuedData.substring(queueIndex);
  +                }
  +                queueIndex = 0;
  +                return beginToken;
               } else {
                   key.setLength(key.length() - 1);
  +                
                   final String replaceWith = (String) hash.get(key.toString());
                   if (replaceWith != null) {
  -                    queuedData = replaceWith;
  +                    replaceData = replaceWith;
  +                    replaceIndex = 0;
                       return read();
                   } else {
  -                    queuedData = beginToken + key.toString() + endToken;
  -                    return read();
  +                    String newData = key.toString() + endToken;
  +                    if (queuedData == null || queueIndex == -1) {
  +                        queuedData = newData;
  +                    } else {
  +                        queuedData = newData + queuedData.substring(queueIndex);
  +                    }
  +                    queueIndex = 0;
  +                    return beginToken;
                   }
               }
           }
  
  
  

Mime
View raw message