diff --git a/src/main/java/io/github/eocqrs/eokson/JsonXML.java b/src/main/java/io/github/eocqrs/eokson/JsonXML.java index 3baa325..fb21d58 100644 --- a/src/main/java/io/github/eocqrs/eokson/JsonXML.java +++ b/src/main/java/io/github/eocqrs/eokson/JsonXML.java @@ -24,8 +24,6 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.xml.XmlMapper; -import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; import org.cactoos.Text; /** @@ -58,15 +56,15 @@ public JsonXML(final Json jsn, final String rt) { @Override public String asString() throws Exception { - final XmlMapper xml = new XmlMapper(); final ObjectMapper mapper = new ObjectMapper(); final JsonNode node = mapper.readTree( new Jocument( this.json ).pretty() ); - xml.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); - return xml.writer() + return new WithDeclaration() + .value() + .writer() .withRootName(this.root) .writeValueAsString(node); } diff --git a/src/main/java/io/github/eocqrs/eokson/WithDeclaration.java b/src/main/java/io/github/eocqrs/eokson/WithDeclaration.java new file mode 100644 index 0000000..22b8045 --- /dev/null +++ b/src/main/java/io/github/eocqrs/eokson/WithDeclaration.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package io.github.eocqrs.eokson; + +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; + +/** + * XML Mapper With Declaration. + * + * @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) + * @since 0.3.2 + */ +final class WithDeclaration implements Scalar { + + @Override + public XmlMapper value() { + final XmlMapper xml = new XmlMapper(); + xml.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true); + return xml; + } +} diff --git a/src/test/java/io/github/eocqrs/eokson/WithDeclarationTest.java b/src/test/java/io/github/eocqrs/eokson/WithDeclarationTest.java new file mode 100644 index 0000000..1ccff47 --- /dev/null +++ b/src/test/java/io/github/eocqrs/eokson/WithDeclarationTest.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package io.github.eocqrs.eokson; + +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +/** + * Test case for {@link WithDeclaration}. + * + * @author Aliaksei Bialiauski (abialiauski.dev@gmail.com) + * @since 0.3.2 + */ +final class WithDeclarationTest { + + @Test + void readsMapperInRightFormat() { + final XmlMapper mapper = new WithDeclaration().value(); + MatcherAssert.assertThat( + "Mapper is present", + mapper, + Matchers.notNullValue() + ); + } +}