All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cz.jalasoft.util.text.TextFragment Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package cz.jalasoft.util.text;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A fragment that represents a piece of text.
 *
 * Created by Honza Lastovicka on 17.4.15.
 */
public final class TextFragment extends Fragment {

    private final String text;

    TextFragment(String text) {
        this.text = text;
    }

    public String text() {
        return text;
    }

    public FragmentList findFragmentsBetweenText(String startString, String endString) {
        return findFragmentsBetween(escaped(startString), escaped(endString));
    }

    private String escaped(String string) {
        String escaped = string.replaceAll("([\\\\\\.\\[\\{\\(\\)\\}\\]\\*\\+\\?\\^\\$\\|])", "\\\\$1");
        return escaped;
    }

    public FragmentList findFragmentsBetween(String startString, String endString) {
        Pattern startPattern = Pattern.compile(startString);
        Pattern endPattern = Pattern.compile(endString);

        return findFragmentsBetween(startPattern, endPattern);
    }

    public FragmentList findFragmentsBetween(Pattern startPattern, Pattern endPattern) {
        List result = new ArrayList<>();

        Matcher startMatcher = startPattern.matcher(text);
        Matcher endMatcher = endPattern.matcher(text);

        while(startMatcher.find()) {
            int startIndex = startMatcher.end();
            int endIndex = -1;

            while(endMatcher.find()) {
                endIndex = endMatcher.start();
                if (endIndex > startIndex) {
                    break;
                }
            }

            if (endIndex < 0) {
                break;
            }

            String newFragment = text.substring(startIndex, endIndex);
            result.add(new TextFragment(newFragment));
        }

        return new FragmentList(result);
    }

    public FragmentList findFragmentsMatching(String stringPattern) {
        Pattern pattern = Pattern.compile(stringPattern);
        return findFragmentsMatching(pattern);
    }

    public FragmentList findFragmentsMatching(Pattern pattern) {
        List fragments = new ArrayList<>();

        Matcher matcher = pattern.matcher(text);
        while(matcher.find()) {
            fragments.add(new RegexFragment(matcher));
        }
        return new FragmentList<>(fragments);
    }

    @Override
    TextFragment getThis() {
        return this;
    }

    /*
    public  U convert(Converter converter) throws ConversionException {
        return converter.convert(this);
    }*/
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy