public void testDeleteString() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "helllo"); api.delete(3, 4); assertEquals("\nhello", api.apiContents()); assertInSync(document, api); api.delete(0, 2); assertEquals("ello", api.apiContents()); assertInSync(document, api); }
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); }
public static Map<Integer, Element> serialize(Document doc, Wavelet wavelet) { Map<Integer, Element> result = Maps.newHashMap(); ApiView apiView = new ApiView(doc, wavelet); Doc.N node = Blips.getBody(doc); if (node != null) { // The node is the body; we're after its children node = doc.getFirstChild(node); } while (node != null) { E element = doc.asElement(node); if (element != null) { Element apiElement = xmlToApiElement(doc, element, wavelet); if (apiElement != null) { result.put(apiView.transformToTextOffset(doc.getLocation(element)), apiElement); } } node = doc.getNextSibling(node); } return result; }
public void testStringAppend() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "world"); assertEquals("\nworld", api.apiContents()); assertInSync(document, api); api.insert(1, "hello "); assertEquals("\nhello world", api.apiContents()); assertInSync(document, api); }
/** * Assert that the api view and document are still in sync. */ private void assertInSync(Document document, ApiView api) { ApiView alt = new ApiView(document, mock(Wavelet.class)); assertEquals(alt.apiContents(), api.apiContents()); List<ElementInfo> apiElements = api.getElements(); List<ElementInfo> altElements = alt.getElements(); assertEquals(altElements.size(), apiElements.size()); for (int i = 0; i < apiElements.size(); i++) { ElementInfo altInfo = altElements.get(i); ElementInfo apiInfo = apiElements.get(i); assertEquals(altInfo.element.getType(), apiInfo.element.getType()); assertEquals(altInfo.apiPosition, apiInfo.apiPosition); assertEquals(altInfo.xmlPosition, apiInfo.xmlPosition); } } }
public void testLocateElement() { Document document = BasicFactories.documentProvider().parse( LineContainers.debugContainerWrap("01234567890123456789")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(3, new FormElement(ElementType.BUTTON, "buttonName")); FormElement button1 = new FormElement(ElementType.BUTTON, "buttonName"); assertEquals(3, api.locateElement(button1)); FormElement button2 = new FormElement(ElementType.BUTTON, "notInDocument"); assertEquals(-1, api.locateElement(button2)); api.insert(4, new Gadget("http://test.com")); Gadget gadget1 = new Gadget("http://test.com"); assertEquals(4, api.locateElement(gadget1)); Gadget gadget2 = new Gadget("http://test.com/something"); assertEquals(-1, api.locateElement(gadget2)); assertInSync(document, api); Element inlineBlip = new Element(ElementType.INLINE_BLIP); inlineBlip.setProperty("id", "b+1234"); api.insert(5, inlineBlip); assertEquals(5, api.locateElement(inlineBlip)); }
ApiView view = new ApiView(parentBlip.getContent(), wavelet); int elementApiLocation = view.locateElement(element); int xmlLocation = view.transformToXmlOffset(elementApiLocation + 1);
ApiView view = new ApiView(parentBlip.getContent(), wavelet); int xmlLocation = view.transformToXmlOffset(index);
@Override public Range next() { if (hitsLeft == 0) { return null; } hitsLeft--; String searchIn = apiView.apiContents(); int next = searchIn.indexOf(searchFor, from + 1); if (next == -1) { return null; } from = next; return new Range(from, from + searchFor.length()); }
view.insert(insertAt, toInsert); int annotationStart = view.transformToXmlOffset(insertAt); int annotationEnd = view.transformToXmlOffset(insertAt + toInsert.length());
private static ApiView createApiViewFromXml(String xml) { IndexedDocument<Node, Element, Text> indexedDoc = DocProviders.POJO.parse(LineContainers.debugContainerWrap(xml)); Document doc = new DocumentImpl(createSequencer(indexedDoc), indexedDoc); return new ApiView(doc, mock(Wavelet.class)); }
@Override public Range next() { if (hitsLeft == 0) { return null; } hitsLeft--; for (ApiView.ElementInfo elementInfo : apiView.getElements()) { if (elementInfo.element.getType().equals(elementType) && elementInfo.apiPosition > index) { boolean allMatched = true; for (Entry<String, String> entry : restrictions.entrySet()) { if (!entry.getValue().equals(elementInfo.element.getProperty(entry.getKey()))) { allMatched = false; break; } } if (!allMatched) { continue; } index = elementInfo.apiPosition; return new Range(index, index + 1); } } return null; }
@Override public BlipData toBlipData(ConversationBlip blip, Wavelet wavelet, EventMessageBundle eventMessageBundle) { ConversationBlip parentBlip = findBlipParent(blip); BlipData blipData = new BlipData(); blipData.setCreator(blip.getAuthorId().getAddress()); blipData.setContributors(idsToParticipantIdList(blip.getContributorIds())); blipData.setBlipId(blip.getId()); blipData.setLastModifiedTime(blip.getLastModifiedTime()); blipData.setVersion(blip.getLastModifiedVersion()); blipData.setParentBlipId(parentBlip == null ? null : parentBlip.getId()); blipData.setWaveId(ApiIdSerializer.instance().serialiseWaveId(wavelet.getWaveId())); blipData.setWaveletId(ApiIdSerializer.instance().serialiseWaveletId(wavelet.getId())); blipData.setChildBlipIds(toBlipIdList(findBlipChildren(blip))); ApiView apiView = new ApiView(blip.getContent(), wavelet); // Set content. blipData.setContent(apiView.apiContents()); // Set Annotations. blipData.setAnnotations(extractAnnotations(blip.getContent(), apiView)); // blip.getContent().rangedAnnotations(0, blip.getContent().size(), null), // Set Form Elements. blipData.setElements(ElementSerializer.serialize(blip.getContent(), wavelet)); return blipData; }
/** * Deletes ranges of elements from a document as specified by the iterator. * * @param operation the operation to execute. * @param view the view of the document. * @param hitIterator iterates over the ranges of elements to delete. * @throws InvalidRequestException if the specified range was invalid. */ private void delete(OperationRequest operation, ApiView view, DocumentHitIterator hitIterator) throws InvalidRequestException { Range range = hitIterator.next(); while (range != null) { int start = range.getStart(); int end = range.getEnd(); if (start == 0) { // Can't delete the first new line. start = 1; } if (start >= end) { throw new InvalidRequestException( "Invalid range specified, " + start + ">=" + end, operation); } // Delete using the view. view.delete(start, end); // Shift the iterator to match the updated document. hitIterator.shift(start, end - start); 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(); } }
/** * Pretend we're shifting in the manner of INSERT_AFTER */ public void testTextMatcherShiftInsertAfter() { ApiView apiView = createApiViewFromXml("1 1 1"); DocumentHitIterator it = new DocumentHitIterator.TextMatcher(apiView, "1", -1); for (int i = 0; i < 3; i++) { Range r = it.next(); assertEquals(i * 3 + 1, r.getStart()); apiView.insert(r.getEnd(), "1"); it.shift(r.getEnd(), 1); } assertNull(it.next()); }
/** * Extracts all annotations that span inside the body tag of the given * document. * * @param doc the document to get the annotations from. * @param apiView provides a utility function to convert an xml offset point * into text offset. * @return the annotations represented as a list of {@link Annotation}. */ private static List<Annotation> extractAnnotations(Document doc, ApiView apiView) { List<Annotation> result = Lists.newArrayList(); for (RangedAnnotation<String> annotation : doc.rangedAnnotations(0, doc.size(), null)) { if (annotation.key() != null && annotation.value() != null) { int start = apiView.transformToTextOffset(annotation.start()); int end = apiView.transformToTextOffset(annotation.end()); result.add(new Annotation(annotation.key(), annotation.value(), new Range(start, end))); } } return result; } }
/** * Legacy support method. Return the index of the element that looks like the * one we passed for some value of looks like. */ public int locateElement(Element element) { int index = 0; for (Bit bit : bits) { if (bit.element != null && bit.element.getType().equals(element.getType())) { if (element.getType().equals(ElementType.GADGET)) { if (propertyMatch(bit.element, element, Gadget.URL)) { return index; } } else if (element.getType().equals(ElementType.LABEL)) { if (propertyMatch(bit.element, element, "for")) { return index; } } else if (elementMatch(element, bit.element)) { return index; } } index += bit.size(); } return -1; }
public void testStringAppend() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "world"); assertEquals("\nworld", api.apiContents()); assertInSync(document, api); api.insert(1, "hello "); assertEquals("\nhello world", api.apiContents()); assertInSync(document, api); }