in-ini: support and encode entry names with spaces
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 21:24:53 +0200
changeset 8946c7cc4863c1
parent 88 9860586b3b87
child 90 c96459e02690
in-ini: support and encode entry names with spaces
java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java
java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Functions.java
     1.1 --- a/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 21:15:47 2014 +0200
     1.2 +++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 21:24:53 2014 +0200
     1.3 @@ -19,7 +19,7 @@
     1.4  
     1.5  import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
     1.6  import cz.frantovo.alt2xml.in.Alt2ContentHandler;
     1.7 -import static cz.frantovo.alt2xml.in.Functions.encodeXmlName;
     1.8 +import cz.frantovo.alt2xml.in.Functions;
     1.9  import java.io.BufferedReader;
    1.10  import java.io.IOException;
    1.11  import java.io.InputStreamReader;
    1.12 @@ -105,6 +105,14 @@
    1.13  		}
    1.14  	}
    1.15  
    1.16 +	private static String encodeXmlName(String originalName, int lineNumber) {
    1.17 +		String encodedName = Functions.encodeXmlName(originalName);
    1.18 +		if (!encodedName.equals(originalName)) {
    1.19 +			log.log(Level.FINE, "Line {0}: name „{1} was encoded to „{2}““", new Object[]{lineNumber, originalName, encodedName});
    1.20 +		}
    1.21 +		return encodedName;
    1.22 +	}
    1.23 +
    1.24  	private static class LineContext {
    1.25  
    1.26  		private final Matcher matcher;
    1.27 @@ -133,34 +141,27 @@
    1.28  		SECTION("\\s*\\[\\s*(?<name>[^\\]\\]]+)\\s*\\]\\s*") {
    1.29  					@Override
    1.30  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.31 -						String originalName = lc.matcher.group("name");
    1.32 -						String encodedName = encodeXmlName(originalName);
    1.33 -
    1.34 -						if (!encodedName.equals(originalName)) {
    1.35 -							log.log(Level.FINE, "Line {0}: section name „{1} was encoded to „{2}““", new Object[]{fc.lineNumber, originalName, encodedName});
    1.36 -						}
    1.37 -
    1.38 +						String name = encodeXmlName(lc.matcher.group("name"), fc.lineNumber);
    1.39  						fc.outputEndSection(fc.lastSection);
    1.40 -						fc.outputStartSection(encodedName);
    1.41 -						fc.lastSection = encodedName;
    1.42 +						fc.outputStartSection(name);
    1.43 +						fc.lastSection = name;
    1.44  					}
    1.45  
    1.46  				},
    1.47  		ENTRY(
    1.48 -				"\\s*(?<key>[^=\\s]+)\\s*=\\s*\"(?<value>[^']+)\"\\s*((;|#)\\s*(?<comment>.*)){0,1}", // quoted value → include spaces + might have comment
    1.49 -				"\\s*(?<key>[^=\\s]+)\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*)){0,1}", // apostrophed value → include spaces + might have comment
    1.50 -				"\\s*(?<key>[^=\\s]+)\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces + no comments
    1.51 +				"\\s*(?<key>[^=]+)\\s*=\\s*\"(?<value>[^']+)\"\\s*((;|#)\\s*(?<comment>.*)){0,1}", // quoted value → include spaces + might have comment
    1.52 +				"\\s*(?<key>[^=]+)\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*)){0,1}", // apostrophed value → include spaces + might have comment
    1.53 +				"\\s*(?<key>[^=]+)\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces + no comments
    1.54  		) {
    1.55  					@Override
    1.56  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.57 -						String key = lc.matcher.group("key");
    1.58 +						String key = encodeXmlName(lc.matcher.group("key"), fc.lineNumber);
    1.59  						String value = lc.matcher.group("value");
    1.60  
    1.61  						if (lc.matcher.groupCount() > 2) {
    1.62  							String comment = lc.matcher.group("comment");
    1.63  							// TODO: comment → LexicalHandler
    1.64  							log.log(Level.FINER, "Line {0}: comment for entry „{1}“ is: {2}", new Object[]{fc.lineNumber, key, comment});
    1.65 -
    1.66  						}
    1.67  
    1.68  						fc.contentHandler.indentation(fc.lastSection == null ? 1 : 2);
     2.1 --- a/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Functions.java	Sat Sep 06 21:15:47 2014 +0200
     2.2 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Functions.java	Sat Sep 06 21:24:53 2014 +0200
     2.3 @@ -30,7 +30,11 @@
     2.4  	 * @return valid name of XML element or attribute
     2.5  	 */
     2.6  	public static String encodeXmlName(String name) {
     2.7 -		return name.replaceAll("\\s", "-");
     2.8 +		if (name == null) {
     2.9 +			return String.valueOf(name);
    2.10 +		} else {
    2.11 +			return name.replaceAll("\\s", "-");
    2.12 +		}
    2.13  	}
    2.14  
    2.15  	private Functions() {