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@77
|
25 |
import java.util.logging.Level;
|
franta-hg@77
|
26 |
import java.util.logging.Logger;
|
franta-hg@77
|
27 |
import java.util.regex.Matcher;
|
franta-hg@77
|
28 |
import java.util.regex.Pattern;
|
franta-hg@2
|
29 |
import org.xml.sax.InputSource;
|
franta-hg@2
|
30 |
import org.xml.sax.SAXException;
|
franta-hg@2
|
31 |
|
franta-hg@2
|
32 |
/**
|
franta-hg@2
|
33 |
*
|
franta-hg@17
|
34 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@2
|
35 |
*/
|
franta-hg@17
|
36 |
public class Reader extends AbstractAlt2XmlReader {
|
franta-hg@13
|
37 |
|
franta-hg@77
|
38 |
public static final String ROOT_ELEMENT = "ini";
|
franta-hg@77
|
39 |
private static final Logger log = Logger.getLogger(Reader.class.getName());
|
franta-hg@77
|
40 |
|
franta-hg@2
|
41 |
@Override
|
franta-hg@6
|
42 |
public void parse(InputSource input) throws IOException, SAXException {
|
franta-hg@59
|
43 |
outputStart();
|
franta-hg@77
|
44 |
|
franta-hg@77
|
45 |
try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
|
franta-hg@78
|
46 |
FileContext fc = new FileContext(contentHandler);
|
franta-hg@77
|
47 |
for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
|
franta-hg@77
|
48 |
for (LINE_TYPE lineType : LINE_TYPE.values()) {
|
franta-hg@78
|
49 |
boolean lineProcessed = lineType.processLine(currentLine, fc);
|
franta-hg@77
|
50 |
if (lineProcessed) {
|
franta-hg@77
|
51 |
break;
|
franta-hg@77
|
52 |
}
|
franta-hg@77
|
53 |
}
|
franta-hg@77
|
54 |
}
|
franta-hg@78
|
55 |
fc.outputEndSection(fc.lastSection);
|
franta-hg@78
|
56 |
|
franta-hg@77
|
57 |
}
|
franta-hg@77
|
58 |
|
franta-hg@59
|
59 |
outputEnd();
|
franta-hg@59
|
60 |
}
|
franta-hg@59
|
61 |
|
franta-hg@59
|
62 |
private void outputStart() throws SAXException {
|
franta-hg@21
|
63 |
contentHandler.startDocument();
|
franta-hg@76
|
64 |
contentHandler.lineBreak();
|
franta-hg@77
|
65 |
contentHandler.startElement(null, null, ROOT_ELEMENT, null);
|
franta-hg@75
|
66 |
contentHandler.lineBreak();
|
franta-hg@59
|
67 |
}
|
franta-hg@59
|
68 |
|
franta-hg@59
|
69 |
private void outputEnd() throws SAXException {
|
franta-hg@76
|
70 |
contentHandler.endElement(null, null, "ini");
|
franta-hg@75
|
71 |
contentHandler.lineBreak();
|
franta-hg@21
|
72 |
contentHandler.endDocument();
|
franta-hg@6
|
73 |
}
|
franta-hg@76
|
74 |
|
franta-hg@77
|
75 |
private static class FileContext {
|
franta-hg@77
|
76 |
|
franta-hg@77
|
77 |
private final Alt2ContentHandler contentHandler;
|
franta-hg@78
|
78 |
private String lastSection;
|
franta-hg@77
|
79 |
|
franta-hg@77
|
80 |
public FileContext(Alt2ContentHandler contentHandler) {
|
franta-hg@77
|
81 |
this.contentHandler = contentHandler;
|
franta-hg@77
|
82 |
}
|
franta-hg@78
|
83 |
|
franta-hg@78
|
84 |
protected void outputStartSection(String name) throws SAXException {
|
franta-hg@78
|
85 |
contentHandler.indentation(1);
|
franta-hg@78
|
86 |
contentHandler.startElement(null, null, name, null);
|
franta-hg@78
|
87 |
contentHandler.lineBreak();
|
franta-hg@78
|
88 |
}
|
franta-hg@78
|
89 |
|
franta-hg@78
|
90 |
protected void outputEndSection(String name) throws SAXException {
|
franta-hg@78
|
91 |
if (name != null) {
|
franta-hg@78
|
92 |
contentHandler.indentation(1);
|
franta-hg@78
|
93 |
contentHandler.endElement(null, null, name);
|
franta-hg@78
|
94 |
contentHandler.lineBreak();
|
franta-hg@78
|
95 |
}
|
franta-hg@78
|
96 |
}
|
franta-hg@77
|
97 |
}
|
franta-hg@77
|
98 |
|
franta-hg@77
|
99 |
private static class LineContext {
|
franta-hg@77
|
100 |
|
franta-hg@77
|
101 |
private final String line;
|
franta-hg@77
|
102 |
private final Matcher matcher;
|
franta-hg@77
|
103 |
|
franta-hg@77
|
104 |
public LineContext(String line, Matcher matcher) {
|
franta-hg@77
|
105 |
this.line = line;
|
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@80
|
112 |
COMMENT("\\s*(;|#)\\s*(.*)") {
|
franta-hg@77
|
113 |
|
franta-hg@77
|
114 |
@Override
|
franta-hg@78
|
115 |
public void processLine(LineContext lc, FileContext fc) throws SAXException {
|
franta-hg@78
|
116 |
log.log(Level.FINER, "Comment: {0}", lc.matcher.group(2));
|
franta-hg@77
|
117 |
}
|
franta-hg@77
|
118 |
|
franta-hg@77
|
119 |
},
|
franta-hg@80
|
120 |
SECTION("\\s*\\[\\s*([^\\s\\]]+)\\s*\\]\\s*") {
|
franta-hg@77
|
121 |
|
franta-hg@77
|
122 |
@Override
|
franta-hg@78
|
123 |
public void processLine(LineContext lc, FileContext fc) throws SAXException {
|
franta-hg@78
|
124 |
String name = lc.matcher.group(1);
|
franta-hg@78
|
125 |
fc.outputEndSection(fc.lastSection);
|
franta-hg@78
|
126 |
fc.outputStartSection(name);
|
franta-hg@78
|
127 |
fc.lastSection = name;
|
franta-hg@77
|
128 |
}
|
franta-hg@77
|
129 |
|
franta-hg@77
|
130 |
},
|
franta-hg@80
|
131 |
ENTRY("\\s*([^=\\s]+)\\s*=\\s*(.+)") {
|
franta-hg@77
|
132 |
|
franta-hg@77
|
133 |
@Override
|
franta-hg@78
|
134 |
public void processLine(LineContext lc, FileContext fc) throws SAXException {
|
franta-hg@78
|
135 |
String key = lc.matcher.group(1);
|
franta-hg@78
|
136 |
String value = lc.matcher.group(2);
|
franta-hg@78
|
137 |
|
franta-hg@78
|
138 |
fc.contentHandler.indentation(2);
|
franta-hg@78
|
139 |
fc.contentHandler.textElement(value, null, null, key, null);
|
franta-hg@78
|
140 |
fc.contentHandler.lineBreak();
|
franta-hg@78
|
141 |
|
franta-hg@77
|
142 |
}
|
franta-hg@77
|
143 |
|
franta-hg@77
|
144 |
},;
|
franta-hg@77
|
145 |
|
franta-hg@77
|
146 |
private LINE_TYPE(String pattern) {
|
franta-hg@77
|
147 |
this.pattern = Pattern.compile(pattern);
|
franta-hg@77
|
148 |
}
|
franta-hg@77
|
149 |
|
franta-hg@77
|
150 |
private final Pattern pattern;
|
franta-hg@77
|
151 |
|
franta-hg@78
|
152 |
protected boolean processLine(String currentLine, FileContext fileContext) throws SAXException {
|
franta-hg@77
|
153 |
Matcher m = pattern.matcher(currentLine);
|
franta-hg@77
|
154 |
if (m.matches()) {
|
franta-hg@77
|
155 |
processLine(new LineContext(currentLine, m), fileContext);
|
franta-hg@77
|
156 |
return true;
|
franta-hg@77
|
157 |
} else {
|
franta-hg@77
|
158 |
return false;
|
franta-hg@77
|
159 |
}
|
franta-hg@77
|
160 |
}
|
franta-hg@77
|
161 |
|
franta-hg@78
|
162 |
public abstract void processLine(LineContext lc, FileContext fc) throws SAXException;
|
franta-hg@77
|
163 |
}
|
franta-hg@77
|
164 |
|
franta-hg@2
|
165 |
}
|