@Override public void setElementAttribute(E element, String name, String value) { String currentValue = getAttribute(element, name); if ((value == null && currentValue == null) || (value != null && value.equals(currentValue))) { // Redundant. Do nothing, no operation. return; } try { begin(); consume(setAttribute(name, value, at(Locator.before(doc, element)))); } finally { end(); } }
/** * Deletes a content element. Executes the delete in this document, and * places the caret in the deleted element's spot. Finally triggers * a single operation event matching the executed delete. * * @param element the element to delete */ public void deleteNode(E element) { try { begin(); consume(deleteElement(element, at(Locator.before(doc, element)))); } finally { end(); } }
/** * Deletes all children of a content element. Executes the delete in * this document, and places the caret in the emptied element. Finally * triggers a single operation event matching the executed delete. * * @param element the element to delete */ public void emptyElement(E element) { try { begin(); // Construct and apply delete operation consume(emptyElement(element, at(Locator.start(doc, element)))); } finally { end(); } }
@Override public void insertText(int location, String text) { Preconditions.checkPositionIndex(location, size()); // TODO(danilatos): Get the schema constraints from the document // and use the corresponding permitted characters from there. // text = getPermittedCharactersForPoint(point).convertString(text); try { begin(); consume(insertText(text, at(location))); } finally { end(); } }
@Override public void setElementAttributes(E element, Attributes attrs) { Preconditions.checkArgument(element != getDocumentElement(), "Cannot touch root element"); try { begin(); consume(setAttributes(attrs, at(Locator.before(doc, element)))); } finally { end(); } }
@Override public void updateElementAttributes(E element, Map<String, String> attrs) { Preconditions.checkArgument(element != getDocumentElement(), "Cannot touch root element"); try { begin(); consume(updateAttributes(attrs, at(Locator.before(doc, element)))); } finally { end(); } }
@Override public E insertXml(Point<N> point, XmlStringBuilder xml) { Point.checkPoint(this, point, "MutableDocumentImpl.insertXml"); try { begin(); int where = doc.getLocation(point); Builder builder = at(where); appendXmlToBuilder(xml, builder); consume(builder); return Point.elementAfter(this, doc.locate(where)); } finally { end(); } }
@Override public E createElement(Point<N> point, String tagName, Map<String, String> attributes) { // TODO(danilatos): Validate point is in document. indexed doc should throw an exception // when calling getLocation anyway. Preconditions.checkNotNull(tagName, "createElement: tagName must not be null"); Point.checkPoint(this, point, "MutableDocumentImpl.createElement"); try { begin(); int location = doc.getLocation(point); consume(createElement(tagName, new AttributesImpl(attributes), at(location))); Point<N> result = doc.locate(location); return doc.asElement(result.isInTextNode() ? doc.getNextSibling(result.getContainer()) : result.getNodeAfter()); } finally { end(); } }
private Builder deleteRangeInternal(int startLocation, int endLocation) { Builder builder = at(startLocation); Point<N> start = doc.locate(startLocation); Point<N> end = doc.locate(endLocation);