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