# HG changeset patch
# User František Kučera <franta-hg@frantovo.cz>
# Date 1410031493 -7200
# Node ID 46c7cc4863c163ffd817c7f16eac2193dc4e6039
# Parent  9860586b3b8711280d1ede7b84d5d1564975dd14
in-ini: support and encode entry names with spaces

diff -r 9860586b3b87 -r 46c7cc4863c1 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:15:47 2014 +0200
+++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 21:24:53 2014 +0200
@@ -19,7 +19,7 @@
 
 import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
 import cz.frantovo.alt2xml.in.Alt2ContentHandler;
-import static cz.frantovo.alt2xml.in.Functions.encodeXmlName;
+import cz.frantovo.alt2xml.in.Functions;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -105,6 +105,14 @@
 		}
 	}
 
+	private static String encodeXmlName(String originalName, int lineNumber) {
+		String encodedName = Functions.encodeXmlName(originalName);
+		if (!encodedName.equals(originalName)) {
+			log.log(Level.FINE, "Line {0}: name „{1} was encoded to „{2}““", new Object[]{lineNumber, originalName, encodedName});
+		}
+		return encodedName;
+	}
+
 	private static class LineContext {
 
 		private final Matcher matcher;
@@ -133,34 +141,27 @@
 		SECTION("\\s*\\[\\s*(?<name>[^\\]\\]]+)\\s*\\]\\s*") {
 					@Override
 					public void processLine(LineContext lc, FileContext fc) throws SAXException {
-						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});
-						}
-
+						String name = encodeXmlName(lc.matcher.group("name"), fc.lineNumber);
 						fc.outputEndSection(fc.lastSection);
-						fc.outputStartSection(encodedName);
-						fc.lastSection = encodedName;
+						fc.outputStartSection(name);
+						fc.lastSection = name;
 					}
 
 				},
 		ENTRY(
-				"\\s*(?<key>[^=\\s]+)\\s*=\\s*\"(?<value>[^']+)\"\\s*((;|#)\\s*(?<comment>.*)){0,1}", // quoted value → include spaces + might have comment
-				"\\s*(?<key>[^=\\s]+)\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*)){0,1}", // apostrophed value → include spaces + might have comment
-				"\\s*(?<key>[^=\\s]+)\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces + no comments
+				"\\s*(?<key>[^=]+)\\s*=\\s*\"(?<value>[^']+)\"\\s*((;|#)\\s*(?<comment>.*)){0,1}", // quoted value → include spaces + might have comment
+				"\\s*(?<key>[^=]+)\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*)){0,1}", // apostrophed value → include spaces + might have comment
+				"\\s*(?<key>[^=]+)\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces + no comments
 		) {
 					@Override
 					public void processLine(LineContext lc, FileContext fc) throws SAXException {
-						String key = lc.matcher.group("key");
+						String key = encodeXmlName(lc.matcher.group("key"), fc.lineNumber);
 						String value = lc.matcher.group("value");
 
 						if (lc.matcher.groupCount() > 2) {
 							String comment = lc.matcher.group("comment");
 							// TODO: comment → LexicalHandler
 							log.log(Level.FINER, "Line {0}: comment for entry „{1}“ is: {2}", new Object[]{fc.lineNumber, key, comment});
-
 						}
 
 						fc.contentHandler.indentation(fc.lastSection == null ? 1 : 2);
diff -r 9860586b3b87 -r 46c7cc4863c1 java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Functions.java
--- a/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Functions.java	Sat Sep 06 21:15:47 2014 +0200
+++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Functions.java	Sat Sep 06 21:24:53 2014 +0200
@@ -30,7 +30,11 @@
 	 * @return valid name of XML element or attribute
 	 */
 	public static String encodeXmlName(String name) {
-		return name.replaceAll("\\s", "-");
+		if (name == null) {
+			return String.valueOf(name);
+		} else {
+			return name.replaceAll("\\s", "-");
+		}
 	}
 
 	private Functions() {