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

cdc.io.txt.LinesParser Maven / Gradle / Ivy

package cdc.io.txt;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import cdc.util.function.Evaluation;

/**
 * Utilities to parse text sources and call {@link LinesHandler}.
 * 

* WARNING: With current implementation, if the last line is empty, it will be skipped.
* This content: * *

 * {@code
 * [11111]
 * [22222]
 * []
 * }
 * 
* and this one: * *
 * {@code
 * [11111]
 * [22222]
 * }
 * 
* will appear identical (2 lines). *

* One may consider using {@link Files#lines(java.nio.file.Path)} * or {@link Files#lines(java.nio.file.Path, java.nio.charset.Charset)}. * * @author Damien Carbonne */ public final class LinesParser { private static final Logger LOGGER = LogManager.getLogger(LinesParser.class); private LinesParser() { } /** * Parses a reader and invokes a LineHandler. * * @param reader The reader. * @param handler The handler. * @throws IOException When an IO exception occurs */ public static void parse(Reader reader, LinesHandler handler) throws IOException { LOGGER.trace("parse(...)"); final BufferedReader r; if (reader instanceof BufferedReader) { r = (BufferedReader) reader; } else { r = new BufferedReader(reader); } String line = null; int number = 0; LOGGER.trace("invoke processBegin()"); handler.processBegin(); Evaluation evaluation = Evaluation.CONTINUE; while (evaluation == Evaluation.CONTINUE && (line = r.readLine()) != null) { number++; LOGGER.trace("invoke processLine({})", line); evaluation = handler.processLine(line, number); } LOGGER.trace("invoke processEnd()"); handler.processEnd(); } /** * Parses an InputStream using a specified charset and invokes a LineHandler. * * @param in The input stream. * @param systemId The system id. * @param charset The name of the charset to use. * @param handler The handler. * @throws IOException When an IO exception occurs */ public static void parse(InputStream in, String systemId, Charset charset, LinesHandler handler) throws IOException { if (charset != null) { try (final Reader reader = new BufferedReader(new InputStreamReader(in, charset))) { parse(reader, handler); } } else { try (final Reader reader = new BufferedReader(new InputStreamReader(in))) { parse(reader, handler); } } } /** * Parses an InputStream using platform default charset and invokes a LineHandler. * * @param in The input stream. * @param systemId The system id. * @param handler The handler. * @throws IOException When an IO exception occurs */ public static void parse(InputStream in, String systemId, LinesHandler handler) throws IOException { parse(in, null, handler); } /** * Parses a file using a specified charset and invokes a LineHandler. * * @param filename The file name. * @param charset The name of the charset to use. * @param handler The handler. * @throws IOException When an IO exception occurs */ public static void parse(String filename, Charset charset, LinesHandler handler) throws IOException { try (final InputStream in = new BufferedInputStream(new FileInputStream(filename))) { parse(in, filename, charset, handler); } } /** * Parses a file using platform default charset and invokes a LineHandler. * * @param filename The file name. * @param handler The handler. * @throws IOException When an IO exception occurs */ public static void parse(String filename, LinesHandler handler) throws IOException { parse(filename, null, handler); } /** * Parses a file using a specified charset and invokes a LineHandler. * * @param file The file. * @param charset The name of the charset to use. * @param handler The handler. * @throws IOException When an IO exception occurs */ public static void parse(File file, Charset charset, LinesHandler handler) throws IOException { try (final InputStream in = new BufferedInputStream(new FileInputStream(file))) { parse(in, file.getPath(), charset, handler); } } /** * Parses a file using platform default charset and invokes a LineHandler. * * @param file The file. * @param handler The handler. * @throws IOException When an IO exception occurs */ public static void parse(File file, LinesHandler handler) throws IOException { parse(file, null, handler); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy