in-ini: quoted/apostrophed values → include spaces (unquoted → strip spaces)
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 19:10:52 +0200
changeset 81a4335757acf1
parent 80 c5816dd82cbb
child 82 547f1246fe35
in-ini: quoted/apostrophed values → include spaces (unquoted → strip spaces)
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 18:53:33 2014 +0200
     1.2 +++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 19:10:52 2014 +0200
     1.3 @@ -22,6 +22,8 @@
     1.4  import java.io.BufferedReader;
     1.5  import java.io.IOException;
     1.6  import java.io.InputStreamReader;
     1.7 +import java.util.ArrayList;
     1.8 +import java.util.List;
     1.9  import java.util.logging.Level;
    1.10  import java.util.logging.Logger;
    1.11  import java.util.regex.Matcher;
    1.12 @@ -98,11 +100,9 @@
    1.13  
    1.14  	private static class LineContext {
    1.15  
    1.16 -		private final String line;
    1.17  		private final Matcher matcher;
    1.18  
    1.19 -		public LineContext(String line, Matcher matcher) {
    1.20 -			this.line = line;
    1.21 +		public LineContext(Matcher matcher) {
    1.22  			this.matcher = matcher;
    1.23  		}
    1.24  	}
    1.25 @@ -110,7 +110,6 @@
    1.26  	private enum LINE_TYPE {
    1.27  
    1.28  		COMMENT("\\s*(;|#)\\s*(.*)") {
    1.29 -
    1.30  					@Override
    1.31  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.32  						log.log(Level.FINER, "Comment: {0}", lc.matcher.group(2));
    1.33 @@ -118,7 +117,6 @@
    1.34  
    1.35  				},
    1.36  		SECTION("\\s*\\[\\s*([^\\s\\]]+)\\s*\\]\\s*") {
    1.37 -
    1.38  					@Override
    1.39  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.40  						String name = lc.matcher.group(1);
    1.41 @@ -128,8 +126,11 @@
    1.42  					}
    1.43  
    1.44  				},
    1.45 -		ENTRY("\\s*([^=\\s]+)\\s*=\\s*(.+)") {
    1.46 -
    1.47 +		ENTRY(
    1.48 +				"\\s*([^=\\s]+)\\s*=\\s*\"([^\"]+)\"", // quoted value → include spaces
    1.49 +				"\\s*([^=\\s]+)\\s*=\\s*'([^']+)'", // apostrophed value → include spaces
    1.50 +				"\\s*([^=\\s]+)\\s*=\\s*(.+)" // unquoted value → strip spaces
    1.51 +		) {
    1.52  					@Override
    1.53  					public void processLine(LineContext lc, FileContext fc) throws SAXException {
    1.54  						String key = lc.matcher.group(1);
    1.55 @@ -143,20 +144,23 @@
    1.56  
    1.57  				},;
    1.58  
    1.59 -		private LINE_TYPE(String pattern) {
    1.60 -			this.pattern = Pattern.compile(pattern);
    1.61 +		private LINE_TYPE(String... patterns) {
    1.62 +			for (String pattern : patterns) {
    1.63 +				this.patterns.add(Pattern.compile(pattern));
    1.64 +			}
    1.65  		}
    1.66  
    1.67 -		private final Pattern pattern;
    1.68 +		private final List<Pattern> patterns = new ArrayList<>();
    1.69  
    1.70  		protected boolean processLine(String currentLine, FileContext fileContext) throws SAXException {
    1.71 -			Matcher m = pattern.matcher(currentLine);
    1.72 -			if (m.matches()) {
    1.73 -				processLine(new LineContext(currentLine, m), fileContext);
    1.74 -				return true;
    1.75 -			} else {
    1.76 -				return false;
    1.77 +			for (Pattern pattern : patterns) {
    1.78 +				Matcher m = pattern.matcher(currentLine);
    1.79 +				if (m.matches()) {
    1.80 +					processLine(new LineContext(m), fileContext);
    1.81 +					return true;
    1.82 +				}
    1.83  			}
    1.84 +			return false;
    1.85  		}
    1.86  
    1.87  		public abstract void processLine(LineContext lc, FileContext fc) throws SAXException;