com.adobe.platform.operation.internal.cpf.dto.StructuredData Maven / Gradle / Ivy
The newest version!
package com.adobe.platform.operation.internal.cpf.dto;
import com.adobe.platform.operation.internal.ExtensionMediaTypeMapping;
import com.adobe.platform.operation.internal.util.JsonUtil;
import com.adobe.platform.operation.internal.util.PathUtil;
import com.adobe.platform.operation.internal.util.StringUtil;
import com.adobe.platform.operation.pdfops.constants.PDFElementType;
import org.apache.commons.collections4.CollectionUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StructuredData {
private static final String DEFAULT_FILE_NAME = "structuredData";
private static final String PATH_DELIMINATOR = "/";
private static final Map renditionToPathMapping = new HashMap() {
{
put(PDFElementType.TABLES, "table");
put(PDFElementType.FIGURES, "figure");
}
};
private String fileFormat;
private JSONObject data;
private Map elementContentNameToType;
public static StructuredData deserializeData(InputStream inputStream, ExtensionMediaTypeMapping mediaType) {
String fileFormat = mediaType.getExtension();
if (mediaType.equals(ExtensionMediaTypeMapping.JSON)) {
Map jsonContent = JsonUtil.deserializeJsonStream(inputStream, Map.class);
return new StructuredData(fileFormat, new JSONObject(jsonContent));
}
return null;
}
public StructuredData(String fileFormat, JSONObject data) {
this.fileFormat = fileFormat;
this.data = data;
preprocessData();
}
public JSONObject getData() {
return data;
}
public String getFileFormat() {
return fileFormat;
}
public String getFileName() {
return PathUtil.getFileNameWithExtension(DEFAULT_FILE_NAME, fileFormat);
}
public PDFElementType getPDFElementType(String contentName) {
if (elementContentNameToType != null) {
return elementContentNameToType.get(contentName);
}
return null;
}
public void preprocessData() {
JSONArray elements = new JSONArray();
if (data.has("elements")) {
elements = data.getJSONArray("elements");
}
int jsonElements = elements.length();
for (int i=0; i();
}
elementContentNameToType.put(name, pdfElementType);
}
private static PDFElementType pdfRenditionIdentifier(JSONObject pdfElement) {
if (isRenditionElement(pdfElement)) {
String path = pdfElement.getString("Path");
String[] splitPath = path.split(PATH_DELIMINATOR);
String identifier = splitPath[splitPath.length - 1];
for (Map.Entry mapping : renditionToPathMapping.entrySet()) {
if (identifier.toLowerCase().startsWith(mapping.getValue().toLowerCase())) {
return mapping.getKey();
}
}
}
return null;
}
private static boolean isRenditionElement(JSONObject pdfElement) {
if (pdfElement != null && pdfElement.has("filePaths") && pdfElement.has("Path") &&
CollectionUtils.isNotEmpty(pdfElement.getJSONArray("filePaths").toList()) &&
StringUtil.isNotBlank(pdfElement.getString("Path"))) {
return true;
}
return false;
}
}