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@86: fc.lineNumber++;
franta-hg@86: boolean lineProcessed = false;
franta-hg@77: for (LINE_TYPE lineType : LINE_TYPE.values()) {
franta-hg@86: lineProcessed = lineType.processLine(currentLine, fc);
franta-hg@77: if (lineProcessed) {
franta-hg@77: break;
franta-hg@77: }
franta-hg@77: }
franta-hg@86: if (!lineProcessed) {
franta-hg@86: log.log(Level.SEVERE, "Invalid line in INI file: {0}", currentLine);
franta-hg@86: }
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@86: private int lineNumber;
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@86: BLANK_LINE("\\s*") {
franta-hg@86: @Override
franta-hg@86: public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@86: log.log(Level.FINEST, "Line {0}: skipping blank line", fc.lineNumber);
franta-hg@86: }
franta-hg@86: },
franta-hg@83: COMMENT("\\s*(;|#)\\s*(?.*)") {
franta-hg@77: @Override
franta-hg@78: public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@82: // TODO: comment → LexicalHandler
franta-hg@86: log.log(Level.FINER, "Line {0}: comment: {1}", new Object[]{fc.lineNumber, lc.matcher.group("comment")});
franta-hg@77: }
franta-hg@77:
franta-hg@77: },
franta-hg@83: SECTION("\\s*\\[\\s*(?[^\\s\\]]+)\\s*\\]\\s*") {
franta-hg@77: @Override
franta-hg@78: public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@86: // TODO: escaping for section names with spaces
franta-hg@83: String name = lc.matcher.group("name");
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@84: "\\s*(?[^=\\s]+)\\s*=\\s*\"(?[^']+)\"\\s*((;|#)\\s*(?.*)){0,1}", // quoted value → include spaces + might have comment
franta-hg@84: "\\s*(?[^=\\s]+)\\s*=\\s*'(?[^']+)'\\s*((;|#)\\s*(?.*)){0,1}", // apostrophed value → include spaces + might have comment
franta-hg@84: "\\s*(?[^=\\s]+)\\s*=\\s*(?.+)" // unquoted value → strip spaces + no comments
franta-hg@81: ) {
franta-hg@77: @Override
franta-hg@78: public void processLine(LineContext lc, FileContext fc) throws SAXException {
franta-hg@83: String key = lc.matcher.group("key");
franta-hg@83: String value = lc.matcher.group("value");
franta-hg@78:
franta-hg@84: if (lc.matcher.groupCount() > 2) {
franta-hg@83: String comment = lc.matcher.group("comment");
franta-hg@82: // TODO: comment → LexicalHandler
franta-hg@86: log.log(Level.FINER, "Line {0}: comment for entry „{1}“ is: {2}", new Object[]{fc.lineNumber, key, comment});
franta-hg@83:
franta-hg@82: }
franta-hg@82:
franta-hg@85: fc.contentHandler.indentation(fc.lastSection == null ? 1 : 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@86: protected boolean processLine(String currentLine, FileContext fc) 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@86: log.log(Level.FINEST, "Line {0}: pattern „{1}“ matches „{2}“", new Object[]{fc.lineNumber, pattern, currentLine});
franta-hg@86: processLine(new LineContext(m), fc);
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: }