java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 20:52:58 +0200
changeset 86 1d31d9cd28c8
parent 85 4f7028b5f2fe
child 88 9860586b3b87
permissions -rw-r--r--
in-ini: log invalid lines + skip blank lines + log line numbers
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.alt2xml.in.ini;
    19 
    20 import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
    21 import cz.frantovo.alt2xml.in.Alt2ContentHandler;
    22 import java.io.BufferedReader;
    23 import java.io.IOException;
    24 import java.io.InputStreamReader;
    25 import java.util.ArrayList;
    26 import java.util.List;
    27 import java.util.logging.Level;
    28 import java.util.logging.Logger;
    29 import java.util.regex.Matcher;
    30 import java.util.regex.Pattern;
    31 import org.xml.sax.InputSource;
    32 import org.xml.sax.SAXException;
    33 
    34 /**
    35  *
    36  * @author Ing. František Kučera (frantovo.cz)
    37  */
    38 public class Reader extends AbstractAlt2XmlReader {
    39 
    40 	public static final String ROOT_ELEMENT = "ini";
    41 	private static final Logger log = Logger.getLogger(Reader.class.getName());
    42 
    43 	@Override
    44 	public void parse(InputSource input) throws IOException, SAXException {
    45 		outputStart();
    46 
    47 		try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
    48 			FileContext fc = new FileContext(contentHandler);
    49 			for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
    50 				fc.lineNumber++;
    51 				boolean lineProcessed = false;
    52 				for (LINE_TYPE lineType : LINE_TYPE.values()) {
    53 					lineProcessed = lineType.processLine(currentLine, fc);
    54 					if (lineProcessed) {
    55 						break;
    56 					}
    57 				}
    58 				if (!lineProcessed) {
    59 					log.log(Level.SEVERE, "Invalid line in INI file: {0}", currentLine);
    60 				}
    61 			}
    62 			fc.outputEndSection(fc.lastSection);
    63 
    64 		}
    65 
    66 		outputEnd();
    67 	}
    68 
    69 	private void outputStart() throws SAXException {
    70 		contentHandler.startDocument();
    71 		contentHandler.lineBreak();
    72 		contentHandler.startElement(null, null, ROOT_ELEMENT, null);
    73 		contentHandler.lineBreak();
    74 	}
    75 
    76 	private void outputEnd() throws SAXException {
    77 		contentHandler.endElement(null, null, "ini");
    78 		contentHandler.lineBreak();
    79 		contentHandler.endDocument();
    80 	}
    81 
    82 	private static class FileContext {
    83 
    84 		private final Alt2ContentHandler contentHandler;
    85 		private String lastSection;
    86 		private int lineNumber;
    87 
    88 		public FileContext(Alt2ContentHandler contentHandler) {
    89 			this.contentHandler = contentHandler;
    90 		}
    91 
    92 		protected void outputStartSection(String name) throws SAXException {
    93 			contentHandler.indentation(1);
    94 			contentHandler.startElement(null, null, name, null);
    95 			contentHandler.lineBreak();
    96 		}
    97 
    98 		protected void outputEndSection(String name) throws SAXException {
    99 			if (name != null) {
   100 				contentHandler.indentation(1);
   101 				contentHandler.endElement(null, null, name);
   102 				contentHandler.lineBreak();
   103 			}
   104 		}
   105 	}
   106 
   107 	private static class LineContext {
   108 
   109 		private final Matcher matcher;
   110 
   111 		public LineContext(Matcher matcher) {
   112 			this.matcher = matcher;
   113 		}
   114 	}
   115 
   116 	private enum LINE_TYPE {
   117 
   118 		BLANK_LINE("\\s*") {
   119 					@Override
   120 					public void processLine(LineContext lc, FileContext fc) throws SAXException {
   121 						log.log(Level.FINEST, "Line {0}: skipping blank line", fc.lineNumber);
   122 					}
   123 				},
   124 		COMMENT("\\s*(;|#)\\s*(?<comment>.*)") {
   125 					@Override
   126 					public void processLine(LineContext lc, FileContext fc) throws SAXException {
   127 						// TODO: comment → LexicalHandler
   128 						log.log(Level.FINER, "Line {0}: comment: {1}", new Object[]{fc.lineNumber, lc.matcher.group("comment")});
   129 					}
   130 
   131 				},
   132 		SECTION("\\s*\\[\\s*(?<name>[^\\s\\]]+)\\s*\\]\\s*") {
   133 					@Override
   134 					public void processLine(LineContext lc, FileContext fc) throws SAXException {
   135 						// TODO: escaping for section names with spaces
   136 						String name = lc.matcher.group("name");
   137 						fc.outputEndSection(fc.lastSection);
   138 						fc.outputStartSection(name);
   139 						fc.lastSection = name;
   140 					}
   141 
   142 				},
   143 		ENTRY(
   144 				"\\s*(?<key>[^=\\s]+)\\s*=\\s*\"(?<value>[^']+)\"\\s*((;|#)\\s*(?<comment>.*)){0,1}", // quoted value → include spaces + might have comment
   145 				"\\s*(?<key>[^=\\s]+)\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*)){0,1}", // apostrophed value → include spaces + might have comment
   146 				"\\s*(?<key>[^=\\s]+)\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces + no comments
   147 		) {
   148 					@Override
   149 					public void processLine(LineContext lc, FileContext fc) throws SAXException {
   150 						String key = lc.matcher.group("key");
   151 						String value = lc.matcher.group("value");
   152 
   153 						if (lc.matcher.groupCount() > 2) {
   154 							String comment = lc.matcher.group("comment");
   155 							// TODO: comment → LexicalHandler
   156 							log.log(Level.FINER, "Line {0}: comment for entry „{1}“ is: {2}", new Object[]{fc.lineNumber, key, comment});
   157 
   158 						}
   159 
   160 						fc.contentHandler.indentation(fc.lastSection == null ? 1 : 2);
   161 						fc.contentHandler.textElement(value, null, null, key, null);
   162 						fc.contentHandler.lineBreak();
   163 
   164 					}
   165 
   166 				},;
   167 
   168 		private LINE_TYPE(String... patterns) {
   169 			for (String pattern : patterns) {
   170 				this.patterns.add(Pattern.compile(pattern));
   171 			}
   172 		}
   173 
   174 		private final List<Pattern> patterns = new ArrayList<>();
   175 
   176 		protected boolean processLine(String currentLine, FileContext fc) throws SAXException {
   177 			for (Pattern pattern : patterns) {
   178 				Matcher m = pattern.matcher(currentLine);
   179 				if (m.matches()) {
   180 					log.log(Level.FINEST, "Line {0}: pattern „{1}“ matches „{2}“", new Object[]{fc.lineNumber, pattern, currentLine});
   181 					processLine(new LineContext(m), fc);
   182 					return true;
   183 				}
   184 			}
   185 			return false;
   186 		}
   187 
   188 		public abstract void processLine(LineContext lc, FileContext fc) throws SAXException;
   189 	}
   190 
   191 }