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

shz.core.io.FileMatcher Maven / Gradle / Ivy

There is a newer version: 2024.0.2
Show newest version
package shz.core.io;

import shz.core.NullHelp;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class FileMatcher {
    private FileMatcher() {
        throw new IllegalStateException();
    }

    public static final class FileInfo {
        final String path;
        final List lines;

        public FileInfo(String path, List lines) {
            this.path = path;
            this.lines = lines;
        }

        @Override
        public String toString() {
            return "FileInfo{" +
                    "path='" + path + '\'' +
                    ", lines=" + lines +
                    '}';
        }
    }

    public static final class Line {
        final int row;
        final List columns;

        public Line(int row, List columns) {
            this.row = row;
            this.columns = columns;
        }

        @Override
        public String toString() {
            return "Line{" +
                    "row=" + row +
                    ", columns=" + columns +
                    '}';
        }
    }

    public static final class Column {
        final int start;
        final int end;

        public Column(int start, int end) {
            this.start = start;
            this.end = end;
        }

        @Override
        public String toString() {
            return "Column{" +
                    "start=" + start +
                    ", end=" + end +
                    '}';
        }
    }

    @FunctionalInterface
    public interface FileMatchPattern {
        Pattern get(File dir, File file);
    }

    public static List match(File file, FileMatchPattern pattern, boolean recursive) {
        if (file == null || pattern == null) return Collections.emptyList();
        List result = new LinkedList<>();
        FileVisitor.visit(file, (dir, f) -> {
            Pattern p = pattern.get(dir, f);
            if (p == null) return true;
            try (BufferedReader br = IOHelp.newBufferedReader(f.toPath())) {
                List lines = new LinkedList<>();
                FileInfo fileInfo = new FileInfo(f.getAbsolutePath(), lines);
                int row = 0;
                String line;
                while ((line = br.readLine()) != null) {
                    ++row;
                    List columns = columns(line, p);
                    if (columns != null) lines.add(new Line(row, columns));
                }
                if (!lines.isEmpty()) result.add(fileInfo);
            } catch (IOException ignored) {
            }
            return false;
        }, recursive);
        return result.isEmpty() ? Collections.emptyList() : result;
    }

    private static List columns(String line, Pattern pattern) {
        if (NullHelp.isEmpty(line)) return null;
        Matcher matcher = pattern.matcher(line);
        if (!matcher.find()) return null;
        matcher.reset();
        List columns = new ArrayList<>();
        while (matcher.find()) columns.add(new Column(matcher.start(), matcher.end()));
        return columns;
    }

    public static List match(File file, FileMatchPattern pattern) {
        return match(file, pattern, true);
    }

    public static List match(File file, Pattern pattern, boolean recursive) {
        return match(file, pattern == null ? null : (dir, f) -> pattern, true);
    }

    public static List match(File file, Pattern pattern) {
        return match(file, pattern, true);
    }

    public static List match(File file, String regex, boolean recursive) {
        return match(file, NullHelp.isEmpty(regex) ? null : Pattern.compile(regex), recursive);
    }

    public static List match(File file, String regex) {
        return match(file, regex, true);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy