new DocumentModifyAction( modifyHow, values, annotationName, elements, bundledAnnotations, useMarkup)));
public void testDocumentModifyParametersForAnnotate() { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); blip.all().replace("foo foo foo"); blip.all("foo").annotate("key", "value1", "value2", "value3"); List<OperationRequest> ops = blip.getOperationQueue().getPendingOperations(); DocumentModifyAction action = (DocumentModifyAction) ops.get(ops.size() - 1).getParameter( ParamsProperty.MODIFY_ACTION); assertEquals("key", action.getAnnotationKey()); assertEquals(Arrays.asList("value1", "value2", "value3"), action.getValues()); }
DocumentModifyAction modifyAction, int valueIndex) throws InvalidRequestException { if (modifyAction.hasTextAt(valueIndex)) { String toInsert = modifyAction.getValue(valueIndex); if (modifyAction.getBundledAnnotations() != null) { int annotationStart = view.transformToXmlOffset(insertAt); int annotationEnd = view.transformToXmlOffset(insertAt + toInsert.length()); for (BundledAnnotation ia : modifyAction.getBundledAnnotations()) { setDocumentAnnotation(operation, doc, annotationStart, annotationEnd, ia.key, ia.value); Element element = modifyAction.getElement(valueIndex); if (element != null) { if (element.isGadget()) {
/** * 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(); } }
/** * 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(); } }
public void testDocumentModifyParametersForInsertInsertAfterAndReplace() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); blip.at(0).insert(new Image("http://a/b.gif", 100, 100, "Foo"), Plaintext.of("bold"), Plaintext.of("text")); List<OperationRequest> ops = blip.getOperationQueue().getPendingOperations(); DocumentModifyAction action = (DocumentModifyAction) ops.get(ops.size() - 1).getParameter( ParamsProperty.MODIFY_ACTION); assertEquals(3, action.getValues().size()); assertFalse(action.hasTextAt(0)); assertNull(action.getValues().get(0)); assertTrue(action.hasTextAt(1)); assertEquals("bold", action.getValues().get(1)); assertTrue(action.hasTextAt(2)); assertEquals("text", action.getValues().get(2)); assertEquals(3, action.getElements().size()); Element el = action.getElements().get(0); assertTrue(el instanceof Image); assertEquals("http://a/b.gif", ((Image) el).getUrl()); assertNull(action.getElements().get(1)); assertNull(action.getElements().get(2)); }
@SuppressWarnings("unchecked") public void testDocumentModifyParametersForUpdateElement() { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); Map<String, String> newProperties = new HashMap<String, String>(); newProperties.put("url", "http://www.google.com/gadget.xml"); blip.first(ElementType.GADGET).updateElement(newProperties); List<OperationRequest> ops = blip.getOperationQueue().getPendingOperations(); DocumentModifyAction action = (DocumentModifyAction) ops.get(0).getParameter( ParamsProperty.MODIFY_ACTION); assertEquals("http://www.google.com/gadget.xml", action.getElement(0).getProperty("url")); }
public void testDocumentModifyParametersForClearAnnotation() { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); blip.all().clearAnnotation("key"); List<OperationRequest> ops = blip.getOperationQueue().getPendingOperations(); DocumentModifyAction action = (DocumentModifyAction) ops.get(ops.size() - 1).getParameter( ParamsProperty.MODIFY_ACTION); assertEquals("key", action.getAnnotationKey()); }
DocumentHitIterator hitIterator = getDocumentHitIterator(operation, view); switch (modifyAction.getModifyHow()) { case ANNOTATE: annotate(operation, doc, view, hitIterator, modifyAction); default: throw new UnsupportedOperationException( "Unsupported ModifyHow " + modifyAction.getModifyHow());
public void testDocumentModifyParametersForInsertInsertAfterAndReplace() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); blip.at(0).insert(new Image("http://a/b.gif", 100, 100, "Foo"), Plaintext.of("bold"), Plaintext.of("text")); List<OperationRequest> ops = blip.getOperationQueue().getPendingOperations(); DocumentModifyAction action = (DocumentModifyAction) ops.get(ops.size() - 1).getParameter( ParamsProperty.MODIFY_ACTION); assertEquals(3, action.getValues().size()); assertFalse(action.hasTextAt(0)); assertNull(action.getValues().get(0)); assertTrue(action.hasTextAt(1)); assertEquals("bold", action.getValues().get(1)); assertTrue(action.hasTextAt(2)); assertEquals("text", action.getValues().get(2)); assertEquals(3, action.getElements().size()); Element el = action.getElements().get(0); assertTrue(el instanceof Image); assertEquals("http://a/b.gif", ((Image) el).getUrl()); assertNull(action.getElements().get(1)); assertNull(action.getElements().get(2)); }
/** * 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 testDocumentModifyParametersForClearAnnotation() { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); blip.all().clearAnnotation("key"); List<OperationRequest> ops = blip.getOperationQueue().getPendingOperations(); DocumentModifyAction action = (DocumentModifyAction) ops.get(ops.size() - 1).getParameter( ParamsProperty.MODIFY_ACTION); assertEquals("key", action.getAnnotationKey()); }
public void testInsertAfter() throws Exception { String toInsert = "insertedText"; OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.INSERT_AFTER, Collections.singletonList(toInsert), NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.INDEX, CONTENT_START_TEXT)); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("Content should be insterted after the first character", INITIAL_CONTENT.charAt(0) + toInsert + INITIAL_CONTENT.substring(1), after); }
public void testDocumentModifyParametersForAnnotate() { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); blip.all().replace("foo foo foo"); blip.all("foo").annotate("key", "value1", "value2", "value3"); List<OperationRequest> ops = blip.getOperationQueue().getPendingOperations(); DocumentModifyAction action = (DocumentModifyAction) ops.get(ops.size() - 1).getParameter( ParamsProperty.MODIFY_ACTION); assertEquals("key", action.getAnnotationKey()); assertEquals(Arrays.asList("value1", "value2", "value3"), action.getValues()); }
@SuppressWarnings("unchecked") public void testDocumentModifyParametersForUpdateElement() { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); Map<String, String> newProperties = new HashMap<String, String>(); newProperties.put("url", "http://www.google.com/gadget.xml"); blip.first(ElementType.GADGET).updateElement(newProperties); List<OperationRequest> ops = blip.getOperationQueue().getPendingOperations(); DocumentModifyAction action = (DocumentModifyAction) ops.get(0).getParameter( ParamsProperty.MODIFY_ACTION); assertEquals("http://www.google.com/gadget.xml", action.getElement(0).getProperty("url")); }
public void testInsertAfter() throws Exception { String toInsert = "insertedText"; OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.INSERT_AFTER, Collections.singletonList(toInsert), NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.INDEX, CONTENT_START_TEXT)); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("Content should be insterted after the first character", INITIAL_CONTENT.charAt(0) + toInsert + INITIAL_CONTENT.substring(1), after); }
public void testDelete() throws Exception { OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.DELETE, NO_VALUES, NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.INDEX, CONTENT_START_TEXT)); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("First character should be deleted", INITIAL_CONTENT.substring(1), after); }
public void testDelete() throws Exception { OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.DELETE, NO_VALUES, NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.INDEX, CONTENT_START_TEXT)); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("First character should be deleted", INITIAL_CONTENT.substring(1), after); }
public void testReplace() throws Exception { String replacement = "replacedText"; OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.REPLACE, Collections.singletonList(replacement), NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.RANGE, new Range(CONTENT_START_TEXT, CONTENT_START_TEXT + INITIAL_CONTENT.length()))); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("The entire text should be replaced", replacement, after); }
public void testReplace() throws Exception { String replacement = "replacedText"; OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.REPLACE, Collections.singletonList(replacement), NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.RANGE, new Range(CONTENT_START_TEXT, CONTENT_START_TEXT + INITIAL_CONTENT.length()))); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("The entire text should be replaced", replacement, after); }