# HG changeset patch # User František Kučera # Date 1410029578 -7200 # Node ID 1d31d9cd28c83ab55c8cd2ffa6e6fb538fdd3865 # Parent 4f7028b5f2fe9842614407fb5cc095947f176393 in-ini: log invalid lines + skip blank lines + log line numbers diff -r 4f7028b5f2fe -r 1d31d9cd28c8 java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java --- a/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java Sat Sep 06 20:31:05 2014 +0200 +++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java Sat Sep 06 20:52:58 2014 +0200 @@ -47,12 +47,17 @@ try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) { FileContext fc = new FileContext(contentHandler); for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) { + fc.lineNumber++; + boolean lineProcessed = false; for (LINE_TYPE lineType : LINE_TYPE.values()) { - boolean lineProcessed = lineType.processLine(currentLine, fc); + lineProcessed = lineType.processLine(currentLine, fc); if (lineProcessed) { break; } } + if (!lineProcessed) { + log.log(Level.SEVERE, "Invalid line in INI file: {0}", currentLine); + } } fc.outputEndSection(fc.lastSection); @@ -78,6 +83,7 @@ private final Alt2ContentHandler contentHandler; private String lastSection; + private int lineNumber; public FileContext(Alt2ContentHandler contentHandler) { this.contentHandler = contentHandler; @@ -109,17 +115,24 @@ private enum LINE_TYPE { + BLANK_LINE("\\s*") { + @Override + public void processLine(LineContext lc, FileContext fc) throws SAXException { + log.log(Level.FINEST, "Line {0}: skipping blank line", fc.lineNumber); + } + }, COMMENT("\\s*(;|#)\\s*(?.*)") { @Override public void processLine(LineContext lc, FileContext fc) throws SAXException { // TODO: comment → LexicalHandler - log.log(Level.FINER, "Comment: {0}", lc.matcher.group("comment")); + log.log(Level.FINER, "Line {0}: comment: {1}", new Object[]{fc.lineNumber, lc.matcher.group("comment")}); } }, SECTION("\\s*\\[\\s*(?[^\\s\\]]+)\\s*\\]\\s*") { @Override public void processLine(LineContext lc, FileContext fc) throws SAXException { + // TODO: escaping for section names with spaces String name = lc.matcher.group("name"); fc.outputEndSection(fc.lastSection); fc.outputStartSection(name); @@ -140,7 +153,7 @@ if (lc.matcher.groupCount() > 2) { String comment = lc.matcher.group("comment"); // TODO: comment → LexicalHandler - log.log(Level.FINER, "Comment for entry „{0}“ is: {1}", new Object[]{key, comment}); + log.log(Level.FINER, "Line {0}: comment for entry „{1}“ is: {2}", new Object[]{fc.lineNumber, key, comment}); } @@ -160,12 +173,12 @@ private final List patterns = new ArrayList<>(); - protected boolean processLine(String currentLine, FileContext fileContext) throws SAXException { + protected boolean processLine(String currentLine, FileContext fc) throws SAXException { for (Pattern pattern : patterns) { Matcher m = pattern.matcher(currentLine); if (m.matches()) { - log.log(Level.FINEST, "Line „{0}“ matches pattern „{1}“", new Object[]{currentLine, pattern}); - processLine(new LineContext(m), fileContext); + log.log(Level.FINEST, "Line {0}: pattern „{1}“ matches „{2}“", new Object[]{fc.lineNumber, pattern, currentLine}); + processLine(new LineContext(m), fc); return true; } }