java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:56:03 +0200
changeset 111 e4900596abdb
parent 102 a7f7b9094cc3
child 113 871c05ca7118
permissions -rw-r--r--
fix license version: GNU GPLv3
franta-hg@11
     1
/**
franta-hg@11
     2
 * Alt2XML
franta-hg@11
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@11
     4
 *
franta-hg@11
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@11
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@111
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@11
     8
 *
franta-hg@11
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@11
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@11
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@11
    12
 * GNU General Public License for more details.
franta-hg@11
    13
 *
franta-hg@11
    14
 * You should have received a copy of the GNU General Public License
franta-hg@11
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@11
    16
 */
franta-hg@76
    17
package cz.frantovo.alt2xml.in.ini;
franta-hg@2
    18
franta-hg@17
    19
import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
franta-hg@77
    20
import cz.frantovo.alt2xml.in.Alt2ContentHandler;
franta-hg@89
    21
import cz.frantovo.alt2xml.in.Functions;
franta-hg@77
    22
import java.io.BufferedReader;
franta-hg@2
    23
import java.io.IOException;
franta-hg@77
    24
import java.io.InputStreamReader;
franta-hg@81
    25
import java.util.ArrayList;
franta-hg@81
    26
import java.util.List;
franta-hg@77
    27
import java.util.logging.Level;
franta-hg@77
    28
import java.util.logging.Logger;
franta-hg@77
    29
import java.util.regex.Matcher;
franta-hg@77
    30
import java.util.regex.Pattern;
franta-hg@2
    31
import org.xml.sax.InputSource;
franta-hg@2
    32
import org.xml.sax.SAXException;
franta-hg@92
    33
import org.xml.sax.helpers.AttributesImpl;
franta-hg@2
    34
franta-hg@2
    35
/**
franta-hg@95
    36
 * Reads INI files with sections and entries.
franta-hg@95
    37
 * Example:
franta-hg@95
    38
 * <pre>; this is comment
franta-hg@95
    39
 *random=value outside of any groups
franta-hg@95
    40
 *
franta-hg@95
    41
 *[some_section]
franta-hg@95
    42
 *
franta-hg@95
    43
 *; simple entry:
franta-hg@95
    44
 *key=value
franta-hg@95
    45
 *
franta-hg@95
    46
 *; entry starting/ending with whitespace
franta-hg@95
    47
 *white="  spaces everywhere  " ; might have comment
franta-hg@95
    48
 *alternative='  spaces everywhere  ' ; same
franta-hg@95
    49
 *
franta-hg@95
    50
 *; entries with subkeys:
franta-hg@95
    51
 *key[subkey_a]=value
franta-hg@95
    52
 *key[subkey_b]=value
franta-hg@95
    53
 *
franta-hg@95
    54
 *# alternative way to comment
franta-hg@95
    55
 *
franta-hg@95
    56
 *[another secion]
franta-hg@95
    57
 *yes=there might be spaces in names
franta-hg@95
    58
 *because=they are encoded before putting into XML element names
franta-hg@95
    59
 * </pre>
franta-hg@2
    60
 *
franta-hg@17
    61
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@2
    62
 */
franta-hg@17
    63
public class Reader extends AbstractAlt2XmlReader {
franta-hg@13
    64
franta-hg@77
    65
	public static final String ROOT_ELEMENT = "ini";
franta-hg@77
    66
	private static final Logger log = Logger.getLogger(Reader.class.getName());
franta-hg@77
    67
franta-hg@2
    68
	@Override
franta-hg@6
    69
	public void parse(InputSource input) throws IOException, SAXException {
franta-hg@59
    70
		outputStart();
franta-hg@77
    71
franta-hg@77
    72
		try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
franta-hg@78
    73
			FileContext fc = new FileContext(contentHandler);
franta-hg@77
    74
			for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
franta-hg@86
    75
				fc.lineNumber++;
franta-hg@86
    76
				boolean lineProcessed = false;
franta-hg@77
    77
				for (LINE_TYPE lineType : LINE_TYPE.values()) {
franta-hg@86
    78
					lineProcessed = lineType.processLine(currentLine, fc);
franta-hg@77
    79
					if (lineProcessed) {
franta-hg@77
    80
						break;
franta-hg@77
    81
					}
franta-hg@77
    82
				}
franta-hg@86
    83
				if (!lineProcessed) {
franta-hg@86
    84
					log.log(Level.SEVERE, "Invalid line in INI file: {0}", currentLine);
franta-hg@86
    85
				}
franta-hg@77
    86
			}
franta-hg@78
    87
			fc.outputEndSection(fc.lastSection);
franta-hg@78
    88
franta-hg@77
    89
		}
franta-hg@77
    90
franta-hg@59
    91
		outputEnd();
franta-hg@59
    92
	}
franta-hg@59
    93
franta-hg@59
    94
	private void outputStart() throws SAXException {
franta-hg@21
    95
		contentHandler.startDocument();
franta-hg@76
    96
		contentHandler.lineBreak();
franta-hg@77
    97
		contentHandler.startElement(null, null, ROOT_ELEMENT, null);
franta-hg@75
    98
		contentHandler.lineBreak();
franta-hg@59
    99
	}
franta-hg@59
   100
franta-hg@59
   101
	private void outputEnd() throws SAXException {
franta-hg@102
   102
		contentHandler.endElement(null, null, ROOT_ELEMENT);
franta-hg@75
   103
		contentHandler.lineBreak();
franta-hg@21
   104
		contentHandler.endDocument();
franta-hg@6
   105
	}
franta-hg@76
   106
franta-hg@77
   107
	private static class FileContext {
franta-hg@77
   108
franta-hg@77
   109
		private final Alt2ContentHandler contentHandler;
franta-hg@78
   110
		private String lastSection;
franta-hg@86
   111
		private int lineNumber;
franta-hg@77
   112
franta-hg@77
   113
		public FileContext(Alt2ContentHandler contentHandler) {
franta-hg@77
   114
			this.contentHandler = contentHandler;
franta-hg@77
   115
		}
franta-hg@78
   116
franta-hg@78
   117
		protected void outputStartSection(String name) throws SAXException {
franta-hg@78
   118
			contentHandler.indentation(1);
franta-hg@78
   119
			contentHandler.startElement(null, null, name, null);
franta-hg@78
   120
			contentHandler.lineBreak();
franta-hg@78
   121
		}
franta-hg@78
   122
franta-hg@78
   123
		protected void outputEndSection(String name) throws SAXException {
franta-hg@78
   124
			if (name != null) {
franta-hg@78
   125
				contentHandler.indentation(1);
franta-hg@78
   126
				contentHandler.endElement(null, null, name);
franta-hg@78
   127
				contentHandler.lineBreak();
franta-hg@78
   128
			}
franta-hg@78
   129
		}
franta-hg@77
   130
	}
franta-hg@77
   131
franta-hg@89
   132
	private static String encodeXmlName(String originalName, int lineNumber) {
franta-hg@89
   133
		String encodedName = Functions.encodeXmlName(originalName);
franta-hg@89
   134
		if (!encodedName.equals(originalName)) {
franta-hg@89
   135
			log.log(Level.FINE, "Line {0}: name „{1} was encoded to „{2}““", new Object[]{lineNumber, originalName, encodedName});
franta-hg@89
   136
		}
franta-hg@89
   137
		return encodedName;
franta-hg@89
   138
	}
franta-hg@89
   139
franta-hg@77
   140
	private static class LineContext {
franta-hg@77
   141
franta-hg@77
   142
		private final Matcher matcher;
franta-hg@77
   143
franta-hg@81
   144
		public LineContext(Matcher matcher) {
franta-hg@77
   145
			this.matcher = matcher;
franta-hg@77
   146
		}
franta-hg@77
   147
	}
franta-hg@77
   148
franta-hg@77
   149
	private enum LINE_TYPE {
franta-hg@77
   150
franta-hg@86
   151
		BLANK_LINE("\\s*") {
franta-hg@86
   152
					@Override
franta-hg@86
   153
					public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@86
   154
						log.log(Level.FINEST, "Line {0}: skipping blank line", fc.lineNumber);
franta-hg@86
   155
					}
franta-hg@86
   156
				},
franta-hg@83
   157
		COMMENT("\\s*(;|#)\\s*(?<comment>.*)") {
franta-hg@77
   158
					@Override
franta-hg@78
   159
					public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@82
   160
						// TODO: comment → LexicalHandler
franta-hg@86
   161
						log.log(Level.FINER, "Line {0}: comment: {1}", new Object[]{fc.lineNumber, lc.matcher.group("comment")});
franta-hg@77
   162
					}
franta-hg@77
   163
franta-hg@77
   164
				},
franta-hg@95
   165
		SECTION("\\s*\\[\\s*(?<name>[^\\]]+)\\s*\\]\\s*") {
franta-hg@77
   166
					@Override
franta-hg@78
   167
					public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@89
   168
						String name = encodeXmlName(lc.matcher.group("name"), fc.lineNumber);
franta-hg@78
   169
						fc.outputEndSection(fc.lastSection);
franta-hg@89
   170
						fc.outputStartSection(name);
franta-hg@89
   171
						fc.lastSection = name;
franta-hg@77
   172
					}
franta-hg@77
   173
franta-hg@77
   174
				},
franta-hg@81
   175
		ENTRY(
franta-hg@94
   176
				"\\s*(?<key>[^=\\]]+?[^=\\s\\]]*)(\\[(?<subkey>[^\\]]+)\\])?\\s*=\\s*\"(?<value>[^']+)\"\\s*((;|#)\\s*(?<comment>.*))?", // quoted value → include spaces + might have comment
franta-hg@94
   177
				"\\s*(?<key>[^=\\]]+?[^=\\s\\]]*)(\\[(?<subkey>[^\\]]+)\\])?\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*))?", // apostrophed value → include spaces + might have comment
franta-hg@94
   178
				"\\s*(?<key>[^=\\]]+?[^=\\s\\]]*)(\\[(?<subkey>[^\\]]+)\\])?\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces + no comments
franta-hg@81
   179
		) {
franta-hg@77
   180
					@Override
franta-hg@78
   181
					public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@89
   182
						String key = encodeXmlName(lc.matcher.group("key"), fc.lineNumber);
franta-hg@83
   183
						String value = lc.matcher.group("value");
franta-hg@78
   184
franta-hg@92
   185
						if (lc.matcher.groupCount() > 4) {
franta-hg@83
   186
							String comment = lc.matcher.group("comment");
franta-hg@82
   187
							// TODO: comment → LexicalHandler
franta-hg@86
   188
							log.log(Level.FINER, "Line {0}: comment for entry „{1}“ is: {2}", new Object[]{fc.lineNumber, key, comment});
franta-hg@82
   189
						}
franta-hg@82
   190
franta-hg@92
   191
						AttributesImpl attributes = null;
franta-hg@92
   192
						String subkey = lc.matcher.group("subkey");
franta-hg@92
   193
						if (subkey != null) {
franta-hg@92
   194
							attributes = new AttributesImpl();
franta-hg@92
   195
							attributes.addAttribute(null, "sub", "sub", "xs:string", subkey);
franta-hg@92
   196
						}
franta-hg@92
   197
franta-hg@85
   198
						fc.contentHandler.indentation(fc.lastSection == null ? 1 : 2);
franta-hg@92
   199
						fc.contentHandler.textElement(value, null, null, key, attributes);
franta-hg@78
   200
						fc.contentHandler.lineBreak();
franta-hg@78
   201
franta-hg@77
   202
					}
franta-hg@77
   203
franta-hg@77
   204
				},;
franta-hg@77
   205
franta-hg@95
   206
		/**
franta-hg@95
   207
		 * @param patterns regular expression (or expressions) that describes this line type
franta-hg@95
   208
		 */
franta-hg@81
   209
		private LINE_TYPE(String... patterns) {
franta-hg@81
   210
			for (String pattern : patterns) {
franta-hg@81
   211
				this.patterns.add(Pattern.compile(pattern));
franta-hg@81
   212
			}
franta-hg@77
   213
		}
franta-hg@77
   214
franta-hg@81
   215
		private final List<Pattern> patterns = new ArrayList<>();
franta-hg@77
   216
franta-hg@95
   217
		/**
franta-hg@95
   218
		 *
franta-hg@95
   219
		 * @param currentLine input line to be parsed
franta-hg@95
   220
		 * @param fc
franta-hg@95
   221
		 * @return whether line matches and was thus processed
franta-hg@95
   222
		 * @throws SAXException
franta-hg@95
   223
		 */
franta-hg@86
   224
		protected boolean processLine(String currentLine, FileContext fc) throws SAXException {
franta-hg@81
   225
			for (Pattern pattern : patterns) {
franta-hg@81
   226
				Matcher m = pattern.matcher(currentLine);
franta-hg@81
   227
				if (m.matches()) {
franta-hg@86
   228
					log.log(Level.FINEST, "Line {0}: pattern „{1}“ matches „{2}“", new Object[]{fc.lineNumber, pattern, currentLine});
franta-hg@86
   229
					processLine(new LineContext(m), fc);
franta-hg@81
   230
					return true;
franta-hg@81
   231
				}
franta-hg@77
   232
			}
franta-hg@81
   233
			return false;
franta-hg@77
   234
		}
franta-hg@77
   235
franta-hg@78
   236
		public abstract void processLine(LineContext lc, FileContext fc) throws SAXException;
franta-hg@77
   237
	}
franta-hg@2
   238
}