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

net.jangaroo.extxml.util.FileScanner Maven / Gradle / Ivy

The newest version!
package net.jangaroo.extxml.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;

/**
 * An awk-like file processor that scans a text file line by line and dispatches to
 * processing functions based on regular expressions.
 */
public class FileScanner {

  public FileScanner add(Rule rule) {
    rules.add(rule);
    return this;
  }

  public State scan(File file, State state) throws IOException {
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")));
      String line;
      while ((line = reader.readLine()) != null) {
        for (Rule rule : rules) {
          Matcher matcher = rule.createMatcher(line);
          if (matcher.find()) {
            List groups = new ArrayList(matcher.groupCount());
            for (int i = 1; i <= matcher.groupCount(); ++i) {
              groups.add(matcher.group(i));
            }
            rule.matched(state, groups);
            break;
          }
        }
      }
    } finally {
      if (reader != null) {
        reader.close();
      }
    }
    return state;
  }

  private List> rules = new ArrayList>();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy