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@77
|
50 |
for (LINE_TYPE lineType : LINE_TYPE.values()) {
|
franta-hg@78
|
51 |
boolean lineProcessed = lineType.processLine(currentLine, fc);
|
franta-hg@77
|
52 |
if (lineProcessed) {
|
franta-hg@77
|
53 |
break;
|
franta-hg@77
|
54 |
}
|
franta-hg@77
|
55 |
}
|
franta-hg@77
|
56 |
}
|
franta-hg@78
|
57 |
fc.outputEndSection(fc.lastSection);
|
franta-hg@78
|
58 |
|
franta-hg@77
|
59 |
}
|
franta-hg@77
|
60 |
|
franta-hg@59
|
61 |
outputEnd();
|
franta-hg@59
|
62 |
}
|
franta-hg@59
|
63 |
|
franta-hg@59
|
64 |
private void outputStart() throws SAXException {
|
franta-hg@21
|
65 |
contentHandler.startDocument();
|
franta-hg@76
|
66 |
contentHandler.lineBreak();
|
franta-hg@77
|
67 |
contentHandler.startElement(null, null, ROOT_ELEMENT, null);
|
franta-hg@75
|
68 |
contentHandler.lineBreak();
|
franta-hg@59
|
69 |
}
|
franta-hg@59
|
70 |
|
franta-hg@59
|
71 |
private void outputEnd() throws SAXException {
|
franta-hg@76
|
72 |
contentHandler.endElement(null, null, "ini");
|
franta-hg@75
|
73 |
contentHandler.lineBreak();
|
franta-hg@21
|
74 |
contentHandler.endDocument();
|
franta-hg@6
|
75 |
}
|
franta-hg@76
|
76 |
|
franta-hg@77
|
77 |
private static class FileContext {
|
franta-hg@77
|
78 |
|
franta-hg@77
|
79 |
private final Alt2ContentHandler contentHandler;
|
franta-hg@78
|
80 |
private String lastSection;
|
franta-hg@77
|
81 |
|
franta-hg@77
|
82 |
public FileContext(Alt2ContentHandler contentHandler) {
|
franta-hg@77
|
83 |
this.contentHandler = contentHandler;
|
franta-hg@77
|
84 |
}
|
franta-hg@78
|
85 |
|
franta-hg@78
|
86 |
protected void outputStartSection(String name) throws SAXException {
|
franta-hg@78
|
87 |
contentHandler.indentation(1);
|
franta-hg@78
|
88 |
contentHandler.startElement(null, null, name, null);
|
franta-hg@78
|
89 |
contentHandler.lineBreak();
|
franta-hg@78
|
90 |
}
|
franta-hg@78
|
91 |
|
franta-hg@78
|
92 |
protected void outputEndSection(String name) throws SAXException {
|
franta-hg@78
|
93 |
if (name != null) {
|
franta-hg@78
|
94 |
contentHandler.indentation(1);
|
franta-hg@78
|
95 |
contentHandler.endElement(null, null, name);
|
franta-hg@78
|
96 |
contentHandler.lineBreak();
|
franta-hg@78
|
97 |
}
|
franta-hg@78
|
98 |
}
|
franta-hg@77
|
99 |
}
|
franta-hg@77
|
100 |
|
franta-hg@77
|
101 |
private static class LineContext {
|
franta-hg@77
|
102 |
|
franta-hg@77
|
103 |
private final Matcher matcher;
|
franta-hg@77
|
104 |
|
franta-hg@81
|
105 |
public LineContext(Matcher matcher) {
|
franta-hg@77
|
106 |
this.matcher = matcher;
|
franta-hg@77
|
107 |
}
|
franta-hg@77
|
108 |
}
|
franta-hg@77
|
109 |
|
franta-hg@77
|
110 |
private enum LINE_TYPE {
|
franta-hg@77
|
111 |
|
franta-hg@83
|
112 |
COMMENT("\\s*(;|#)\\s*(?<comment>.*)") {
|
franta-hg@77
|
113 |
@Override
|
franta-hg@78
|
114 |
public void processLine(LineContext lc, FileContext fc) throws SAXException {
|
franta-hg@82
|
115 |
// TODO: comment → LexicalHandler
|
franta-hg@83
|
116 |
log.log(Level.FINER, "Comment: {0}", lc.matcher.group("comment"));
|
franta-hg@77
|
117 |
}
|
franta-hg@77
|
118 |
|
franta-hg@77
|
119 |
},
|
franta-hg@83
|
120 |
SECTION("\\s*\\[\\s*(?<name>[^\\s\\]]+)\\s*\\]\\s*") {
|
franta-hg@77
|
121 |
@Override
|
franta-hg@78
|
122 |
public void processLine(LineContext lc, FileContext fc) throws SAXException {
|
franta-hg@83
|
123 |
String name = lc.matcher.group("name");
|
franta-hg@78
|
124 |
fc.outputEndSection(fc.lastSection);
|
franta-hg@78
|
125 |
fc.outputStartSection(name);
|
franta-hg@78
|
126 |
fc.lastSection = name;
|
franta-hg@77
|
127 |
}
|
franta-hg@77
|
128 |
|
franta-hg@77
|
129 |
},
|
franta-hg@81
|
130 |
ENTRY(
|
franta-hg@84
|
131 |
"\\s*(?<key>[^=\\s]+)\\s*=\\s*\"(?<value>[^']+)\"\\s*((;|#)\\s*(?<comment>.*)){0,1}", // quoted value → include spaces + might have comment
|
franta-hg@84
|
132 |
"\\s*(?<key>[^=\\s]+)\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*)){0,1}", // apostrophed value → include spaces + might have comment
|
franta-hg@84
|
133 |
"\\s*(?<key>[^=\\s]+)\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces + no comments
|
franta-hg@81
|
134 |
) {
|
franta-hg@77
|
135 |
@Override
|
franta-hg@78
|
136 |
public void processLine(LineContext lc, FileContext fc) throws SAXException {
|
franta-hg@83
|
137 |
String key = lc.matcher.group("key");
|
franta-hg@83
|
138 |
String value = lc.matcher.group("value");
|
franta-hg@78
|
139 |
|
franta-hg@84
|
140 |
if (lc.matcher.groupCount() > 2) {
|
franta-hg@83
|
141 |
String comment = lc.matcher.group("comment");
|
franta-hg@82
|
142 |
// TODO: comment → LexicalHandler
|
franta-hg@84
|
143 |
log.log(Level.FINER, "Comment for entry „{0}“ is: {1}", new Object[]{key, comment});
|
franta-hg@83
|
144 |
|
franta-hg@82
|
145 |
}
|
franta-hg@82
|
146 |
|
franta-hg@85
|
147 |
fc.contentHandler.indentation(fc.lastSection == null ? 1 : 2);
|
franta-hg@78
|
148 |
fc.contentHandler.textElement(value, null, null, key, null);
|
franta-hg@78
|
149 |
fc.contentHandler.lineBreak();
|
franta-hg@78
|
150 |
|
franta-hg@77
|
151 |
}
|
franta-hg@77
|
152 |
|
franta-hg@77
|
153 |
},;
|
franta-hg@77
|
154 |
|
franta-hg@81
|
155 |
private LINE_TYPE(String... patterns) {
|
franta-hg@81
|
156 |
for (String pattern : patterns) {
|
franta-hg@81
|
157 |
this.patterns.add(Pattern.compile(pattern));
|
franta-hg@81
|
158 |
}
|
franta-hg@77
|
159 |
}
|
franta-hg@77
|
160 |
|
franta-hg@81
|
161 |
private final List<Pattern> patterns = new ArrayList<>();
|
franta-hg@77
|
162 |
|
franta-hg@78
|
163 |
protected boolean processLine(String currentLine, FileContext fileContext) throws SAXException {
|
franta-hg@81
|
164 |
for (Pattern pattern : patterns) {
|
franta-hg@81
|
165 |
Matcher m = pattern.matcher(currentLine);
|
franta-hg@81
|
166 |
if (m.matches()) {
|
franta-hg@82
|
167 |
log.log(Level.FINEST, "Line „{0}“ matches pattern „{1}“", new Object[]{currentLine, pattern});
|
franta-hg@81
|
168 |
processLine(new LineContext(m), fileContext);
|
franta-hg@81
|
169 |
return true;
|
franta-hg@81
|
170 |
}
|
franta-hg@77
|
171 |
}
|
franta-hg@81
|
172 |
return false;
|
franta-hg@77
|
173 |
}
|
franta-hg@77
|
174 |
|
franta-hg@78
|
175 |
public abstract void processLine(LineContext lc, FileContext fc) throws SAXException;
|
franta-hg@77
|
176 |
}
|
franta-hg@77
|
177 |
|
franta-hg@2
|
178 |
}
|