From 87f806d3e4aba137d933f31a902197a19f96b7c4 Mon Sep 17 00:00:00 2001 From: Magesh Umasankar Date: Thu, 28 Feb 2002 18:23:38 +0000 Subject: [PATCH] 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. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271605 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/filters/BaseFilterReader.java | 35 +++++- .../ant/filters/BaseParamFilterReader.java | 106 ++++++++++++++++++ .../apache/tools/ant/filters/HeadFilter.java | 62 ++-------- .../tools/ant/filters/LineContains.java | 63 +++-------- .../apache/tools/ant/filters/PrefixLines.java | 63 +++-------- .../tools/ant/filters/ReplaceTokens.java | 52 ++------- .../tools/ant/filters/StripLineBreaks.java | 43 ++----- .../tools/ant/filters/StripLineComments.java | 59 ++-------- .../tools/ant/filters/TabsToSpaces.java | 44 ++------ .../apache/tools/ant/filters/TailFilter.java | 77 +++---------- .../{ => filters}/util/ChainReaderHelper.java | 2 +- .../apache/tools/ant/taskdefs/LoadFile.java | 14 ++- .../tools/ant/taskdefs/LoadProperties.java | 2 +- 13 files changed, 240 insertions(+), 382 deletions(-) create mode 100644 proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseParamFilterReader.java rename proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/{ => filters}/util/ChainReaderHelper.java (99%) diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseFilterReader.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseFilterReader.java index 357c2ba72..cbea49c33 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseFilterReader.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseFilterReader.java @@ -66,6 +66,9 @@ import java.io.StringReader; 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 @@ public abstract class BaseFilterReader * @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"); } @@ -144,4 +147,34 @@ public abstract class BaseFilterReader } 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; + } } diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseParamFilterReader.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseParamFilterReader.java new file mode 100644 index 000000000..ff9109c48 --- /dev/null +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/BaseParamFilterReader.java @@ -0,0 +1,106 @@ +/* + * 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; + } +} diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/HeadFilter.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/HeadFilter.java index d445c3448..f0eb99847 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/HeadFilter.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/HeadFilter.java @@ -57,7 +57,6 @@ import java.io.IOException; 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 @@ import org.apache.tools.ant.types.Parameterizable; * @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 @@ public final class HeadFilter 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++; } } @@ -166,20 +143,6 @@ public final class HeadFilter return lines; } - /** - * 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. @@ -191,22 +154,15 @@ public final class HeadFilter return newFilter; } - /** - * 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; } } diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/LineContains.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/LineContains.java index 77a29a20a..8e9fd6b0d 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/LineContains.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/LineContains.java @@ -58,7 +58,6 @@ import java.io.Reader; 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 @@ import org.apache.tools.ant.types.Parameterizable; * @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 @@ public final class LineContains * 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 @@ public final class LineContains 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); @@ -191,20 +175,6 @@ public final class LineContains return contains; } - /** - * 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. @@ -216,22 +186,15 @@ public final class LineContains return newFilter; } - /** - * 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()); } } } diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/PrefixLines.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/PrefixLines.java index 2988dc86c..6ba85a936 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/PrefixLines.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/PrefixLines.java @@ -57,7 +57,6 @@ import java.io.IOException; 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 @@ import org.apache.tools.ant.types.Parameterizable; * @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 @@ public final class PrefixLines 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(); } } @@ -171,20 +155,6 @@ public final class PrefixLines return prefix; } - /** - * 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. @@ -196,22 +166,15 @@ public final class PrefixLines return newFilter; } - /** - * 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; } } diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/ReplaceTokens.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/ReplaceTokens.java index 6a791774f..fd7ab0a50 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/ReplaceTokens.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/ReplaceTokens.java @@ -58,7 +58,6 @@ import java.io.Reader; 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 @@ import org.apache.tools.ant.types.Parameterizable; * @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 @@ public final class ReplaceTokens /** 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; @@ -225,20 +218,6 @@ public final class ReplaceTokens return hash; } - /** - * 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. @@ -252,32 +231,25 @@ public final class ReplaceTokens return newFilter; } - /** - * 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); } } diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineBreaks.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineBreaks.java index b2d1136ec..e448bcd31 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineBreaks.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineBreaks.java @@ -57,7 +57,6 @@ import java.io.IOException; 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 @@ import org.apache.tools.ant.types.Parameterizable; * @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 @@ public final class StripLineBreaks */ 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 @@ public final class StripLineBreaks * next one. */ public final int read() throws IOException { - if (!initialized) { + if (!getInitialized()) { initialize(); setInitialized(true); } @@ -150,20 +144,6 @@ public final class StripLineBreaks return lineBreaks; } - /** - * 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. @@ -175,23 +155,16 @@ public final class StripLineBreaks return newFilter; } - /** - * 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; } } diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineComments.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineComments.java index 825d966a9..f9521c65b 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineComments.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/StripLineComments.java @@ -58,7 +58,6 @@ import java.io.Reader; 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 @@ import org.apache.tools.ant.types.Parameterizable; * @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 @@ public final class StripLineComments 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); @@ -193,20 +177,6 @@ public final class StripLineComments return comments; } - /** - * 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. @@ -218,22 +188,15 @@ public final class StripLineComments return newFilter; } - /** - * 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()); } } } diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TabsToSpaces.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TabsToSpaces.java index bcbc6fc38..8c91aa286 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TabsToSpaces.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TabsToSpaces.java @@ -57,7 +57,6 @@ import java.io.IOException; 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 @@ import org.apache.tools.ant.types.Parameterizable; * @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 @@ public final class TabsToSpaces /** 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; @@ -154,20 +147,6 @@ public final class TabsToSpaces return tabLength; } - /** - * 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. @@ -179,24 +158,17 @@ public final class TabsToSpaces return newFilter; } - /** - * 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; } } diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TailFilter.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TailFilter.java index dbf839322..f3d7f0fd6 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TailFilter.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/TailFilter.java @@ -57,7 +57,6 @@ import java.io.IOException; 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 @@ import org.apache.tools.ant.types.Parameterizable; * @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 @@ public final class TailFilter } } - if (ignoreLineFeed) { - if (ch == '\n') { - ch = in.read(); - } - ignoreLineFeed = false; - } - - switch (ch) { - case '\r': - ch = '\n'; - ignoreLineFeed = true; - //fall through - case '\n': - linesRead++; + if (ch == '\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; } @@ -212,20 +188,6 @@ public final class TailFilter return lines; } - /** - * 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. @@ -237,22 +199,15 @@ public final class TailFilter return newFilter; } - /** - * 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; } } diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/util/ChainReaderHelper.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java similarity index 99% rename from proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/util/ChainReaderHelper.java rename to proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java index ba423bde2..0a1a7ba38 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/util/ChainReaderHelper.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java @@ -51,7 +51,7 @@ * information on the Apache Software Foundation, please see * . */ -package org.apache.tools.ant.util; +package org.apache.tools.ant.filters.util; import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadFile.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadFile.java index ca569c691..66f7c3b8c 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadFile.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadFile.java @@ -57,7 +57,7 @@ import org.apache.tools.ant.Task; 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 @@ public final class LoadFile extends Task { 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(); diff --git a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java index b7251fa2d..0306dc548 100644 --- a/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java +++ b/proposal/sandbox/filterreaders/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java @@ -57,7 +57,7 @@ import org.apache.tools.ant.Task; 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;