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, version 3 of the License.
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.
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/>.
17 package cz.frantovo.alt2xml.in.properties;
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;
35 * @author Ing. František Kučera (frantovo.cz)
37 public class Reader extends AbstractAlt2XmlReader {
39 public static final String PROPERTY_BATCH_MODE = constructPropertyName("properties", "batch-mode");
40 private static final Logger log = Logger.getLogger(Reader.class.getName());
44 * TODO: same format as Java XML properties
47 * TODO: support also nested mode <code>aaa.bbb.ccc=xxx</code> →
48 * <code><aaa><bbb><ccc>xxx</ccc></bbb></aaa></code></p>
51 public void parse(InputSource input) throws IOException, SAXException {
52 if (isInBatchMode()) {
53 log.log(Level.FINE, "Reading in batch mode");
56 log.log(Level.FINE, "Reading in streaming mode");
61 private void parseAsBatch(InputSource input) throws IOException, SAXException {
62 Properties loadedData = new Properties();
63 loadedData.load(input.getByteStream());
66 outputProperties(loadedData);
70 private void parseAsStream(InputSource input) throws IOException, SAXException {
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();
82 continuingLine.append(currentLine);
84 String wholeLogicalLine = (continuingLine == null) ? currentLine : continuingLine.append("\n").append(currentLine).toString();
85 continuingLine = null;
87 loadedData.load(new StringReader(wholeLogicalLine));
88 outputProperties(loadedData);
98 * Default mode is streaming.
100 * @return false = do streaming | true = parse whole input as one batch and when all parsed,
103 private boolean isInBatchMode() {
104 return Boolean.parseBoolean(String.valueOf(properties.get(PROPERTY_BATCH_MODE)));
107 private void outputStart() throws SAXException {
108 contentHandler.startDocument();
109 contentHandler.startElement(null, null, "properties", null);
111 contentHandler.lineBreak();
114 private void outputProperty(String key, String value) throws SAXException {
115 contentHandler.indentation(1);
117 AttributesImpl attributes = new AttributesImpl();
118 attributes.addAttribute(null, "name", "name", "xs:string", key);
120 contentHandler.startElement(null, null, "property", attributes);
121 contentHandler.characters(value.toCharArray(), 0, value.length());
122 contentHandler.endElement(null, null, "property");
124 contentHandler.lineBreak();
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);
135 private void outputEnd() throws SAXException {
136 contentHandler.endElement(null, null, "properties");
137 contentHandler.lineBreak();
138 contentHandler.endDocument();