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