in-ini: log invalid lines + skip blank lines + log line numbers
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 20:52:58 +0200
changeset 861d31d9cd28c8
parent 85 4f7028b5f2fe
child 87 233907073718
in-ini: log invalid lines + skip blank lines + log line numbers
java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java
     1.1 --- a/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 20:31:05 2014 +0200
     1.2 +++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 20:52:58 2014 +0200
     1.3 @@ -47,12 +47,17 @@
     1.4  		try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
     1.5  			FileContext fc = new FileContext(contentHandler);
     1.6  			for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
     1.7 +				fc.lineNumber++;
     1.8 +				boolean lineProcessed = false;
     1.9  				for (LINE_TYPE lineType : LINE_TYPE.values()) {
    1.10 -					boolean lineProcessed = lineType.processLine(currentLine, fc);
    1.11 +					lineProcessed = lineType.processLine(currentLine, fc);
    1.12  					if (lineProcessed) {
    1.13  						break;
    1.14  					}
    1.15  				}
    1.16 +				if (!lineProcessed) {
    1.17 +					log.log(Level.SEVERE, "Invalid line in INI file: {0}", currentLine);
    1.18 +				}
    1.19  			}
    1.20  			fc.outputEndSection(fc.lastSection);
    1.21  
    1.22 @@ -78,6 +83,7 @@
    1.23  
    1.24  		private final Alt2ContentHandler contentHandler;
    1.25  		private String lastSection;
    1.26 +		private int lineNumber;
    1.27  
    1.28  		public FileContext(Alt2ContentHandler contentHandler) {
    1.29  			this.contentHandler = contentHandler;
    1.30 @@ -109,17 +115,24 @@
    1.31  
    1.32  	private enum LINE_TYPE {
    1.33  
    1.34 +		BLANK_LINE("\\s*") {
    1.35 +					@Override
    1.36 +					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.37 +						log.log(Level.FINEST, "Line {0}: skipping blank line", fc.lineNumber);
    1.38 +					}
    1.39 +				},
    1.40  		COMMENT("\\s*(;|#)\\s*(?<comment>.*)") {
    1.41  					@Override
    1.42  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.43  						// TODO: comment → LexicalHandler
    1.44 -						log.log(Level.FINER, "Comment: {0}", lc.matcher.group("comment"));
    1.45 +						log.log(Level.FINER, "Line {0}: comment: {1}", new Object[]{fc.lineNumber, lc.matcher.group("comment")});
    1.46  					}
    1.47  
    1.48  				},
    1.49  		SECTION("\\s*\\[\\s*(?<name>[^\\s\\]]+)\\s*\\]\\s*") {
    1.50  					@Override
    1.51  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.52 +						// TODO: escaping for section names with spaces
    1.53  						String name = lc.matcher.group("name");
    1.54  						fc.outputEndSection(fc.lastSection);
    1.55  						fc.outputStartSection(name);
    1.56 @@ -140,7 +153,7 @@
    1.57  						if (lc.matcher.groupCount() > 2) {
    1.58  							String comment = lc.matcher.group("comment");
    1.59  							// TODO: comment → LexicalHandler
    1.60 -							log.log(Level.FINER, "Comment for entry „{0}“ is: {1}", new Object[]{key, comment});
    1.61 +							log.log(Level.FINER, "Line {0}: comment for entry „{1}“ is: {2}", new Object[]{fc.lineNumber, key, comment});
    1.62  
    1.63  						}
    1.64  
    1.65 @@ -160,12 +173,12 @@
    1.66  
    1.67  		private final List<Pattern> patterns = new ArrayList<>();
    1.68  
    1.69 -		protected boolean processLine(String currentLine, FileContext fileContext) throws SAXException {
    1.70 +		protected boolean processLine(String currentLine, FileContext fc) throws SAXException {
    1.71  			for (Pattern pattern : patterns) {
    1.72  				Matcher m = pattern.matcher(currentLine);
    1.73  				if (m.matches()) {
    1.74 -					log.log(Level.FINEST, "Line „{0}“ matches pattern „{1}“", new Object[]{currentLine, pattern});
    1.75 -					processLine(new LineContext(m), fileContext);
    1.76 +					log.log(Level.FINEST, "Line {0}: pattern „{1}“ matches „{2}“", new Object[]{fc.lineNumber, pattern, currentLine});
    1.77 +					processLine(new LineContext(m), fc);
    1.78  					return true;
    1.79  				}
    1.80  			}