# HG changeset patch
# User František Kučera <franta-hg@frantovo.cz>
# Date 1410030947 -7200
# Node ID 9860586b3b8711280d1ede7b84d5d1564975dd14
# Parent  23390707371808769574182082e8cad3455707df
in-ini: encode section names with spaces

diff -r 233907073718 -r 9860586b3b87 java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java
--- a/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 21:14:48 2014 +0200
+++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 21:15:47 2014 +0200
@@ -19,6 +19,7 @@
 
 import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
 import cz.frantovo.alt2xml.in.Alt2ContentHandler;
+import static cz.frantovo.alt2xml.in.Functions.encodeXmlName;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -129,14 +130,19 @@
 					}
 
 				},
-		SECTION("\\s*\\[\\s*(?<name>[^\\s\\]]+)\\s*\\]\\s*") {
+		SECTION("\\s*\\[\\s*(?<name>[^\\]\\]]+)\\s*\\]\\s*") {
 					@Override
 					public void processLine(LineContext lc, FileContext fc) throws SAXException {
-						// TODO: escaping for section names with spaces
-						String name = lc.matcher.group("name");
+						String originalName = lc.matcher.group("name");
+						String encodedName = encodeXmlName(originalName);
+
+						if (!encodedName.equals(originalName)) {
+							log.log(Level.FINE, "Line {0}: section name „{1} was encoded to „{2}““", new Object[]{fc.lineNumber, originalName, encodedName});
+						}
+
 						fc.outputEndSection(fc.lastSection);
-						fc.outputStartSection(name);
-						fc.lastSection = name;
+						fc.outputStartSection(encodedName);
+						fc.lastSection = encodedName;
 					}
 
 				},
diff -r 233907073718 -r 9860586b3b87 java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Functions.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Functions.java	Sat Sep 06 21:15:47 2014 +0200
@@ -0,0 +1,38 @@
+/**
+ * Alt2XML
+ * Copyright © 2014 František Kučera (frantovo.cz)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package cz.frantovo.alt2xml.in;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class Functions {
+
+	/**
+	 * TODO: real lossless encoding + decoding (this is just a dummy code)
+	 *
+	 * @param name any string
+	 * @return valid name of XML element or attribute
+	 */
+	public static String encodeXmlName(String name) {
+		return name.replaceAll("\\s", "-");
+	}
+
+	private Functions() {
+	}
+}