Commit a0ac4c81 authored by Stephan Kreutzer's avatar Stephan Kreutzer

json_to_xml_2: Issue #19, parser made entirely stream-based.

parent 139e1d1b
/* Copyright (C) 2018 Stephan Kreutzer
/* Copyright (C) 2018-2020 Stephan Kreutzer
*
* This file is part of json_to_xml_2, a submodule of the
* digital_publishing_workflow_tools package.
......@@ -16,7 +16,7 @@
* along with json_to_xml_2. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file $/json_to_xml/json_to_xml_2/JsonNode.java
* @file $/json_to_xml/json_to_xml_2/JsonToken.java
* @brief Carries information about a JSON token.
* @author Stephan Kreutzer
* @since 2018-04-13
......@@ -24,9 +24,9 @@
public class JsonNode
public class JsonToken
{
public JsonNode(String token, boolean isWhitespace)
public JsonToken(String token, boolean isWhitespace)
{
this.token = token;
this.isWhitespace = isWhitespace;
......
This diff is collapsed.
......@@ -475,8 +475,6 @@ public class json_to_xml_2
}
List<JsonNode> tokens = new ArrayList<JsonNode>();
try
{
BufferedReader reader = new BufferedReader(
......@@ -486,84 +484,41 @@ public class json_to_xml_2
try
{
int character = reader.read();
String buffer = new String();
boolean isWhitespace = false;
while (character >= 0)
try
{
if (character == '{' ||
character == '}' ||
character == '"' ||
character == ':' ||
character == '[' ||
character == ']' ||
character == ',')
{
if (buffer.isEmpty() != true)
{
tokens.add(new JsonNode(buffer, isWhitespace));
buffer = "";
isWhitespace = false;
}
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(outputFile),
"UTF-8"));
tokens.add(new JsonNode(new String() + (char)character, isWhitespace));
}
else if (character == '\\')
try
{
if (buffer.isEmpty() != true)
{
tokens.add(new JsonNode(buffer, isWhitespace));
buffer = "";
isWhitespace = false;
}
tokens.add(new JsonNode(new String() + (char)character, isWhitespace));
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
writer.append("<!-- This file was created by json_to_xml_2, which is free software licensed under the GNU Affero General Public License 3 or any later version (see https://gitlab.com/publishing-systems/clients/ and https://publishing-systems.org). -->\n");
character = reader.read();
ParserJson parser = new ParserJson();
if (character >= 0)
{
// isWhitespace, ignored for convenience reasons, likely irrelevant
// for escape sequences anyway.
tokens.add(new JsonNode(new String() + (char)character, false));
}
else
if (parser.parse(reader, getInfoMessages(), writer) != 0)
{
break;
throw constructTermination("messageParserErrorWhileParsing", null, null);
}
}
else if (Character.isWhitespace(character) == true)
finally
{
if (isWhitespace != true &&
buffer.isEmpty() != true)
{
tokens.add(new JsonNode(buffer, isWhitespace));
buffer = "";
}
buffer += (char)character;
isWhitespace = true;
writer.close();
}
else
{
if (isWhitespace == true &&
buffer.isEmpty() != true)
{
tokens.add(new JsonNode(buffer, isWhitespace));
buffer = "";
isWhitespace = false;
}
buffer += (char)character;
}
character = reader.read();
}
if (buffer.isEmpty() != true)
catch (FileNotFoundException ex)
{
tokens.add(new JsonNode(buffer, isWhitespace));
throw constructTermination("messageParserErrorWhileParsing", ex, null);
}
catch (UnsupportedEncodingException ex)
{
throw constructTermination("messageParserErrorWhileParsing", ex, null);
}
catch (IOException ex)
{
throw constructTermination("messageParserErrorWhileParsing", ex, null);
}
}
finally
......@@ -584,51 +539,6 @@ public class json_to_xml_2
throw constructTermination("messageTokenizerErrorWhileTokenizing", ex, null);
}
/*
for (int i = 0; i < tokens.size(); i++)
{
System.out.println(i + ": \"" + tokens.get(i).getToken() + "\" (" + tokens.get(i).isWhitespace() + ").");
}
*/
ParserJson parser = new ParserJson(tokens, getInfoMessages());
try
{
BufferedWriter outputWriter = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(outputFile),
"UTF-8"));
try
{
outputWriter.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
outputWriter.append("<!-- This file was created by json_to_xml_2, which is free software licensed under the GNU Affero General Public License 3 or any later version (see https://gitlab.com/publishing-systems/clients/ and https://publishing-systems.org). -->\n");
if (parser.parse(outputWriter) != 0)
{
throw constructTermination("messageParserErrorWhileParsing", null, null);
}
}
finally
{
outputWriter.close();
}
}
catch (FileNotFoundException ex)
{
throw constructTermination("messageParserErrorWhileParsing", ex, null);
}
catch (UnsupportedEncodingException ex)
{
throw constructTermination("messageParserErrorWhileParsing", ex, null);
}
catch (IOException ex)
{
throw constructTermination("messageParserErrorWhileParsing", ex, null);
}
return 0;
}
......
# Copyright (C) 2018-2019 Stephan Kreutzer
# Copyright (C) 2018-2020 Stephan Kreutzer
#
# This file is part of json_to_xml_2, a submodule of the
# digital_publishing_workflow_tools package.
......@@ -20,7 +20,6 @@ messageParserUnknownTokenInObject = Parser: Unknown token "{0}" in object.
messageParserUnknownTokenInArray = Parser: Unknown token "{0}" in array.
messageParserUnsupportedEscapeSequence = Parser: Escape sequence "{0}" not supported.
messageParserEscapeSequenceIncomplete = Parser: Escape sequence "{0}" incomplete.
messageParserEscapeSequenceAtStart = Parser: Escape sequence "{0}" found at start.
messageParserDoubleQuoteInTextLiteral = Parser: Double quotes in text literal.
messageParserMoreTextAfterWhitespaceInTextLiteral = Parser: More text after whitespace following a text literal.
messageParsingError = Parsing error: "{0}" expected, found "{1}".
......
# Copyright (C) 2018-2019 Stephan Kreutzer
# Copyright (C) 2018-2020 Stephan Kreutzer
#
# This file is part of json_to_xml_2, a submodule of the
# digital_publishing_workflow_tools package.
......@@ -20,7 +20,6 @@ messageParserUnknownTokenInObject = Parser: Unbekanntes Token "{0}" in Objekt.
messageParserUnknownTokenInArray = Parser: Unbekanntes Token "{0}" in Array.
messageParserUnsupportedEscapeSequence = Parser: Escape-Sequenz "{0}" wird nicht unterstützt.
messageParserEscapeSequenceIncomplete = Parser: Escape-Sequenz "{0}" unvollständig.
messageParserEscapeSequenceAtStart = Parser: Escape-Sequenz "{0}" am Start vorgefunden.
messageParserDoubleQuoteInTextLiteral = Parser: Anführungszeichen in Textliteral.
messageParserMoreTextAfterWhitespaceInTextLiteral = Parser: Weiterer Text nach Leerraum-Zeichen, der einem Textliteral folgte.
messageParsingError = Parsing-Fehler: "{0}" erwartet, "{1}" vorgefunden.
......
# Copyright (C) 2016-2018 Stephan Kreutzer
# Copyright (C) 2016-2020 Stephan Kreutzer
#
# This file is part of json_to_xml_2, a submodule of the
# digital_publishing_workflow_tools package.
......@@ -32,18 +32,18 @@ InfoMessage.class: InfoMessage.java
ProgramTerminationException.class: ProgramTerminationException.java
javac -encoding UTF-8 ProgramTerminationException.java
JsonNode.class: JsonNode.java
javac -encoding UTF-8 JsonNode.java
JsonToken.class: JsonToken.java
javac -encoding UTF-8 JsonToken.java
ParserJson.class: ParserJson.java
javac -encoding UTF-8 ParserJson.java
json_to_xml_2.class: json_to_xml_2.java ParserJson.class JsonNode.class InfoMessage.class ProgramTerminationException.class
json_to_xml_2.class: json_to_xml_2.java ParserJson.class JsonToken.class InfoMessage.class ProgramTerminationException.class
javac -encoding UTF-8 json_to_xml_2.java
clean:
rm -f json_to_xml_2.class
rm -f ParserJson*.class
rm -f JsonNode.class
rm -f JsonToken.class
rm -f ProgramTerminationException.class
rm -f InfoMessage.class
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment