/** * Inserts computed contents at the matched positions. * * @param functions the functions to compute the new contents based on the * matched contents. * @return an instance of this blip references, for chaining. */ public BlipContentRefs insert(BlipContentFunction... functions) { return insert(null, functions); }
/** * Appends the given argument (element, text, or markup) to the blip. * * @param argument the element, text, or markup to be appended. * @return an instance of {@link BlipContentRefs}. */ public BlipContentRefs append(BlipContent argument) { return BlipContentRefs.all(this).insertAfter(argument); }
/** * Replaces the matched positions with computed contents. * * @param functions the functions to compute the new contents. * @return an instance of this blip references, for chaining. */ public BlipContentRefs replace(BlipContentFunction... functions) { return replace(null, functions); }
public void testDocumentOperations() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); List<BlipContent> newLines = blip.all("\n").values(); assertEquals(2, newLines.size()); assertEquals(Arrays.asList(Plaintext.of("\n"), Plaintext.of("\n")), newLines); blip.first("world").replace("jupiter"); String[] bits = blip.getContent().split("\n"); assertEquals(3, bits.length); assertEquals("hello jupiter!", bits[1]); blip.range(2, 5).delete(); assertTrue(blip.getContent().startsWith("\nho jupiter!")); blip.first("ho").insertAfter("la"); assertTrue(blip.getContent().startsWith("\nhola jupiter!")); blip.at(3).insert(" "); assertTrue(blip.getContent().startsWith("\nho la jupiter!")); blip.all().delete(); blip.at(1).insert("world!"); blip.first("world").insert(new BlipContentFunction() { @Override public BlipContent call(BlipContent source) { return Plaintext.of("Hello " + source.getText().length() + " "); } }); assertEquals("\nHello 5 world!", blip.getContent()); }
/** * Creates a new shadow blip. * * Note that this method will not know the id of the new blip since the * server will generate it. * * @param blip the blip to create a new shadow blip for. */ private void createShadowBlip(Blip blip) { Blip newBlip = blip.continueThread(); newBlip.all().replace(blip.getContent()); newBlip.all().annotate(ECHOEY_ANNOTATION, blip.getBlipId()); }
public void testDocumentModify() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); blip.all().replace("a text with text and then some text"); assertEquals("\na text with text and then some text", blip.getContent()); blip.at(8).insert("text "); blip.all("text").replace("foo", "bar"); assertEquals("\na foo bar with foo and then some bar", blip.getContent()); }
@Override public void renderLogin(String userRecordKey, Wavelet wavelet) { // Clear login form. wavelet.getRootBlip().all().delete(); PersistenceManager pm = SingletonPersistenceManagerFactory.get().getPersistenceManager(); OAuthUser userProfile = null; try { userProfile = pm.getObjectById(OAuthUser.class, userRecordKey); } catch (JDOObjectNotFoundException objectNotFound) { LOG.severe("Error fetching object from datastore with key: " + userRecordKey); } finally { pm.close(); } String url = userProfile.getAuthUrl(); // Add authentication prompt and insert link to service provider log-in page // to wavelet. wavelet.getRootBlip().all().delete(); StringBuilder b = new StringBuilder(); b.append("\n"); int startIndex = b.length(); b.append(LOGIN_LINK_TEXT + "\n\n"); wavelet.getRootBlip().append(b.toString()); // Add button to click when authentication is complete. wavelet.getRootBlip().append(new FormElement(ElementType.BUTTON, LOGIN_BUTTON_ID, LOGIN_BUTTON_CAPTION)); // Linkify the authorization link. wavelet.getRootBlip().range(startIndex, startIndex + LOGIN_LINK_TEXT.length()).annotate( LINK_ANNOTATION_KEY, url); }
/** * Copies the content of the source blip into the target blip. * * @param fromBlip the source blip. * @param toBlip the target blip. */ public static void copyBlipContents(Blip fromBlip, Blip toBlip) { for (BlipContent blipContent: fromBlip.all().values()) { toBlip.append(blipContent); } for (Annotation annotation : fromBlip.getAnnotations()) { Range range = annotation.getRange(); toBlip.range(range.getStart() + 1, range.getEnd() + 1).annotate(annotation.getName(), annotation.getValue()); } }
@Override public void renderLogin(String userRecordKey, Wavelet wavelet) { // Clear login form. wavelet.getRootBlip().all().delete(); // TODO (elizabethford): Eventually have buildUrl from within gadget with gadget fetching // request key from datastore. // Add the gadget. String gadgetString = ""; try { String gadgetUrl = "http://" + remoteHost + GADGET_PATH; gadgetString = gadgetUrl + "?" + URLEncoder.encode("key", CHARACTER_ENCODING) + "=" + URLEncoder.encode(userRecordKey, CHARACTER_ENCODING); } catch (UnsupportedEncodingException e) { LOG.warning(e.toString()); } Gadget gadget = new Gadget(gadgetString); LOG.info(gadgetString); wavelet.getRootBlip().append(gadget); } }
blip.range(5, 8).annotate(key, "bold"); assertEquals(1, blip.getAnnotations().size()); assertEquals(new Annotation(key, "bold", 3, 8), blip.getAnnotations().get(key).get(0)); blip.range(4, 12).annotate(key, "italic"); assertEquals(1, blip.getAnnotations().size()); assertEquals(2, blip.getAnnotations().get(key).size()); blip.range(6, 7).clearAnnotation(key); assertEquals(3, blip.getAnnotations().get(key).size()); assertEquals(new Annotation(key, "bold", 3, 4), blip.getAnnotations().get(key).get(0)); blip.range(3, 5).annotate("foo", "bar"); assertEquals(2, blip.getAnnotations().namesSet().size()); assertEquals(3, blip.getAnnotations().get(key).size()); assertEquals(1, blip.getAnnotations().get("foo").size()); blip.range(3, 5).clearAnnotation("foo"); blip.all().clearAnnotation(key); assertNull(blip.getAnnotations().get(key));
public void testUpdateElement() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); String url = "http://www.test.com/image.png"; blip.append(new Image(url, 100, 100, "Cool pix.")); assertEquals(2, blip.getElements().size()); // Update the image by appending a query param to the URL. blip.first(ElementType.IMAGE).updateElement(new MapFunction() { @Override public Map<String, String> call(BlipContent source) { Image matchedImage = (Image) source; Map<String, String> properties = new HashMap<String, String>(); properties.put("url", matchedImage.getUrl() + "?version=newversion"); return properties; } }); Image image = (Image) blip.first(ElementType.IMAGE).value(); assertEquals(url + "?version=newversion", image.getUrl()); }
/** * Inserts computed contents just after the matched positions. * * @param functions the functions to compute the new contents based on the * matched contents. * @return an instance of this blip references, for chaining. */ public BlipContentRefs insertAfter(BlipContentFunction... functions) { return insertAfter(null, functions); }
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()); }
@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 testIteration() throws Exception { Blip blip = newBlip("\naaa 012 aaa 345 aaa 322", Collections.<Annotation>emptyList()); Range[] expectedRanges = {new Range(1, 4), new Range(9, 12), new Range(17, 20)}; BlipContentRefs blipRefs = blip.all("aaa"); int index = 0; for (Range range : blipRefs) { assertEquals(expectedRanges[index++], range); } assertEquals(3, index); // Now let's make sure that we can iterate again. index = 0; for (Range range : blipRefs) { assertEquals(expectedRanges[index++], range); } assertEquals(3, index); // Assert iteration with blip refs that has no match. assertFalse(blip.all("invalid").iterator().hasNext()); }
public void testDocumentOperations() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); List<BlipContent> newLines = blip.all("\n").values(); assertEquals(2, newLines.size()); assertEquals(Arrays.asList(Plaintext.of("\n"), Plaintext.of("\n")), newLines); blip.first("world").replace("jupiter"); String[] bits = blip.getContent().split("\n"); assertEquals(3, bits.length); assertEquals("hello jupiter!", bits[1]); blip.range(2, 5).delete(); assertTrue(blip.getContent().startsWith("\nho jupiter!")); blip.first("ho").insertAfter("la"); assertTrue(blip.getContent().startsWith("\nhola jupiter!")); blip.at(3).insert(" "); assertTrue(blip.getContent().startsWith("\nho la jupiter!")); blip.all().delete(); blip.at(1).insert("world!"); blip.first("world").insert(new BlipContentFunction() { @Override public BlipContent call(BlipContent source) { return Plaintext.of("Hello " + source.getText().length() + " "); } }); assertEquals("\nHello 5 world!", blip.getContent()); }
/** * Updates an existing shadow blip. * * @param shadowBlip the blip that is shadowing. * @param blipToShadow the blip being shadowed. */ private void updateShadowBlip(Blip shadowBlip, Blip blipToShadow) { Preconditions.checkNotNull(shadowBlip, "Shadow blip can't be null"); Preconditions.checkNotNull(blipToShadow, "Blip to shadow can't be null"); shadowBlip.all().replace(blipToShadow.getContent()); for (Annotation annotation : blipToShadow.getAnnotations()) { if (annotation.getName().equals(ECHOEY_ANNOTATION)) { continue; } shadowBlip.range(annotation.getRange().getStart(), annotation.getRange().getEnd()).annotate( annotation.getName(), annotation.getValue()); } shadowBlip.all().annotate(ECHOEY_ANNOTATION, blipToShadow.getBlipId()); shadowBlipMap.put(blipToShadow.getBlipId(), shadowBlip.getBlipId()); }
public void testDocumentModify() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); blip.all().replace("a text with text and then some text"); assertEquals("\na text with text and then some text", blip.getContent()); blip.at(8).insert("text "); blip.all("text").replace("foo", "bar"); assertEquals("\na foo bar with foo and then some bar", blip.getContent()); }
public void testDeleteRangeThatSpansAcrossAnnotationEndPoint() throws Exception { Blip blip = newBlip("\nFoo bar.", Arrays.asList(new Annotation(ANNOTATION_KEY, "bold", 1, 3))); // Delete "oo" blip.range(2, 4).delete(); assertEquals("\nF bar.", blip.getContent()); assertEquals(new Range(1, 2), blip.getAnnotations().get(ANNOTATION_KEY).get(0).getRange()); }
blip.range(5, 8).annotate(key, "bold"); assertEquals(1, blip.getAnnotations().size()); assertEquals(new Annotation(key, "bold", 3, 8), blip.getAnnotations().get(key).get(0)); blip.range(4, 12).annotate(key, "italic"); assertEquals(1, blip.getAnnotations().size()); assertEquals(2, blip.getAnnotations().get(key).size()); blip.range(6, 7).clearAnnotation(key); assertEquals(3, blip.getAnnotations().get(key).size()); assertEquals(new Annotation(key, "bold", 3, 4), blip.getAnnotations().get(key).get(0)); blip.range(3, 5).annotate("foo", "bar"); assertEquals(2, blip.getAnnotations().namesSet().size()); assertEquals(3, blip.getAnnotations().get(key).size()); assertEquals(1, blip.getAnnotations().get("foo").size()); blip.range(3, 5).clearAnnotation("foo"); blip.all().clearAnnotation(key); assertNull(blip.getAnnotations().get(key));