Search in sources :

Example 11 with Annotation

use of org.xwiki.annotation.Annotation in project xwiki-platform by xwiki.

the class AnnotationMaintainerTest method copyOriginalSelections.

/**
 * Helper function to set the original selected contents in the updated annotations of this document from the
 * original list of annotations.
 *
 * @param doc the document for which to set the updated annotations original selected contents
 */
private void copyOriginalSelections(MockDocument doc) {
    // set the original selections of updated annotations from the original annotations of the document
    for (Annotation updatedAnn : doc.getUpdatedAnnotations()) {
        if (updatedAnn.getState() != AnnotationState.UPDATED) {
            continue;
        }
        Annotation originalAnn = getAnnotation(updatedAnn.getId(), doc.getAnnotations());
        updatedAnn.setOriginalSelection(originalAnn.getSelection());
    }
}
Also used : Annotation(org.xwiki.annotation.Annotation)

Example 12 with Annotation

use of org.xwiki.annotation.Annotation in project xwiki-platform by xwiki.

the class TestDocumentFactory method saveKeyToDoc.

@Override
protected void saveKeyToDoc(String currentKey, String currentValue, org.xwiki.annotation.MockDocument doc, String docName) throws IOException {
    // test if it's a modified annotation and parse & save as a modified annotation
    if (currentKey.equals("annotationUpdated") && doc instanceof MockDocument) {
        Annotation ann = parseAnnotation(currentValue, docName);
        ((MockDocument) doc).getUpdatedAnnotations().add(ann);
    } else {
        super.saveKeyToDoc(currentKey, currentValue, doc, docName);
    }
}
Also used : Annotation(org.xwiki.annotation.Annotation)

Example 13 with Annotation

use of org.xwiki.annotation.Annotation in project xwiki-platform by xwiki.

the class DefaultIOService method getAnnotations.

/**
 * {@inheritDoc}
 * <p>
 * This implementation retrieves all the objects of the annotation class in the document where target points to, and
 * which have the target set to {@code target}.
 * </p>
 *
 * @see org.xwiki.annotation.io.IOService#getAnnotations(String)
 */
@Override
public Collection<Annotation> getAnnotations(String target) throws IOServiceException {
    try {
        // parse the target and extract the local reference serialized from it, by the same rules
        EntityReference targetReference = referenceResolver.resolve(target, EntityType.DOCUMENT);
        // build the target identifier for the annotation
        String localTargetId = target;
        // and the name of the document where it should be stored
        String docName = target;
        if (targetReference.getType() == EntityType.DOCUMENT || targetReference.getType() == EntityType.OBJECT_PROPERTY) {
            localTargetId = localSerializer.serialize(targetReference);
            docName = serializer.serialize(targetReference.extractReference(EntityType.DOCUMENT));
        }
        // get the document
        XWikiContext deprecatedContext = getXWikiContext();
        XWikiDocument document = deprecatedContext.getWiki().getDocument(docName, deprecatedContext);
        // and the annotation class objects in it
        List<BaseObject> objects = document.getXObjects(configuration.getAnnotationClassReference());
        // and build a list of Annotation objects
        List<Annotation> result = new ArrayList<Annotation>();
        if (objects == null) {
            return Collections.<Annotation>emptySet();
        }
        for (BaseObject object : objects) {
            // if it's not on the required target, ignore it
            if (object == null || !localTargetId.equals(object.getStringValue(Annotation.TARGET_FIELD))) {
                continue;
            }
            // use the object number as annotation id
            result.add(loadAnnotationFromObject(object, deprecatedContext));
        }
        return result;
    } catch (XWikiException e) {
        throw new IOServiceException("An exception has occurred while loading the annotations", e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) IOServiceException(org.xwiki.annotation.io.IOServiceException) EntityReference(org.xwiki.model.reference.EntityReference) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) Annotation(org.xwiki.annotation.Annotation) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 14 with Annotation

use of org.xwiki.annotation.Annotation 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)

Aggregations

Annotation (org.xwiki.annotation.Annotation)14 ArrayList (java.util.ArrayList)5 XWikiException (com.xpn.xwiki.XWikiException)4 IOServiceException (org.xwiki.annotation.io.IOServiceException)3 AnnotationField (org.xwiki.annotation.rest.model.jaxb.AnnotationField)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 Map (java.util.Map)2 AnnotationServiceException (org.xwiki.annotation.AnnotationServiceException)2 AnnotationResponse (org.xwiki.annotation.rest.model.jaxb.AnnotationResponse)2 ObjectFactory (org.xwiki.annotation.rest.model.jaxb.ObjectFactory)2 EntityReference (org.xwiki.model.reference.EntityReference)2 HashMap (java.util.HashMap)1 List (java.util.List)1 PUT (javax.ws.rs.PUT)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 AlteredContent (org.xwiki.annotation.content.AlteredContent)1 AnnotationEvent (org.xwiki.annotation.renderer.AnnotationEvent)1 AnnotatedContent (org.xwiki.annotation.rest.model.jaxb.AnnotatedContent)1