in-ini: recognize comment, section and entry + log them
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 18:12:15 +0200
changeset 7736ee0aefea27
parent 76 56b731d14aec
child 78 afcb4ccc6594
in-ini: recognize comment, section and entry + log them
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 17:19:21 2014 +0200
     1.2 +++ b/java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java	Sat Sep 06 18:12:15 2014 +0200
     1.3 @@ -18,7 +18,14 @@
     1.4  package cz.frantovo.alt2xml.in.ini;
     1.5  
     1.6  import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
     1.7 +import cz.frantovo.alt2xml.in.Alt2ContentHandler;
     1.8 +import java.io.BufferedReader;
     1.9  import java.io.IOException;
    1.10 +import java.io.InputStreamReader;
    1.11 +import java.util.logging.Level;
    1.12 +import java.util.logging.Logger;
    1.13 +import java.util.regex.Matcher;
    1.14 +import java.util.regex.Pattern;
    1.15  import org.xml.sax.InputSource;
    1.16  import org.xml.sax.SAXException;
    1.17  
    1.18 @@ -28,17 +35,32 @@
    1.19   */
    1.20  public class Reader extends AbstractAlt2XmlReader {
    1.21  
    1.22 +	public static final String ROOT_ELEMENT = "ini";
    1.23 +	private static final Logger log = Logger.getLogger(Reader.class.getName());
    1.24 +
    1.25  	@Override
    1.26  	public void parse(InputSource input) throws IOException, SAXException {
    1.27  		outputStart();
    1.28 -		// TODO: INI → SAX
    1.29 +
    1.30 +		try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
    1.31 +			FileContext c = new FileContext(contentHandler);
    1.32 +			for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
    1.33 +				for (LINE_TYPE lineType : LINE_TYPE.values()) {
    1.34 +					boolean lineProcessed = lineType.processLine(currentLine, c);
    1.35 +					if (lineProcessed) {
    1.36 +						break;
    1.37 +					}
    1.38 +				}
    1.39 +			}
    1.40 +		}
    1.41 +
    1.42  		outputEnd();
    1.43  	}
    1.44  
    1.45  	private void outputStart() throws SAXException {
    1.46  		contentHandler.startDocument();
    1.47  		contentHandler.lineBreak();
    1.48 -		contentHandler.startElement(null, null, "ini", null);
    1.49 +		contentHandler.startElement(null, null, ROOT_ELEMENT, null);
    1.50  		contentHandler.lineBreak();
    1.51  	}
    1.52  
    1.53 @@ -48,4 +70,70 @@
    1.54  		contentHandler.endDocument();
    1.55  	}
    1.56  
    1.57 +	private static class FileContext {
    1.58 +
    1.59 +		private final Alt2ContentHandler contentHandler;
    1.60 +
    1.61 +		public FileContext(Alt2ContentHandler contentHandler) {
    1.62 +			this.contentHandler = contentHandler;
    1.63 +		}
    1.64 +	}
    1.65 +
    1.66 +	private static class LineContext {
    1.67 +
    1.68 +		private final String line;
    1.69 +		private final Matcher matcher;
    1.70 +
    1.71 +		public LineContext(String line, Matcher matcher) {
    1.72 +			this.line = line;
    1.73 +			this.matcher = matcher;
    1.74 +		}
    1.75 +	}
    1.76 +
    1.77 +	private enum LINE_TYPE {
    1.78 +
    1.79 +		COMMENT("(;|#)\\s*(.*)") {
    1.80 +
    1.81 +					@Override
    1.82 +					public void processLine(LineContext lineContext, FileContext fileContext) {
    1.83 +						log.log(Level.FINER, "Comment: {0}", lineContext.matcher.group(2));
    1.84 +					}
    1.85 +
    1.86 +				},
    1.87 +		SECTION("\\[(.+)\\]") {
    1.88 +
    1.89 +					@Override
    1.90 +					public void processLine(LineContext lineContext, FileContext fileContext) {
    1.91 +						log.log(Level.WARNING, "Section: {0}", lineContext.matcher.group(1));
    1.92 +					}
    1.93 +
    1.94 +				},
    1.95 +		ENTRY("\\s*([^=]+)\\s*=\\s*(.+)") {
    1.96 +
    1.97 +					@Override
    1.98 +					public void processLine(LineContext lineContext, FileContext fileContext) {
    1.99 +						log.log(Level.WARNING, "Entry: {0} = {1}", new Object[]{lineContext.matcher.group(1), lineContext.matcher.group(2)});
   1.100 +					}
   1.101 +
   1.102 +				},;
   1.103 +
   1.104 +		private LINE_TYPE(String pattern) {
   1.105 +			this.pattern = Pattern.compile(pattern);
   1.106 +		}
   1.107 +
   1.108 +		private final Pattern pattern;
   1.109 +
   1.110 +		protected boolean processLine(String currentLine, FileContext fileContext) {
   1.111 +			Matcher m = pattern.matcher(currentLine);
   1.112 +			if (m.matches()) {
   1.113 +				processLine(new LineContext(currentLine, m), fileContext);
   1.114 +				return true;
   1.115 +			} else {
   1.116 +				return false;
   1.117 +			}
   1.118 +		}
   1.119 +
   1.120 +		public abstract void processLine(LineContext lineContext, FileContext fileContext);
   1.121 +	}
   1.122 +
   1.123  }