3 * Copyright © 2014 František Kučera (frantovo.cz)
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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package cz.frantovo.alt2xml.in.ini;
19 import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
20 import cz.frantovo.alt2xml.in.Alt2ContentHandler;
21 import cz.frantovo.alt2xml.in.Functions;
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 import org.xml.sax.helpers.AttributesImpl;
36 * Reads INI files with sections and entries.
38 * <pre>; this is comment
39 *random=value outside of any groups
46 *; entry starting/ending with whitespace
47 *white=" spaces everywhere " ; might have comment
48 *alternative=' spaces everywhere ' ; same
50 *; entries with subkeys:
54 *# alternative way to comment
57 *yes=there might be spaces in names
58 *because=they are encoded before putting into XML element names
61 * @author Ing. František Kučera (frantovo.cz)
63 public class Reader extends AbstractAlt2XmlReader {
65 public static final String ROOT_ELEMENT = "ini";
66 private static final Logger log = Logger.getLogger(Reader.class.getName());
69 public void parse(InputSource input) throws IOException, SAXException {
72 try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
73 FileContext fc = new FileContext(contentHandler);
74 for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
76 boolean lineProcessed = false;
77 for (LINE_TYPE lineType : LINE_TYPE.values()) {
78 lineProcessed = lineType.processLine(currentLine, fc);
84 log.log(Level.SEVERE, "Invalid line in INI file: {0}", currentLine);
87 fc.outputEndSection(fc.lastSection);
94 private void outputStart() throws SAXException {
95 contentHandler.startDocument();
96 contentHandler.lineBreak();
97 contentHandler.startElement(null, null, ROOT_ELEMENT, null);
98 contentHandler.lineBreak();
101 private void outputEnd() throws SAXException {
102 contentHandler.endElement(null, null, ROOT_ELEMENT);
103 contentHandler.lineBreak();
104 contentHandler.endDocument();
107 private static class FileContext {
109 private final Alt2ContentHandler contentHandler;
110 private String lastSection;
111 private int lineNumber;
113 public FileContext(Alt2ContentHandler contentHandler) {
114 this.contentHandler = contentHandler;
117 protected void outputStartSection(String name) throws SAXException {
118 contentHandler.indentation(1);
119 contentHandler.startElement(null, null, name, null);
120 contentHandler.lineBreak();
123 protected void outputEndSection(String name) throws SAXException {
125 contentHandler.indentation(1);
126 contentHandler.endElement(null, null, name);
127 contentHandler.lineBreak();
132 private static String encodeXmlName(String originalName, int lineNumber) {
133 String encodedName = Functions.encodeXmlName(originalName);
134 if (!encodedName.equals(originalName)) {
135 log.log(Level.FINE, "Line {0}: name „{1} was encoded to „{2}““", new Object[]{lineNumber, originalName, encodedName});
140 private static class LineContext {
142 private final Matcher matcher;
144 public LineContext(Matcher matcher) {
145 this.matcher = matcher;
149 private enum LINE_TYPE {
153 public void processLine(LineContext lc, FileContext fc) throws SAXException {
154 log.log(Level.FINEST, "Line {0}: skipping blank line", fc.lineNumber);
157 COMMENT("\\s*(;|#)\\s*(?<comment>.*)") {
159 public void processLine(LineContext lc, FileContext fc) throws SAXException {
160 // TODO: comment → LexicalHandler
161 log.log(Level.FINER, "Line {0}: comment: {1}", new Object[]{fc.lineNumber, lc.matcher.group("comment")});
165 SECTION("\\s*\\[\\s*(?<name>[^\\]]+)\\s*\\]\\s*") {
167 public void processLine(LineContext lc, FileContext fc) throws SAXException {
168 String name = encodeXmlName(lc.matcher.group("name"), fc.lineNumber);
169 fc.outputEndSection(fc.lastSection);
170 fc.outputStartSection(name);
171 fc.lastSection = name;
176 "\\s*(?<key>[^=\\]]+?[^=\\s\\]]*)(\\[(?<subkey>[^\\]]+)\\])?\\s*=\\s*\"(?<value>[^']+)\"\\s*((;|#)\\s*(?<comment>.*))?", // quoted value → include spaces + might have comment
177 "\\s*(?<key>[^=\\]]+?[^=\\s\\]]*)(\\[(?<subkey>[^\\]]+)\\])?\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*))?", // apostrophed value → include spaces + might have comment
178 "\\s*(?<key>[^=\\]]+?[^=\\s\\]]*)(\\[(?<subkey>[^\\]]+)\\])?\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces + no comments
181 public void processLine(LineContext lc, FileContext fc) throws SAXException {
182 String key = encodeXmlName(lc.matcher.group("key"), fc.lineNumber);
183 String value = lc.matcher.group("value");
185 if (lc.matcher.groupCount() > 4) {
186 String comment = lc.matcher.group("comment");
187 // TODO: comment → LexicalHandler
188 log.log(Level.FINER, "Line {0}: comment for entry „{1}“ is: {2}", new Object[]{fc.lineNumber, key, comment});
191 AttributesImpl attributes = null;
192 String subkey = lc.matcher.group("subkey");
193 if (subkey != null) {
194 attributes = new AttributesImpl();
195 attributes.addAttribute(null, "sub", "sub", "xs:string", subkey);
198 fc.contentHandler.indentation(fc.lastSection == null ? 1 : 2);
199 fc.contentHandler.textElement(value, null, null, key, attributes);
200 fc.contentHandler.lineBreak();
207 * @param patterns regular expression (or expressions) that describes this line type
209 private LINE_TYPE(String... patterns) {
210 for (String pattern : patterns) {
211 this.patterns.add(Pattern.compile(pattern));
215 private final List<Pattern> patterns = new ArrayList<>();
219 * @param currentLine input line to be parsed
221 * @return whether line matches and was thus processed
222 * @throws SAXException
224 protected boolean processLine(String currentLine, FileContext fc) throws SAXException {
225 for (Pattern pattern : patterns) {
226 Matcher m = pattern.matcher(currentLine);
228 log.log(Level.FINEST, "Line {0}: pattern „{1}“ matches „{2}“", new Object[]{fc.lineNumber, pattern, currentLine});
229 processLine(new LineContext(m), fc);
236 public abstract void processLine(LineContext lc, FileContext fc) throws SAXException;