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