⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tokenfilter.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        /**         * Filter a string 'line' replacing from with to         * (C&P from the Replace task)         * @param line the string to be filtered         * @return the filtered line         */        public String filter(String line) {            if (from == null) {                throw new BuildException("Missing from in stringreplace");            }            StringBuffer ret = new StringBuffer();            int start = 0;            int found = line.indexOf(from);            while (found >= 0) {                // write everything up to the from                if (found > start) {                    ret.append(line.substring(start, found));                }                // write the replacement to                if (to != null) {                    ret.append(to);                }                // search again                start = found + from.length();                found = line.indexOf(from, start);            }            // write the remaining characters            if (line.length() > start) {                ret.append(line.substring(start, line.length()));            }            return ret.toString();        }    }    /**     * Simple filter to filter lines contains strings     */    public static class ContainsString extends ProjectComponent        implements Filter {        private String contains;        /**         * the contains attribute         * @param contains the string that the token should contain         */        public void setContains(String contains) {            this.contains = contains;        }        /**         * Filter strings that contain the contains attribute         *         * @param string the string to be filtered         * @return null if the string does not contain "contains",         *              string otherwise         */        public String filter(String string) {            if (contains == null) {                throw new BuildException("Missing contains in containsstring");            }            if (string.indexOf(contains) > -1) {                return string;            }            return null;        }    }    /**     * filter to replace regex.     */    public static class ReplaceRegex extends ChainableReaderFilter {        private String             from;        private String             to;        private RegularExpression  regularExpression;        private Substitution       substitution;        private boolean            initialized = false;        private String             flags = "";        private int                options;        private Regexp             regexp;        /**         * the from attribute         * @param from the regex string         */        public void setPattern(String from) {            this.from = from;        }        /**         * the to attribute         * @param to the replacement string         */        public void setReplace(String to) {            this.to = to;        }        /**         * @param flags the regex flags         */        public void setFlags(String flags) {            this.flags = flags;        }        private void initialize() {            if (initialized) {                return;            }            options = convertRegexOptions(flags);            if (from == null) {                throw new BuildException("Missing pattern in replaceregex");            }            regularExpression = new RegularExpression();            regularExpression.setPattern(from);            regexp = regularExpression.getRegexp(getProject());            if (to == null) {                to = "";            }            substitution = new Substitution();            substitution.setExpression(to);        }        /**         * @param line the string to modify         * @return the modified string         */        public String filter(String line) {            initialize();            if (!regexp.matches(line, options)) {                return line;            }            return regexp.substitute(                line, substitution.getExpression(getProject()), options);        }    }    /**     * filter to filter tokens matching regular expressions.     */    public static class ContainsRegex extends ChainableReaderFilter {        private String             from;        private String             to;        private RegularExpression  regularExpression;        private Substitution       substitution;        private boolean            initialized = false;        private String             flags = "";        private int                options;        private Regexp             regexp;        /**         * @param from the regex pattern         */        public void setPattern(String from) {            this.from = from;        }        /**         * @param to the replacement string         */        public void setReplace(String to) {            this.to = to;        }        /**         * @param flags the regex flags         */        public void setFlags(String flags) {            this.flags = flags;        }        private void initialize() {            if (initialized) {                return;            }            options = convertRegexOptions(flags);            if (from == null) {                throw new BuildException("Missing from in containsregex");            }            regularExpression = new RegularExpression();            regularExpression.setPattern(from);            regexp = regularExpression.getRegexp(getProject());            if (to == null) {                return;            }            substitution = new Substitution();            substitution.setExpression(to);        }        /**         * apply regex and substitution on a string         * @param string the string to apply filter on         * @return the filtered string         */        public String filter(String string) {            initialize();            if (!regexp.matches(string, options)) {                return null;            }            if (substitution == null) {                return string;            }            return regexp.substitute(                string, substitution.getExpression(getProject()), options);        }    }    /** Filter to trim white space */    public static class Trim extends ChainableReaderFilter {        /**         * @param line the string to be trimmed         * @return the trimmed string         */        public String filter(String line) {            return line.trim();        }    }    /** Filter remove empty tokens */    public static class IgnoreBlank extends ChainableReaderFilter {        /**         * @param line the line to modify         * @return the trimmed line         */        public String filter(String line) {            if (line.trim().length() == 0) {                return null;            }            return line;        }    }    /**     * Filter to delete characters     */    public static class DeleteCharacters extends ProjectComponent        implements Filter, ChainableReader {        // Attributes        /** the list of characters to remove from the input */        private String deleteChars = "";        /**         * Set the list of characters to delete         * @param deleteChars the list of characters         */        public void setChars(String deleteChars) {            this.deleteChars = resolveBackSlash(deleteChars);        }        /**         * remove characters from a string         * @param string the string to remove the characters from         * @return the converted string         */        public String filter(String string) {            StringBuffer output = new StringBuffer(string.length());            for (int i = 0; i < string.length(); ++i) {                char ch = string.charAt(i);                if (!(isDeleteCharacter(ch))) {                    output.append(ch);                }            }            return output.toString();        }        /**         * factory method to provide a reader that removes         * the characters from a reader as part of a filter         * chain         * @param reader the reader object         * @return the chained reader object         */        public Reader chain(Reader reader) {            return new BaseFilterReader(reader) {                /**                 * @return the next non delete character                 */                public int read()                    throws IOException {                    while (true) {                        int c = in.read();                        if (c == -1) {                            return c;                        }                        if (!(isDeleteCharacter((char) c))) {                            return c;                        }                    }                }            };        }        /**         *  check if the character c is to be deleted         *         * @param c char to test         * @return true if the supplied char is in the list to be stripped.         */        private boolean isDeleteCharacter(char c) {            for (int d = 0; d < deleteChars.length(); ++d) {                if (deleteChars.charAt(d) ==  c) {                    return true;                }            }            return false;        }    }    // --------------------------------------------------------    //  static utility methods - could be placed somewhere else    // --------------------------------------------------------    /**     * xml does not do "c" like interpretation of strings.     * i.e. \n\r\t etc.     * this method processes \n, \r, \t, \f, \\     * also subs \s -> " \n\r\t\f"     * a trailing '\' will be ignored     *     * @param input raw string with possible embedded '\'s     * @return converted string     */    public static String resolveBackSlash(String input) {        return StringUtils.resolveBackSlash(input);    }    /**     * convert regex option flag characters to regex options     * <dl>     *   <li>g -  Regexp.REPLACE_ALL</li>     *   <li>i -  Regexp.MATCH_CASE_INSENSITIVE</li>     *   <li>m -  Regexp.MATCH_MULTILINE</li>     *   <li>s -  Regexp.MATCH_SINGLELINE</li>     * </dl>     * @param flags the string containing the flags     * @return the Regexp option bits     */    public static int convertRegexOptions(String flags) {        if (flags == null) {            return 0;        }        int options = 0;        if (flags.indexOf('g') != -1) {            options |= Regexp.REPLACE_ALL;        }        if (flags.indexOf('i') != -1) {            options |= Regexp.MATCH_CASE_INSENSITIVE;        }        if (flags.indexOf('m') != -1) {            options |= Regexp.MATCH_MULTILINE;        }        if (flags.indexOf('s') != -1) {            options |= Regexp.MATCH_SINGLELINE;        }        return options;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -