java/alt2xml-in-ini/src/cz/frantovo/alt2xml/in/ini/Reader.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 18:12:15 +0200
changeset 77 36ee0aefea27
parent 76 56b731d14aec
child 78 afcb4ccc6594
permissions -rw-r--r--
in-ini: recognize comment, section and entry + log them
     1 /**
     2  * Alt2XML
     3  * Copyright © 2014 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package cz.frantovo.alt2xml.in.ini;
    19 
    20 import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
    21 import cz.frantovo.alt2xml.in.Alt2ContentHandler;
    22 import java.io.BufferedReader;
    23 import java.io.IOException;
    24 import java.io.InputStreamReader;
    25 import java.util.logging.Level;
    26 import java.util.logging.Logger;
    27 import java.util.regex.Matcher;
    28 import java.util.regex.Pattern;
    29 import org.xml.sax.InputSource;
    30 import org.xml.sax.SAXException;
    31 
    32 /**
    33  *
    34  * @author Ing. František Kučera (frantovo.cz)
    35  */
    36 public class Reader extends AbstractAlt2XmlReader {
    37 
    38 	public static final String ROOT_ELEMENT = "ini";
    39 	private static final Logger log = Logger.getLogger(Reader.class.getName());
    40 
    41 	@Override
    42 	public void parse(InputSource input) throws IOException, SAXException {
    43 		outputStart();
    44 
    45 		try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream()))) {
    46 			FileContext c = new FileContext(contentHandler);
    47 			for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
    48 				for (LINE_TYPE lineType : LINE_TYPE.values()) {
    49 					boolean lineProcessed = lineType.processLine(currentLine, c);
    50 					if (lineProcessed) {
    51 						break;
    52 					}
    53 				}
    54 			}
    55 		}
    56 
    57 		outputEnd();
    58 	}
    59 
    60 	private void outputStart() throws SAXException {
    61 		contentHandler.startDocument();
    62 		contentHandler.lineBreak();
    63 		contentHandler.startElement(null, null, ROOT_ELEMENT, null);
    64 		contentHandler.lineBreak();
    65 	}
    66 
    67 	private void outputEnd() throws SAXException {
    68 		contentHandler.endElement(null, null, "ini");
    69 		contentHandler.lineBreak();
    70 		contentHandler.endDocument();
    71 	}
    72 
    73 	private static class FileContext {
    74 
    75 		private final Alt2ContentHandler contentHandler;
    76 
    77 		public FileContext(Alt2ContentHandler contentHandler) {
    78 			this.contentHandler = contentHandler;
    79 		}
    80 	}
    81 
    82 	private static class LineContext {
    83 
    84 		private final String line;
    85 		private final Matcher matcher;
    86 
    87 		public LineContext(String line, Matcher matcher) {
    88 			this.line = line;
    89 			this.matcher = matcher;
    90 		}
    91 	}
    92 
    93 	private enum LINE_TYPE {
    94 
    95 		COMMENT("(;|#)\\s*(.*)") {
    96 
    97 					@Override
    98 					public void processLine(LineContext lineContext, FileContext fileContext) {
    99 						log.log(Level.FINER, "Comment: {0}", lineContext.matcher.group(2));
   100 					}
   101 
   102 				},
   103 		SECTION("\\[(.+)\\]") {
   104 
   105 					@Override
   106 					public void processLine(LineContext lineContext, FileContext fileContext) {
   107 						log.log(Level.WARNING, "Section: {0}", lineContext.matcher.group(1));
   108 					}
   109 
   110 				},
   111 		ENTRY("\\s*([^=]+)\\s*=\\s*(.+)") {
   112 
   113 					@Override
   114 					public void processLine(LineContext lineContext, FileContext fileContext) {
   115 						log.log(Level.WARNING, "Entry: {0} = {1}", new Object[]{lineContext.matcher.group(1), lineContext.matcher.group(2)});
   116 					}
   117 
   118 				},;
   119 
   120 		private LINE_TYPE(String pattern) {
   121 			this.pattern = Pattern.compile(pattern);
   122 		}
   123 
   124 		private final Pattern pattern;
   125 
   126 		protected boolean processLine(String currentLine, FileContext fileContext) {
   127 			Matcher m = pattern.matcher(currentLine);
   128 			if (m.matches()) {
   129 				processLine(new LineContext(currentLine, m), fileContext);
   130 				return true;
   131 			} else {
   132 				return false;
   133 			}
   134 		}
   135 
   136 		public abstract void processLine(LineContext lineContext, FileContext fileContext);
   137 	}
   138 
   139 }