Skip to content

Commit

Permalink
Equalize saved XML log with XML for docket creation
Browse files Browse the repository at this point in the history
- make metadata available for docket generation
- use docket module to save XML log (= input to docket XSLT procss)
- deduplicate code
  • Loading branch information
matthias-ronge committed Feb 14, 2022
1 parent b27573b commit 106dded
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 568 deletions.
53 changes: 53 additions & 0 deletions Kitodo-API/src/main/java/org/kitodo/api/docket/DocketData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,83 @@

public class DocketData {

/** The metadata file. */
private String metadataFile;

/** The docket data of the parent. */
private DocketData parent;

/** The name of the process. */
private String processName;

/** The id of the process. */
private String processId;

/** The name of the project. */
private String projectName;

/** The name of the used ruleset. */
private String rulesetName;

/** The creation Date of the process. */
private String creationDate;

/** A comment. */
private String comment;

/** The template properties. */
private List<Property> templateProperties;

/** The workpiece properties. */
private List<Property> workpieceProperties;

/** The process properties. */
private List<Property> processProperties;

/**
* Gets the metadataFile.
*
* @return The metadataFile.
*/
public String metadataFile() {
return metadataFile;
}

/**
* Sets the metadataFile.
*
* @param metadataFile
* The metadata file.
*/
public void setMetadataFile(String metadataFile) {
this.metadataFile = metadataFile;
}


/**
* Gets the processName.
*
* @return The processName.
*/
public DocketData getParent() {
return parent;
}

/**
* Sets the parent.
*
* @param parent
* The docket data of the parent.
*/
public void setParent(DocketData parent) {
this.parent = parent;
}

/**
* Gets the parent.
*
* @return The parent.
*/
public String getProcessName() {
return processName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public interface DocketInterface {
*/
File generateMultipleDockets(Collection<DocketData> docketData, URI xslFileUri) throws IOException;

/**
* Save XML log, which is used as input for docket XSLT transformation.
*
* @param process the data shown in the docket
* @param destination where to save the file
*/
void exportXmlLog(DocketData docketData, String destination) throws IOException;
}
16 changes: 10 additions & 6 deletions Kitodo-Docket/src/main/java/org/kitodo/docket/Docket.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,31 @@ public class Docket implements DocketInterface {

@Override
public File generateDocket(DocketData docketData, URI xslFileUri) throws IOException {
ExportDocket exportDocket = new ExportDocket();

File file = File.createTempFile("docket.pdf", ".tmp");

try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
exportDocket.startExport(docketData, fileOutputStream, new File(xslFileUri));
ExportDocket.startExport(docketData, fileOutputStream, new File(xslFileUri));
}

return file;
}

@Override
public File generateMultipleDockets(Collection<DocketData> docketData, URI xslFileUri) throws IOException {
ExportDocket exportDocket = new ExportDocket();

File file = File.createTempFile("docket_multipage.pdf", ".tmp");

try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
exportDocket.startExport(docketData, fileOutputStream, new File(xslFileUri));
ExportDocket.startExport(docketData, fileOutputStream, new File(xslFileUri));
}

return file;
}

@Override
public void exportXmlLog(DocketData docketData, String destination) throws IOException {
File file = new File(destination);
try(FileOutputStream fileOutputStream = new FileOutputStream(file)){
ExportXmlLog.startExport(docketData, fileOutputStream);
}
}
}
12 changes: 5 additions & 7 deletions Kitodo-Docket/src/main/java/org/kitodo/docket/ExportDocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ public class ExportDocket {
* @throws IOException
* Throws IOException, when pdfGeneration fails
*/
void startExport(DocketData docketData, OutputStream outputStream, File xsltFile) throws IOException {
ExportXmlLog exl = new ExportXmlLog();
static void startExport(DocketData docketData, OutputStream outputStream, File xsltFile) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
exl.startExport(docketData, out);
ExportXmlLog.startExport(docketData, out);

byte[] pdfBytes = generatePdfBytes(out, xsltFile);

Expand All @@ -68,17 +67,16 @@ void startExport(DocketData docketData, OutputStream outputStream, File xsltFile
* @throws IOException
* Throws IOException, when pdfGeneration fails.
*/
void startExport(Iterable<DocketData> docketDataList, OutputStream os, File xsltFile) throws IOException {
ExportXmlLog exl = new ExportXmlLog();
static void startExport(Iterable<DocketData> docketDataList, OutputStream os, File xsltFile) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
exl.startMultipleExport(docketDataList, out);
ExportXmlLog.startMultipleExport(docketDataList, out);

byte[] pdfBytes = generatePdfBytes(out, xsltFile);

os.write(pdfBytes);
}

private byte[] generatePdfBytes(ByteArrayOutputStream out, File xsltFile) throws IOException {
private static byte[] generatePdfBytes(ByteArrayOutputStream out, File xsltFile) throws IOException {
// generate pdf file
StreamSource source = new StreamSource(new ByteArrayInputStream(out.toByteArray()));
StreamSource transformSource = new StreamSource(xsltFile);
Expand Down
154 changes: 145 additions & 9 deletions Kitodo-Docket/src/main/java/org/kitodo/docket/ExportXmlLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@

package org.kitodo.docket;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import java.util.Map;
import java.util.Objects;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.jaxen.JaxenException;
import org.jaxen.jdom.JDOMXPath;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.kitodo.api.docket.DocketData;
import org.kitodo.api.docket.Property;
import org.kitodo.config.KitodoConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -54,7 +67,7 @@ public class ExportXmlLog {
* @throws IOException
* Throws IOException, when document creation fails.
*/
void startExport(DocketData docketData, OutputStream os) throws IOException {
static void startExport(DocketData docketData, OutputStream os) throws IOException {
try {
Document doc = createDocument(docketData, true);

Expand All @@ -80,7 +93,7 @@ void startExport(DocketData docketData, OutputStream os) throws IOException {
* The output stream, to write the docket to.
*/

void startMultipleExport(Iterable<DocketData> docketDataList, OutputStream outputStream) {
static void startMultipleExport(Iterable<DocketData> docketDataList, OutputStream outputStream) {
Document answer = new Document();
Element root = new Element("processes");
answer.setRootElement(root);
Expand Down Expand Up @@ -124,7 +137,7 @@ void startMultipleExport(Iterable<DocketData> docketDataList, OutputStream outpu
* the docketData to export
* @return a new xml document
*/
private Document createDocument(DocketData docketData, boolean addNamespace) {
private static Document createDocument(DocketData docketData, boolean addNamespace) {

Element processElm = new Element("process");
final Document doc = new Document(processElm);
Expand Down Expand Up @@ -236,11 +249,18 @@ private Document createDocument(DocketData docketData, boolean addNamespace) {
digdoc.addContent(docElements);
processElements.add(digdoc);


// METS information
Element metsElement = new Element("metsInformation", xmlns);
List<Element> metadataElements = createMetadataElements(xmlns, docketData);
metsElement.addContent(metadataElements);
processElements.add(metsElement);

processElm.setContent(processElements);
return doc;
}

private List<Element> prepareProperties(List<Property> properties, Namespace xmlns) {
private static List<Element> prepareProperties(List<Property> properties, Namespace xmlns) {
ArrayList<Element> preparedProperties = new ArrayList<>();
for (Property property : properties) {
Element propertyElement = new Element(PROPERTY, xmlns);
Expand All @@ -250,23 +270,139 @@ private List<Element> prepareProperties(List<Property> properties, Namespace xml
} else {
propertyElement.setAttribute(VALUE, "");
}

Element label = new Element(LABEL, xmlns);

label.setText(property.getTitle());
propertyElement.addContent(label);
preparedProperties.add(propertyElement);
}
return preparedProperties;
}

private String replacer(String in) {
private static List<Element> createMetadataElements(Namespace xmlns, DocketData docketData) {
List<Element> metadataElements = new ArrayList<>();
try {
HashMap<String, Namespace> namespaces = new HashMap<>();

HashMap<String, String> names = getNamespacesFromConfig();
for (Map.Entry<String, String> entry : names.entrySet()) {
String key = entry.getKey();
namespaces.put(key, Namespace.getNamespace(key, entry.getValue()));
}

prepareMetadataElements(metadataElements, false, docketData, namespaces, xmlns);
if (Objects.nonNull(docketData.getParent())) {
prepareMetadataElements(metadataElements, true, docketData.getParent(), namespaces, xmlns);
}

} catch (IOException | JDOMException | JaxenException e) {
logger.error(e.getMessage(), e);
}
return metadataElements;
}

private static HashMap<String, String> getNamespacesFromConfig() {
HashMap<String, String> nss = new HashMap<>();
try {
File file = new File(KitodoConfig.getKitodoConfigDirectory() + "kitodo_exportXml.xml");
if (file.exists() && file.canRead()) {
XMLConfiguration config = new XMLConfiguration(file);
config.setListDelimiter('&');
config.setReloadingStrategy(new FileChangedReloadingStrategy());

int count = config.getMaxIndex("namespace");
for (int i = 0; i <= count; i++) {
String name = config.getString("namespace(" + i + ")[@name]");
String value = config.getString("namespace(" + i + ")[@value]");
nss.put(name, value);
}
}
} catch (ConfigurationException | RuntimeException e) {
logger.debug(e.getMessage(), e);
nss = new HashMap<>();
}
return nss;
}

private static void prepareMetadataElements(List<Element> metadataElements, boolean useAnchor, DocketData docketData,
HashMap<String, Namespace> namespaces, Namespace xmlns)
throws IOException, JDOMException, JaxenException {
HashMap<String, String> fields = getMetsFieldsFromConfig(useAnchor);
Document metsDoc = new SAXBuilder().build(docketData.metadataFile());
prepareMetadataElements(metadataElements, fields, metsDoc, namespaces, xmlns);
}

private static HashMap<String, String> getMetsFieldsFromConfig(boolean useAnchor) {
String xmlpath = "mets." + PROPERTY;
if (useAnchor) {
xmlpath = "anchor." + PROPERTY;
}

HashMap<String, String> fields = new HashMap<>();
try {
File file = new File(KitodoConfig.getKitodoConfigDirectory() + "kitodo_exportXml.xml");
if (file.exists() && file.canRead()) {
XMLConfiguration config = new XMLConfiguration(file);
config.setListDelimiter('&');
config.setReloadingStrategy(new FileChangedReloadingStrategy());

int count = config.getMaxIndex(xmlpath);
for (int i = 0; i <= count; i++) {
String name = config.getString(xmlpath + "(" + i + ")[@name]");
String value = config.getString(xmlpath + "(" + i + ")[@value]");
fields.put(name, value);
}
}
} catch (ConfigurationException | RuntimeException e) {
logger.debug(e.getMessage(), e);
fields = new HashMap<>();
}
return fields;
}

private static void prepareMetadataElements(List<Element> metadataElements, Map<String, String> fields, Document document,
HashMap<String, Namespace> namespaces, Namespace xmlns) throws JaxenException {
for (Map.Entry<String, String> entry : fields.entrySet()) {
String key = entry.getKey();
List<Element> metsValues = getMetsValues(entry.getValue(), document, namespaces);
for (Element element : metsValues) {
Element ele = new Element(PROPERTY, xmlns);
ele.setAttribute("name", key);
ele.addContent(element.getTextTrim());
metadataElements.add(ele);
}
}
}

/**
* Get METS values.
*
* @param expr
* String
* @param element
* Object
* @param namespaces
* HashMap
* @return list of elements
*/
@SuppressWarnings("unchecked")
private static List<Element> getMetsValues(String expr, Object element,
HashMap<String, Namespace> namespaces) throws JaxenException {
JDOMXPath xpath = new JDOMXPath(expr.trim().replace("\n", ""));
// Add all namespaces
for (Map.Entry<String, Namespace> entry : namespaces.entrySet()) {
xpath.addNamespace(entry.getKey(), entry.getValue().getURI());
}
return xpath.selectNodes(element);
}

private static String replacer(String in) {
in = in.replace("°", "?");
in = in.replace("^", "?");
in = in.replace("|", "?");
in = in.replace(">", "?");
in = in.replace("<", "?");
return in;
}

}
Loading

0 comments on commit 106dded

Please sign in to comment.