franta-hg@11: /**
franta-hg@11: * Alt2XML
franta-hg@11: * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@11: *
franta-hg@11: * This program is free software: you can redistribute it and/or modify
franta-hg@11: * it under the terms of the GNU General Public License as published by
franta-hg@11: * the Free Software Foundation, either version 3 of the License, or
franta-hg@11: * (at your option) any later version.
franta-hg@11: *
franta-hg@11: * This program is distributed in the hope that it will be useful,
franta-hg@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@11: * GNU General Public License for more details.
franta-hg@11: *
franta-hg@11: * You should have received a copy of the GNU General Public License
franta-hg@11: * along with this program. If not, see .
franta-hg@11: */
franta-hg@76: package cz.frantovo.alt2xml.in.ini;
franta-hg@2:
franta-hg@17: import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
franta-hg@77: import cz.frantovo.alt2xml.in.Alt2ContentHandler;
franta-hg@77: import java.io.BufferedReader;
franta-hg@2: import java.io.IOException;
franta-hg@77: import java.io.InputStreamReader;
franta-hg@81: import java.util.ArrayList;
franta-hg@81: import java.util.List;
franta-hg@77: import java.util.logging.Level;
franta-hg@77: import java.util.logging.Logger;
franta-hg@77: import java.util.regex.Matcher;
franta-hg@77: import java.util.regex.Pattern;
franta-hg@2: import org.xml.sax.InputSource;
franta-hg@2: import org.xml.sax.SAXException;
franta-hg@2:
franta-hg@2: /**
franta-hg@2: *
franta-hg@17: * @author Ing. František Kučera (frantovo.cz)
franta-hg@2: */
franta-hg@17: public class Reader extends AbstractAlt2XmlReader {
franta-hg@13:
franta-hg@77: public static final String ROOT_ELEMENT = "ini";
franta-hg@77: private static final Logger log = Logger.getLogger(Reader.class.getName());
franta-hg@77:
franta-hg@2: @Override
franta-hg@6: public void parse(InputSource input) throws IOException, SAXException {
franta-hg@59: outputStart();
franta-hg@77:
franta-hg@77: try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
franta-hg@78: FileContext fc = new FileContext(contentHandler);
franta-hg@77: for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
franta-hg@77: for (LINE_TYPE lineType : LINE_TYPE.values()) {
franta-hg@78: boolean lineProcessed = lineType.processLine(currentLine, fc);
franta-hg@77: if (lineProcessed) {
franta-hg@77: break;
franta-hg@77: }
franta-hg@77: }
franta-hg@77: }
franta-hg@78: fc.outputEndSection(fc.lastSection);
franta-hg@78:
franta-hg@77: }
franta-hg@77:
franta-hg@59: outputEnd();
franta-hg@59: }
franta-hg@59:
franta-hg@59: private void outputStart() throws SAXException {
franta-hg@21: contentHandler.startDocument();
franta-hg@76: contentHandler.lineBreak();
franta-hg@77: contentHandler.startElement(null, null, ROOT_ELEMENT, null);
franta-hg@75: contentHandler.lineBreak();
franta-hg@59: }
franta-hg@59:
franta-hg@59: private void outputEnd() throws SAXException {
franta-hg@76: contentHandler.endElement(null, null, "ini");
franta-hg@75: contentHandler.lineBreak();
franta-hg@21: contentHandler.endDocument();
franta-hg@6: }
franta-hg@76:
franta-hg@77: private static class FileContext {
franta-hg@77:
franta-hg@77: private final Alt2ContentHandler contentHandler;
franta-hg@78: private String lastSection;
franta-hg@77:
franta-hg@77: public FileContext(Alt2ContentHandler contentHandler) {
franta-hg@77: this.contentHandler = contentHandler;
franta-hg@77: }
franta-hg@78:
franta-hg@78: protected void outputStartSection(String name) throws SAXException {
franta-hg@78: contentHandler.indentation(1);
franta-hg@78: contentHandler.startElement(null, null, name, null);
franta-hg@78: contentHandler.lineBreak();
franta-hg@78: }
franta-hg@78:
franta-hg@78: protected void outputEndSection(String name) throws SAXException {
franta-hg@78: if (name != null) {
franta-hg@78: contentHandler.indentation(1);
franta-hg@78: contentHandler.endElement(null, null, name);
franta-hg@78: contentHandler.lineBreak();
franta-hg@78: }
franta-hg@78: }
franta-hg@77: }
franta-hg@77:
franta-hg@77: private static class LineContext {
franta-hg@77:
franta-hg@77: private final Matcher matcher;
franta-hg@77:
franta-hg@81: public LineContext(Matcher matcher) {
franta-hg@77: this.matcher = matcher;
franta-hg@77: }
franta-hg@77: }
franta-hg@77:
franta-hg@77: private enum LINE_TYPE {
franta-hg@77:
franta-hg@80: COMMENT("\\s*(;|#)\\s*(.*)") {
franta-hg@77: @Override
franta-hg@78: public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@78: log.log(Level.FINER, "Comment: {0}", lc.matcher.group(2));
franta-hg@77: }
franta-hg@77:
franta-hg@77: },
franta-hg@80: SECTION("\\s*\\[\\s*([^\\s\\]]+)\\s*\\]\\s*") {
franta-hg@77: @Override
franta-hg@78: public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@78: String name = lc.matcher.group(1);
franta-hg@78: fc.outputEndSection(fc.lastSection);
franta-hg@78: fc.outputStartSection(name);
franta-hg@78: fc.lastSection = name;
franta-hg@77: }
franta-hg@77:
franta-hg@77: },
franta-hg@81: ENTRY(
franta-hg@81: "\\s*([^=\\s]+)\\s*=\\s*\"([^\"]+)\"", // quoted value → include spaces
franta-hg@81: "\\s*([^=\\s]+)\\s*=\\s*'([^']+)'", // apostrophed value → include spaces
franta-hg@81: "\\s*([^=\\s]+)\\s*=\\s*(.+)" // unquoted value → strip spaces
franta-hg@81: ) {
franta-hg@77: @Override
franta-hg@78: public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@78: String key = lc.matcher.group(1);
franta-hg@78: String value = lc.matcher.group(2);
franta-hg@78:
franta-hg@78: fc.contentHandler.indentation(2);
franta-hg@78: fc.contentHandler.textElement(value, null, null, key, null);
franta-hg@78: fc.contentHandler.lineBreak();
franta-hg@78:
franta-hg@77: }
franta-hg@77:
franta-hg@77: },;
franta-hg@77:
franta-hg@81: private LINE_TYPE(String... patterns) {
franta-hg@81: for (String pattern : patterns) {
franta-hg@81: this.patterns.add(Pattern.compile(pattern));
franta-hg@81: }
franta-hg@77: }
franta-hg@77:
franta-hg@81: private final List patterns = new ArrayList<>();
franta-hg@77:
franta-hg@78: protected boolean processLine(String currentLine, FileContext fileContext) throws SAXException {
franta-hg@81: for (Pattern pattern : patterns) {
franta-hg@81: Matcher m = pattern.matcher(currentLine);
franta-hg@81: if (m.matches()) {
franta-hg@81: processLine(new LineContext(m), fileContext);
franta-hg@81: return true;
franta-hg@81: }
franta-hg@77: }
franta-hg@81: return false;
franta-hg@77: }
franta-hg@77:
franta-hg@78: public abstract void processLine(LineContext lc, FileContext fc) throws SAXException;
franta-hg@77: }
franta-hg@77:
franta-hg@2: }