private void createEntry(WaveletId waveletId) { String waveletIdStr = WaveletBasedConversation.idFor(waveletId); ObservableMutableDocument<? super E, E, ?> doc = getDocument(); E container = doc.createChildElement(doc.getDocumentElement(), WaveletBasedSupplement.CONVERSATION_TAG, new AttributesImpl(WaveletBasedSupplement.ID_ATTR, waveletIdStr)); }
private <N, F extends N> F createBefore(ObservableMutableDocument<N, F, ?> doc, F element, Map<String, String> attributes) { Point<N> where = Point.before(doc, element); return doc.createElement(where, childTag, attributes); }
/** * Deletes the container element. */ void delete() { doc.deleteNode(container); } }
@Override public void onElementAdded(E newElement) { ObservableMutableDocument<? super E, E, ?> document = getDocument(); assert container.equals(document.getParentElement(newElement)); if (!tag.equals(document.getTagName(newElement))) { return; } // Possibly changing an existing value? if (valueElement != null) { if (document.getLocation(newElement) < document.getLocation(valueElement)) { // New element loses. redundantElements.add(newElement); } else { // New element wins. redundantElements.add(valueElement); changeValue(newElement); } } else { // New element is the new value. changeValue(newElement); } }
@Override public void onElementAdded(E element) { assert getDocument().getParentElement(element).equals(parent) : "Received event for unrelated element"; if (childTag.equals(getDocument().getTagName(element))) { T child = factory.adapt(router, element); T sibling = getPreviousKnownValue(element); orderedValues.add(sibling == null ? 0 : orderedValues.indexOf(sibling) + 1, child); elementToValue.put(element, child); valueToElement.put(child, element); fireElementAdded(child); } }
/** * Populates a document with an initial map state defined by an entry list. * * @return a map-context view of the document state. */ private static <N, E extends N> ValueContext<N, E> substrate( ObservableMutableDocument<N, E, ?> doc, int value) { // Insert container element E container = doc.createChildElement(doc.getDocumentElement(), CONTAINER_TAG, Collections.<String,String>emptyMap()); doc.setElementAttribute(container, VALUE_ATTR, Serializer.INTEGER.toString(value)); return new ValueContext<N, E>(doc, container); }
void removeEntries(K key) { N curChild = context.doc.getFirstChild(context.container); String keyString = keySerializer.toString(key); E e = DocHelper.getFirstChildElement(context.doc, context.container); while (e != null) { if (ENTRY_TAG.equals(context.doc.getTagName(e)) && keyString.equals(context.doc.getAttribute(e, KEY_ATTR))) { context.doc.deleteNode(e); } e = DocHelper.getNextSiblingElement(context.doc, e); } }
/** * Asserts that the manifest content within the "conversation" tag matches an * expected string. */ private void assertManifestXml(final String expected) { manifestDoc.with(new Action() { @Override public <N, E extends N, T extends N> void exec(MutableDocument<N, E, T> doc) { XmlStringBuilder exp = XmlStringBuilder.createFromXmlString(expected); assertStructureEquivalent(exp.wrap("conversation"), doc); } }); }
@Override public <N, E extends N, T extends N> void exec(ObservableMutableDocument<N, E, T> doc) { E container = doc.getDocumentElement(); E entry = DocHelper.getFirstChildElement(doc, container); // Skip over anything we don't care about while (entry != null && !ENTRY_TAG.equals(doc.getTagName(entry))) { entry = DocHelper.getNextSiblingElement(doc, entry); } for (Boolean b : state) { assertNotNull(entry); Attributes attrs = b != null ? new AttributesImpl(VALUE_ATTR, b.toString()) : Attributes.EMPTY_MAP; assertEquals(attrs, doc.getAttributes(entry)); entry = DocHelper.getNextSiblingElement(doc, entry); // Skip over anything we don't care about while (entry != null && !ENTRY_TAG.equals(doc.getTagName(entry))) { entry = DocHelper.getNextSiblingElement(doc, entry); } } assertNull("Unexpected element in subtrate: " + entry, entry); } });
/** * Convenience method for getting an attribute value, substituting an empty * string for a null attribute. * * @param document document to get from * @param element element to get from * @param attributeName name of attribute to get * * @return value of attribute, or empty string if the value is null */ private String getNonNullAttribute(ObservableMutableDocument<? super E, E, ?> document, E element, String attributeName) { String attribute = document.getAttribute(element, attributeName); return attribute == null ? "" : attribute; } }
/** * Exposes a document as a collection of per-wavelet read-state objects. * * @param router router for read-state document * @return wavelet read-state. */ private static <E> WaveletReadStateCollection<E> createWaveletReadState( DocumentEventRouter<? super E, E, ?> router, Listener listener) { E container = router.getDocument().getDocumentElement(); return WaveletReadStateCollection.create(router, container, listener); }
/** * Creates an entry element for the specified map entry. * * @param value */ private void createEntry(C value) { Map<String, String> attrs = new HashMap<String, String>(); attrs.put(valueAttrName, serializer.toString(value)); E entry = getDocument().createChildElement(container, entryTagName, attrs); }
@Override public void set(Boolean newValue) { Boolean oldValue = get(); if (equals(oldValue, newValue)) { return; } if (newValue != null) { // Add an element to reflect new value. getDocument().createChildElement(container, tag, new AttributesImpl(valueAttr, Serializer.BOOLEAN.toString(newValue))); cleanup(); } else { // Erase all elements. Cleanup first, so that removal of real element // does not promote a redundant element temporarily. cleanup(); getDocument().deleteNode(valueElement); } assert equals(newValue, get()); }
public void onElementRemoved(E element) { if (WaveletBasedSupplement.CONVERSATION_TAG.equals(getDocument().getTagName(element))) { WaveletId waveletId = valueOf(element); if (waveletId != null) { WaveletThreadState state = waveletSupplements.remove(waveletId); if (state == null) { // Not good - there was a collapsed-state element and we weren't // tracking it... // TODO(user): this is the same problem as the read state // tracker } } } }
void setValue(C value) { context.doc.setElementAttribute(context.container, VALUE_ATTR, serializer.toString(value)); }
/** * Populates a document with an initial map state defined by an entry list. * * @return a map-context view of the document state. */ private static <N, E extends N> ValueContext<N, E> substrate( ObservableMutableDocument<N, E, ?> doc, int value) { // Insert container element E container = doc.createChildElement(doc.getDocumentElement(), CONTAINER_TAG, Collections.<String,String>emptyMap()); doc.setElementAttribute(container, VALUE_ATTR, Serializer.INTEGER.toString(value)); return new ValueContext<N, E>(doc, container); }
void removeEntries(K key) { N curChild = context.doc.getFirstChild(context.container); String keyString = keySerializer.toString(key); E e = DocHelper.getFirstChildElement(context.doc, context.container); while (e != null) { if (ENTRY_TAG.equals(context.doc.getTagName(e)) && keyString.equals(context.doc.getAttribute(e, KEY_ATTR))) { context.doc.deleteNode(e); } e = DocHelper.getNextSiblingElement(context.doc, e); } }
/** * Asserts that the manifest content within the "conversation" tag matches an * expected string. */ private void assertManifestXml(final String expected) { manifestDoc.with(new Action() { @Override public <N, E extends N, T extends N> void exec(MutableDocument<N, E, T> doc) { XmlStringBuilder exp = XmlStringBuilder.createFromXmlString(expected); assertStructureEquivalent(exp.wrap("conversation"), doc); } }); }
@Override public <N, E extends N, T extends N> void exec(ObservableMutableDocument<N, E, T> doc) { E container = doc.getDocumentElement(); E entry = DocHelper.getFirstChildElement(doc, container); // Skip over anything we don't care about while (entry != null && !ENTRY_TAG.equals(doc.getTagName(entry))) { entry = DocHelper.getNextSiblingElement(doc, entry); } for (Boolean b : state) { assertNotNull(entry); Attributes attrs = b != null ? new AttributesImpl(VALUE_ATTR, b.toString()) : Attributes.EMPTY_MAP; assertEquals(attrs, doc.getAttributes(entry)); entry = DocHelper.getNextSiblingElement(doc, entry); // Skip over anything we don't care about while (entry != null && !ENTRY_TAG.equals(doc.getTagName(entry))) { entry = DocHelper.getNextSiblingElement(doc, entry); } } assertNull("Unexpected element in subtrate: " + entry, entry); } });
private String valueOf(E element) { return getDocument().getAttribute(element, WaveletBasedSupplement.ID_ATTR); }