Refactor phases 4.1

master
trifonovt 1 month ago
parent f337af56b5
commit 1ba8cccb62

@ -26,9 +26,21 @@ public class BasicMimeAndExtensionDocumentTypeDetector implements DocumentTypeDe
@Override
public DetectionResult detect(SourceDescriptor sourceDescriptor) {
String normalizedMediaType = DocumentImportSupport.normalizeMediaType(sourceDescriptor.mediaType());
if (sourceDescriptor.sourceType() == at.procon.dip.domain.document.SourceType.TED_PACKAGE) {
Map<String, String> attributes = new HashMap<>();
attributes.put("sourceType", sourceDescriptor.sourceType().name());
if (StringUtils.hasText(sourceDescriptor.fileName())) {
attributes.put("fileName", sourceDescriptor.fileName());
}
return new DetectionResult(DocumentType.TED_PACKAGE, DocumentFamily.PROCUREMENT,
normalizedMediaType != null ? normalizedMediaType : "application/gzip", null, attributes);
}
DocumentType hintedType = detectByHint(sourceDescriptor);
String extension = DocumentImportSupport.extensionOf(sourceDescriptor.fileName());
DocumentType documentType = detectByMediaType(normalizedMediaType);
DocumentType documentType = hintedType != null ? hintedType : detectByMediaType(normalizedMediaType);
if (documentType == DocumentType.UNKNOWN) {
documentType = detectByExtension(extension);
}
@ -46,10 +58,28 @@ public class BasicMimeAndExtensionDocumentTypeDetector implements DocumentTypeDe
if (StringUtils.hasText(sourceDescriptor.fileName())) {
attributes.put("fileName", sourceDescriptor.fileName());
}
if (hintedType != null) {
attributes.put("documentTypeHint", hintedType.name());
}
return new DetectionResult(documentType, family, normalizedMediaType, languageCode, attributes);
}
private DocumentType detectByHint(SourceDescriptor sourceDescriptor) {
if (sourceDescriptor.attributes() == null) {
return null;
}
String hint = sourceDescriptor.attributes().get("documentTypeHint");
if (!StringUtils.hasText(hint)) {
return null;
}
try {
return DocumentType.valueOf(hint.trim().toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException ignored) {
return null;
}
}
private DocumentType detectByMediaType(String mediaType) {
if (!StringUtils.hasText(mediaType)) {
return DocumentType.UNKNOWN;

Loading…
Cancel
Save