in-ini: encode section names with spaces
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 21:15:47 +0200
changeset 889860586b3b87
parent 87 233907073718
child 89 46c7cc4863c1
in-ini: encode section 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:14:48 2014 +0200
     1.2 +++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 21:15:47 2014 +0200
     1.3 @@ -19,6 +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 java.io.BufferedReader;
     1.9  import java.io.IOException;
    1.10  import java.io.InputStreamReader;
    1.11 @@ -129,14 +130,19 @@
    1.12  					}
    1.13  
    1.14  				},
    1.15 -		SECTION("\\s*\\[\\s*(?<name>[^\\s\\]]+)\\s*\\]\\s*") {
    1.16 +		SECTION("\\s*\\[\\s*(?<name>[^\\]\\]]+)\\s*\\]\\s*") {
    1.17  					@Override
    1.18  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.19 -						// TODO: escaping for section names with spaces
    1.20 -						String name = lc.matcher.group("name");
    1.21 +						String originalName = lc.matcher.group("name");
    1.22 +						String encodedName = encodeXmlName(originalName);
    1.23 +
    1.24 +						if (!encodedName.equals(originalName)) {
    1.25 +							log.log(Level.FINE, "Line {0}: section name „{1} was encoded to „{2}““", new Object[]{fc.lineNumber, originalName, encodedName});
    1.26 +						}
    1.27 +
    1.28  						fc.outputEndSection(fc.lastSection);
    1.29 -						fc.outputStartSection(name);
    1.30 -						fc.lastSection = name;
    1.31 +						fc.outputStartSection(encodedName);
    1.32 +						fc.lastSection = encodedName;
    1.33  					}
    1.34  
    1.35  				},
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/alt2xml-lib-input/src/cz/frantovo/alt2xml/in/Functions.java	Sat Sep 06 21:15:47 2014 +0200
     2.3 @@ -0,0 +1,38 @@
     2.4 +/**
     2.5 + * Alt2XML
     2.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, either version 3 of the License, or
    2.11 + * (at your option) any later version.
    2.12 + *
    2.13 + * This program is distributed in the hope that it will be useful,
    2.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.16 + * GNU General Public License for more details.
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License
    2.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.20 + */
    2.21 +package cz.frantovo.alt2xml.in;
    2.22 +
    2.23 +/**
    2.24 + *
    2.25 + * @author Ing. František Kučera (frantovo.cz)
    2.26 + */
    2.27 +public class Functions {
    2.28 +
    2.29 +	/**
    2.30 +	 * TODO: real lossless encoding + decoding (this is just a dummy code)
    2.31 +	 *
    2.32 +	 * @param name any string
    2.33 +	 * @return valid name of XML element or attribute
    2.34 +	 */
    2.35 +	public static String encodeXmlName(String name) {
    2.36 +		return name.replaceAll("\\s", "-");
    2.37 +	}
    2.38 +
    2.39 +	private Functions() {
    2.40 +	}
    2.41 +}