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@2
|
34 |
|
franta-hg@2
|
35 |
/**
|
franta-hg@2
|
36 |
*
|
franta-hg@17
|
37 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@2
|
38 |
*/
|
franta-hg@17
|
39 |
public class Reader extends AbstractAlt2XmlReader {
|
franta-hg@13
|
40 |
|
franta-hg@77
|
41 |
public static final String ROOT_ELEMENT = "ini";
|
franta-hg@77
|
42 |
private static final Logger log = Logger.getLogger(Reader.class.getName());
|
franta-hg@77
|
43 |
|
franta-hg@2
|
44 |
@Override
|
franta-hg@6
|
45 |
public void parse(InputSource input) throws IOException, SAXException {
|
franta-hg@59
|
46 |
outputStart();
|
franta-hg@77
|
47 |
|
franta-hg@77
|
48 |
try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
|
franta-hg@78
|
49 |
FileContext fc = new FileContext(contentHandler);
|
franta-hg@77
|
50 |
for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
|
franta-hg@86
|
51 |
fc.lineNumber++;
|
franta-hg@86
|
52 |
boolean lineProcessed = false;
|
franta-hg@77
|
53 |
for (LINE_TYPE lineType : LINE_TYPE.values()) {
|
franta-hg@86
|
54 |
lineProcessed = lineType.processLine(currentLine, fc);
|
franta-hg@77
|
55 |
if (lineProcessed) {
|
franta-hg@77
|
56 |
break;
|
franta-hg@77
|
57 |
}
|
franta-hg@77
|
58 |
}
|
franta-hg@86
|
59 |
if (!lineProcessed) {
|
franta-hg@86
|
60 |
log.log(Level.SEVERE, "Invalid line in INI file: {0}", currentLine);
|
franta-hg@86
|
61 |
}
|
franta-hg@77
|
62 |
}
|
franta-hg@78
|
63 |
fc.outputEndSection(fc.lastSection);
|
franta-hg@78
|
64 |
|
franta-hg@77
|
65 |
}
|
franta-hg@77
|
66 |
|
franta-hg@59
|
67 |
outputEnd();
|
franta-hg@59
|
68 |
}
|
franta-hg@59
|
69 |
|
franta-hg@59
|
70 |
private void outputStart() throws SAXException {
|
franta-hg@21
|
71 |
contentHandler.startDocument();
|
franta-hg@76
|
72 |
contentHandler.lineBreak();
|
franta-hg@77
|
73 |
contentHandler.startElement(null, null, ROOT_ELEMENT, null);
|
franta-hg@75
|
74 |
contentHandler.lineBreak();
|
franta-hg@59
|
75 |
}
|
franta-hg@59
|
76 |
|
franta-hg@59
|
77 |
private void outputEnd() throws SAXException {
|
franta-hg@76
|
78 |
contentHandler.endElement(null, null, "ini");
|
franta-hg@75
|
79 |
contentHandler.lineBreak();
|
franta-hg@21
|
80 |
contentHandler.endDocument();
|
franta-hg@6
|
81 |
}
|
franta-hg@76
|
82 |
|
franta-hg@77
|
83 |
private static class FileContext {
|
franta-hg@77
|
84 |
|
franta-hg@77
|
85 |
private final Alt2ContentHandler contentHandler;
|
franta-hg@78
|
86 |
private String lastSection;
|
franta-hg@86
|
87 |
private int lineNumber;
|
franta-hg@77
|
88 |
|
franta-hg@77
|
89 |
public FileContext(Alt2ContentHandler contentHandler) {
|
franta-hg@77
|
90 |
this.contentHandler = contentHandler;
|
franta-hg@77
|
91 |
}
|
franta-hg@78
|
92 |
|
franta-hg@78
|
93 |
protected void outputStartSection(String name) throws SAXException {
|
franta-hg@78
|
94 |
contentHandler.indentation(1);
|
franta-hg@78
|
95 |
contentHandler.startElement(null, null, name, null);
|
franta-hg@78
|
96 |
contentHandler.lineBreak();
|
franta-hg@78
|
97 |
}
|
franta-hg@78
|
98 |
|
franta-hg@78
|
99 |
protected void outputEndSection(String name) throws SAXException {
|
franta-hg@78
|
100 |
if (name != null) {
|
franta-hg@78
|
101 |
contentHandler.indentation(1);
|
franta-hg@78
|
102 |
contentHandler.endElement(null, null, name);
|
franta-hg@78
|
103 |
contentHandler.lineBreak();
|
franta-hg@78
|
104 |
}
|
franta-hg@78
|
105 |
}
|
franta-hg@77
|
106 |
}
|
franta-hg@77
|
107 |
|
franta-hg@89
|
108 |
private static String encodeXmlName(String originalName, int lineNumber) {
|
franta-hg@89
|
109 |
String encodedName = Functions.encodeXmlName(originalName);
|
franta-hg@89
|
110 |
if (!encodedName.equals(originalName)) {
|
franta-hg@89
|
111 |
log.log(Level.FINE, "Line {0}: name „{1} was encoded to „{2}““", new Object[]{lineNumber, originalName, encodedName});
|
franta-hg@89
|
112 |
}
|
franta-hg@89
|
113 |
return encodedName;
|
franta-hg@89
|
114 |
}
|
franta-hg@89
|
115 |
|
franta-hg@77
|
116 |
private static class LineContext {
|
franta-hg@77
|
117 |
|
franta-hg@77
|
118 |
private final Matcher matcher;
|
franta-hg@77
|
119 |
|
franta-hg@81
|
120 |
public LineContext(Matcher matcher) {
|
franta-hg@77
|
121 |
this.matcher = matcher;
|
franta-hg@77
|
122 |
}
|
franta-hg@77
|
123 |
}
|
franta-hg@77
|
124 |
|
franta-hg@77
|
125 |
private enum LINE_TYPE {
|
franta-hg@77
|
126 |
|
franta-hg@86
|
127 |
BLANK_LINE("\\s*") {
|
franta-hg@86
|
128 |
@Override
|
franta-hg@86
|
129 |
public void processLine(LineContext lc, FileContext fc) throws SAXException {
|
franta-hg@86
|
130 |
log.log(Level.FINEST, "Line {0}: skipping blank line", fc.lineNumber);
|
franta-hg@86
|
131 |
}
|
franta-hg@86
|
132 |
},
|
franta-hg@83
|
133 |
COMMENT("\\s*(;|#)\\s*(?<comment>.*)") {
|
franta-hg@77
|
134 |
@Override
|
franta-hg@78
|
135 |
public void processLine(LineContext lc, FileContext fc) throws SAXException {
|
franta-hg@82
|
136 |
// TODO: comment → LexicalHandler
|
franta-hg@86
|
137 |
log.log(Level.FINER, "Line {0}: comment: {1}", new Object[]{fc.lineNumber, lc.matcher.group("comment")});
|
franta-hg@77
|
138 |
}
|
franta-hg@77
|
139 |
|
franta-hg@77
|
140 |
},
|
franta-hg@88
|
141 |
SECTION("\\s*\\[\\s*(?<name>[^\\]\\]]+)\\s*\\]\\s*") {
|
franta-hg@77
|
142 |
@Override
|
franta-hg@78
|
143 |
public void processLine(LineContext lc, FileContext fc) throws SAXException {
|
franta-hg@89
|
144 |
String name = encodeXmlName(lc.matcher.group("name"), fc.lineNumber);
|
franta-hg@78
|
145 |
fc.outputEndSection(fc.lastSection);
|
franta-hg@89
|
146 |
fc.outputStartSection(name);
|
franta-hg@89
|
147 |
fc.lastSection = name;
|
franta-hg@77
|
148 |
}
|
franta-hg@77
|
149 |
|
franta-hg@77
|
150 |
},
|
franta-hg@81
|
151 |
ENTRY(
|
franta-hg@91
|
152 |
"\\s*(?<key>[^=\\s]+)\\s*=\\s*\"(?<value>[^']+)\"\\s*((;|#)\\s*(?<comment>.*)){0,1}", // quoted value → include spaces + might have comment
|
franta-hg@91
|
153 |
"\\s*(?<key>[^=\\s]+)\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*)){0,1}", // apostrophed value → include spaces + might have comment
|
franta-hg@91
|
154 |
"\\s*(?<key>[^=\\s]+)\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces + no comments
|
franta-hg@81
|
155 |
) {
|
franta-hg@77
|
156 |
@Override
|
franta-hg@78
|
157 |
public void processLine(LineContext lc, FileContext fc) throws SAXException {
|
franta-hg@89
|
158 |
String key = encodeXmlName(lc.matcher.group("key"), fc.lineNumber);
|
franta-hg@83
|
159 |
String value = lc.matcher.group("value");
|
franta-hg@78
|
160 |
|
franta-hg@84
|
161 |
if (lc.matcher.groupCount() > 2) {
|
franta-hg@83
|
162 |
String comment = lc.matcher.group("comment");
|
franta-hg@82
|
163 |
// TODO: comment → LexicalHandler
|
franta-hg@86
|
164 |
log.log(Level.FINER, "Line {0}: comment for entry „{1}“ is: {2}", new Object[]{fc.lineNumber, key, comment});
|
franta-hg@82
|
165 |
}
|
franta-hg@82
|
166 |
|
franta-hg@85
|
167 |
fc.contentHandler.indentation(fc.lastSection == null ? 1 : 2);
|
franta-hg@78
|
168 |
fc.contentHandler.textElement(value, null, null, key, null);
|
franta-hg@78
|
169 |
fc.contentHandler.lineBreak();
|
franta-hg@78
|
170 |
|
franta-hg@77
|
171 |
}
|
franta-hg@77
|
172 |
|
franta-hg@77
|
173 |
},;
|
franta-hg@77
|
174 |
|
franta-hg@81
|
175 |
private LINE_TYPE(String... patterns) {
|
franta-hg@81
|
176 |
for (String pattern : patterns) {
|
franta-hg@81
|
177 |
this.patterns.add(Pattern.compile(pattern));
|
franta-hg@81
|
178 |
}
|
franta-hg@77
|
179 |
}
|
franta-hg@77
|
180 |
|
franta-hg@81
|
181 |
private final List<Pattern> patterns = new ArrayList<>();
|
franta-hg@77
|
182 |
|
franta-hg@86
|
183 |
protected boolean processLine(String currentLine, FileContext fc) throws SAXException {
|
franta-hg@81
|
184 |
for (Pattern pattern : patterns) {
|
franta-hg@81
|
185 |
Matcher m = pattern.matcher(currentLine);
|
franta-hg@81
|
186 |
if (m.matches()) {
|
franta-hg@86
|
187 |
log.log(Level.FINEST, "Line {0}: pattern „{1}“ matches „{2}“", new Object[]{fc.lineNumber, pattern, currentLine});
|
franta-hg@86
|
188 |
processLine(new LineContext(m), fc);
|
franta-hg@81
|
189 |
return true;
|
franta-hg@81
|
190 |
}
|
franta-hg@77
|
191 |
}
|
franta-hg@81
|
192 |
return false;
|
franta-hg@77
|
193 |
}
|
franta-hg@77
|
194 |
|
franta-hg@78
|
195 |
public abstract void processLine(LineContext lc, FileContext fc) throws SAXException;
|
franta-hg@77
|
196 |
}
|
franta-hg@77
|
197 |
|
franta-hg@2
|
198 |
}
|