Skip to content

Commit

Permalink
#3304 switched to JAXB for handling XML generation
Browse files Browse the repository at this point in the history
  • Loading branch information
moellerth committed Sep 19, 2024
1 parent f3eff2e commit f0da924
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextElement> labl;

}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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> fileDscr, DataDscr dataDscr) {
Expand All @@ -22,26 +18,15 @@ public CodeBook(StdyDscr stdyDscr, List<FileDscr> 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> fileDscr;

@JacksonXmlProperty(localName = "dataDscr")
@XmlElement(name="dataDscr")
private DataDscr dataDscr;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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> var;

}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
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;

/**
* General element for a language annotated XML element containing a string,
* 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextElement> labl;

@JacksonXmlProperty(localName = "qstn")
@JacksonXmlElementWrapper(useWrapping = false)
@XmlElement(name="qstn")
List<TextElement> qstn;

@JacksonXmlProperty(localName = "catgry")
@JacksonXmlElementWrapper(useWrapping = false)
@XmlElement(name="catgry")
List<Catgry> catgry;

}
Original file line number Diff line number Diff line change
@@ -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;




Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit f0da924

Please sign in to comment.