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@77
|
46 |
FileContext c = 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@77
|
49 |
boolean lineProcessed = lineType.processLine(currentLine, c);
|
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@77
|
55 |
}
|
franta-hg@77
|
56 |
|
franta-hg@59
|
57 |
outputEnd();
|
franta-hg@59
|
58 |
}
|
franta-hg@59
|
59 |
|
franta-hg@59
|
60 |
private void outputStart() throws SAXException {
|
franta-hg@21
|
61 |
contentHandler.startDocument();
|
franta-hg@76
|
62 |
contentHandler.lineBreak();
|
franta-hg@77
|
63 |
contentHandler.startElement(null, null, ROOT_ELEMENT, null);
|
franta-hg@75
|
64 |
contentHandler.lineBreak();
|
franta-hg@59
|
65 |
}
|
franta-hg@59
|
66 |
|
franta-hg@59
|
67 |
private void outputEnd() throws SAXException {
|
franta-hg@76
|
68 |
contentHandler.endElement(null, null, "ini");
|
franta-hg@75
|
69 |
contentHandler.lineBreak();
|
franta-hg@21
|
70 |
contentHandler.endDocument();
|
franta-hg@6
|
71 |
}
|
franta-hg@76
|
72 |
|
franta-hg@77
|
73 |
private static class FileContext {
|
franta-hg@77
|
74 |
|
franta-hg@77
|
75 |
private final Alt2ContentHandler contentHandler;
|
franta-hg@77
|
76 |
|
franta-hg@77
|
77 |
public FileContext(Alt2ContentHandler contentHandler) {
|
franta-hg@77
|
78 |
this.contentHandler = contentHandler;
|
franta-hg@77
|
79 |
}
|
franta-hg@77
|
80 |
}
|
franta-hg@77
|
81 |
|
franta-hg@77
|
82 |
private static class LineContext {
|
franta-hg@77
|
83 |
|
franta-hg@77
|
84 |
private final String line;
|
franta-hg@77
|
85 |
private final Matcher matcher;
|
franta-hg@77
|
86 |
|
franta-hg@77
|
87 |
public LineContext(String line, Matcher matcher) {
|
franta-hg@77
|
88 |
this.line = line;
|
franta-hg@77
|
89 |
this.matcher = matcher;
|
franta-hg@77
|
90 |
}
|
franta-hg@77
|
91 |
}
|
franta-hg@77
|
92 |
|
franta-hg@77
|
93 |
private enum LINE_TYPE {
|
franta-hg@77
|
94 |
|
franta-hg@77
|
95 |
COMMENT("(;|#)\\s*(.*)") {
|
franta-hg@77
|
96 |
|
franta-hg@77
|
97 |
@Override
|
franta-hg@77
|
98 |
public void processLine(LineContext lineContext, FileContext fileContext) {
|
franta-hg@77
|
99 |
log.log(Level.FINER, "Comment: {0}", lineContext.matcher.group(2));
|
franta-hg@77
|
100 |
}
|
franta-hg@77
|
101 |
|
franta-hg@77
|
102 |
},
|
franta-hg@77
|
103 |
SECTION("\\[(.+)\\]") {
|
franta-hg@77
|
104 |
|
franta-hg@77
|
105 |
@Override
|
franta-hg@77
|
106 |
public void processLine(LineContext lineContext, FileContext fileContext) {
|
franta-hg@77
|
107 |
log.log(Level.WARNING, "Section: {0}", lineContext.matcher.group(1));
|
franta-hg@77
|
108 |
}
|
franta-hg@77
|
109 |
|
franta-hg@77
|
110 |
},
|
franta-hg@77
|
111 |
ENTRY("\\s*([^=]+)\\s*=\\s*(.+)") {
|
franta-hg@77
|
112 |
|
franta-hg@77
|
113 |
@Override
|
franta-hg@77
|
114 |
public void processLine(LineContext lineContext, FileContext fileContext) {
|
franta-hg@77
|
115 |
log.log(Level.WARNING, "Entry: {0} = {1}", new Object[]{lineContext.matcher.group(1), lineContext.matcher.group(2)});
|
franta-hg@77
|
116 |
}
|
franta-hg@77
|
117 |
|
franta-hg@77
|
118 |
},;
|
franta-hg@77
|
119 |
|
franta-hg@77
|
120 |
private LINE_TYPE(String pattern) {
|
franta-hg@77
|
121 |
this.pattern = Pattern.compile(pattern);
|
franta-hg@77
|
122 |
}
|
franta-hg@77
|
123 |
|
franta-hg@77
|
124 |
private final Pattern pattern;
|
franta-hg@77
|
125 |
|
franta-hg@77
|
126 |
protected boolean processLine(String currentLine, FileContext fileContext) {
|
franta-hg@77
|
127 |
Matcher m = pattern.matcher(currentLine);
|
franta-hg@77
|
128 |
if (m.matches()) {
|
franta-hg@77
|
129 |
processLine(new LineContext(currentLine, m), fileContext);
|
franta-hg@77
|
130 |
return true;
|
franta-hg@77
|
131 |
} else {
|
franta-hg@77
|
132 |
return false;
|
franta-hg@77
|
133 |
}
|
franta-hg@77
|
134 |
}
|
franta-hg@77
|
135 |
|
franta-hg@77
|
136 |
public abstract void processLine(LineContext lineContext, FileContext fileContext);
|
franta-hg@77
|
137 |
}
|
franta-hg@77
|
138 |
|
franta-hg@2
|
139 |
}
|