# HG changeset patch # User Artem Tikhomirov # Date 1342556061 -7200 # Node ID f3fab7a208416e11c6358b228d1cb9992497d54c # Parent 59b7c817bc4d7068380e46fdb4335d65cfe3ec88 Refactor LineReader utility as stanalone class to facilitate reuse diff -r 59b7c817bc4d -r f3fab7a20841 src/org/tmatesoft/hg/internal/LineReader.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/tmatesoft/hg/internal/LineReader.java Tue Jul 17 22:14:21 2012 +0200 @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2012 TMate Software Ltd + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For information on how to redistribute this software under + * the terms of a license other than GNU General Public License + * contact TMate Software at support@hg4j.com + */ +package org.tmatesoft.hg.internal; + +import static org.tmatesoft.hg.util.LogFacility.Severity.Warn; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Collection; + +import org.tmatesoft.hg.repo.HgInvalidFileException; +import org.tmatesoft.hg.repo.ext.MqManager; +import org.tmatesoft.hg.util.LogFacility; + +/** + * Handy class to read line-based configuration files + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public final class LineReader { + + public interface LineConsumer { + // boolean begin(File f, T paramObj) throws IOException; + boolean consume(String line, T paramObj) throws IOException; + // boolean end(File f, T paramObj) throws IOException; + } + + public static class SimpleLineCollector implements LineReader.LineConsumer> { + + public boolean consume(String line, Collection result) throws IOException { + result.add(line); + return true; + } + } + + private final File file; + private final LogFacility log; + private boolean trimLines = true; + private boolean skipEmpty = true; + private String ignoreThatStarts = null; + + public LineReader(File f, LogFacility logFacility) { + file = f; + log = logFacility; + } + + /** + * default: true + * false to return line as is + */ + public LineReader trimLines(boolean trim) { + trimLines = trim; + return this; + } + + /** + * default: true + * false to pass empty lines to consumer + */ + public LineReader skipEmpty(boolean skip) { + skipEmpty = skip; + return this; + } + + /** + * default: doesn't skip any line. + * set e.g. to "#" or "//" to skip lines that start with such prefix + */ + public LineReader ignoreLineComments(String lineStart) { + ignoreThatStarts = lineStart; + return this; + } + + public void read(LineConsumer consumer, T paramObj) throws HgInvalidFileException { + BufferedReader statusFileReader = null; + try { +// consumer.begin(file, paramObj); + statusFileReader = new BufferedReader(new FileReader(file)); + String line; + boolean ok = true; + while (ok && (line = statusFileReader.readLine()) != null) { + if (trimLines) { + line = line.trim(); + } + if (ignoreThatStarts != null && line.startsWith(ignoreThatStarts)) { + continue; + } + if (!skipEmpty || line.length() > 0) { + ok = consumer.consume(line, paramObj); + } + } + } catch (IOException ex) { + throw new HgInvalidFileException(ex.getMessage(), ex, file); + } finally { + try { + statusFileReader.close(); + } catch (IOException ex) { + log.dump(MqManager.class, Warn, ex, null); + } +// try { +// consumer.end(file, paramObj); +// } catch (IOException ex) { +// log.warn(MqManager.class, ex, null); +// } + } + } + } \ No newline at end of file diff -r 59b7c817bc4d -r f3fab7a20841 src/org/tmatesoft/hg/repo/ext/MqManager.java --- a/src/org/tmatesoft/hg/repo/ext/MqManager.java Thu Jul 12 20:02:49 2012 +0200 +++ b/src/org/tmatesoft/hg/repo/ext/MqManager.java Tue Jul 17 22:14:21 2012 +0200 @@ -18,12 +18,9 @@ import static org.tmatesoft.hg.util.LogFacility.Severity.Warn; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; @@ -31,6 +28,7 @@ import java.util.Map; import org.tmatesoft.hg.core.Nodeid; +import org.tmatesoft.hg.internal.LineReader; import org.tmatesoft.hg.repo.HgInternals; import org.tmatesoft.hg.repo.HgInvalidControlFileException; import org.tmatesoft.hg.repo.HgInvalidFileException; @@ -71,14 +69,14 @@ File queues = new File(repoDir, "patches.queues"); if (queues.isFile()) { LineReader lr = new LineReader(queues, log).trimLines(true).skipEmpty(true); - lr.read(new SimpleLineCollector(), queueNames = new LinkedList()); + lr.read(new LineReader.SimpleLineCollector(), queueNames = new LinkedList()); } final String queueLocation; // path under .hg to patch queue information (status, series and diff files) File activeQueueFile = new File(repoDir, "patches.queue"); // file is there only if it's not default queue ('patches') that is active if (activeQueueFile.isFile()) { ArrayList contents = new ArrayList(); - new LineReader(activeQueueFile, log).read(new SimpleLineCollector(), contents); + new LineReader(activeQueueFile, log).read(new LineReader.SimpleLineCollector(), contents); if (contents.isEmpty()) { log.dump(getClass(), Warn, "File %s with active queue name is empty", activeQueueFile.getName()); activeQueue = PATCHES_DIR; @@ -104,7 +102,7 @@ final File fileStatus = new File(repoDir, queueLocation + "status"); final File fileSeries = new File(repoDir, queueLocation + "series"); if (fileStatus.isFile()) { - new LineReader(fileStatus, log).read(new LineConsumer>() { + new LineReader(fileStatus, log).read(new LineReader.LineConsumer>() { public boolean consume(String line, List result) throws IOException { int sep = line.indexOf(':'); @@ -125,7 +123,7 @@ name2patch.put(pr.getName(), pr); } LinkedList knownPatchNames = new LinkedList(); - new LineReader(fileSeries, log).read(new SimpleLineCollector(), knownPatchNames); + new LineReader(fileSeries, log).read(new LineReader.SimpleLineCollector(), knownPatchNames); // XXX read other queues? allKnown = new ArrayList(knownPatchNames.size()); for (String name : knownPatchNames) { @@ -143,14 +141,6 @@ } } - static class SimpleLineCollector implements LineConsumer> { - - public boolean consume(String line, Collection result) throws IOException { - result.add(line); - return true; - } - } - /** * Number of patches not yet applied * @return positive value when there are @@ -246,87 +236,4 @@ return String.format(fmt, name, ni, location); } } - - // TODO refine API and extract into separate classes - - interface LineConsumer { -// boolean begin(File f, T paramObj) throws IOException; - boolean consume(String line, T paramObj) throws IOException; -// boolean end(File f, T paramObj) throws IOException; - } - - class LineReader { - - private final File file; - private final LogFacility log; - private boolean trimLines = true; - private boolean skipEmpty = true; - private String ignoreThatStars = null; - - LineReader(File f, LogFacility logFacility) { - file = f; - log = logFacility; - } - - /** - * default: true - * false to return line as is - */ - LineReader trimLines(boolean trim) { - trimLines = trim; - return this; - } - - /** - * default: true - * false to pass empty lines to consumer - */ - LineReader skipEmpty(boolean skip) { - skipEmpty = skip; - return this; - } - - /** - * default: doesn't skip any line. - * set e.g. to "#" or "//" to skip lines that start with such prefix - */ - LineReader ignoreLineComments(String lineStart) { - ignoreThatStars = lineStart; - return this; - } - - void read(LineConsumer consumer, T paramObj) throws HgInvalidFileException { - BufferedReader statusFileReader = null; - try { -// consumer.begin(file, paramObj); - statusFileReader = new BufferedReader(new FileReader(file)); - String line; - boolean ok = true; - while (ok && (line = statusFileReader.readLine()) != null) { - if (trimLines) { - line = line.trim(); - } - if (ignoreThatStars != null && line.startsWith(ignoreThatStars)) { - continue; - } - if (!skipEmpty || line.length() > 0) { - ok = consumer.consume(line, paramObj); - } - } - } catch (IOException ex) { - throw new HgInvalidFileException(ex.getMessage(), ex, file); - } finally { - try { - statusFileReader.close(); - } catch (IOException ex) { - log.dump(MqManager.class, Warn, ex, null); - } -// try { -// consumer.end(file, paramObj); -// } catch (IOException ex) { -// log.warn(MqManager.class, ex, null); -// } - } - } - } }