Skip to content

Commit

Permalink
#354: tiny refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
unixoid committed Jan 19, 2025
1 parent b3e88dc commit d8b56dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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.
*
Expand All @@ -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 <code>null</code>, when the given string does
Expand Down Expand Up @@ -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();
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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));
}

Expand All @@ -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);
}
}

0 comments on commit d8b56dd

Please sign in to comment.