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