use of eu.transkribus.core.io.util.ImgFilenameFilter in project TranskribusCore by Transkribus.
the class TrpDocUploadBuilder method validateAndNormalize.
/**
*Ensures that all images have filenames assigned and page indices are iterated throughout the structure
* If page indices start from 0 they will be incremented by 1 in order to be compatible with METS-style counting.
* If XML filenames have the "page/" dir prefix, it will be removed.
* @param pages
*/
public static void validateAndNormalize(List<PageUploadDescriptor> pages) {
if (pages.isEmpty()) {
throw new IllegalArgumentException("Image list is empty!");
}
ImgFilenameFilter imgNameFilter = new ImgFilenameFilter();
// check page indices
int i = pages.get(0).getPageNr();
// check if it starts with 1 or 0
boolean pageCountFromZero = false;
if (i == 0) {
// increment all indexes by 1
pageCountFromZero = true;
} else if (i < 0 || i > 1) {
throw new IllegalArgumentException("page indexes have to start with 1 or 0!");
}
for (PageUploadDescriptor img : pages) {
// check page indexes for continuity
if (img.getPageNr() != i) {
throw new IllegalArgumentException("Page indexes are inconsistent!");
} else {
i++;
}
// correct the index if counting starts from zero as METS also includes counts starting from 1
if (pageCountFromZero) {
img.setPageNr(img.getPageNr() + 1);
}
// ensure that at least the img filename is set
if (StringUtils.isEmpty(img.getFileName())) {
throw new IllegalArgumentException("Image filename is empty for page index: " + img.getPageNr());
}
if (!imgNameFilter.accept(null, img.getFileName())) {
throw new IllegalArgumentException("Image type is not supported: " + img.getFileName());
}
if (!StringUtils.isEmpty(img.getPageXmlName()) && img.getPageXmlName().startsWith(LocalDocConst.PAGE_FILE_SUB_FOLDER + "/")) {
// remove the "page/" prefix in XML filename if existent
img.setPageXmlName(img.getPageXmlName().replaceFirst(LocalDocConst.PAGE_FILE_SUB_FOLDER + "/", ""));
}
}
}
Aggregations