Search in sources :

Example 1 with AnnotationEvent

use of org.xwiki.annotation.renderer.AnnotationEvent in project xwiki-platform by xwiki.

the class AnnotationXHTMLChainingRenderer method onRawText.

@Override
public void onRawText(String text, Syntax syntax) {
    // FIXME: this is going to be messy, messy because of the raw block syntax which can be HTML and produce very
    // invalid html.
    SortedMap<Integer, List<AnnotationEvent>> currentBookmarks = getAnnotationGenerator().getAnnotationEvents();
    if (currentBookmarks != null) {
        // open all annotations that start in this event
        for (Map.Entry<Integer, List<AnnotationEvent>> bookmark : currentBookmarks.entrySet()) {
            for (AnnotationEvent annEvt : bookmark.getValue()) {
                if (annEvt.getType() == AnnotationEventType.START) {
                    getAnnotationsMarkerPrinter().beginAnnotation(annEvt.getAnnotation());
                }
            }
        }
    }
    // open all annotation markers in case there was any annotation enclosing this block
    getAnnotationsMarkerPrinter().openAllAnnotationMarkers();
    // Store the raw text as it is ftm. Should handle syntax in the future
    super.onRawText(text, syntax);
    if (currentBookmarks != null) {
        // close all annotations that start in this event.
        for (Map.Entry<Integer, List<AnnotationEvent>> bookmark : currentBookmarks.entrySet()) {
            for (AnnotationEvent annEvt : bookmark.getValue()) {
                if (annEvt.getType() == AnnotationEventType.END) {
                    getAnnotationsMarkerPrinter().endAnnotation(annEvt.getAnnotation());
                }
            }
        }
    }
}
Also used : List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) SortedMap(java.util.SortedMap) AnnotationEvent(org.xwiki.annotation.renderer.AnnotationEvent)

Example 2 with AnnotationEvent

use of org.xwiki.annotation.renderer.AnnotationEvent in project xwiki-platform by xwiki.

the class AnnotationGeneratorChainingListener method mapAnnotations.

/**
 * Helper method to map the annotations on the plainTextContent and identify the events where annotations start and
 * end.
 */
private void mapAnnotations() {
    for (Annotation ann : annotations) {
        // clean it up and its context
        String annotationContext = ann.getSelectionInContext();
        if (StringUtils.isEmpty(annotationContext)) {
            // TODO: mark it somehow...
            continue;
        }
        // build the cleaned version of the annotation by cleaning its left context, selection and right context and
        // concatenating them together
        String alteredsLeftContext = StringUtils.isEmpty(ann.getSelectionLeftContext()) ? "" : selectionAlterer.alter(ann.getSelectionLeftContext()).getContent().toString();
        String alteredRightContext = StringUtils.isEmpty(ann.getSelectionRightContext()) ? "" : selectionAlterer.alter(ann.getSelectionRightContext()).getContent().toString();
        String alteredSelection = StringUtils.isEmpty(ann.getSelection()) ? "" : selectionAlterer.alter(ann.getSelection()).getContent().toString();
        String cleanedContext = alteredsLeftContext + alteredSelection + alteredRightContext;
        // find the annotation with its context in the plain text representation of the content
        int contextIndex = plainTextContent.indexOf(cleanedContext);
        if (contextIndex >= 0) {
            // find the indexes where annotation starts and ends inside the cleaned context
            int alteredSelectionStartIndex = alteredsLeftContext.length();
            int alteredSelectionEndIndex = alteredSelectionStartIndex + alteredSelection.length() - 1;
            // get the start and end events for the annotation
            // annotation starts before char at annotationIndex and ends after char at annotationIndex +
            // alteredSelection.length() - 1
            Object[] startEvt = getEventAndOffset(contextIndex + alteredSelectionStartIndex, false);
            Object[] endEvt = getEventAndOffset(contextIndex + alteredSelectionEndIndex, true);
            if (startEvt != null & endEvt != null) {
                // store the bookmarks
                addBookmark((Event) startEvt[0], new AnnotationEvent(AnnotationEventType.START, ann), (Integer) startEvt[1]);
                addBookmark((Event) endEvt[0], new AnnotationEvent(AnnotationEventType.END, ann), (Integer) endEvt[1]);
            } else {
                // TODO: mark it somehow...
                continue;
            }
        } else {
            // TODO: mark it somehow...
            continue;
        }
    }
}
Also used : Annotation(org.xwiki.annotation.Annotation) AnnotationEvent(org.xwiki.annotation.renderer.AnnotationEvent)

Example 3 with AnnotationEvent

use of org.xwiki.annotation.renderer.AnnotationEvent in project xwiki-platform by xwiki.

the class AnnotationMarkersXHTMLPrinter method printXMLWithAnnotations.

/**
 * Helper function to help render a piece of text with annotation events inside it, at the specified offsets. To be
 * used for the events which generate text and need to generate annotation markers inside them.
 *
 * @param text the text to render
 * @param annotations the map of indexes and annotation events to render in this text
 */
public void printXMLWithAnnotations(String text, SortedMap<Integer, List<AnnotationEvent>> annotations) {
    // iterate through the indexes of annotations events, print the chunks in between and then handle the annotation
    // events
    int previous = 0;
    for (int index : annotations.keySet()) {
        // create the current chunk
        String currentChunk = text.substring(previous, index);
        // print the current chunk
        if (currentChunk.length() > 0) {
            printXML(currentChunk);
        }
        // handle all annotations at this position
        for (AnnotationEvent evt : annotations.get(index)) {
            switch(evt.getType()) {
                case START:
                    beginAnnotation(evt.getAnnotation());
                    break;
                case END:
                    endAnnotation(evt.getAnnotation());
                    break;
                default:
                    // nothing
                    break;
            }
        }
        // and prepare next iteration
        previous = index;
    }
    // print the last chunk of text
    String chunk = text.substring(previous);
    if (chunk.length() > 0) {
        printXML(chunk);
    }
}
Also used : AnnotationEvent(org.xwiki.annotation.renderer.AnnotationEvent)

Aggregations

AnnotationEvent (org.xwiki.annotation.renderer.AnnotationEvent)3 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 Annotation (org.xwiki.annotation.Annotation)1