use of org.xwiki.annotation.content.AlteredContent in project xwiki-platform by xwiki.
the class AbstractAnnotationMaintainer method updateAnnotations.
@Override
public void updateAnnotations(String target, String previousContent, String currentContent) throws MaintainerServiceException {
Collection<Annotation> annotations;
try {
annotations = ioService.getAnnotations(target);
if (annotations.size() == 0) {
// no annotations, nothing to do
return;
}
// store the annotations to save after update
List<Annotation> toUpdate = new ArrayList<Annotation>();
// produce the ptr of the previous and current, wrt to syntax
String syntaxId = ioContentService.getSourceSyntax(target);
String renderedPreviousContent = renderPlainText(previousContent, syntaxId);
String renderedCurrentContent = renderPlainText(currentContent, syntaxId);
// create the diffs
Collection<XDelta> differences = getDiffService().getDifferences(renderedPreviousContent, renderedCurrentContent);
// text space normalized version
if (differences.size() > 0) {
// compute the spaceless version of the renderedPreviousContent to be able to map the annotation on it
// (so that matching is done in the same way as for rendering), and then go back to the normalized
// version
AlteredContent spacelessRenderedPreviousContent = spaceStripperContentAlterer.alter(renderedPreviousContent);
// recompute properties for all annotations and store the ones to update
for (Annotation annotation : annotations) {
boolean wasUpdated = recomputeProperties(annotation, differences, renderedPreviousContent, spacelessRenderedPreviousContent, renderedCurrentContent);
if (wasUpdated) {
toUpdate.add(annotation);
}
}
}
// finally store the updates
ioService.updateAnnotations(target, toUpdate);
} catch (Exception e) {
throw new MaintainerServiceException("An exception occurred while updating annotations for content at " + target, e);
}
}
use of org.xwiki.annotation.content.AlteredContent in project xwiki-platform by xwiki.
the class AnnotationGeneratorChainingListener method handleRawText.
/**
* Helper function to help handle raw text, such as the raw blocks or the verbatim blocks.
*
* @param text the raw text to handle
*/
private void handleRawText(String text) {
// normalize the protected string before adding it to the plain text version
AlteredContent cleanedContent = selectionAlterer.alter(text);
// put this event in the mapping only if it has indeed generated something
String cleanedContentString = cleanedContent.getContent().toString();
if (!StringUtils.isEmpty(cleanedContentString)) {
plainTextContent.append(cleanedContentString);
eventsMapping.put(plainTextContent.length() - 1, getLast());
// also store this event in the list of events with altered content
alteredEventsContent.put(getLast(), cleanedContent);
}
}
use of org.xwiki.annotation.content.AlteredContent in project xwiki-platform by xwiki.
the class PlainTextNormalizingChainingRenderer method onRawText.
@Override
public void onRawText(String text, Syntax syntax) {
if (StringUtils.isNotEmpty(text)) {
// adding it to the plain text version
if (Character.isWhitespace(text.charAt(0))) {
// if there is a space right at the beginning of the raw text, we need to print a space
printSpace();
}
AlteredContent cleanedContent = textCleaner.alter(text);
printText(cleanedContent.getContent().toString());
if (Character.isWhitespace(text.charAt(text.length() - 1))) {
// if there is a space right at the end of the text, we need to print a space
printSpace();
}
}
}
use of org.xwiki.annotation.content.AlteredContent in project xwiki-platform by xwiki.
the class AnnotationGeneratorChainingListener method getEventAndOffset.
/**
* Helper function to get the event where the passed index falls in, based on the isEnd setting to know if the
* offset should be given before the character at the index position or after it.
*
* @param index the index to get the event for
* @param isEnd {@code true} if the index should be considered as an end index, {@code false} otherwise
* @return an array of objects to hold the event reference, on the first position, and the offset inside this event
* on the second
*/
private Object[] getEventAndOffset(int index, boolean isEnd) {
Map.Entry<Integer, Event> previous = null;
for (Map.Entry<Integer, Event> range : eventsMapping.entrySet()) {
// first event that contains the index
if (index <= range.getKey()) {
// get this event
Event evt = range.getValue();
// compute the start index wrt to the end index of the previous event
int startIndex = 0;
if (previous != null) {
startIndex = previous.getKey() + 1;
}
// compute the offset inside this event wrt the start index of this event
int offset = index - startIndex;
// adjust this offset if the content of this event was altered
AlteredContent alteredEventContent = alteredEventsContent.get(evt);
if (alteredEventContent != null) {
offset = alteredEventContent.getInitialOffset(offset);
}
// end indexes are specified after the character/position
if (isEnd) {
offset += 1;
}
// return the result
return new Object[] { evt, offset };
}
// else advance one more step, storing the previous event
previous = range;
}
// nothing was found, return null. However this shouldn't happen :)
return null;
}
use of org.xwiki.annotation.content.AlteredContent in project xwiki-platform by xwiki.
the class PlainTextNormalizingChainingRenderer method onVerbatim.
@Override
public void onVerbatim(String content, boolean inline, Map<String, String> parameters) {
if (!inline || Character.isWhitespace(content.charAt(0))) {
// if there is a space right at the beginning of the verbatim string, or the verbatim string is block, we
// need to print a space
printSpace();
}
AlteredContent cleanedContent = textCleaner.alter(content);
printText(cleanedContent.getContent().toString());
if (!inline || Character.isWhitespace(content.charAt(content.length() - 1))) {
// print a space after
printSpace();
}
}
Aggregations