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, either version 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
18 package cz.frantovo.alt2xml.in.ini;
20 import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
21 import cz.frantovo.alt2xml.in.Alt2ContentHandler;
22 import cz.frantovo.alt2xml.in.Functions;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.helpers.AttributesImpl;
38 * @author Ing. František Kučera (frantovo.cz)
40 public class Reader extends AbstractAlt2XmlReader {
42 public static final String ROOT_ELEMENT = "ini";
43 private static final Logger log = Logger.getLogger(Reader.class.getName());
46 public void parse(InputSource input) throws IOException, SAXException {
49 try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
50 FileContext fc = new FileContext(contentHandler);
51 for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
53 boolean lineProcessed = false;
54 for (LINE_TYPE lineType : LINE_TYPE.values()) {
55 lineProcessed = lineType.processLine(currentLine, fc);
61 log.log(Level.SEVERE, "Invalid line in INI file: {0}", currentLine);
64 fc.outputEndSection(fc.lastSection);
71 private void outputStart() throws SAXException {
72 contentHandler.startDocument();
73 contentHandler.lineBreak();
74 contentHandler.startElement(null, null, ROOT_ELEMENT, null);
75 contentHandler.lineBreak();
78 private void outputEnd() throws SAXException {
79 contentHandler.endElement(null, null, "ini");
80 contentHandler.lineBreak();
81 contentHandler.endDocument();
84 private static class FileContext {
86 private final Alt2ContentHandler contentHandler;
87 private String lastSection;
88 private int lineNumber;
90 public FileContext(Alt2ContentHandler contentHandler) {
91 this.contentHandler = contentHandler;
94 protected void outputStartSection(String name) throws SAXException {
95 contentHandler.indentation(1);
96 contentHandler.startElement(null, null, name, null);
97 contentHandler.lineBreak();
100 protected void outputEndSection(String name) throws SAXException {
102 contentHandler.indentation(1);
103 contentHandler.endElement(null, null, name);
104 contentHandler.lineBreak();
109 private static String encodeXmlName(String originalName, int lineNumber) {
110 String encodedName = Functions.encodeXmlName(originalName);
111 if (!encodedName.equals(originalName)) {
112 log.log(Level.FINE, "Line {0}: name „{1} was encoded to „{2}““", new Object[]{lineNumber, originalName, encodedName});
117 private static class LineContext {
119 private final Matcher matcher;
121 public LineContext(Matcher matcher) {
122 this.matcher = matcher;
126 private enum LINE_TYPE {
130 public void processLine(LineContext lc, FileContext fc) throws SAXException {
131 log.log(Level.FINEST, "Line {0}: skipping blank line", fc.lineNumber);
134 COMMENT("\\s*(;|#)\\s*(?<comment>.*)") {
136 public void processLine(LineContext lc, FileContext fc) throws SAXException {
137 // TODO: comment → LexicalHandler
138 log.log(Level.FINER, "Line {0}: comment: {1}", new Object[]{fc.lineNumber, lc.matcher.group("comment")});
142 SECTION("\\s*\\[\\s*(?<name>[^\\]\\]]+)\\s*\\]\\s*") {
144 public void processLine(LineContext lc, FileContext fc) throws SAXException {
145 String name = encodeXmlName(lc.matcher.group("name"), fc.lineNumber);
146 fc.outputEndSection(fc.lastSection);
147 fc.outputStartSection(name);
148 fc.lastSection = name;
153 "\\s*(?<key>[^=\\s\\]]+)(\\[(?<subkey>[^\\]]+)\\]){0,1}\\s*=\\s*\"(?<value>[^']+)\"\\s*((;|#)\\s*(?<comment>.*)){0,1}", // quoted value → include spaces + might have comment
154 "\\s*(?<key>[^=\\s\\]]+)(\\[(?<subkey>[^\\]]+)\\]){0,1}\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*)){0,1}", // apostrophed value → include spaces + might have comment
155 "\\s*(?<key>[^=\\s\\]]+)(\\[(?<subkey>[^\\]]+)\\]){0,1}\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces + no comments
158 public void processLine(LineContext lc, FileContext fc) throws SAXException {
159 String key = encodeXmlName(lc.matcher.group("key"), fc.lineNumber);
160 String value = lc.matcher.group("value");
162 if (lc.matcher.groupCount() > 4) {
163 String comment = lc.matcher.group("comment");
164 // TODO: comment → LexicalHandler
165 log.log(Level.FINER, "Line {0}: comment for entry „{1}“ is: {2}", new Object[]{fc.lineNumber, key, comment});
168 AttributesImpl attributes = null;
169 String subkey = lc.matcher.group("subkey");
170 if (subkey != null) {
171 attributes = new AttributesImpl();
172 attributes.addAttribute(null, "sub", "sub", "xs:string", subkey);
175 fc.contentHandler.indentation(fc.lastSection == null ? 1 : 2);
176 fc.contentHandler.textElement(value, null, null, key, attributes);
177 fc.contentHandler.lineBreak();
183 private LINE_TYPE(String... patterns) {
184 for (String pattern : patterns) {
185 this.patterns.add(Pattern.compile(pattern));
189 private final List<Pattern> patterns = new ArrayList<>();
191 protected boolean processLine(String currentLine, FileContext fc) throws SAXException {
192 for (Pattern pattern : patterns) {
193 Matcher m = pattern.matcher(currentLine);
195 log.log(Level.FINEST, "Line {0}: pattern „{1}“ matches „{2}“", new Object[]{fc.lineNumber, pattern, currentLine});
196 processLine(new LineContext(m), fc);
203 public abstract void processLine(LineContext lc, FileContext fc) throws SAXException;