public static XmlStringBuilder apiElementToXml(Element e) { ElementSerializer serializer = typeToSerializer.get(e.getType()); if (serializer == null) { return null; } return serializer.toXml(e); }
/** * @returns the api representation of the current contents */ public String apiContents() { StringBuilder res = new StringBuilder(); for (Bit bit : bits) { if (bit.string != null) { res.append(bit.string); } else { if (bit.element != null && bit.element.getType().equals(ElementType.LINE)) { res.append('\n'); } else { res.append(' '); } } } return res.toString(); }
/** * Checks whether the given {@code element} is a match or not, by checking * the type and the properties. * * @param element the element to check. * @return {@code true} if the element type matches the target type, and * the all filters/restrictions are satisfied. */ private boolean match(Element element) { if (element.getType() != target) { return false; } if (restrictions == null) { return true; } for (Entry<String, String> entry : restrictions.entrySet()) { if (!entry.getValue().equals(element.getProperty(entry.getKey()))) { return false; } } return true; } }
/** * Returns the offset of this blip if it is inline, or -1 if it's not. If the * parent is not in the offset, this method will always return -1 since it * can't determine the inline blip status. * * @return the offset of this blip if it is inline, or -1 if it's not inline * or if the parent is not in the context. * @deprecated please use {@code getThread().getLocation()} to get the offset * of the inline reply thread that contains this blip. */ @Deprecated public int getInlineBlipOffset() { Blip parent = getParentBlip(); if (parent == null) { return -1; } for (Entry<Integer, Element> entry : parent.getElements().entrySet()) { Element element = entry.getValue(); if (element.getType() == ElementType.INLINE_BLIP && blipId.equals(element.getProperty(INLINE_BLIP_ELEMENT_ID_KEY))) { return entry.getKey(); } } return -1; }
/** * 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; }
@Override public JsonElement serialize(Element src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty(TYPE_TAG, src.getType().toString()); JsonObject properties = new JsonObject(); if (src.isAttachment()) {
@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 JsonElement serialize(Element src, Type typeOfSrc, JsonSerializationContext context) { if (src.getType() == ElementType.ATTACHMENT) { src = new Element(ElementType.IMAGE, createImageProperties(src.getProperties())); } return super.serialize(src, typeOfSrc, context); }
Element element) throws InvalidRequestException { Preconditions.checkArgument(element.isGadget(), "Called with non-gadget element type %s", element.getType());
public void testElementTypeAndPropertyConstruction() { Map<String, String> properties = new HashMap<String, String>(); properties.put(NAME, VALUE); Element element = new Element(ElementType.GADGET, properties); assertEquals(ElementType.GADGET, element.getType()); assertEquals(1, element.getProperties().size()); assertEquals(VALUE, element.getProperty(NAME)); }
public void testElementTypeAndPropertyConstruction() { Map<String, String> properties = new HashMap<String, String>(); properties.put(NAME, VALUE); Element element = new Element(ElementType.GADGET, properties); assertEquals(ElementType.GADGET, element.getType()); assertEquals(1, element.getProperties().size()); assertEquals(VALUE, element.getProperty(NAME)); }
public void testElementTypeConstruction() { Element element = new Element(ElementType.BUTTON); assertEquals(ElementType.BUTTON, element.getType()); assertEquals(0, element.getProperties().size()); }
public void testElementTypeConstruction() { Element element = new Element(ElementType.BUTTON); assertEquals(ElementType.BUTTON, element.getType()); assertEquals(0, element.getProperties().size()); }
/** * 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); } } }
/** * 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 testInsertTextWithNewline() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "hello\nworld"); assertEquals("\nhello\nworld", api.apiContents()); assertInSync(document, api); List<ElementInfo> elements = api.getElements(); assertEquals(2, elements.size()); assertEquals(ElementType.LINE, elements.get(0).element.getType()); assertEquals(ElementType.LINE, elements.get(1).element.getType()); }
public void testInsertTextWithNewline() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "hello\nworld"); assertEquals("\nhello\nworld", api.apiContents()); assertInSync(document, api); List<ElementInfo> elements = api.getElements(); assertEquals(2, elements.size()); assertEquals(ElementType.LINE, elements.get(0).element.getType()); assertEquals(ElementType.LINE, elements.get(1).element.getType()); }
ElementType type = entry.getValue().getType(); Element result = null; if (FormElement.getFormElementTypes().contains(type)) {
public void testInlineBlip() throws Exception { Blip blip = newBlip("\n1234", Collections.<Annotation>emptyList()); assertEquals(-1, blip.getInlineBlipOffset()); Blip inlineBlip = blip.insertInlineBlip(3); assertTrue(blip.getChildBlipIds().contains(inlineBlip.getBlipId())); assertEquals(3, inlineBlip.getInlineBlipOffset()); assertEquals("\n12 34", blip.getContent()); assertEquals(ElementType.INLINE_BLIP, blip.getElements().get(3).getType()); }
public void testInlineBlip() throws Exception { Blip blip = newBlip("\n1234", Collections.<Annotation>emptyList()); assertEquals(-1, blip.getInlineBlipOffset()); Blip inlineBlip = blip.insertInlineBlip(3); assertTrue(blip.getChildBlipIds().contains(inlineBlip.getBlipId())); assertEquals(3, inlineBlip.getInlineBlipOffset()); assertEquals("\n12 34", blip.getContent()); assertEquals(ElementType.INLINE_BLIP, blip.getElements().get(3).getType()); }