use of eu.transkribus.core.model.beans.mets.FileType in project TranskribusCore by Transkribus.
the class MetsUtil method getImagesToUpload.
public static List<PageUploadDescriptor> getImagesToUpload(Mets mets) {
// check filesection. needs img group and xml group to distinguish them without going for mimetypes
List<FileGrpType> typeGrps = getMasterFileGrp(mets);
boolean hasXml = true;
List<FileType> xmlGrp = null;
List<FileType> imgGrp = null;
for (FileGrpType type : typeGrps) {
switch(type.getID()) {
case TrpMetsBuilder.IMG_GROUP_ID:
imgGrp = type.getFile();
break;
case TrpMetsBuilder.PAGE_GROUP_ID:
xmlGrp = type.getFile();
break;
default:
break;
}
}
if (imgGrp == null) {
throw new IllegalArgumentException("METS file has no image file list!");
}
if (xmlGrp == null) {
logger.debug("METS file has no xml file list!");
}
List<DivType> pageDivs = getPageDivsFromStructMap(mets);
if (pageDivs == null)
throw new IllegalArgumentException("No valid StructMap was found!");
List<PageUploadDescriptor> images = new ArrayList<PageUploadDescriptor>(pageDivs.size());
for (DivType div : pageDivs) {
PageUploadDescriptor image = buildUploadImage(div, imgGrp, xmlGrp);
images.add(image);
}
return images;
}
use of eu.transkribus.core.model.beans.mets.FileType in project TranskribusCore by Transkribus.
the class MetsUtil method getTrpPages.
/**
* Builds the set of TrpPage objects with
* local file references from the mets master file group and structmap.
* The method is strict regarding PAGE XML existence! Each image file must have a correspondent PAGE XML.
* @param mets
* @param parentDir
* @return
* @throws IOException
*/
public static List<TrpPage> getTrpPages(Mets mets, File parentDir) throws IOException {
// check filesection. needs img group and xml group to distinguish them without going for mimetypes
List<FileGrpType> typeGrps = getMasterFileGrp(mets);
List<FileType> xmlGrp = null;
List<FileType> imgGrp = null;
for (FileGrpType type : typeGrps) {
switch(type.getID()) {
case TrpMetsBuilder.IMG_GROUP_ID:
imgGrp = type.getFile();
break;
case TrpMetsBuilder.PAGE_GROUP_ID:
xmlGrp = type.getFile();
break;
default:
break;
}
}
if (imgGrp == null)
throw new IOException("METS file has no image file list!");
if (xmlGrp == null)
throw new IOException("METS file has no xml file list!");
List<DivType> pageDivs = getPageDivsFromStructMap(mets);
if (pageDivs == null) {
throw new IOException("No valid StructMap was found!");
}
List<TrpPage> pages = new ArrayList<TrpPage>(pageDivs.size());
for (DivType div : pageDivs) {
TrpPage page = buildPage(div, imgGrp, xmlGrp, parentDir);
pages.add(page);
}
return pages;
}
use of eu.transkribus.core.model.beans.mets.FileType in project TranskribusCore by Transkribus.
the class MetsUtil method buildPage.
private static TrpPage buildPage(DivType div, List<FileType> imgGrp, List<FileType> xmlGrp, File parentDir) throws IOException {
TrpPage page = new TrpPage();
int nr = div.getORDER().intValue();
page.setPageNr(nr);
File imgFile = null;
File xmlFile = null;
// FIXME this will only work for local files
for (Fptr ptr : div.getFptr()) {
FileType type = (FileType) ptr.getArea().getFILEID();
if (imgGrp.contains(type)) {
imgFile = MetsUtil.getFile(type, parentDir);
} else if (xmlGrp.contains(type)) {
xmlFile = MetsUtil.getFile(type, parentDir);
}
}
if (imgFile == null) {
logger.error("No master image mapped for page " + nr + " in the structmap!");
} else {
logger.info("Page " + page.getPageNr() + " image: " + imgFile.getAbsolutePath());
}
// FIXME NullpointerException if imgFile == null!
page.setUrl(imgFile.toURI().toURL());
page.setKey(null);
page.setDocId(-1);
page.setImgFileName(imgFile.getName());
if (xmlFile == null) {
logger.error("No master xml mapped for page " + nr + " in the structmap!");
} else {
logger.info("Page " + page.getPageNr() + " xml: " + xmlFile.getAbsolutePath());
}
// FIXME NullpointerException if xmlFile == null!
TrpTranscriptMetadata tmd = new TrpTranscriptMetadata();
tmd.setPageReferenceForLocalDocs(page);
tmd.setPageId(page.getPageId());
tmd.setUrl(xmlFile.toURI().toURL());
tmd.setKey(null);
tmd.setStatus(EditStatus.NEW);
tmd.setTimestamp(new Date().getTime());
tmd.setUserName("LocalDocReader");
page.getTranscripts().add(tmd);
return page;
}
use of eu.transkribus.core.model.beans.mets.FileType in project TranskribusCore by Transkribus.
the class TrpMetsBuilder method buildMets.
/**
* Generate a METS containing
* <ul>
* <li>TrpDocMetadata embedded in sourceMd</li>
* <li>all page images</li>
* <li>the most recent PAGE XML files from the Doc</li>
* </ul>
*
* If a local document is passed, all hrefs will contain the relative paths to files based on the localFolder!
*
* @param doc
* @param exportImages
* @param pageIndices
* @return
* @throws IOException if image/xml files can't be accessed for reading the mimetype etc.
*/
public static Mets buildMets(TrpDoc doc, boolean exportPage, boolean exportAlto, boolean exportImages, Set<Integer> pageIndices) throws IOException {
Mets mets = new Mets();
TrpDocMetadata md = doc.getMd();
File localFolder = md.getLocalFolder();
boolean isLocalDoc = localFolder != null;
mets.setLABEL(md.getTitle());
mets.setOBJID("" + md.getDocId());
mets.setPROFILE(TRP_METS_PROFILE);
// FIXME remove TYPE
// mets.setTYPE(TRP_METS_PROFILE);
// metsHdr
MetsHdr hdr = buildMetsHdr(md);
mets.setMetsHdr(hdr);
// TODO dcmd_elec omitted meanwhile
// md_orig
AmdSecType amdSec = new AmdSecType();
amdSec.setID(SOURCE_MD_ID_CONST);
MdSecType sourceMdSec = buildSourceMdSec(md);
amdSec.getSourceMD().add(sourceMdSec);
mets.getAmdSec().add(amdSec);
// structmap div, linking to the sourceMd section with dmd
DivType div = new DivType();
div.getADMID().add(sourceMdSec);
div.setID(TRP_DOC_DIV_ID);
FileSec fileSec = new FileSec();
StructMapType structMap = new StructMapType();
structMap.setID(TRP_STRUCTMAP_ID);
structMap.setTYPE("MANUSCRIPT");
structMap.setDiv(div);
List<TrpPage> pages = doc.getPages();
FimgStoreGetClient client = null;
if (!isLocalDoc) {
// TODO maybe we need this stuff in the docMetadata?
URL url = pages.get(0).getUrl();
client = new FimgStoreGetClient(url);
}
FileGrp masterGrp = new FileGrp();
masterGrp.setID(MASTER_FILE_GRP_ID);
FileGrpType imgGrp = new FileGrpType();
imgGrp.setID(IMG_GROUP_ID);
FileGrpType pageGrp = new FileGrpType();
pageGrp.setID(PAGE_GROUP_ID);
FileGrpType altoGrp = new FileGrpType();
altoGrp.setID(ALTO_GROUP_ID);
int i = -1;
for (TrpPage p : pages) {
i++;
if (pageIndices != null && !pageIndices.contains(i)) {
continue;
}
// build a page div for the structmap
DivType pageDiv = new DivType();
pageDiv.setID("PAGE_" + p.getPageNr());
pageDiv.setTYPE("SINGLE_PAGE");
pageDiv.setORDER(BigInteger.valueOf(p.getPageNr()));
final String imgId = "IMG_" + p.getPageNr();
final String xmlId = PAGE_GROUP_ID + "_" + p.getPageNr();
final String altoId = ALTO_GROUP_ID + "_" + p.getPageNr();
/* only the most recent transcript is added here for now
*
* TODO how to deal with imagestore files? use orig image? right now, it's just the view file...
* TODO thumbnails not yet included
*/
if (exportImages) {
FileType img = buildFileType(localFolder, imgId, p, p.getPageNr(), client);
imgGrp.getFile().add(img);
// linking images
Fptr imgPtr = buildFptr(img);
pageDiv.getFptr().add(imgPtr);
}
// TODO error handling.. if no transcript??
if (exportPage) {
// xmlfiletype: just add the most recent transcript
TrpTranscriptMetadata tMd;
// get the transcript chosen for export
tMd = p.getCurrentTranscript();
FileType xml = buildFileType(md.getLocalFolder(), xmlId, tMd, p.getPageNr(), client);
pageGrp.getFile().add(xml);
Fptr xmlPtr = buildFptr(xml);
pageDiv.getFptr().add(xmlPtr);
}
// creat ALTO fileGrp
if (exportAlto) {
FileType altoFt = new FileType();
altoFt.setCHECKSUMTYPE(ChecksumUtils.ChkSumAlg.MD5.toString());
// TODO calculate checksum
altoFt.setCHECKSUM("");
FLocat fLocat = new FLocat();
fLocat.setLOCTYPE("OTHER");
fLocat.setOTHERLOCTYPE("FILE");
altoFt.setID(altoId);
altoFt.setSEQ(p.getPageNr());
// String tmpImgName = img.getFLocat().get(0).getHref();
String relAltoPath = "alto".concat(File.separator).concat(p.getImgFileName().substring(0, p.getImgFileName().lastIndexOf(".")).concat(".xml"));
fLocat.setHref(relAltoPath);
// String absAltoPath = tMd.getUrl().getPath().replace("page", "alto");
final String path = FileUtils.toFile(p.getUrl()).getAbsolutePath();
String absAltoPath = path.substring(0, path.lastIndexOf(File.separator));
absAltoPath = absAltoPath.concat("/alto/").concat(p.getImgFileName().substring(0, p.getImgFileName().lastIndexOf(".")).concat(".xml"));
// logger.info("alto path starts with: " + absAltoPath);
if (absAltoPath.startsWith("\\")) /*|| absAltoPath.startsWith("/")*/
{
// logger.info("alto path starts with \\ or /");
absAltoPath = absAltoPath.substring(1);
}
String mime = MimeTypes.getMimeType("xml");
altoFt.setMIMETYPE(mime);
File altoTmp = new File(absAltoPath);
if (altoTmp.exists()) {
// logger.info("alto file exist at " + absAltoPath);
Date date = new Date(altoTmp.lastModified());
XMLGregorianCalendar cal = JaxbUtils.getXmlCalendar(date);
altoFt.setCREATED(cal);
} else {
logger.info("alto file does not exist at " + absAltoPath);
}
// System.out.println("relAltoPath " + relAltoPath);
// System.out.println("absAltoPath " + absAltoPath);
// System.in.read();
altoFt.getFLocat().add(fLocat);
altoGrp.getFile().add(altoFt);
Fptr altoPtr = buildFptr(altoFt);
pageDiv.getFptr().add(altoPtr);
}
div.getDiv().add(pageDiv);
}
fileSec.getFileGrp().add(masterGrp);
mets.setFileSec(fileSec);
if (exportImages) {
masterGrp.getFileGrp().add(imgGrp);
}
if (exportPage) {
masterGrp.getFileGrp().add(pageGrp);
}
if (exportAlto) {
masterGrp.getFileGrp().add(altoGrp);
}
mets.getStructMap().add(structMap);
return mets;
}
use of eu.transkribus.core.model.beans.mets.FileType in project TranskribusCore by Transkribus.
the class TrpMetsBuilder method buildFileType.
/**
* @param localFolder null if isLocalDoc
* @param id
* @param o
* @param client
* @return
* @throws IOException
*/
private static FileType buildFileType(File localFolder, String id, ITrpFile o, final int seq, FimgStoreGetClient client) throws IOException {
FileType fType = new FileType();
fType.setID(id);
String mime = null;
Date date = null;
FLocat fLocat = new FLocat();
String loc = null;
if (localFolder != null) {
URL url = o.getUrl();
if (!url.getProtocol().contains("file")) {
throw new IOException("Doc contains local folder reference but an URL refers to a non-local file! " + url.toString());
}
final String path = FileUtils.toFile(url).getAbsolutePath();
File f = new File(path);
mime = MimeTypes.getMimeType(FilenameUtils.getExtension(f.getName()));
date = new Date(f.lastModified());
fLocat.setLOCTYPE("OTHER");
fLocat.setOTHERLOCTYPE("FILE");
// remove protocol and localfolder, i.e. get relative path to this file
// loc = path.substring(localFolder.getAbsolutePath().length() + 1); // BUG: localFolder != path!!
loc = FilenameUtils.getName(path);
if (id.startsWith(PAGE_GROUP_ID)) {
// append relative folder for PAGE XML files
loc = "page/" + loc;
}
logger.debug("loc = " + loc);
if (o.getMd5Sum() != null) {
fType.setCHECKSUMTYPE(ChecksumUtils.ChkSumAlg.MD5.toString());
fType.setCHECKSUM(o.getMd5Sum());
}
} else {
try {
FimgStoreFileMd fMd = client.getFileMd(o.getKey());
date = fMd.getUploadDate();
mime = fMd.getMimetype();
fLocat.setLOCTYPE("URL");
// full URL in case of remote file
loc = o.getUrl().toString();
} catch (IOException e) {
logger.error(e.getMessage(), e);
throw new IOException("FileMetadata could not be retrieved from imagestore for key: " + o.getKey());
}
}
fType.setMIMETYPE(mime);
XMLGregorianCalendar cal = JaxbUtils.getXmlCalendar(date);
fType.setCREATED(cal);
fType.setSEQ(seq);
fLocat.setHref(loc);
fType.getFLocat().add(fLocat);
return fType;
}
Aggregations