Search in sources :

Example 1 with MCRAltoWordChange

use of org.mycore.viewer.alto.model.MCRAltoWordChange in project mycore by MyCoRe-Org.

the class MCRDefaultAltoChangeApplier method applyChange.

@Override
public void applyChange(MCRAltoChangeSet changeSet) {
    String derivateID = changeSet.getDerivateID();
    changeSet.getWordChanges().stream().forEach(change -> {
        List<MCRAltoWordChange> list = fileChangeMap.computeIfAbsent(change.getFile(), (k) -> new ArrayList<>());
        list.add(change);
    });
    fileChangeMap.keySet().forEach(file -> {
        LOGGER.info("Open file {} to apply changes!", file);
        MCRPath altoFilePath = MCRPath.getPath(derivateID, file);
        if (!Files.exists(altoFilePath)) {
            LOGGER.warn("Could not find file {} which was referenced by alto change!", altoFilePath);
            throw new MCRException(new IOException("Alto-File " + altoFilePath + " does not exist"));
        }
        Document altoDocument = readALTO(altoFilePath);
        List<MCRAltoWordChange> wordChangesInThisFile = fileChangeMap.get(file);
        wordChangesInThisFile.stream().forEach(wordChange -> {
            String xpath = String.format(Locale.ROOT, "//alto:String[number(@HPOS)=number('%d') and number(@VPOS)=number('%d')]", wordChange.getHpos(), wordChange.getVpos());
            List<Element> wordToChange = XPathFactory.instance().compile(xpath, Filters.element(), null, MCRConstants.ALTO_NAMESPACE).evaluate(altoDocument);
            if (wordToChange.size() != 1) {
                LOGGER.warn("Found {} words to change.", wordToChange.size());
            }
            wordToChange.forEach(word -> {
                word.setAttribute("CONTENT", wordChange.getTo());
                word.setAttribute("WC", "1");
            });
        });
        storeALTO(altoFilePath, altoDocument);
    });
}
Also used : MCRAltoWordChange(org.mycore.viewer.alto.model.MCRAltoWordChange) MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document)

Aggregations

IOException (java.io.IOException)1 Document (org.jdom2.Document)1 Element (org.jdom2.Element)1 MCRException (org.mycore.common.MCRException)1 MCRPath (org.mycore.datamodel.niofs.MCRPath)1 MCRAltoWordChange (org.mycore.viewer.alto.model.MCRAltoWordChange)1