Return-Path: Delivered-To: apmail-jakarta-ant-dev-archive@apache.org Received: (qmail 44285 invoked from network); 28 Feb 2002 18:24:00 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 28 Feb 2002 18:24:00 -0000 Received: (qmail 14194 invoked by uid 97); 28 Feb 2002 18:23:44 -0000 Delivered-To: qmlist-jakarta-archive-ant-dev@jakarta.apache.org Received: (qmail 14142 invoked by uid 97); 28 Feb 2002 18:23:43 -0000 Mailing-List: contact ant-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Ant Developers List" Reply-To: "Ant Developers List" Delivered-To: mailing list ant-dev@jakarta.apache.org Received: (qmail 14131 invoked by uid 97); 28 Feb 2002 18:23:42 -0000 Date: 28 Feb 2002 18:23:38 -0000 Message-ID: <20020228182338.92000.qmail@icarus.apache.org> From: umagesh@apache.org To: jakarta-ant-cvs@apache.org Subject: cvs commit: jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/util ChainReaderHelper.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N umagesh 02/02/28 10:23:38 Modified: proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters BaseFilterReader.java HeadFilter.java LineContains.java PrefixLines.java ReplaceTokens.java StripLineBreaks.java StripLineComments.java TabsToSpaces.java TailFilter.java proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs LoadFile.java LoadProperties.java Added: proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters BaseParamFilterReader.java proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/util ChainReaderHelper.java Removed: proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/util ChainReaderHelper.java Log: Refactored code (as suggested by Costin and Adam) - first run. 1. Moved ChainReaderHelper.java to filters/util 2. Moved setInitialized, getInitialized to base class. 3. Introduced BaseParamFilterReader that implements Parameterizable and has setParameter 4. Null check introduced for LoadFile 5. Convenience method readLine() introduced into BaseFilterReader. Revision Changes Path 1.2 +34 -1 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseFilterReader.java Index: BaseFilterReader.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseFilterReader.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- BaseFilterReader.java 28 Feb 2002 01:48:49 -0000 1.1 +++ BaseFilterReader.java 28 Feb 2002 18:23:37 -0000 1.2 @@ -66,6 +66,9 @@ public abstract class BaseFilterReader extends FilterReader { + /** Have the parameters passed been interpreted? */ + private boolean initialized = false; + /** * This constructor is a dummy constructor and is * not meant to be used by any class other than Ant's @@ -132,7 +135,7 @@ * @exception IllegalArgumentException If n is negative. * @exception IOException If an I/O error occurs */ - public final long skip(long n) throws IOException { + public final long skip(final long n) throws IOException { if (n < 0L) { throw new IllegalArgumentException("skip value is negative"); } @@ -143,5 +146,35 @@ } } return n; + } + + /** + * Set the initialized status. + */ + protected final void setInitialized(final boolean initialized) { + this.initialized = initialized; + } + + /** + * Get the initialized status. + */ + protected final boolean getInitialized() { + return initialized; + } + + /** + * Read till EOL + */ + protected final String readLine() throws IOException { + int ch = in.read(); + String line = (ch == -1) ? null : ""; + while (ch != -1) { + line += (char) ch; + if (ch == '\n') { + break; + } + ch = in.read(); + } + return line; } } 1.8 +9 -53 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/HeadFilter.java Index: HeadFilter.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/HeadFilter.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- HeadFilter.java 28 Feb 2002 01:48:49 -0000 1.7 +++ HeadFilter.java 28 Feb 2002 18:23:37 -0000 1.8 @@ -57,7 +57,6 @@ import java.io.Reader; import org.apache.tools.ant.types.Parameter; -import org.apache.tools.ant.types.Parameterizable; /** * Read the first n lines (Default is first 10 lines) @@ -76,27 +75,18 @@ * @author Magesh Umasankar */ public final class HeadFilter - extends BaseFilterReader - implements Parameterizable, ChainableReader + extends BaseParamFilterReader + implements ChainableReader { /** Lines key to represent the number of lines to be returned. */ private static final String LINES_KEY = "lines"; - /** The passed in parameter array. */ - private Parameter[] parameters; - - /** Have the parameters passed been interpreted? */ - private boolean initialized = false; - /** Number of lines currently read in. */ private long linesRead = 0; /** Default number of lines returned. */ private long lines = 10; - /** If the next character being read is a linefeed, must it be ignored? */ - private boolean ignoreLineFeed = false; - /** * This constructor is a dummy constructor and is * not meant to be used by any class other than Ant's @@ -131,21 +121,8 @@ ch = in.read(); - if (ignoreLineFeed) { - if (ch == '\n') { - ch = in.read(); - } - ignoreLineFeed = false; - } - - switch (ch) { - case '\r': - ch = '\n'; - ignoreLineFeed = true; - //fall through - case '\n': - linesRead++; - break; + if (ch == '\n') { + linesRead++; } } @@ -167,20 +144,6 @@ } /** - * Set the initialized status. - */ - private final void setInitialized(final boolean initialized) { - this.initialized = initialized; - } - - /** - * Get the initialized status. - */ - private final boolean getInitialized() { - return initialized; - } - - /** * Create a new HeadFilter using the passed in * Reader for instantiation. */ @@ -192,21 +155,14 @@ } /** - * Set Parameters - */ - public final void setParameters(final Parameter[] parameters) { - this.parameters = parameters; - setInitialized(false); - } - - /** * Scan for the lines parameter. */ private final void initialize() { - if (parameters != null) { - for (int i = 0; i < parameters.length; i++) { - if (LINES_KEY.equals(parameters[i].getName())) { - lines = new Long(parameters[i].getValue()).longValue(); + Parameter[] params = getParameters(); + if (params != null) { + for (int i = 0; i < params.length; i++) { + if (LINES_KEY.equals(params[i].getName())) { + lines = new Long(params[i].getValue()).longValue(); break; } } 1.7 +13 -50 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/LineContains.java Index: LineContains.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/LineContains.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- LineContains.java 28 Feb 2002 01:48:49 -0000 1.6 +++ LineContains.java 28 Feb 2002 18:23:37 -0000 1.7 @@ -58,7 +58,6 @@ import java.util.Vector; import org.apache.tools.ant.types.Parameter; -import org.apache.tools.ant.types.Parameterizable; /** * Filter Reader to fetch only those lines that contain user specified @@ -84,18 +83,12 @@ * @author Magesh Umasankar */ public final class LineContains - extends BaseFilterReader - implements Parameterizable, ChainableReader + extends BaseParamFilterReader + implements ChainableReader { /** contains key */ private static final String CONTAINS_KEY = "contains"; - /** The passed in parameter array. */ - private Parameter[] parameters; - - /** Have the parameters passed been interpreted? */ - private boolean initialized = false; - /** Vector that holds the strings that input lines must contain. */ private Vector contains = new Vector(); @@ -126,9 +119,9 @@ * user defined values. */ public final int read() throws IOException { - if (!initialized) { + if (!getInitialized()) { initialize(); - initialized = true; + setInitialized(true); } int ch = -1; @@ -141,19 +134,10 @@ line = line.substring(1); } } else { - ch = in.read(); - while (ch != -1) { - if (line == null) { - line = ""; - } - line = line + (char) ch; - if (ch == '\n') { - break; - } - ch = in.read(); - } - - if (line != null) { + line = readLine(); + if (line == null) { + ch = -1; + } else { int containsSize = contains.size(); for (int i = 0; i < containsSize; i++) { String containsStr = (String) contains.elementAt(i); @@ -192,20 +176,6 @@ } /** - * Set the initialized status. - */ - private final void setInitialized(final boolean initialized) { - this.initialized = initialized; - } - - /** - * Get the initialized status. - */ - private final boolean getInitialized() { - return initialized; - } - - /** * Create a new LineContains using the passed in * Reader for instantiation. */ @@ -217,21 +187,14 @@ } /** - * Set Parameters - */ - public final void setParameters(final Parameter[] parameters) { - this.parameters = parameters; - initialized = false; - } - - /** * Parse params to add user defined contains strings. */ private final void initialize() { - if (parameters != null) { - for (int i = 0; i < parameters.length; i++) { - if (CONTAINS_KEY.equals(parameters[i].getType())) { - contains.addElement(parameters[i].getValue()); + Parameter[] params = getParameters(); + if (params != null) { + for (int i = 0; i < params.length; i++) { + if (CONTAINS_KEY.equals(params[i].getType())) { + contains.addElement(params[i].getValue()); } } } 1.7 +13 -50 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/PrefixLines.java Index: PrefixLines.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/PrefixLines.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- PrefixLines.java 28 Feb 2002 01:48:49 -0000 1.6 +++ PrefixLines.java 28 Feb 2002 18:23:37 -0000 1.7 @@ -57,7 +57,6 @@ import java.io.Reader; import org.apache.tools.ant.types.Parameter; -import org.apache.tools.ant.types.Parameterizable; /** * Attach a prefix to every line @@ -76,18 +75,12 @@ * @author Magesh Umasankar */ public final class PrefixLines - extends BaseFilterReader - implements Parameterizable, ChainableReader + extends BaseParamFilterReader + implements ChainableReader { /** prefix key */ private static final String PREFIX_KEY = "prefix"; - /** The passed in parameter array. */ - private Parameter[] parameters; - - /** Have the parameters passed been interpreted? */ - private boolean initialized = false; - /** The prefix to be used. */ private String prefix = null; @@ -135,22 +128,13 @@ queuedData = null; } } else { - ch = in.read(); - while (ch != -1) { - if (queuedData == null) { - if (prefix != null) { - queuedData = prefix; - } else { - queuedData = ""; - } + queuedData = readLine(); + if (queuedData == null) { + ch = -1; + } else { + if (prefix != null) { + queuedData = prefix + queuedData; } - queuedData += (char) ch; - if (ch == '\n') { - break; - } - ch = in.read(); - } - if (queuedData != null) { return read(); } } @@ -172,20 +156,6 @@ } /** - * Set the initialized status. - */ - private final void setInitialized(final boolean initialized) { - this.initialized = initialized; - } - - /** - * Get the initialized status. - */ - private final boolean getInitialized() { - return initialized; - } - - /** * Create a new PrefixLines using the passed in * Reader for instantiation. */ @@ -197,21 +167,14 @@ } /** - * Set Parameters - */ - public final void setParameters(final Parameter[] parameters) { - this.parameters = parameters; - setInitialized(false); - } - - /** * Initialize prefix if available from the param element. */ private final void initialize() { - if (parameters != null) { - for (int i = 0; i < parameters.length; i++) { - if (PREFIX_KEY.equals(parameters[i].getName())) { - prefix = parameters[i].getValue(); + Parameter[] params = getParameters(); + if (params != null) { + for (int i = 0; i < params.length; i++) { + if (PREFIX_KEY.equals(params[i].getName())) { + prefix = params[i].getValue(); break; } } 1.8 +12 -40 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/ReplaceTokens.java Index: ReplaceTokens.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/ReplaceTokens.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- ReplaceTokens.java 28 Feb 2002 01:48:49 -0000 1.7 +++ ReplaceTokens.java 28 Feb 2002 18:23:37 -0000 1.8 @@ -58,7 +58,6 @@ import java.util.Hashtable; import org.apache.tools.ant.types.Parameter; -import org.apache.tools.ant.types.Parameterizable; /** * Replace tokens with user supplied values @@ -81,8 +80,8 @@ * @author Magesh Umasankar */ public final class ReplaceTokens - extends BaseFilterReader - implements Parameterizable, ChainableReader + extends BaseParamFilterReader + implements ChainableReader { /** Default begin token character. */ private static final char DEFAULT_BEGIN_TOKEN = '@'; @@ -93,15 +92,9 @@ /** Data that must be read from, if not null. */ private String queuedData = null; - /** The passed in parameter array. */ - private Parameter[] parameters; - /** Hashtable to hold the replacee-replacer pairs. */ private Hashtable hash = new Hashtable(); - /** Have the parameters passed been interpreted? */ - private boolean initialized; - /** Begin token. */ private char beginToken = DEFAULT_BEGIN_TOKEN; @@ -226,20 +219,6 @@ } /** - * Set the initialized status. - */ - private final void setInitialized(final boolean initialized) { - this.initialized = initialized; - } - - /** - * Get the initialized status. - */ - private final boolean getInitialized() { - return initialized; - } - - /** * Create a new ReplaceTokens using the passed in * Reader for instantiation. */ @@ -253,31 +232,24 @@ } /** - * Set Parameters - */ - public final void setParameters(final Parameter[] parameters) { - this.parameters = parameters; - setInitialized(false); - } - - /** * Initialize tokens and load the replacee-replacer hashtable. */ private final void initialize() { - if (parameters != null) { - for (int i = 0; i < parameters.length; i++) { - if (parameters[i] != null) { - final String type = parameters[i].getType(); + Parameter[] params = getParameters(); + if (params != null) { + for (int i = 0; i < params.length; i++) { + if (params[i] != null) { + final String type = params[i].getType(); if ("tokenchar".equals(type)) { - final String name = parameters[i].getName(); + final String name = params[i].getName(); if ("begintoken".equals(name)) { - beginToken = parameters[i].getValue().charAt(0); + beginToken = params[i].getValue().charAt(0); } else if ("endtoken".equals(name)) { - endToken = parameters[i].getValue().charAt(0); + endToken = params[i].getValue().charAt(0); } } else if ("token".equals(type)) { - final String name = parameters[i].getName(); - final String value = parameters[i].getValue(); + final String name = params[i].getName(); + final String value = params[i].getValue(); hash.put(name, value); } } 1.9 +8 -35 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineBreaks.java Index: StripLineBreaks.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineBreaks.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- StripLineBreaks.java 28 Feb 2002 01:48:49 -0000 1.8 +++ StripLineBreaks.java 28 Feb 2002 18:23:37 -0000 1.9 @@ -57,7 +57,6 @@ import java.io.Reader; import org.apache.tools.ant.types.Parameter; -import org.apache.tools.ant.types.Parameterizable; /** * Filter to flatten the stream to a single line. @@ -72,8 +71,8 @@ * @author Magesh Umasankar */ public final class StripLineBreaks - extends BaseFilterReader - implements Parameterizable, ChainableReader + extends BaseParamFilterReader + implements ChainableReader { /** * Linebreaks. What do to on funny IBM mainframes with odd line endings? @@ -86,15 +85,10 @@ */ private static final String LINE_BREAKS_KEY = "linebreaks"; - /** The passed in parameter array. */ - private Parameter[] parameters; /** Holds the characters that are recognized as line breaks. */ private String lineBreaks = DEFAULT_LINE_BREAKS; - /** Have the parameters passed been interpreted? */ - private boolean initialized = false; - /** * This constructor is a dummy constructor and is * not meant to be used by any class other than Ant's @@ -120,7 +114,7 @@ * next one. */ public final int read() throws IOException { - if (!initialized) { + if (!getInitialized()) { initialize(); setInitialized(true); } @@ -151,20 +145,6 @@ } /** - * Set the initialized status. - */ - private final void setInitialized(final boolean initialized) { - this.initialized = initialized; - } - - /** - * Get the initialized status. - */ - private final boolean getInitialized() { - return initialized; - } - - /** * Create a new StripLineBreaks object using the passed in * Reader for instantiation. */ @@ -176,22 +156,15 @@ } /** - * Set Parameters - */ - public final void setParameters(final Parameter[] parameters) { - this.parameters = parameters; - setInitialized(false); - } - - /** * Line break characters set using the param element. */ private final void initialize() { String userDefinedLineBreaks = null; - if (parameters != null) { - for (int i = 0; i < parameters.length; i++) { - if (LINE_BREAKS_KEY.equals(parameters[i].getName())) { - userDefinedLineBreaks = parameters[i].getValue(); + Parameter[] params = getParameters(); + if (params != null) { + for (int i = 0; i < params.length; i++) { + if (LINE_BREAKS_KEY.equals(params[i].getName())) { + userDefinedLineBreaks = params[i].getValue(); break; } } 1.8 +11 -48 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineComments.java Index: StripLineComments.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineComments.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- StripLineComments.java 28 Feb 2002 01:48:49 -0000 1.7 +++ StripLineComments.java 28 Feb 2002 18:23:37 -0000 1.8 @@ -58,7 +58,6 @@ import java.util.Vector; import org.apache.tools.ant.types.Parameter; -import org.apache.tools.ant.types.Parameterizable; /** * This is a line comment stripper reader @@ -87,18 +86,12 @@ * @author Magesh Umasankar */ public final class StripLineComments - extends BaseFilterReader - implements Parameterizable, ChainableReader + extends BaseParamFilterReader + implements ChainableReader { /** The type that param recognizes to set the comments. */ private static final String COMMENTS_KEY = "comment"; - /** The passed in parameter array. */ - private Parameter[] parameters; - - /** Have the parameters passed been interpreted? */ - private boolean initialized = false; - /** Vector that holds comments. */ private Vector comments = new Vector(); @@ -144,19 +137,10 @@ line = line.substring(1); } } else { - ch = in.read(); - while (ch != -1) { - if (line == null) { - line = ""; - } - line = line + (char) ch; - if (ch == '\n') { - break; - } - ch = in.read(); - } - - if (line != null) { + line = readLine(); + if (line == null) { + ch = -1; + } else { int commentsSize = comments.size(); for (int i = 0; i < commentsSize; i++) { String comment = (String) comments.elementAt(i); @@ -194,20 +178,6 @@ } /** - * Set the initialized status. - */ - private final void setInitialized(final boolean initialized) { - this.initialized = initialized; - } - - /** - * Get the initialized status. - */ - private final boolean getInitialized() { - return initialized; - } - - /** * Create a new StripLineComments object using the passed in * Reader for instantiation. */ @@ -219,21 +189,14 @@ } /** - * Set Parameters - */ - public final void setParameters(final Parameter[] parameters) { - this.parameters = parameters; - setInitialized(false); - } - - /** * Comments set using the param element. */ private final void initialize() { - if (parameters != null) { - for (int i = 0; i < parameters.length; i++) { - if (COMMENTS_KEY.equals(parameters[i].getType())) { - comments.addElement(parameters[i].getValue()); + Parameter[] params = getParameters(); + if (params != null) { + for (int i = 0; i < params.length; i++) { + if (COMMENTS_KEY.equals(params[i].getType())) { + comments.addElement(params[i].getValue()); } } } 1.6 +8 -36 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TabsToSpaces.java Index: TabsToSpaces.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TabsToSpaces.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- TabsToSpaces.java 28 Feb 2002 01:48:49 -0000 1.5 +++ TabsToSpaces.java 28 Feb 2002 18:23:37 -0000 1.6 @@ -57,7 +57,6 @@ import java.io.Reader; import org.apache.tools.ant.types.Parameter; -import org.apache.tools.ant.types.Parameterizable; /** * Converts tabs to spaces. @@ -76,8 +75,8 @@ * @author Magesh Umasankar */ public final class TabsToSpaces - extends BaseFilterReader - implements Parameterizable, ChainableReader + extends BaseParamFilterReader + implements ChainableReader { /** The default tab length is 8 */ private static final int DEFAULT_TAB_LENGTH = 8; @@ -85,12 +84,6 @@ /** The name that param recognizes to set the tablength. */ private static final String TAB_LENGTH_KEY = "tablength"; - /** The passed in parameter array. */ - private Parameter[] parameters; - - /** Have the parameters passed been interpreted? */ - private boolean initialized; - /** Default tab length. */ private int tabLength = DEFAULT_TAB_LENGTH; @@ -155,20 +148,6 @@ } /** - * Set the initialized status. - */ - private final void setInitialized(final boolean initialized) { - this.initialized = initialized; - } - - /** - * Get the initialized status. - */ - private final boolean getInitialized() { - return initialized; - } - - /** * Create a new TabsToSpaces object using the passed in * Reader for instantiation. */ @@ -180,23 +159,16 @@ } /** - * Set Parameters - */ - public final void setParameters(final Parameter[] parameters) { - this.parameters = parameters; - setInitialized(false); - } - - /** * Initialize tokens */ private final void initialize() { - if (parameters != null) { - for (int i = 0; i < parameters.length; i++) { - if (parameters[i] != null) { - if (TAB_LENGTH_KEY.equals(parameters[i].getName())) { + Parameter[] params = getParameters(); + if (params != null) { + for (int i = 0; i < params.length; i++) { + if (params[i] != null) { + if (TAB_LENGTH_KEY.equals(params[i].getName())) { tabLength = - new Integer(parameters[i].getValue()).intValue(); + new Integer(params[i].getValue()).intValue(); break; } } 1.7 +16 -61 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TailFilter.java Index: TailFilter.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TailFilter.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- TailFilter.java 28 Feb 2002 01:48:49 -0000 1.6 +++ TailFilter.java 28 Feb 2002 18:23:37 -0000 1.7 @@ -57,7 +57,6 @@ import java.io.Reader; import org.apache.tools.ant.types.Parameter; -import org.apache.tools.ant.types.Parameterizable; /** * Read the last n lines. Default is last 10 lines. @@ -76,27 +75,18 @@ * @author Magesh Umasankar */ public final class TailFilter - extends BaseFilterReader - implements Parameterizable, ChainableReader + extends BaseParamFilterReader + implements ChainableReader { /** The name that param recognizes to set the number of lines. */ private static final String LINES_KEY = "lines"; - /** The passed in parameter array. */ - private Parameter[] parameters; - - /** Have the parameters passed been interpreted? */ - private boolean initialized = false; - /** Number of lines currently read in. */ private long linesRead = 0; /** Default number of lines returned. */ private long lines = 10; - /** If the next character being read is a linefeed, must it be ignored? */ - private boolean ignoreLineFeed = false; - /** Buffer to hold in characters read ahead. */ private char[] buffer = new char[4096]; @@ -156,31 +146,17 @@ } } - if (ignoreLineFeed) { - if (ch == '\n') { - ch = in.read(); - } - ignoreLineFeed = false; - } + if (ch == '\n') { + ++linesRead; - switch (ch) { - case '\r': - ch = '\n'; - ignoreLineFeed = true; - //fall through - case '\n': - linesRead++; - - if (linesRead == lines + 1) { - int i = 0; - for (i = returnedCharPos + 1; buffer[i] != '\n'; i++) { - } - returnedCharPos = i; - linesRead--; + if (linesRead == lines) { + int i = 0; + for (i = returnedCharPos + 1; buffer[i] != '\n'; i++) { } - break; - } - if (ch == -1) { + returnedCharPos = i; + --linesRead; + } + } else if (ch == -1) { break; } @@ -213,20 +189,6 @@ } /** - * Set the initialized status. - */ - private final void setInitialized(final boolean initialized) { - this.initialized = initialized; - } - - /** - * Get the initialized status. - */ - private final boolean getInitialized() { - return initialized; - } - - /** * Create a new TailFilter using the passed in * Reader for instantiation. */ @@ -238,21 +200,14 @@ } /** - * Set Parameters - */ - public final void setParameters(final Parameter[] parameters) { - this.parameters = parameters; - setInitialized(false); - } - - /** * Scan for the lines parameter. */ private final void initialize() { - if (parameters != null) { - for (int i = 0; i < parameters.length; i++) { - if (LINES_KEY.equals(parameters[i].getName())) { - setLines(new Long(parameters[i].getValue()).longValue()); + Parameter[] params = getParameters(); + if (params != null) { + for (int i = 0; i < params.length; i++) { + if (LINES_KEY.equals(params[i].getName())) { + setLines(new Long(params[i].getValue()).longValue()); break; } } 1.1 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseParamFilterReader.java Index: BaseParamFilterReader.java =================================================================== /* * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Ant", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */ package org.apache.tools.ant.filters; import java.io.FilterReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.types.Parameterizable; /** * Parameterized Base class for core filter readers. * * @author Magesh Umasankar */ public abstract class BaseParamFilterReader extends BaseFilterReader implements Parameterizable { /** The passed in parameter array. */ private Parameter[] parameters; /** * This constructor is a dummy constructor and is * not meant to be used by any class other than Ant's * introspection mechanism. This will close the filter * that is created making it useless for further operations. */ public BaseParamFilterReader() { super(); } /** * Create a new filtered reader. * * @param in a Reader object providing the underlying stream. */ public BaseParamFilterReader(final Reader in) { super(in); } /** * Set Parameters */ public final void setParameters(final Parameter[] parameters) { this.parameters = parameters; setInitialized(false); } protected final Parameter[] getParameters() { return parameters; } } 1.1 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java Index: ChainReaderHelper.java =================================================================== /* * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Ant", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */ package org.apache.tools.ant.filters.util; import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.filters.ChainableReader; import org.apache.tools.ant.types.AntFilterReader; import org.apache.tools.ant.types.FilterChain; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.types.Parameterizable; import java.io.*; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Vector; /** * Process a FilterReader chain. * * @author Magesh Umasankar * @created 23 February 2002 */ public final class ChainReaderHelper { /** * The primary reader to which the reader chain is to be attached. */ public Reader primaryReader; /** * The size of the buffer to be used. */ public int bufferSize = 4096; /** * Chain of filters */ public Vector filterChains = new Vector(); /** * Sets the primary reader */ public final void setPrimaryReader(Reader rdr) { primaryReader = rdr; } /** * Sets the buffer size to be used. Defaults to 4096, * if this method is not invoked. */ public final void setBufferSize(int size) { bufferSize = size; } /** * Sets the collection of filter reader sets */ public final void setFilterChains(Vector fchain) { filterChains = fchain; } /** * Process the reader chain */ public final String processStream() throws BuildException, IOException { if (primaryReader == null) { throw new BuildException("primaryReader must not be null."); } Reader instream = primaryReader; final char[] buffer = new char[bufferSize]; final int filterReadersCount = filterChains.size(); final Vector finalFilters = new Vector(); for (int i = 0; i < filterReadersCount; i++) { final FilterChain filterchain = (FilterChain) filterChains.elementAt(i); final Vector filterReaders = filterchain.getFilterReaders(); final int readerCount = filterReaders.size(); for (int j = 0; j < readerCount; j++) { finalFilters.addElement(filterReaders.elementAt(j)); } } final int filtersCount = finalFilters.size(); if (filtersCount > 0) { for (int i = 0; i < filtersCount; i++) { Object o = finalFilters.elementAt(i); if (o instanceof AntFilterReader) { final AntFilterReader filter = (AntFilterReader) finalFilters.elementAt(i); final String className = filter.getClassName(); final Path classpath = filter.getClasspath(); final Project project = filter.getProject(); if (className != null) { try { Class clazz = null; if (classpath == null) { clazz = Class.forName(className); } else { AntClassLoader al = new AntClassLoader(project, classpath); clazz = al.loadClass(className); AntClassLoader.initializeClass(clazz); } if (clazz != null) { if (!FilterReader.class.isAssignableFrom(clazz)) { throw new BuildException(className + " does not extend java.io.FilterReader"); } final Constructor[] constructors = clazz.getConstructors(); int j = 0; for (; j < constructors.length; j++) { Class[] types = constructors[j] .getParameterTypes(); if (types.length == 1 && types[0].isAssignableFrom(Reader.class)) { break; } } final Reader[] rdr = {instream}; instream = (Reader) constructors[j].newInstance(rdr); if (Parameterizable.class.isAssignableFrom(clazz)) { final Parameter[] params = filter.getParams(); ((Parameterizable) instream).setParameters(params); } } } catch (final ClassNotFoundException cnfe) { throw new BuildException(cnfe); } catch (final InstantiationException ie) { throw new BuildException(ie); } catch (final IllegalAccessException iae) { throw new BuildException(iae); } catch (final InvocationTargetException ite) { throw new BuildException(ite); } } } else if (o instanceof ChainableReader && o instanceof Reader) { instream = ((ChainableReader) o).chain(instream); } } } int bufferLength = 0; String text = null; while (bufferLength != -1) { bufferLength = instream.read(buffer); if (bufferLength != -1) { if (text == null) { text = new String(buffer, 0, bufferLength); } else { text += new String(buffer, 0, bufferLength); } } } return text; } } 1.9 +8 -6 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadFile.java Index: LoadFile.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadFile.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- LoadFile.java 27 Feb 2002 21:50:40 -0000 1.8 +++ LoadFile.java 28 Feb 2002 18:23:38 -0000 1.9 @@ -57,7 +57,7 @@ import org.apache.tools.ant.Project; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.FilterChain; -import org.apache.tools.ant.util.ChainReaderHelper; +import org.apache.tools.ant.filters.util.ChainReaderHelper; import java.io.*; import java.util.Vector; @@ -197,12 +197,14 @@ String text = crh.processStream(); - if(evaluateProperties) { - text = project.replaceProperties(text); + if (text != null) { + if(evaluateProperties) { + text = project.replaceProperties(text); + } + project.setNewProperty(property, text); + log("loaded " + text.length() + " characters",Project.MSG_VERBOSE); + log(property+" := "+text,Project.MSG_DEBUG); } - project.setNewProperty(property, text); - log("loaded " + text.length() + " characters",Project.MSG_VERBOSE); - log(property+" := "+text,Project.MSG_DEBUG); } catch (final IOException ioe) { final String message = "Unable to load file: " + ioe.toString(); 1.5 +1 -1 jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java Index: LoadProperties.java =================================================================== RCS file: /home/cvs/jakarta-ant/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- LoadProperties.java 27 Feb 2002 21:50:40 -0000 1.4 +++ LoadProperties.java 28 Feb 2002 18:23:38 -0000 1.5 @@ -57,7 +57,7 @@ import org.apache.tools.ant.Project; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.FilterChain; -import org.apache.tools.ant.util.ChainReaderHelper; +import org.apache.tools.ant.filters.util.ChainReaderHelper; import java.io.*; import java.util.Vector; -- To unsubscribe, e-mail: For additional commands, e-mail: