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