/** * Clears the annotation for the given ranges of the document as indicated by * the {@link DocumentModifyAction}. * * @param operation the operation to execute. * @param doc the document to annotate. * @param view the view of the document. * @param hitIterator iterates over the ranges to remove the annotation from, * specified in {@link ApiView} offset. * @param modifyAction the {@link DocumentModifyAction} specifying what the * key of the annotation is annotation is. * @throws InvalidRequestException if the annotation could not be set. */ private void clearAnnotation(OperationRequest operation, Document doc, ApiView view, DocumentHitIterator hitIterator, DocumentModifyAction modifyAction) throws InvalidRequestException { Preconditions.checkArgument(modifyAction.getModifyHow() == ModifyHow.CLEAR_ANNOTATION, "This method only supports CLEAR_ANNOTATION"); String annotationKey = modifyAction.getAnnotationKey(); Range range = hitIterator.next(); while (range != null) { int start = view.transformToXmlOffset(range.getStart()); int end = view.transformToXmlOffset(range.getEnd()); setDocumentAnnotation(operation, doc, start, end, annotationKey, null); range = hitIterator.next(); } }
/** * Annotates the given ranges of the document as indicated by the * {@link DocumentModifyAction}. * * @param operation the operation to execute. * @param doc the document to annotate. * @param view the view of the document. * @param hitIterator iterates over the ranges to annotate, specified in * {@link ApiView} offset. * @param modifyAction the {@link DocumentModifyAction} specifying what the * annotation is. * @throws InvalidRequestException if the annotation could not be set. */ private void annotate(OperationRequest operation, Document doc, ApiView view, DocumentHitIterator hitIterator, DocumentModifyAction modifyAction) throws InvalidRequestException { Preconditions.checkArgument( modifyAction.getModifyHow() == ModifyHow.ANNOTATE, "This method only supports ANNOTATE"); String annotationKey = modifyAction.getAnnotationKey(); int valueIndex = 0; Range range = hitIterator.next(); while (range != null) { int start = view.transformToXmlOffset(range.getStart()); int end = view.transformToXmlOffset(range.getEnd()); setDocumentAnnotation( operation, doc, start, end, annotationKey, modifyAction.getValue(valueIndex)); valueIndex++; range = hitIterator.next(); } }
/** * Updates elements in the document. * <b>Note</b>: Only gadget elements are supported, for now. * * @param operation the operation the operation that wants to update elements. * @param doc the document to update elements in. * @param view the {@link ApiView} of that document. * @param hitIterator the iterator over the places where to update elements. * @param modifyAction the action that specifies what to update. * @throws InvalidRequestException if something goes wrong. */ private void updateElement(OperationRequest operation, Document doc, ApiView view, DocumentHitIterator hitIterator, DocumentModifyAction modifyAction) throws InvalidRequestException { Range range = null; for (int index = 0; ((range = hitIterator.next()) != null); ++index) { Element element = modifyAction.getElement(index); if (element != null) { if (element.isGadget()) { int xmlStart = view.transformToXmlOffset(range.getStart()); Doc.E docElem = Point.elementAfter(doc, doc.locate(xmlStart)); updateExistingGadgetElement(doc, docElem, element); } else { // TODO (Yuri Z.) Updating other elements. throw new UnsupportedOperationException( "Can't update other elements than gadgets at the moment"); } } } }
public void testTransformToXmlOffset() { Document document = BasicFactories.documentProvider().parse( LineContainers.debugContainerWrap("some text<gadget></gadget>")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(3, new Image("id", "caption")); List<ElementInfo> apiElements = api.getElements(); for (int i = 0; i < apiElements.size(); i++) { ElementInfo info = apiElements.get(i); assertEquals(info.xmlPosition, api.transformToXmlOffset(info.apiPosition)); } assertInSync(document, api); }
int annotationStart = view.transformToXmlOffset(insertAt); int annotationEnd = view.transformToXmlOffset(insertAt + toInsert.length());
public void testTransformToXmlOffset() { Document document = BasicFactories.documentProvider().parse( LineContainers.debugContainerWrap("some text<gadget></gadget>")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(3, new Image("id", "caption")); List<ElementInfo> apiElements = api.getElements(); for (int i = 0; i < apiElements.size(); i++) { ElementInfo info = apiElements.get(i); assertEquals(info.xmlPosition, api.transformToXmlOffset(info.apiPosition)); } assertInSync(document, api); }
int xmlLocation = view.transformToXmlOffset(index);
int xmlLocation = view.transformToXmlOffset(elementApiLocation + 1);
public void testInsertInlineBlip() throws Exception { OperationContextImpl context = helper.getContext(); OpBasedWavelet wavelet = context.openWavelet(WAVE_ID, WAVELET_ID, ALEX); ObservableConversation conversation = context.openConversation(WAVE_ID, WAVELET_ID, ALEX).getRoot(); int insertAtApiLocation = 2; ApiView apiView = new ApiView(conversation.getRootThread().getFirstBlip().getContent(), wavelet); int insertAtXmlLocation = apiView.transformToXmlOffset(insertAtApiLocation); // Append the inline blip to the root blip String rootBlipId = ConversationUtil.getRootBlipId(conversation); OperationRequest operation = operationRequest(OperationType.DOCUMENT_INSERT_INLINE_BLIP, rootBlipId, Parameter.of(ParamsProperty.BLIP_DATA, blipData), Parameter.of(ParamsProperty.INDEX, insertAtApiLocation)); service.execute(operation, context, ALEX); JsonRpcResponse response = context.getResponse(OPERATION_ID); assertFalse(response.isError()); ConversationBlip newBlip = checkAndGetNewBlip(context, conversation, response); Iterator<? extends LocatedReplyThread<? extends ObservableConversationThread>> it = conversation.getRootThread().getFirstBlip().locateReplyThreads().iterator(); LocatedReplyThread<? extends ObservableConversationThread> inlineReplyThread = it.next(); assertEquals("The inline reply was not located where specified", insertAtXmlLocation, inlineReplyThread.getLocation()); }
public void testInsertInlineBlip() throws Exception { OperationContextImpl context = helper.getContext(); OpBasedWavelet wavelet = context.openWavelet(WAVE_ID, WAVELET_ID, ALEX); ObservableConversation conversation = context.openConversation(WAVE_ID, WAVELET_ID, ALEX).getRoot(); int insertAtApiLocation = 2; ApiView apiView = new ApiView(conversation.getRootThread().getFirstBlip().getContent(), wavelet); int insertAtXmlLocation = apiView.transformToXmlOffset(insertAtApiLocation); // Append the inline blip to the root blip String rootBlipId = ConversationUtil.getRootBlipId(conversation); OperationRequest operation = operationRequest(OperationType.DOCUMENT_INSERT_INLINE_BLIP, rootBlipId, Parameter.of(ParamsProperty.BLIP_DATA, blipData), Parameter.of(ParamsProperty.INDEX, insertAtApiLocation)); service.execute(operation, context, ALEX); JsonRpcResponse response = context.getResponse(OPERATION_ID); assertFalse(response.isError()); ConversationBlip newBlip = checkAndGetNewBlip(context, conversation, response); Iterator<? extends LocatedReplyThread<? extends ObservableConversationThread>> it = conversation.getRootThread().getFirstBlip().locateReplyThreads().iterator(); LocatedReplyThread<? extends ObservableConversationThread> inlineReplyThread = it.next(); assertEquals("The inline reply was not located where specified", insertAtXmlLocation, inlineReplyThread.getLocation()); }