|
|
|
@ -26,9 +26,21 @@ public class BasicMimeAndExtensionDocumentTypeDetector implements DocumentTypeDe
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public DetectionResult detect(SourceDescriptor sourceDescriptor) {
|
|
|
|
public DetectionResult detect(SourceDescriptor sourceDescriptor) {
|
|
|
|
String normalizedMediaType = DocumentImportSupport.normalizeMediaType(sourceDescriptor.mediaType());
|
|
|
|
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());
|
|
|
|
String extension = DocumentImportSupport.extensionOf(sourceDescriptor.fileName());
|
|
|
|
|
|
|
|
|
|
|
|
DocumentType documentType = detectByMediaType(normalizedMediaType);
|
|
|
|
DocumentType documentType = hintedType != null ? hintedType : detectByMediaType(normalizedMediaType);
|
|
|
|
if (documentType == DocumentType.UNKNOWN) {
|
|
|
|
if (documentType == DocumentType.UNKNOWN) {
|
|
|
|
documentType = detectByExtension(extension);
|
|
|
|
documentType = detectByExtension(extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -46,10 +58,28 @@ public class BasicMimeAndExtensionDocumentTypeDetector implements DocumentTypeDe
|
|
|
|
if (StringUtils.hasText(sourceDescriptor.fileName())) {
|
|
|
|
if (StringUtils.hasText(sourceDescriptor.fileName())) {
|
|
|
|
attributes.put("fileName", sourceDescriptor.fileName());
|
|
|
|
attributes.put("fileName", sourceDescriptor.fileName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hintedType != null) {
|
|
|
|
|
|
|
|
attributes.put("documentTypeHint", hintedType.name());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new DetectionResult(documentType, family, normalizedMediaType, languageCode, attributes);
|
|
|
|
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) {
|
|
|
|
private DocumentType detectByMediaType(String mediaType) {
|
|
|
|
if (!StringUtils.hasText(mediaType)) {
|
|
|
|
if (!StringUtils.hasText(mediaType)) {
|
|
|
|
return DocumentType.UNKNOWN;
|
|
|
|
return DocumentType.UNKNOWN;
|
|
|
|
|