CVS: plexus-container-new/src/java/org/apache/plexus/util InterpolationFilterReader.java,1.2,1.3

[email protected] Mon, 19 May 2003 14:04:05 -0500
Newsgroups gmane.comp.java.plexus.devel
Message-ID <[email protected]>
Update of /cvsroot/plexus/plexus-container-new/src/java/org/apache/plexus/util
In directory eng.werken.com:/tmp/cvs-serv28301

Modified Files:
	InterpolationFilterReader.java 
Log Message:
o You can now set the boundary tokens. Specifically I wanted to add @FOO@ so that I could
  mimic and filtering in the java-based plexus builder.

Index: InterpolationFilterReader.java
===================================================================
RCS file: /cvsroot/plexus/plexus-container-new/src/java/org/apache/plexus/util/InterpolationFilterReader.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- InterpolationFilterReader.java	25 Apr 2003 18:11:56 -0000	1.2
+++ InterpolationFilterReader.java	19 May 2003 19:04:02 -0000	1.3
@@ -62,13 +62,13 @@
 
 /**
  */
-public final class InterpolationFilterReader
+public class InterpolationFilterReader
     extends FilterReader
 {
     /** Data to be used before reading from stream again */
     private String queuedData = null;
 
-    /** replacement test from a token */
+    /** replacement text from a token */
     private String replaceData = null;
 
     /** Index into replacement data */
@@ -78,35 +78,60 @@
     private int queueIndex = -1;
 
     /** Hashtable to hold the replacee-replacer pairs (String to String). */
-    private Map hash = new HashMap();
+    private Map variables = new HashMap();
 
     /** Character marking the beginning of a token. */
-    private String beginToken = "${";
+    private String beginToken;
 
     /** Character marking the end of a token. */
-    private String endToken = "}";
+    private String endToken;
 
-    /** */
+    /** Length of begin token. */
     private int beginTokenLength;
 
-    /** */
+    /** Length of end token. */
     private int endTokenLength;
 
+    /** Default begin token. */
+    private static String DEFAULT_BEGIN_TOKEN = "${";
+
+    /** Default end token. */
+    private static String DEFAULT_END_TOKEN = "}";
+
     /**
-     * Creates a new filtered reader.
      *
-     * @param in A Reader object providing the underlying stream.
-     *           Must not be <code>null</code>.
+     * @param in
+     * @param variables
+     * @param beginToken
+     * @param endToken
      */
-    public InterpolationFilterReader( Reader in, Map variables )
+    public InterpolationFilterReader( Reader in, Map variables, String beginToken, String endToken )
     {
         super( in );
 
-        hash = variables;
+        this.variables = variables;
+        this.beginToken = beginToken;
+        this.endToken = endToken;
+
         beginTokenLength = beginToken.length();
         endTokenLength = endToken.length();
     }
 
+    /**
+     *
+     * @param in
+     * @param variables
+     */
+    public InterpolationFilterReader( Reader in, Map variables )
+    {
+        this( in, variables, DEFAULT_BEGIN_TOKEN, DEFAULT_END_TOKEN );
+    }
+
+    /**
+     *
+     * @return
+     * @throws IOException
+     */
     private int getNextChar() throws IOException
     {
         if ( queueIndex != -1 )
@@ -123,6 +148,34 @@
     }
 
     /**
+     * Skips characters.  This method will block until some characters are
+     * available, an I/O error occurs, or the end of the stream is reached.
+     *
+     * @param  n  The number of characters to skip
+     *
+     * @return    the number of characters actually skipped
+     *
+     * @exception  IllegalArgumentException  If <code>n</code> is negative.
+     * @exception  IOException  If an I/O error occurs
+     */
+    public long skip( long n ) throws IOException
+    {
+        if ( n < 0L )
+        {
+            throw new IllegalArgumentException( "skip value is negative" );
+        }
+
+        for ( long i = 0; i < n; i++ )
+        {
+            if ( read() == -1 )
+            {
+                return i;
+            }
+        }
+        return n;
+    }
+
+    /**
      * Reads characters into a portion of an array.  This method will block
      * until some input is available, an I/O error occurs, or the end of the
      * stream is reached.
@@ -162,35 +215,6 @@
     }
 
     /**
-     * Skips characters.  This method will block until some characters are
-     * available, an I/O error occurs, or the end of the stream is reached.
-     *
-     * @param  n  The number of characters to skip
-     *
-     * @return    the number of characters actually skipped
-     *
-     * @exception  IllegalArgumentException  If <code>n</code> is negative.
-     * @exception  IOException  If an I/O error occurs
-     */
-    public long skip( long n ) throws IOException
-    {
-        if ( n < 0L )
-        {
-            throw new IllegalArgumentException( "skip value is negative" );
-        }
-
-        for ( long i = 0; i < n; i++ )
-        {
-            if ( read() == -1 )
-            {
-                return i;
-            }
-        }
-        return n;
-    }
-
-
-    /**
      * Returns the next character in the filtered stream, replacing tokens
      * from the original stream.
      *
@@ -216,13 +240,8 @@
 
         if ( ch == beginToken.charAt( 0 ) )
         {
-            // We are just assuming we have hit the actual begin token.
-            // We could certainly use a better search here to make sure we
-            // don't just have something that looks like the begin token but
-            // I'll make this assumption for the moment.
-            skip( beginTokenLength - 2 );
-
             StringBuffer key = new StringBuffer();
+
             do
             {
                 ch = getNextChar();
@@ -239,107 +258,37 @@
 
             if ( ch == -1 )
             {
-                if ( queuedData == null || queueIndex == -1 )
-                {
-                    queuedData = key.toString();
-                }
-                else
-                {
-                    queuedData = key.toString() + queuedData.substring( queueIndex );
-                }
+                return endToken.charAt(0);
+            }
 
-                queueIndex = 0;
+            String variableKey = key.substring( beginTokenLength - 1, key.length() - endTokenLength );
+            String value = (String) variables.get( variableKey );
 
-                return beginToken.charAt( 0 );
+            if ( value != null )
+            {
+                replaceData = value;
+                replaceIndex = 0;
+                return read();
             }
             else
             {
-                String replaceWith = (String) hash.get( key.substring( 1, key.length() - 1 ) );
-                if ( replaceWith != null )
+                String newData = key.toString();
+
+                if ( queuedData == null || queueIndex == -1 )
                 {
-                    replaceData = replaceWith;
-                    replaceIndex = 0;
-                    return read();
+                    queuedData = newData;
                 }
                 else
                 {
-                    String newData = key.toString();
-                    if ( queuedData == null || queueIndex == -1 )
-                    {
-                        queuedData = newData;
-                    }
-                    else
-                    {
-                        queuedData = newData + queuedData.substring( queueIndex );
-                    }
-                    queueIndex = 0;
-
-                    return beginToken.charAt( 0 );
+                    queuedData = newData + queuedData.substring( queueIndex );
                 }
-            }
-        }
-        return ch;
-    }
-
-    /**
-     * Sets the "begin token" character.
-     *
-     * @param beginToken the character used to denote the beginning of a token
-     */
-    public void setBeginToken( String beginToken )
-    {
-        this.beginToken = beginToken;
-    }
-
-    /**
-     * Returns the "begin token" character.
-     *
-     * @return the character used to denote the beginning of a token
-     */
-    private String getBeginToken()
-    {
-        return beginToken;
-    }
-
-    /**
-     * Sets the "end token" character.
-     *
-     * @param endToken the character used to denote the end of a token
-     */
-    public void setEndToken( String endToken )
-    {
-        this.endToken = endToken;
-    }
 
-    /**
-     * Returns the "end token" character.
-     *
-     * @return the character used to denote the end of a token
-     */
-    private String getEndToken()
-    {
-        return endToken;
-    }
+                queueIndex = 0;
 
-    /**
-     * Sets the map of tokens to replace.
-     *
-     * @param hash A map (String->String) of token keys to replacement
-     * values. Must not be <code>null</code>.
-     */
-    public void setVariables( Map hash )
-    {
-        this.hash = hash;
-    }
+                return beginToken.charAt( 0 );
+            }
+        }
 
-    /**
-     * Returns the map of tokens which will be replaced.
-     *
-     * @return a map (String->String) of token keys to replacement
-     * values
-     */
-    private Map getVariables()
-    {
-        return hash;
+        return ch;
     }
 }