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);
});
}
Aggregations