From d8b56ddfb13b53cadde66a4b3327ab2fabc85f90 Mon Sep 17 00:00:00 2001 From: Dmytro Rud Date: Sun, 19 Jan 2025 16:57:20 +0100 Subject: [PATCH] #354: tiny refactoring --- .../openehealth/ipf/commons/xml/XmlUtils.java | 10 +++---- .../svs/core/converters/SvsConverters.java | 26 +++++-------------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/commons/xml/src/main/java/org/openehealth/ipf/commons/xml/XmlUtils.java b/commons/xml/src/main/java/org/openehealth/ipf/commons/xml/XmlUtils.java index 0853c82ac4..eeb60dc69a 100644 --- a/commons/xml/src/main/java/org/openehealth/ipf/commons/xml/XmlUtils.java +++ b/commons/xml/src/main/java/org/openehealth/ipf/commons/xml/XmlUtils.java @@ -30,6 +30,7 @@ import java.io.ByteArrayOutputStream; import java.io.StringReader; import java.io.StringWriter; +import java.nio.charset.StandardCharsets; import java.util.regex.Pattern; /** @@ -41,17 +42,15 @@ abstract public class XmlUtils { private static final Pattern ROOT_ELEMENT_PATTERN = Pattern.compile( "(?:<\\?xml.+?\\?>)?" + // optional prolog - "(?:\\s*)*" + // optional comments - "\\s*<(?:[\\w.-]+?:)?([\\w.-]+)(?:\\s|(?:/?>))", // open tag of the root element + "(?:\\s*)*" + // optional comments + "\\s*<(?:[\\w.-]+?:)?([\\w.-]+)(?:\\s|(?:/?>))", // open tag of the root element Pattern.DOTALL ); - private XmlUtils() { throw new IllegalStateException("Cannot instantiate helper class"); } - /** * Creates an XML Source from the given XML String. * @@ -62,7 +61,6 @@ public static Source source(String s) { return new StreamSource(new StringReader(s)); } - /** * Returns local name of the root element of the XML document represented * by the given string, or null, when the given string does @@ -92,6 +90,7 @@ public static String renderJaxb(JAXBContext jaxbContext, Object object, Boolean var marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, prettyPrint); + marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name()); var writer = new StringWriter(); marshaller.marshal(object, writer); return writer.toString(); @@ -113,7 +112,6 @@ public static byte[] serialize(Node inputNode) throws Exception { Source sourceObject = new DOMSource(inputNode); Result targetObject = new StreamResult(serializerOutput); - var serializerFactory = TransformerFactory.newInstance(); var serializer = serializerFactory.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); diff --git a/platform-camel/ihe/svs/src/main/java/org/openehealth/ipf/platform/camel/ihe/svs/core/converters/SvsConverters.java b/platform-camel/ihe/svs/src/main/java/org/openehealth/ipf/platform/camel/ihe/svs/core/converters/SvsConverters.java index da9cf4dc5d..80267b45f6 100644 --- a/platform-camel/ihe/svs/src/main/java/org/openehealth/ipf/platform/camel/ihe/svs/core/converters/SvsConverters.java +++ b/platform-camel/ihe/svs/src/main/java/org/openehealth/ipf/platform/camel/ihe/svs/core/converters/SvsConverters.java @@ -22,10 +22,10 @@ import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; -import jakarta.xml.bind.Marshaller; +import org.openehealth.ipf.commons.xml.XmlUtils; + import java.io.ByteArrayInputStream; import java.io.InputStream; -import java.io.StringWriter; import java.nio.charset.StandardCharsets; /** @@ -54,18 +54,12 @@ public static RetrieveValueSetRequest xmlToSvsQuery(final String xml) throws JAX } @Converter - public static String svsQueryToXml(final RetrieveValueSetRequest query) throws JAXBException { - var marshaller = JAXB_CONTEXT_SVS_REQUEST.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); - marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF8"); - var stringWriter = new StringWriter(); - marshaller.marshal(query, stringWriter); - return stringWriter.toString(); + public static String svsQueryToXml(final RetrieveValueSetRequest query) { + return XmlUtils.renderJaxb(JAXB_CONTEXT_SVS_REQUEST, query, true); } @Converter - public static InputStream svsQueryToInputStream(final RetrieveValueSetRequest query) throws JAXBException { + public static InputStream svsQueryToInputStream(final RetrieveValueSetRequest query) { return new ByteArrayInputStream(svsQueryToXml(query).getBytes(StandardCharsets.UTF_8)); } @@ -76,13 +70,7 @@ public static RetrieveValueSetResponse xmlToSvsResponse(final String xml) throws } @Converter - public static String svsResponseToXml(final RetrieveValueSetResponse response) throws JAXBException { - var marshaller = JAXB_CONTEXT_SVS_RESPONSE.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); - marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF8"); - var stringWriter = new StringWriter(); - marshaller.marshal(response, stringWriter); - return stringWriter.toString(); + public static String svsResponseToXml(final RetrieveValueSetResponse response) { + return XmlUtils.renderJaxb(JAXB_CONTEXT_SVS_RESPONSE, response, true); } } \ No newline at end of file