3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package cz.frantovo.alt2xml.in.ini;
20 import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
21 import cz.frantovo.alt2xml.in.Alt2ContentHandler;
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import org.xml.sax.InputSource;
30 import org.xml.sax.SAXException;
34 * @author Ing. František Kučera (frantovo.cz)
36 public class Reader extends AbstractAlt2XmlReader {
38 public static final String ROOT_ELEMENT = "ini";
39 private static final Logger log = Logger.getLogger(Reader.class.getName());
42 public void parse(InputSource input) throws IOException, SAXException {
45 try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
46 FileContext c = new FileContext(contentHandler);
47 for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
48 for (LINE_TYPE lineType : LINE_TYPE.values()) {
49 boolean lineProcessed = lineType.processLine(currentLine, c);
60 private void outputStart() throws SAXException {
61 contentHandler.startDocument();
62 contentHandler.lineBreak();
63 contentHandler.startElement(null, null, ROOT_ELEMENT, null);
64 contentHandler.lineBreak();
67 private void outputEnd() throws SAXException {
68 contentHandler.endElement(null, null, "ini");
69 contentHandler.lineBreak();
70 contentHandler.endDocument();
73 private static class FileContext {
75 private final Alt2ContentHandler contentHandler;
77 public FileContext(Alt2ContentHandler contentHandler) {
78 this.contentHandler = contentHandler;
82 private static class LineContext {
84 private final String line;
85 private final Matcher matcher;
87 public LineContext(String line, Matcher matcher) {
89 this.matcher = matcher;
93 private enum LINE_TYPE {
95 COMMENT("(;|#)\\s*(.*)") {
98 public void processLine(LineContext lineContext, FileContext fileContext) {
99 log.log(Level.FINER, "Comment: {0}", lineContext.matcher.group(2));
103 SECTION("\\[(.+)\\]") {
106 public void processLine(LineContext lineContext, FileContext fileContext) {
107 log.log(Level.WARNING, "Section: {0}", lineContext.matcher.group(1));
111 ENTRY("\\s*([^=]+)\\s*=\\s*(.+)") {
114 public void processLine(LineContext lineContext, FileContext fileContext) {
115 log.log(Level.WARNING, "Entry: {0} = {1}", new Object[]{lineContext.matcher.group(1), lineContext.matcher.group(2)});
120 private LINE_TYPE(String pattern) {
121 this.pattern = Pattern.compile(pattern);
124 private final Pattern pattern;
126 protected boolean processLine(String currentLine, FileContext fileContext) {
127 Matcher m = pattern.matcher(currentLine);
129 processLine(new LineContext(currentLine, m), fileContext);
136 public abstract void processLine(LineContext lineContext, FileContext fileContext);