in-ini: regex with named groups
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 19:56:08 +0200
changeset 8306e866769478
parent 82 547f1246fe35
child 84 1a09966c072c
in-ini: regex with named groups
java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java
     1.1 --- a/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 19:36:22 2014 +0200
     1.2 +++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 19:56:08 2014 +0200
     1.3 @@ -109,18 +109,18 @@
     1.4  
     1.5  	private enum LINE_TYPE {
     1.6  
     1.7 -		COMMENT("\\s*(;|#)\\s*(.*)") {
     1.8 +		COMMENT("\\s*(;|#)\\s*(?<comment>.*)") {
     1.9  					@Override
    1.10  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.11  						// TODO: comment → LexicalHandler
    1.12 -						log.log(Level.FINER, "Comment: {0}", lc.matcher.group(2));
    1.13 +						log.log(Level.FINER, "Comment: {0}", lc.matcher.group("comment"));
    1.14  					}
    1.15  
    1.16  				},
    1.17 -		SECTION("\\s*\\[\\s*([^\\s\\]]+)\\s*\\]\\s*") {
    1.18 +		SECTION("\\s*\\[\\s*(?<name>[^\\s\\]]+)\\s*\\]\\s*") {
    1.19  					@Override
    1.20  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.21 -						String name = lc.matcher.group(1);
    1.22 +						String name = lc.matcher.group("name");
    1.23  						fc.outputEndSection(fc.lastSection);
    1.24  						fc.outputStartSection(name);
    1.25  						fc.lastSection = name;
    1.26 @@ -128,21 +128,25 @@
    1.27  
    1.28  				},
    1.29  		ENTRY(
    1.30 -				"\\s*([^=\\s]+)\\s*=\\s*\"([^\"]+)\"", // quoted value → include spaces
    1.31 -				"\\s*([^=\\s]+)\\s*=\\s*\"([^\"]+)\"\\s*(;|#)*\\s*(.*)", // quoted value → include spaces | with comment
    1.32 -				"\\s*([^=\\s]+)\\s*=\\s*'([^']+)'", // apostrophed value → include spaces | with comment
    1.33 -				"\\s*([^=\\s]+)\\s*=\\s*'([^']+)'\\s*(;|#)*\\s*(.*)", // apostrophed value → include spaces
    1.34 -				"\\s*([^=\\s]+)\\s*=\\s*(.+)" // unquoted value → strip spaces
    1.35 +				"\\s*(?<key>[^=\\s]+)\\s*=\\s*\"(?<value>[^\"]+)\"", // quoted value → include spaces
    1.36 +				"\\s*(?<key>[^=\\s]+)\\s*=\\s*\"(?<value>[^\"]+)\"\\s*(;|#)*\\s*(?<comment>.*)", // quoted value → include spaces | with comment
    1.37 +				"\\s*(?<key>[^=\\s]+)\\s*=\\s*'(?<value>[^']+)'", // apostrophed value → include spaces
    1.38 +				"\\s*(?<key>[^=\\s]+)\\s*=\\s*'(?<value>[^']+)'\\s*((;|#)\\s*(?<comment>.*)){0,1}", // apostrophed value → include spaces | with comment
    1.39 +				"\\s*(?<key>[^=\\s]+)\\s*=\\s*(?<value>.+)" // unquoted value → strip spaces
    1.40  		) {
    1.41  					@Override
    1.42  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.43 -						String key = lc.matcher.group(1);
    1.44 -						String value = lc.matcher.group(2);
    1.45 +						String key = lc.matcher.group("key");
    1.46 +						String value = lc.matcher.group("value");
    1.47  
    1.48 -						if (lc.matcher.groupCount() == 4) {
    1.49 -							String comment = lc.matcher.group(4);
    1.50 +						try {
    1.51 +							String comment = lc.matcher.group("comment");
    1.52  							// TODO: comment → LexicalHandler
    1.53  							log.log(Level.WARNING, "Comment for entry „{0}“ is: {1}", new Object[]{key, comment});
    1.54 +
    1.55 +						} catch (IllegalArgumentException e) {
    1.56 +							log.log(Level.WARNING, "Entry „{0}“ has no comment", key);
    1.57 +
    1.58  						}
    1.59  
    1.60  						fc.contentHandler.indentation(2);