3 * Copyright © 2014 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package cz.frantovo.alt2xml.in.properties;
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;
36 * @author Ing. František Kučera (frantovo.cz)
38 public class Reader extends AbstractAlt2XmlReader {
40 public static final String PROPERTY_BATCH_MODE = constructPropertyName("properties", "batch-mode");
41 private static final Logger log = Logger.getLogger(Reader.class.getName());
45 * TODO: same format as Java XML properties
48 * TODO: support also nested mode <code>aaa.bbb.ccc=xxx</code> →
49 * <code><aaa><bbb><ccc>xxx</ccc></bbb></aaa></code></p>
52 public void parse(InputSource input) throws IOException, SAXException {
53 if (isInBatchMode()) {
54 log.log(Level.FINE, "Reading in batch mode");
57 log.log(Level.FINE, "Reading in streaming mode");
62 private void parseAsBatch(InputSource input) throws IOException, SAXException {
63 Properties loadedData = new Properties();
64 loadedData.load(input.getByteStream());
67 outputProperties(loadedData);
71 private void parseAsStream(InputSource input) throws IOException, SAXException {
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();
83 continuingLine.append(currentLine);
85 String wholeLogicalLine = (continuingLine == null) ? currentLine : continuingLine.append("\n").append(currentLine).toString();
86 continuingLine = null;
88 loadedData.load(new StringReader(wholeLogicalLine));
89 outputProperties(loadedData);
99 * Default mode is streaming.
101 * @return false = do streaming | true = parse whole input as one batch and when all parsed,
104 private boolean isInBatchMode() {
105 return Boolean.parseBoolean(String.valueOf(properties.get(PROPERTY_BATCH_MODE)));
108 private void outputStart() throws SAXException {
109 contentHandler.startDocument();
110 contentHandler.startElement(null, null, "properties", null);
112 contentHandler.lineBreak();
115 private void outputProperty(String key, String value) throws SAXException {
116 contentHandler.indentation(1);
118 AttributesImpl attributes = new AttributesImpl();
119 attributes.addAttribute(null, "name", "name", "xs:string", key);
121 contentHandler.startElement(null, null, "property", attributes);
122 contentHandler.characters(value.toCharArray(), 0, value.length());
123 contentHandler.endElement(null, null, "property");
125 contentHandler.lineBreak();
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);
136 private void outputEnd() throws SAXException {
137 contentHandler.endElement(null, null, "properties");
138 contentHandler.lineBreak();
139 contentHandler.endDocument();