From f0da9241253e4c4eada5b880b332f9b7e1ac3163 Mon Sep 17 00:00:00 2001 From: theresa Date: Thu, 19 Sep 2024 15:22:53 +0200 Subject: [PATCH] #3304 switched to JAXB for handling XML generation --- .../domain/ddiCodebook/Catgry.java | 11 ++++--- .../domain/ddiCodebook/Citation.java | 9 ++++-- .../domain/ddiCodebook/CodeBook.java | 31 +++++-------------- .../domain/ddiCodebook/DataDscr.java | 9 +++--- .../domain/ddiCodebook/FileDscr.java | 10 ++++-- .../domain/ddiCodebook/FileTxt.java | 9 ++++-- .../domain/ddiCodebook/StdyDscr.java | 7 +++-- .../domain/ddiCodebook/TextElement.java | 14 ++++++--- .../domain/ddiCodebook/TitlStmt.java | 13 +++++--- .../domain/ddiCodebook/Var.java | 20 ++++++------ .../domain/ddiCodebook/package-info.java | 21 +++++++++++++ .../service/DataPackageDdiService.java | 28 +++++++++-------- 12 files changed, 107 insertions(+), 75 deletions(-) create mode 100644 src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/package-info.java diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Catgry.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Catgry.java index 612a9083e3..abe09a709d 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Catgry.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Catgry.java @@ -2,18 +2,19 @@ import java.util.List; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import javax.xml.bind.annotation.XmlElement; + import lombok.AllArgsConstructor; @AllArgsConstructor public class Catgry { - @JacksonXmlProperty(localName = "catValu") + public Catgry() {} + + @XmlElement(name="catValu") String catValu; - @JacksonXmlProperty(localName = "labl") - @JacksonXmlElementWrapper(useWrapping = false) + @XmlElement(name="labl") List labl; } diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Citation.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Citation.java index 716c886175..9fdaa46a78 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Citation.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Citation.java @@ -1,12 +1,15 @@ package eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import javax.xml.bind.annotation.XmlElement; + import lombok.AllArgsConstructor; @AllArgsConstructor public class Citation { - @JacksonXmlProperty(localName = "titlStmt") - private TitlStmt titlStmt; + public Citation() {} + + @XmlElement(name="titlStmt") + TitlStmt titlStmt; } diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/CodeBook.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/CodeBook.java index bf759e2a84..e111d85862 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/CodeBook.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/CodeBook.java @@ -2,18 +2,14 @@ import java.util.List; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement; -import lombok.Getter; -import lombok.Setter; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; /** * Defines the mapping of the DDI Codebook standard. */ -@Getter -@Setter -@JacksonXmlRootElement(localName = "codeBook", namespace = "ddi:codebook:2_5") + +@XmlRootElement(name="codeBook") public class CodeBook { public CodeBook(StdyDscr stdyDscr, List fileDscr, DataDscr dataDscr) { @@ -22,26 +18,15 @@ public CodeBook(StdyDscr stdyDscr, List fileDscr, DataDscr dataDscr) { this.dataDscr = dataDscr; } -// @JacksonXmlProperty(localName = "xmlns", isAttribute = true) -// private String nameSpace = "ddi:codebook:2_5"; - -// @JacksonXmlProperty(localName = "xmlns:xsi", isAttribute = true) -// private String xsi = "http://www.w3.org/2001/XMLSchema-instance"; -// -// @JacksonXmlProperty(localName = "xmlns:xs", isAttribute = true) -// private String xs = "http://www.w3.org/2001/XMLSchema"; -// -// @JacksonXmlProperty(localName = "xsi:schemaLocation", isAttribute = true) -// private String schemaLocation = "http://www.ddialliance.org/Specification/DDI-Codebook/2.5/XMLSchema/codebook.xsd"; + public CodeBook() {} - @JacksonXmlProperty(localName = "stdyDscr") + @XmlElement(name="stdyDscr") private StdyDscr stdyDscr; - @JacksonXmlProperty(localName = "fileDscr") - @JacksonXmlElementWrapper(useWrapping = false) + @XmlElement(name="fileDscr") private List fileDscr; - @JacksonXmlProperty(localName = "dataDscr") + @XmlElement(name="dataDscr") private DataDscr dataDscr; } diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/DataDscr.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/DataDscr.java index 8612681a6e..9ba62f2327 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/DataDscr.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/DataDscr.java @@ -2,15 +2,16 @@ import java.util.List; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import javax.xml.bind.annotation.XmlElement; + import lombok.AllArgsConstructor; @AllArgsConstructor public class DataDscr { - @JacksonXmlProperty(localName = "var") - @JacksonXmlElementWrapper(useWrapping = false) + public DataDscr() {} + + @XmlElement(name="var") List var; } diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/FileDscr.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/FileDscr.java index c4ede7e849..242126a4d8 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/FileDscr.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/FileDscr.java @@ -1,14 +1,18 @@ package eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; + import lombok.AllArgsConstructor; @AllArgsConstructor public class FileDscr { - @JacksonXmlProperty(localName = "ID", isAttribute = true) + public FileDscr() {} + + @XmlAttribute(name="ID") String id; - @JacksonXmlProperty(localName = "fileTxt") + @XmlElement(name="fileTxt") FileTxt fileTxt; } diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/FileTxt.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/FileTxt.java index 5f9e18db2b..8950dfeb2d 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/FileTxt.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/FileTxt.java @@ -1,15 +1,18 @@ package eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import javax.xml.bind.annotation.XmlElement; + import lombok.AllArgsConstructor; @AllArgsConstructor public class FileTxt { - @JacksonXmlProperty(localName = "fileName") + public FileTxt() {} + + @XmlElement(name="fileName") String fileName; - @JacksonXmlProperty(localName = "fileCont") + @XmlElement(name="fileCont") TextElement fileCont; } diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/StdyDscr.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/StdyDscr.java index 08bf01808e..55fc9fe8ad 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/StdyDscr.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/StdyDscr.java @@ -1,12 +1,15 @@ package eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import javax.xml.bind.annotation.XmlElement; + import lombok.AllArgsConstructor; @AllArgsConstructor public class StdyDscr { - @JacksonXmlProperty(localName = "citation") + public StdyDscr() {} + + @XmlElement(name="citation") Citation citation; } diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/TextElement.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/TextElement.java index e38c29434b..91d3020315 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/TextElement.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/TextElement.java @@ -1,7 +1,10 @@ package eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlValue; + import lombok.AllArgsConstructor; /** @@ -9,12 +12,15 @@ * e.g. labels (labl in DDI standard). */ @AllArgsConstructor +@XmlAccessorType(XmlAccessType.NONE) public class TextElement { - @JacksonXmlProperty(localName = "xml:lang", isAttribute = true) + public TextElement() {} + + @XmlAttribute(name="xml:lang") private LanguageEnum lang; - @JacksonXmlText + @XmlValue private String value; public String getValue() { diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/TitlStmt.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/TitlStmt.java index d5172cfd01..b050aded1c 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/TitlStmt.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/TitlStmt.java @@ -1,15 +1,18 @@ package eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import javax.xml.bind.annotation.XmlElement; + import lombok.AllArgsConstructor; @AllArgsConstructor public class TitlStmt { - @JacksonXmlProperty(localName = "titl") - private TextElement titl; + public TitlStmt() {} + + @XmlElement(name="titl") + TextElement titl; - @JacksonXmlProperty(localName = "parTitl") - private TextElement parTitle; + @XmlElement(name="parTitl") + TextElement parTitle; } diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Var.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Var.java index 764ab58f72..07937829c7 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Var.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/Var.java @@ -2,29 +2,29 @@ import java.util.List; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; -import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; + import lombok.AllArgsConstructor; @AllArgsConstructor public class Var { - @JacksonXmlProperty(localName = "name", isAttribute = true) + public Var() {} + + @XmlAttribute(name="name") String name; - @JacksonXmlProperty(localName = "files", isAttribute = true) + @XmlAttribute(name="files") String files; - @JacksonXmlProperty(localName = "labl") - @JacksonXmlElementWrapper(useWrapping = false) + @XmlElement(name="labl") List labl; - @JacksonXmlProperty(localName = "qstn") - @JacksonXmlElementWrapper(useWrapping = false) + @XmlElement(name="qstn") List qstn; - @JacksonXmlProperty(localName = "catgry") - @JacksonXmlElementWrapper(useWrapping = false) + @XmlElement(name="catgry") List catgry; } diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/package-info.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/package-info.java new file mode 100644 index 0000000000..5df93d28d0 --- /dev/null +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/domain/ddiCodebook/package-info.java @@ -0,0 +1,21 @@ + +/** + * Configuration for JAXB + */ + +@XmlSchema( + xmlns = { + @XmlNs(prefix = "", namespaceURI = "ddi:codebook:2_5"), + @XmlNs(prefix = "xs", namespaceURI = "http://www.w3.org/2001/XMLSchema") + }, + namespace = "ddi:codebook:2_5", + elementFormDefault = XmlNsForm.QUALIFIED) +package eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook; + +import javax.xml.bind.annotation.XmlNs; +import javax.xml.bind.annotation.XmlNsForm; +import javax.xml.bind.annotation.XmlSchema; + + + + diff --git a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/service/DataPackageDdiService.java b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/service/DataPackageDdiService.java index 9effc907e4..7cb3f4816c 100644 --- a/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/service/DataPackageDdiService.java +++ b/src/main/java/eu/dzhw/fdz/metadatamanagement/datapackagemanagement/service/DataPackageDdiService.java @@ -1,18 +1,18 @@ package eu.dzhw.fdz.metadatamanagement.datapackagemanagement.service; +import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; + import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.google.gson.Gson; -import eu.dzhw.fdz.metadatamanagement.common.domain.I18nString; -import eu.dzhw.fdz.metadatamanagement.common.domain.Person; import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.DataPackage; -import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.Tags; import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook.Catgry; import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook.Citation; import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook.CodeBook; @@ -24,15 +24,12 @@ import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook.TextElement; import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook.TitlStmt; import eu.dzhw.fdz.metadatamanagement.datapackagemanagement.domain.ddiCodebook.Var; -import eu.dzhw.fdz.metadatamanagement.questionmanagement.domain.Question; import eu.dzhw.fdz.metadatamanagement.searchmanagement.documents.DataPackageSearchDocument; import eu.dzhw.fdz.metadatamanagement.searchmanagement.documents.DataSetSubDocument; import eu.dzhw.fdz.metadatamanagement.searchmanagement.documents.QuestionSearchDocument; -import eu.dzhw.fdz.metadatamanagement.searchmanagement.documents.RelatedQuestionSubDocument; import eu.dzhw.fdz.metadatamanagement.searchmanagement.documents.VariableSearchDocument; import eu.dzhw.fdz.metadatamanagement.searchmanagement.documents.VariableSubDocument; import eu.dzhw.fdz.metadatamanagement.variablemanagement.domain.Missing; -import eu.dzhw.fdz.metadatamanagement.variablemanagement.domain.RelatedQuestion; import eu.dzhw.fdz.metadatamanagement.variablemanagement.domain.ScaleLevels; import eu.dzhw.fdz.metadatamanagement.variablemanagement.domain.ValidResponse; import eu.dzhw.fdz.metadatamanagement.variablemanagement.domain.projections.RelatedQuestionSubDocumentProjection; @@ -45,7 +42,6 @@ import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.springframework.core.io.ByteArrayResource; @@ -78,14 +74,20 @@ public class DataPackageDdiService { public ResponseEntity exportDdiVariablesAsXML(String dataPackageId) { try { CodeBook variableMetadata = this.getDdiVariablesMetadata(dataPackageId); - XmlMapper mapper = new XmlMapper(); - ByteArrayResource resource = new ByteArrayResource(mapper.writeValueAsBytes(variableMetadata)); + JAXBContext context = JAXBContext.newInstance(CodeBook.class); + Marshaller mar = context.createMarshaller(); + mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + mar.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.ddialliance.org/Specification/DDI-Codebook/2.5/XMLSchema/codebook.xsd");; + ByteArrayOutputStream res = new ByteArrayOutputStream(); + mar.marshal(variableMetadata, res); + ByteArrayResource resource = new ByteArrayResource(res.toByteArray()); HttpHeaders headers = new HttpHeaders(); - headers.add("Content-Disposition", "attachment; filename=Variables_PID_MDM_Export.xml"); + headers.add("Content-Disposition", "attachment; filename=Variables_DDI_MDM_Export.xml"); return ResponseEntity.ok() .headers(headers) .body(resource); - } catch (IOException ex) { + } catch (JAXBException | JsonProcessingException ex) { + log.error("Error generating XML: " + ex); return new ResponseEntity<>(null, null, HttpStatus.NOT_FOUND); } }