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