From 1eda02547f8519e1dff46dac06734cba6c77b97f Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 6 Oct 2016 13:14:59 +0100 Subject: [PATCH] JAL-2252 now buffered read of 3 lines of a time in help files --- utils/BufferedLineReader.java | 182 +++++++++++++++++++++++++++++++++++++++ utils/HelpLinksChecker.java | 17 +++- utils/MessageBundleChecker.java | 93 ++++++++------------ 3 files changed, 230 insertions(+), 62 deletions(-) create mode 100644 utils/BufferedLineReader.java diff --git a/utils/BufferedLineReader.java b/utils/BufferedLineReader.java new file mode 100644 index 0000000..b813fb2 --- /dev/null +++ b/utils/BufferedLineReader.java @@ -0,0 +1,182 @@ +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.StringReader; + +/** + * A file reader that concatenates lines + * + * @author gmcarstairs + * + */ +public class BufferedLineReader +{ + interface LineCleaner + { + String cleanLine(String l); + } + + /* + * a reader for the file being read + */ + private BufferedReader br; + + /* + * optional handler to post-process each line as it is read + */ + private LineCleaner cleaner; + + /* + * current buffer of post-processed input lines + */ + private String[] buffer; + + private boolean atEof; + + /** + * Constructor + * + * @param reader + * @param bufferSize + * the number of lines to concatenate at a time while reading + * @param tidier + * an optional callback handler to post-process each line after + * reading + * @throws FileNotFoundException + */ + public BufferedLineReader(BufferedReader reader, int bufferSize, + LineCleaner tidier) + throws IOException + { + br = reader; + buffer = new String[bufferSize]; + cleaner = tidier; + + /* + * load up the buffer with N-1 lines, ready for the first read + */ + for (int i = 1; i < bufferSize; i++) + { + readLine(); + } + + } + + /** + * Reads the next line from file, invokes the post-processor if one was + * provided, and returns the 'cleaned' line, or null at end of file. + * + * @return + */ + private String readLine() // throws IOException + { + if (atEof) + { + return null; + } + + String line = null; + try + { + line = br.readLine(); + } catch (IOException e) + { + e.printStackTrace(); + } + if (line == null) + { + atEof = true; + return null; + } + if (cleaner != null) + { + line = cleaner.cleanLine(line); + } + + /* + * shuffle down the lines buffer and add the new line + * in the last position + */ + for (int i = 1; i < buffer.length; i++) + { + buffer[i - 1] = buffer[i]; + } + buffer[buffer.length - 1] = line; + return line; + } + + /** + * Returns a number of concatenated lines from the file, or null at end of + * file. + * + * @return + */ + public String read() + { + if (readLine() == null) + { + return null; + } + StringBuilder result = new StringBuilder(100 * buffer.length); + for (String line : buffer) + { + if (line != null) + { + result.append(line); + } + } + return result.toString(); + } + + /** + * A main 'test' method! + * + * @throws IOException + */ + public static void main(String[] args) throws IOException + { + String data = "Now is the winter\n" + "Of our discontent\n" + + "Made glorious summer\n" + "By this sun of York\n"; + BufferedReader br = new BufferedReader(new StringReader(data)); + BufferedLineReader reader = new BufferedLineReader(br, 3, + new LineCleaner() + { + @Override + public String cleanLine(String l) + { + return l.toUpperCase(); + } + }); + String line = reader.read(); + String expect = "NOW IS THE WINTEROF OUR DISCONTENTMADE GLORIOUS SUMMER"; + if (!line.equals(expect)) + { + System.err.println("Fail: expected '" + expect + "', found '" + line + + ";"); + } + else + { + System.out.println("Line one ok!"); + } + line = reader.read(); + expect = "OF OUR DISCONTENTMADE GLORIOUS SUMMERBY THIS SUN OF YORK"; + if (!line.equals(expect)) + { + System.err.println("Fail: expected '" + expect + "', found '" + line + + "'"); + } + else + { + System.out.println("Line two ok!!"); + } + line = reader.read(); + if (line != null) + { + System.err.println("Fail: expected null at eof, got '" + line + "'"); + } + else + { + System.out.println("EOF ok!!!"); + } + } +} diff --git a/utils/HelpLinksChecker.java b/utils/HelpLinksChecker.java index 7c57cc7..1279b31 100644 --- a/utils/HelpLinksChecker.java +++ b/utils/HelpLinksChecker.java @@ -34,7 +34,7 @@ import java.util.Map; * @author gmcarstairs * */ -public class HelpLinksChecker +public class HelpLinksChecker implements BufferedLineReader.LineCleaner { private static final String HELP_HS = "help.hs"; @@ -358,7 +358,8 @@ public class HelpLinksChecker try { BufferedReader br = new BufferedReader(new FileReader(hrefFile)); - String data = br.readLine(); + BufferedLineReader blr = new BufferedLineReader(br, 3, this); + String data = blr.read(); while (data != null) { if (data.contains(nameAnchor) || data.contains(idAnchor)) @@ -366,7 +367,7 @@ public class HelpLinksChecker found = true; break; } - data = br.readLine(); + data = blr.read(); } br.close(); } catch (IOException e) @@ -544,4 +545,14 @@ public class HelpLinksChecker } return value; } + + /** + * Trim whitespace from concatenated lines but preserve one space for valid + * parsing + */ + @Override + public String cleanLine(String l) + { + return l.trim() + " "; + } } diff --git a/utils/MessageBundleChecker.java b/utils/MessageBundleChecker.java index 15e16cf..4489a93 100644 --- a/utils/MessageBundleChecker.java +++ b/utils/MessageBundleChecker.java @@ -40,7 +40,7 @@ import java.util.regex.Pattern; * @author gmcarstairs * */ -public class MessageBundleChecker +public class MessageBundleChecker implements BufferedLineReader.LineCleaner { /* * regex ^"[^"]*"$ @@ -212,64 +212,37 @@ public class MessageBundleChecker return; } - String[] lines = new String[bufferSize]; BufferedReader br = new BufferedReader(new FileReader(f)); - for (int i = 0; i < bufferSize; i++) - { - String readLine = br.readLine(); - lines[i] = stripCommentsAndTrim(readLine); - } + BufferedLineReader blr = new BufferedLineReader(br, bufferSize, this); int lineNo = 0; - - while (lines[bufferSize - 1] != null) + String line = blr.read(); + while (line != null) { lineNo++; - inspectSourceLines(path, lineNo, lines); - - for (int i = 0; i < bufferSize - 1; i++) - { - lines[i] = lines[i + 1]; - } - lines[bufferSize - 1] = stripCommentsAndTrim(br.readLine()); + inspectSourceLines(path, lineNo, line); + line = blr.read(); } br.close(); } - /* - * removes anything after (and including) '//' - */ - private String stripCommentsAndTrim(String line) - { - if (line != null) - { - int pos = line.indexOf("//"); - if (pos != -1) - { - line = line.substring(0, pos); - } - line = line.replace("\t", " ").trim(); - } - return line; - } - /** * Look for calls to MessageManager methods, possibly split over two or more - * lines + * lines that have been concatenated while parsing the file * * @param path * @param lineNo - * @param lines + * @param line */ - private void inspectSourceLines(String path, int lineNo, String[] lines) + private void inspectSourceLines(String path, int lineNo, String line) { - String lineNos = String.format("%d-%d", lineNo, lineNo + lines.length + String lineNos = String + .format("%d-%d", lineNo, lineNo + bufferSize - 1); - String combined = combineLines(lines); for (String method : METHODS) { - int pos = combined.indexOf(method); + int pos = line.indexOf(method); if (pos == -1) { continue; @@ -278,7 +251,7 @@ public class MessageBundleChecker /* * extract what follows the opening bracket of the method call */ - String methodArgs = combined.substring(pos + method.length()).trim(); + String methodArgs = line.substring(pos + method.length()).trim(); if ("".equals(methodArgs)) { /* @@ -305,7 +278,7 @@ public class MessageBundleChecker if (METHOD3 == method) { System.out.println(String.format("Dynamic key at %s line %s %s", - path.substring(sourcePath.length()), lineNos, combined)); + path.substring(sourcePath.length()), lineNos, line)); continue; } @@ -313,14 +286,14 @@ public class MessageBundleChecker if (messageKey == null) { System.out.println(String.format("Trouble parsing %s line %s %s", - path.substring(sourcePath.length()), lineNos, combined)); + path.substring(sourcePath.length()), lineNos, line)); continue; } if (!(STRING_PATTERN.matcher(messageKey).matches())) { System.out.println(String.format("Dynamic key at %s line %s %s", - path.substring(sourcePath.length()), lineNos, combined)); + path.substring(sourcePath.length()), lineNos, line)); continue; } @@ -384,22 +357,6 @@ public class MessageBundleChecker return endPos == -1 ? null : key.substring(0, endPos); } - private String combineLines(String[] lines) - { - String combined = ""; - if (lines != null) - { - for (String line : lines) - { - if (line != null) - { - combined += line; - } - } - } - return combined; - } - /** * Loads properties from Message.properties * @@ -421,4 +378,22 @@ public class MessageBundleChecker } + /** + * Remove any trailing comments, change tabs to space, and trim + */ + @Override + public String cleanLine(String l) + { + if (l != null) + { + int pos = l.indexOf("//"); + if (pos != -1) + { + l = l.substring(0, pos); + } + l = l.replace("\t", " ").trim(); + } + return l; + } + } -- 1.7.10.2