-
Notifications
You must be signed in to change notification settings - Fork 21
XML to TOON #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
badpirogrammer2
wants to merge
8
commits into
toon-format:main
Choose a base branch
from
badpirogrammer2:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
XML to TOON #27
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
00f4b97
XML to TOON Conversion Summary in JToon
badpirogrammer2 47a8ad3
reverted the version to java 17 and moved un necessary classes.
badpirogrammer2 743edec
implemented xmlmapper
badpirogrammer2 c4d6b67
New test cases added in a nested class
badpirogrammer2 a2127e8
readme updated
badpirogrammer2 11c460c
Merge branch 'main' into main
badpirogrammer2 15cdc00
XML Test Cases Added
badpirogrammer2 e4fe5e5
corrected the mistake
badpirogrammer2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/felipestanzani/jtoon/normalizer/XmlNormalizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.felipestanzani.jtoon.normalizer; | ||
|
|
||
| import tools.jackson.databind.JsonNode; | ||
| import tools.jackson.dataformat.xml.XmlMapper; | ||
|
|
||
| /** | ||
| * Normalizes XML strings to Jackson JsonNode representation. | ||
| * Converts XML structure to JSON-compatible format for TOON encoding. | ||
| */ | ||
| public final class XmlNormalizer { | ||
|
|
||
| private static final XmlMapper XML_MAPPER = XmlMapper.builder().build(); | ||
|
|
||
| private XmlNormalizer() { | ||
| throw new UnsupportedOperationException("Utility class cannot be instantiated"); | ||
| } | ||
|
|
||
| /** | ||
| * Parses an XML string into a JsonNode using the shared XmlMapper. | ||
| * <p> | ||
| * This centralizes XML parsing concerns to keep the public API thin and | ||
| * maintain separation of responsibilities between parsing, normalization, | ||
| * and encoding. | ||
| * </p> | ||
| * | ||
| * @param xml The XML string to parse (must be valid XML) | ||
| * @return Parsed JsonNode | ||
| * @throws IllegalArgumentException if the input is blank or not valid XML | ||
| */ | ||
| public static JsonNode parse(String xml) { | ||
| if (xml == null || xml.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("Invalid XML"); | ||
| } | ||
| try { | ||
| return XML_MAPPER.readTree(xml); | ||
| } catch (Exception e) { | ||
| throw new IllegalArgumentException("Invalid XML", e); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -747,6 +747,115 @@ void noTrailingNewline() { | |
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("XML tests") | ||
| class XmlTests { | ||
|
|
||
| @Test | ||
| @DisplayName("encodes XML with custom options") | ||
| void encodesXmlWithOptions() { | ||
| String xml = "<user><id>123</id><name>Ada</name></user>"; | ||
| EncodeOptions options = new EncodeOptions(4, Delimiter.PIPE, true); | ||
| String result = JToon.encodeXml(xml, options); | ||
| assertEquals("id: \"123\"\nname: Ada", result); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("throws exception for invalid XML") | ||
| void throwsForInvalidXml() { | ||
| String invalidXml = "<user><id>123</id><name>Ada</name>"; | ||
| assertThrows(IllegalArgumentException.class, () -> JToon.encodeXml(invalidXml)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("throws exception for null XML") | ||
| void throwsForNullXml() { | ||
| assertThrows(IllegalArgumentException.class, () -> JToon.encodeXml(null)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("throws exception for empty XML") | ||
| void throwsForEmptyXml() { | ||
| assertThrows(IllegalArgumentException.class, () -> JToon.encodeXml("")); | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("XML structures (positive test cases)") | ||
| class XmlStructuresPositive { | ||
|
|
||
| @Test | ||
| @DisplayName("encodes XML successfully") | ||
| void encodesXmlSuccessfully() { | ||
| String xml = "<user><name>John</name><age>25</age></user>"; | ||
| String result = JToon.encodeXml(xml); | ||
| assertEquals("name: John\nage: \"25\"", result); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("encodes complex XML successfully") | ||
| void encodesComplexXmlSuccessfully() { | ||
| String xml = "<company><name>TechCorp</name><employees><employee><name>Alice</name></employee></employees></company>"; | ||
| String result = JToon.encodeXml(xml); | ||
| assertEquals("name: TechCorp\nemployees:\n employee:\n name: Alice", result); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("encodes XML with attributes") | ||
| void encodesXmlWithAttributes() { | ||
| String xml = "<user id=\"123\" active=\"true\"><name>John</name><email>[email protected]</email></user>"; | ||
| String result = JToon.encodeXml(xml); | ||
| assertEquals("id: \"123\"\nactive: \"true\"\nname: John\nemail: [email protected]", result); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("encodes deeply nested XML with arrays") | ||
| void encodesDeeplyNestedXmlWithArrays() { | ||
| String xml = "<company><name>TechCorp</name><departments><department><name>Engineering</name><employees><employee><name>Alice</name><role>Developer</role></employee><employee><name>Bob</name><role>Manager</role></employee></employees></department><department><name>Marketing</name><employees><employee><name>Carol</name><role>Director</role></employee></employees></department></departments></company>"; | ||
| String result = JToon.encodeXml(xml); | ||
| assertEquals("name: TechCorp\ndepartments:\n department[2]:\n - name: Engineering\n employees:\n employee[2]{name,role}:\n Alice,Developer\n Bob,Manager\n - name: Marketing\n employees:\n employee:\n name: Carol\n role: Director", result); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("encodes XML with mixed content and attributes") | ||
| void encodesXmlWithMixedContentAndAttributes() { | ||
| String xml = "<book isbn=\"978-3-16-148410-0\" category=\"fiction\"><title>The Great Novel</title><author status=\"bestselling\">Jane Doe</author><chapters><chapter number=\"1\" title=\"Introduction\">Welcome to the story</chapter><chapter number=\"2\" title=\"Development\">The plot thickens</chapter></chapters></book>"; | ||
| String result = JToon.encodeXml(xml); | ||
| String expected = "isbn: 978-3-16-148410-0\ncategory: fiction\ntitle: The Great Novel\nauthor:\n status: bestselling\n \"\": Jane Doe\nchapters:\n chapter[2]{number,title,\"\"}:\n \"1\",Introduction,Welcome to the story\n \"2\",Development,The plot thickens"; | ||
| assertEquals(expected, result); | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("XML error handling (negative test cases)") | ||
| class XmlErrorHandling { | ||
|
|
||
| @Test | ||
| @DisplayName("throws exception for invalid XML") | ||
| void throwsForInvalidXml() { | ||
| String invalidXml = "<user><id>123</id><name>Ada</name>"; | ||
| assertThrows(IllegalArgumentException.class, () -> JToon.encodeXml(invalidXml)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("throws exception for null XML input") | ||
| void throwsForNullXml() { | ||
| assertThrows(IllegalArgumentException.class, () -> JToon.encodeXml(null)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("throws exception for empty XML string") | ||
| void throwsForEmptyXml() { | ||
| assertThrows(IllegalArgumentException.class, () -> JToon.encodeXml("")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("throws exception for whitespace-only XML") | ||
| void throwsForWhitespaceOnlyXml() { | ||
| assertThrows(IllegalArgumentException.class, () -> JToon.encodeXml(" ")); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Nested | ||
| @DisplayName("non-JSON-serializable values") | ||
| class NonJson { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.