From 4a97f41eee9e8fddd096fa3b03553b99fe62c521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Wed, 12 Jan 2022 15:30:28 +0100 Subject: [PATCH] Parse JSON settings if needed. --- .../META-INF/MANIFEST.MF | 5 +- .../supplier/pdf/ui/io/GenericPDF.java | 49 +++++++++++++++++-- .../supplier/pdf/ui/io/MethodSettings.java | 33 +++++++++++++ 3 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/src/net/openchrom/xxd/converter/supplier/pdf/ui/io/MethodSettings.java diff --git a/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/META-INF/MANIFEST.MF b/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/META-INF/MANIFEST.MF index 7e22424e6..490d1b71d 100644 --- a/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/META-INF/MANIFEST.MF +++ b/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/META-INF/MANIFEST.MF @@ -31,7 +31,10 @@ Require-Bundle: org.eclipse.ui, org.eclipse.chemclipse.csd.model;bundle-version="0.8.0", org.eclipse.chemclipse.wsd.model;bundle-version="0.8.0", org.eclipse.chemclipse.pdfbox.extensions;bundle-version="0.8.0", - org.eclipse.swtchart.export;bundle-version="0.8.0" + org.eclipse.swtchart.export;bundle-version="0.8.0", + com.fasterxml.jackson.core.jackson-databind;bundle-version="2.9.93", + com.fasterxml.jackson.core.jackson-core;bundle-version="2.9.9", + org.apache.commons.lang;bundle-version="2.6.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Bundle-ActivationPolicy: lazy Import-Package: com.fasterxml.jackson.annotation;version="2.7.1", diff --git a/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/src/net/openchrom/xxd/converter/supplier/pdf/ui/io/GenericPDF.java b/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/src/net/openchrom/xxd/converter/supplier/pdf/ui/io/GenericPDF.java index 3fa302d57..547317bc1 100644 --- a/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/src/net/openchrom/xxd/converter/supplier/pdf/ui/io/GenericPDF.java +++ b/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/src/net/openchrom/xxd/converter/supplier/pdf/ui/io/GenericPDF.java @@ -9,6 +9,7 @@ * Contributors: * Dr. Philip Wenig - initial API and implementation * Christoph Läubrich - Settings support + * Matthias Mailänder - parse JSON settings *******************************************************************************/ package net.openchrom.xxd.converter.supplier.pdf.ui.io; @@ -23,6 +24,7 @@ import java.util.Map; import java.util.Set; +import org.apache.commons.lang.StringUtils; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory; @@ -56,6 +58,10 @@ import org.eclipse.chemclipse.wsd.model.core.IChromatogramWSD; import org.eclipse.core.runtime.IProgressMonitor; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.exc.MismatchedInputException; + import net.openchrom.xxd.converter.supplier.pdf.ui.io.generic.ImageRunnableGeneric; import net.openchrom.xxd.converter.supplier.pdf.ui.settings.ReportSettingsGeneric; @@ -285,6 +291,20 @@ private ImageRunnableGeneric createImages(IChromatogram chromat return imageRunnable; } + private static boolean isJSON(String tree) { + + try { + final ObjectMapper mapper = new ObjectMapper(); + mapper.readTree(tree); + return true; + } catch(JsonProcessingException e) { + return false; + } catch(IOException e) { + logger.warn(e); + return false; + } + } + private PDTable getHeaderDataTable(Map headerDataMap) { PDTable pdTable = new PDTable(); @@ -294,10 +314,31 @@ private PDTable getHeaderDataTable(Map headerDataMap) { pdTable.addColumn("Value", 95.0f); // for(Map.Entry entry : headerDataMap.entrySet()) { - List row = new ArrayList<>(); - row.add(normalizeText(entry.getKey())); - row.add(normalizeText(entry.getValue())); - pdTable.addDataRow(row); + String name = entry.getKey(); + String value = entry.getValue(); + if(isJSON(value)) { + final ObjectMapper mapper = new ObjectMapper(); + try { + MethodSettings methodSettings = mapper.readValue(value, MethodSettings.class); + for(String key : methodSettings.getSettings().keySet()) { + name = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(key), ' '); + value = methodSettings.getSettings().get(key).toString(); + List row = new ArrayList<>(); + row.add(StringUtils.capitalize(name)); + row.add(normalizeText(value)); + pdTable.addDataRow(row); + } + } catch(MismatchedInputException e) { + // ignore + } catch(IOException e) { + logger.warn(e); + } + } else { + List row = new ArrayList<>(); + row.add(normalizeText(name)); + row.add(normalizeText(value)); + pdTable.addDataRow(row); + } } // return pdTable; diff --git a/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/src/net/openchrom/xxd/converter/supplier/pdf/ui/io/MethodSettings.java b/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/src/net/openchrom/xxd/converter/supplier/pdf/ui/io/MethodSettings.java new file mode 100644 index 000000000..3f72ab4b8 --- /dev/null +++ b/openchrom/plugins/net.openchrom.xxd.converter.supplier.pdf.ui/src/net/openchrom/xxd/converter/supplier/pdf/ui/io/MethodSettings.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2022 Lablicate GmbH. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Matthias Mailänder - initial API and implementation + *******************************************************************************/ +package net.openchrom.xxd.converter.supplier.pdf.ui.io; + +import java.util.LinkedHashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonAnySetter; + +public class MethodSettings { + + Map settings = new LinkedHashMap<>(); + + @JsonAnySetter + void setSettings(String name, Object value) { + + settings.put(name, value); + } + + public Map getSettings() { + + return settings; + } +}