Add option to apply multiple XSLT files during import
These changes add the option to apply multiple XSL transformations sequentially to an import XML document, similar to how the current import of MARCXML documents works. The XSLT files need to be separated by a comma in the <mappingFile> element of the corresponding OPAC configuration in kitodo_opac.xml.
This commit is contained in:
parent
7606f93ec2
commit
b4754d6266
|
@ -14,6 +14,7 @@ package org.kitodo.api.schemaconverter;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
/** Enables the conversion of a DataRecord from one format to another. */
|
||||
public interface SchemaConverterInterface {
|
||||
|
@ -24,11 +25,11 @@ public interface SchemaConverterInterface {
|
|||
* @param record DataRecord to be converted
|
||||
* @param targetMetadataFormat MetadataFormat to which the given DataRecord is converted
|
||||
* @param targetFileFormat FileFormat to which the given DataRecord is converted
|
||||
* @param mappingFile mapping file; if null, the schema converter module uses a default mapping
|
||||
* @param mappingFiles list of mapping files; if empty, the schema converter module uses a default mapping
|
||||
* @return The result of the conversion as a DataRecord.
|
||||
*/
|
||||
DataRecord convert(DataRecord record, MetadataFormat targetMetadataFormat, FileFormat targetFileFormat,
|
||||
File mappingFile) throws IOException, URISyntaxException;
|
||||
List<File> mappingFiles) throws IOException, URISyntaxException;
|
||||
|
||||
/**
|
||||
* Check and return whether the current SchemaConverter supports the given MetadataFormat as a target format or not.
|
||||
|
|
|
@ -107,7 +107,7 @@ public class OPACConfig {
|
|||
* @param catalogName String identifying the catalog by its title
|
||||
* @return HierarchicalConfiguration for catalog's "mappingFile"
|
||||
*/
|
||||
public static String getXsltMappingFile(String catalogName) {
|
||||
public static String getXsltMappingFiles(String catalogName) {
|
||||
return getCatalog(catalogName).getString("mappingFile");
|
||||
}
|
||||
|
||||
|
|
|
@ -81,12 +81,12 @@ public class XMLSchemaConverter implements SchemaConverterInterface {
|
|||
* @param record DataRecord to be converted
|
||||
* @param targetMetadataFormat MetadataFormat to which the given DataRecord is converted
|
||||
* @param targetFileFormat FileFormat to which the given DataRecord is converted
|
||||
* @param mappingFile mapping file; if null, the schema converter module uses a default mapping
|
||||
* @param mappingFiles list of mapping files; if empty, the schema converter module uses a default mapping
|
||||
* @return The result of the conversion as a DataRecord.
|
||||
*/
|
||||
@Override
|
||||
public DataRecord convert(DataRecord record, MetadataFormat targetMetadataFormat, FileFormat targetFileFormat,
|
||||
File mappingFile) throws IOException, URISyntaxException {
|
||||
List<File> mappingFiles) throws IOException, URISyntaxException {
|
||||
if (!(supportsSourceMetadataFormat(record.getMetadataFormat())
|
||||
&& supportsSourceFileFormat(record.getFileFormat())
|
||||
&& supportsTargetMetadataFormat(targetMetadataFormat)
|
||||
|
@ -99,11 +99,7 @@ public class XMLSchemaConverter implements SchemaConverterInterface {
|
|||
String xmlString = (String)record.getOriginalData();
|
||||
String conversionResult;
|
||||
|
||||
if (Objects.nonNull(mappingFile)) {
|
||||
try (InputStream fileStream = Files.newInputStream(mappingFile.toPath())) {
|
||||
conversionResult = transformXmlByXslt(xmlString, fileStream);
|
||||
}
|
||||
} else {
|
||||
if (mappingFiles.isEmpty()) {
|
||||
List<MetadataFormatConversion> xslFiles = supportedSourceMetadataFormats.get(record.getMetadataFormat());
|
||||
URI xsltDir = Paths.get(KitodoConfig.getParameter("directory.xslt")).toUri();
|
||||
for (MetadataFormatConversion metadataFormatConversion : xslFiles) {
|
||||
|
@ -116,8 +112,14 @@ public class XMLSchemaConverter implements SchemaConverterInterface {
|
|||
xmlString = transformXmlByXslt(xmlString, fileStream);
|
||||
}
|
||||
}
|
||||
conversionResult = xmlString;
|
||||
} else {
|
||||
for (File mappingFile : mappingFiles) {
|
||||
try (InputStream fileStream = Files.newInputStream(mappingFile.toPath())) {
|
||||
xmlString = transformXmlByXslt(xmlString, fileStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
conversionResult = xmlString;
|
||||
|
||||
DataRecord resultRecord = new DataRecord();
|
||||
resultRecord.setOriginalData(conversionResult);
|
||||
|
|
|
@ -21,6 +21,7 @@ import java.net.URISyntaxException;
|
|||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
@ -28,7 +29,6 @@ import javax.xml.parsers.ParserConfigurationException;
|
|||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.kitodo.api.schemaconverter.DataRecord;
|
||||
import org.kitodo.api.schemaconverter.FileFormat;
|
||||
|
@ -63,7 +63,7 @@ public class XmlSchemaConverterTest {
|
|||
|
||||
try (InputStream inputStream = Files.newInputStream(Paths.get(MODS_TEST_FILE_PATH))) {
|
||||
testRecord.setOriginalData(IOUtils.toString(inputStream, Charset.defaultCharset()));
|
||||
internalFormatRecord = converter.convert(testRecord, MetadataFormat.KITODO, FileFormat.XML, null);
|
||||
internalFormatRecord = converter.convert(testRecord, MetadataFormat.KITODO, FileFormat.XML, Collections.emptyList());
|
||||
}
|
||||
|
||||
Assert.assertNotNull("Conversion result is empty!", internalFormatRecord);
|
||||
|
@ -110,7 +110,7 @@ public class XmlSchemaConverterTest {
|
|||
|
||||
try (InputStream inputStream = Files.newInputStream(Paths.get(MARC_TEST_FILE_PATH))) {
|
||||
testRecord.setOriginalData(IOUtils.toString(inputStream, Charset.defaultCharset()));
|
||||
internalFormatRecord = converter.convert(testRecord, MetadataFormat.KITODO, FileFormat.XML, null);
|
||||
internalFormatRecord = converter.convert(testRecord, MetadataFormat.KITODO, FileFormat.XML, Collections.emptyList());
|
||||
}
|
||||
|
||||
Assert.assertNotNull("Conversion result is empty!", internalFormatRecord);
|
||||
|
|
|
@ -518,9 +518,9 @@ public class ImportService {
|
|||
LinkedList<TempProcess> childProcesses = new LinkedList<>();
|
||||
if (!childRecords.isEmpty()) {
|
||||
SchemaConverterInterface converter = getSchemaConverter(childRecords.get(0));
|
||||
File mappingFile = getMappingFile(opac);
|
||||
List<File> mappingFiles = getMappingFiles(opac);
|
||||
for (DataRecord childRecord : childRecords) {
|
||||
DataRecord internalRecord = converter.convert(childRecord, MetadataFormat.KITODO, FileFormat.XML, mappingFile);
|
||||
DataRecord internalRecord = converter.convert(childRecord, MetadataFormat.KITODO, FileFormat.XML, mappingFiles);
|
||||
Document childDocument = XMLUtils.parseXMLString((String)internalRecord.getOriginalData());
|
||||
childProcesses.add(createTempProcessFromDocument(childDocument, templateId, projectId));
|
||||
}
|
||||
|
@ -547,7 +547,7 @@ public class ImportService {
|
|||
// depending on metadata and return form, call corresponding schema converter module!
|
||||
SchemaConverterInterface converter = getSchemaConverter(dataRecord);
|
||||
|
||||
File mappingFile = getMappingFile(opac);
|
||||
List<File> mappingFiles = getMappingFiles(opac);
|
||||
|
||||
// transform dataRecord to Kitodo internal format using appropriate SchemaConverter!
|
||||
File debugFolder = ConfigCore.getKitodoDebugDirectory();
|
||||
|
@ -555,7 +555,7 @@ public class ImportService {
|
|||
FileUtils.writeStringToFile(new File(debugFolder, "catalogRecord.xml"),
|
||||
(String) dataRecord.getOriginalData(), StandardCharsets.UTF_8);
|
||||
}
|
||||
DataRecord internalRecord = converter.convert(dataRecord, MetadataFormat.KITODO, FileFormat.XML, mappingFile);
|
||||
DataRecord internalRecord = converter.convert(dataRecord, MetadataFormat.KITODO, FileFormat.XML, mappingFiles);
|
||||
if (Objects.nonNull(debugFolder)) {
|
||||
FileUtils.writeStringToFile(new File(debugFolder, "internalRecord.xml"),
|
||||
(String) internalRecord.getOriginalData(), StandardCharsets.UTF_8);
|
||||
|
@ -578,16 +578,18 @@ public class ImportService {
|
|||
return kitodoNode.getChildNodes();
|
||||
}
|
||||
|
||||
private File getMappingFile(String opac) throws URISyntaxException {
|
||||
File mappingFile = null;
|
||||
private List<File> getMappingFiles(String opac) throws URISyntaxException {
|
||||
List<File> mappingFiles = new ArrayList<>();
|
||||
|
||||
String mappingFileName = OPACConfig.getXsltMappingFile(opac);
|
||||
if (!StringUtils.isBlank(mappingFileName)) {
|
||||
URI xsltFile = Paths.get(ConfigCore.getParameter(ParameterCore.DIR_XSLT)).toUri()
|
||||
.resolve(new URI(mappingFileName));
|
||||
mappingFile = ServiceManager.getFileService().getFile(xsltFile);
|
||||
String mappingFileNames = OPACConfig.getXsltMappingFiles(opac);
|
||||
if (!StringUtils.isBlank(mappingFileNames)) {
|
||||
for (String mappingFileName : mappingFileNames.split(",")) {
|
||||
URI xsltFile = Paths.get(ConfigCore.getParameter(ParameterCore.DIR_XSLT)).toUri()
|
||||
.resolve(new URI(mappingFileName.trim()));
|
||||
mappingFiles.add(ServiceManager.getFileService().getFile(xsltFile));
|
||||
}
|
||||
}
|
||||
return mappingFile;
|
||||
return mappingFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue