java/alt2xml-in-properties/src/cz/frantovo/alt2xml/in/properties/Reader.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 02 Oct 2016 11:46:19 +0200
changeset 107 c5a987931ef9
parent 75 ed9ec17aa67f
child 111 e4900596abdb
permissions -rw-r--r--
in-properties: fix typo in comment
     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.properties;
    19 
    20 import cz.frantovo.alt2xml.AbstractAlt2XmlReader;
    21 import java.io.BufferedReader;
    22 import java.io.IOException;
    23 import java.io.InputStreamReader;
    24 import java.io.StringReader;
    25 import java.nio.charset.StandardCharsets;
    26 import java.util.Map.Entry;
    27 import java.util.Properties;
    28 import java.util.logging.Level;
    29 import java.util.logging.Logger;
    30 import org.xml.sax.InputSource;
    31 import org.xml.sax.SAXException;
    32 import org.xml.sax.helpers.AttributesImpl;
    33 
    34 /**
    35  *
    36  * @author Ing. František Kučera (frantovo.cz)
    37  */
    38 public class Reader extends AbstractAlt2XmlReader {
    39 
    40 	public static final String PROPERTY_BATCH_MODE = constructPropertyName("properties", "batch-mode");
    41 	private static final Logger log = Logger.getLogger(Reader.class.getName());
    42 
    43 	/**
    44 	 * <p>
    45 	 * TODO: same format as Java XML properties
    46 	 * </p>
    47 	 * <p>
    48 	 * TODO: support also nested mode <code>aaa.bbb.ccc=xxx</code> →
    49 	 * <code>&lt;aaa&gt;&lt;bbb&gt;&lt;ccc&gt;xxx&lt;/ccc&gt;&lt;/bbb&gt;&lt;/aaa&gt;</code></p>
    50 	 */
    51 	@Override
    52 	public void parse(InputSource input) throws IOException, SAXException {
    53 		if (isInBatchMode()) {
    54 			log.log(Level.FINE, "Reading in batch mode");
    55 			parseAsBatch(input);
    56 		} else {
    57 			log.log(Level.FINE, "Reading in streaming mode");
    58 			parseAsStream(input);
    59 		}
    60 	}
    61 
    62 	private void parseAsBatch(InputSource input) throws IOException, SAXException {
    63 		Properties loadedData = new Properties();
    64 		loadedData.load(input.getByteStream());
    65 
    66 		outputStart();
    67 		outputProperties(loadedData);
    68 		outputEnd();
    69 	}
    70 
    71 	private void parseAsStream(InputSource input) throws IOException, SAXException {
    72 
    73 		outputStart();
    74 
    75 		try (BufferedReader br = new BufferedReader(new InputStreamReader(input.getByteStream(), StandardCharsets.ISO_8859_1))) {
    76 			StringBuilder continuingLine = null;
    77 			Properties loadedData = new Properties();
    78 			for (String currentLine = br.readLine(); currentLine != null; currentLine = br.readLine()) {
    79 				if (currentLine.endsWith("\\")) {
    80 					if (continuingLine == null) {
    81 						continuingLine = new StringBuilder();
    82 					}
    83 					continuingLine.append(currentLine);
    84 				} else {
    85 					String wholeLogicalLine = (continuingLine == null) ? currentLine : continuingLine.append("\n").append(currentLine).toString();
    86 					continuingLine = null;
    87 
    88 					loadedData.load(new StringReader(wholeLogicalLine));
    89 					outputProperties(loadedData);
    90 					loadedData.clear();
    91 				}
    92 			}
    93 		}
    94 
    95 		outputEnd();
    96 	}
    97 
    98 	/**
    99 	 * Default mode is streaming.
   100 	 *
   101 	 * @return false = do streaming | true = parse whole input as one batch and when all parsed,
   102 	 * flush output
   103 	 */
   104 	private boolean isInBatchMode() {
   105 		return Boolean.parseBoolean(String.valueOf(properties.get(PROPERTY_BATCH_MODE)));
   106 	}
   107 
   108 	private void outputStart() throws SAXException {
   109 		contentHandler.startDocument();
   110 		contentHandler.startElement(null, null, "properties", null);
   111 
   112 		contentHandler.lineBreak();
   113 	}
   114 
   115 	private void outputProperty(String key, String value) throws SAXException {
   116 		contentHandler.indentation(1);
   117 
   118 		AttributesImpl attributes = new AttributesImpl();
   119 		attributes.addAttribute(null, "name", "name", "xs:string", key);
   120 
   121 		contentHandler.startElement(null, null, "property", attributes);
   122 		contentHandler.characters(value.toCharArray(), 0, value.length());
   123 		contentHandler.endElement(null, null, "property");
   124 
   125 		contentHandler.lineBreak();
   126 	}
   127 
   128 	private void outputProperties(Properties loadedData) throws SAXException {
   129 		for (Entry<Object, Object> e : loadedData.entrySet()) {
   130 			String key = (String) e.getKey();
   131 			String value = (String) e.getValue();
   132 			outputProperty(key, value);
   133 		}
   134 	}
   135 
   136 	private void outputEnd() throws SAXException {
   137 		contentHandler.endElement(null, null, "properties");
   138 		contentHandler.lineBreak();
   139 		contentHandler.endDocument();
   140 	}
   141 }