jef.tools.string.StringSpliter.java Source code

Java tutorial

Introduction

Here is the source code for jef.tools.string.StringSpliter.java

Source

/*
 * JEF - Copyright 2009-2010 Jiyi (mr.jiyi@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jef.tools.string;

import org.apache.commons.lang.ArrayUtils;

public class StringSpliter {
    /**
     * ????String,???,
     * String??
     * ?SubstringSubstring??int????
     * 
     * Chart of a String cutter 
     * -----------============------------------
     * |           |         |                 |
     * begin          keyStr                 end
     * 
     */
    private Substring keyword;
    private Substring source;
    public static final int MODE_FROM_LEFT = 0;// ??
    public static final int MODE_FROM_RIGHT = 1;// ??
    private int findMode = MODE_FROM_LEFT;

    public StringSpliter(Substring source) {
        this.source = source;
        this.keyword = null;
    }

    //   public StringSpliter(Substring source, int begin, int end) {
    //      this.source = source;
    //      this.keyword = source.sub(begin, end);
    //   }

    public StringSpliter(String source) {
        this.source = new Substring(source);
        this.keyword = null;
    }

    public Substring getKeyword() {
        return keyword;
    }

    public Substring getSource() {
        return source;
    }

    public void setSource(Substring source) {
        this.source = source;
        this.keyword = null;
    }

    /**
     * key???key
     * @param chars
     */
    public boolean expandKey(char[] chars) {
        boolean res = expandKeyLeft(chars);
        if (expandKeyRight(chars)) {
            res = true;
        }
        return res;
    }

    /**
     * ?key
     * @param chars
     * @return
     */
    public boolean expandKeyLeft(char[] chars) {
        boolean res = false;
        if (keyword == null)
            return res;
        Substring left = getLeft();
        while (left.lastCharacter() != null && ArrayUtils.contains(chars, left.lastCharacter().charValue())) {
            left.setEnd(left.getEnd() - 1);
            res = true;
        }
        if (res)
            this.keyword = source.subAbsoultOffset(left.getEnd(), keyword.getEnd());
        return res;
    }

    /**
     * ???key
     * @param chars
     * @return
     */
    public boolean expandKeyRight(char[] chars) {
        boolean res = false;
        if (keyword == null)
            return res;
        Substring right = getRight();
        while (right.firstCharacter() != null && ArrayUtils.contains(chars, right.firstCharacter().charValue())) {
            right.setStart(right.getStart() + 1);
            res = true;
        }
        if (res)
            this.keyword = source.subAbsoultOffset(keyword.getStart(), right.getStart());
        return res;
    }

    public boolean expandKeyLeft(int count) {
        if (source.getStart() > keyword.getStart() - count) {//keysouce
            return false;
        }
        if (keyword.getEnd() < keyword.getStart() - count) {//keykey end
            return false;
        }
        keyword.setStart(keyword.getStart() - count);
        return true;

    }

    public boolean expandKeyRight(int count) {
        if (source.getEnd() < keyword.getEnd() + count) {
            return false;
        }
        if (keyword.getStart() > keyword.getEnd() + count) {
            return false;
        }
        keyword.setEnd(keyword.getEnd() + count);
        return true;
    }

    /**
     * ?key????
     * @param chars
     * @return
     */
    public boolean expandKeyUntil(char[] chars) {
        boolean res = expandKeyLeftUntil(chars);
        if (expandKeyRightUntil(chars)) {
            res = true;
        }
        return res;
    }

    /**
     * ?Key????
     * @param chars
     * @return
     */
    public boolean expandKeyLeftUntil(char[] chars) {
        boolean res = false;
        if (keyword == null)
            return res;
        Substring left = getLeft();
        while (left.lastCharacter() != null && !ArrayUtils.contains(chars, left.lastCharacter().charValue())) {
            left.setEnd(left.getEnd() - 1);
            res = true;
        }
        if (res)
            this.keyword = source.subAbsoultOffset(left.getEnd(), keyword.getEnd());
        return res;
    }

    /**
     * ??Key????
     * @param chars
     * @return
     */
    public boolean expandKeyRightUntil(char[] chars) {
        boolean res = false;
        if (keyword == null)
            return res;
        Substring right = getRight();
        while (right.firstCharacter() != null && !ArrayUtils.contains(chars, right.firstCharacter().charValue())) {
            right.setStart(right.getStart() + 1);
            res = true;
        }
        if (res)
            this.keyword = source.subAbsoultOffset(keyword.getStart(), right.getStart());
        return res;
    }

    /**
     * ?
     * @return
     */
    public Substring getLeft() {
        if (keyword == null) {
            if (findMode == MODE_FROM_LEFT) {
                return source;
            } else {
                return null;
            }
        } else {
            return source.subAbsoultOffset(source.getStart(), keyword.getStart());
        }
    }

    /**
     * ??
     * 
     * @return
     */
    public Substring getRight() {
        if (keyword == null) {
            if (findMode == MODE_FROM_LEFT) {
                return null;
            } else {
                return source;
            }
        } else {
            return source.subAbsoultOffset(keyword.getEnd(), source.getEnd());
        }
    }

    /**
     * key
     */
    public Substring getLeftWithKey() {
        if (keyword == null) {
            if (findMode == MODE_FROM_LEFT) {
                return null;
            } else {
                return source;
            }
        } else {
            return source.subAbsoultOffset(source.getStart(), keyword.getEnd());
        }
    }

    /**
     * key?
     */
    public Substring getRightWithKey() {
        if (keyword == null) {
            if (findMode == MODE_FROM_LEFT) {
                return null;
            } else {
                return source;
            }
        } else {
            return source.subAbsoultOffset(keyword.getStart(), source.getEnd());
        }
    }

    /**
     * ?keyStr?Substring.
     * @param str
     */
    public boolean setKey(String str) {
        if (str == null) {
            this.keyword = null;
            return false;
        }
        int n = -1;
        if (findMode == MODE_FROM_RIGHT) {
            n = source.lastIndexOf(str);
        } else {
            n = source.indexOf(str);
        }
        if (n > -1) {
            this.keyword = source.sub(n, n + str.length());
            return true;
        } else {
            this.keyword = null;
            return false;
        }
    }

    public boolean setKey(int ketStart, int keyEnd) {
        try {
            this.keyword = source.sub(ketStart, keyEnd);
            return true;
        } catch (IllegalArgumentException e) {
            return false;
        }
    }

    public boolean setKey(char str) {
        if (str == 0) {
            this.keyword = null;
            return false;
        }
        int n = -1;
        if (findMode == MODE_FROM_RIGHT) {
            n = source.lastIndexOf(str);
        } else {
            n = source.indexOf(str);
        }
        if (n > -1) {
            this.keyword = source.sub(n, n + 1);
            return true;
        } else {
            this.keyword = null;
            return false;
        }
    }

    //???
    public boolean setKeyOfAny(String[] ss) {
        int find = -1;
        int length = 0;
        for (String str : ss) {
            if (findMode == MODE_FROM_RIGHT) {
                int n = source.lastIndexOf(str);
                if (n > find || find == -1) {
                    find = n;
                    length = str.length();
                }
            } else {
                int n = source.indexOf(str);
                if (n > -1 && (n < find || find == -1)) {
                    find = n;
                    length = str.length();
                }
            }
        }
        if (find > -1) {
            this.keyword = source.sub(find, find + length);
        } else {
            this.keyword = null;
        }
        return keyword != null;
    }

    public boolean setKeyOfAny(char[] chars) {
        int find = -1;
        for (char c : chars) {
            if (findMode == MODE_FROM_RIGHT) {
                int n = source.lastIndexOf(c);
                if (n > find || find == -1) {
                    find = n;
                }
            } else {
                int n = source.indexOf(c);
                if (n > -1 && (n < find || find == -1)) {
                    find = n;
                }
            }
        }
        if (find > -1) {
            this.keyword = source.sub(find, find + 1);
        } else {
            this.keyword = null;
        }
        return keyword != null;
    }

    public int getMode() {
        return findMode;
    }

    public void setMode(int mode) {
        this.findMode = mode;
    }
}