@Override public E asElement(N node) { return inner.asElement(node); }
@Override public Object value(ReadableDocument<Object, Object, Object> doc, Object node) { Object ret = doc.asElement(super.value(doc, node)); assert ret != null; return ret; } }
/** * @return true if the node is an element with the given tag name */ public static <N, E extends N> boolean isMatchingElement( final ReadableDocument<N, E, ?> doc, N node, String tagName) { E el = doc.asElement(node); return el != null && doc.getTagName(el).equals(tagName); }
/** * Same as {@link #enclosingElement(ReadableDocument, Point)}, except * does it for a node. So if it is an element, return it, if it is * a text node, return its parent. */ public static <N, E extends N, T extends N> E enclosingElement( ReadableDocument<N,E,T> doc, N node) { E el = doc.asElement(node); return el == null ? doc.getParentElement(node) : el; }
/** * Returns a node as an element if it is one; otherwise, finds the next * sibling of that node that is an element. * * @param doc document accessor * @param node reference node * @return the next element in the inclusive sibling chain from {@code node}. */ public static <N, E extends N> E getNextElementInclusive(ReadableDocument<N, E, ?> doc, N node, boolean forward) { E asElement = doc.asElement(node); while (node != null && asElement == null) { node = forward ? doc.getNextSibling(node) : doc.getPreviousSibling(node); asElement = doc.asElement(node); } return asElement; }
/** * @param doc * @param node * @return true if node prefers to be output inline */ private <E extends N, T extends N> boolean isInline( ReadableDocument<N, E, T> doc, N node) { E element = doc.asElement(node); T text = doc.asText(node); return text != null || "|b|u|i|".contains("|" + doc.getTagName(element).toLowerCase() + "|"); }
/** * Construct a point at the start of either an element or text node. * @param doc Document containing the node * @param node The node to get the start of * @return The Point.El start if node is an element, otherwise the position at text offset 0. */ public static <N, E extends N, T extends N> Point<N> textOrElementStart( ReadableDocument<N,E,T> doc, N node) { E elt = doc.asElement(node); return elt == null ? inText(node, 0) : start(doc, elt); // change based on type of node }
@Override public Object next(ReadableDocument<Object, Object, Object> doc, Object current, Object stopAt) { Object next = super.next(doc, current, stopAt); while (next != null && doc.asElement(next) == null) { next = super.next(doc, next, stopAt); } return next; }
@Override public Object next(ReadableDocument<Object, Object, Object> doc, Object current, Object stopAt) { Object next = super.next(doc, current, stopAt); while (next != null && !doc.getTagName(doc.asElement(next)).equals(tagName)) { next = super.next(doc, next, stopAt); } return next; } }
/** * @param doc * @param node * @return true if the node is a line container element * * NOTE(danilatos): In the future, the match may involve more than * just a tag name check. Other element types, such as table cells, * might be line containers. */ public static <N, E extends N> boolean isLineContainer( final ReadableDocument<N, E, ?> doc, N node) { E el = doc.asElement(node); if (el != null) { return isLineContainerTagname(doc.getTagName(el)); } else { return false; } }
/** * Construct a point at the end of either an element or text node. * @param doc Document containing the node * @param node The node to get the start of * @return The Point.El end if node is an element, otherwise the position at the last text offset. */ public static <N, E extends N, T extends N> Point<N> textOrElementEnd( ReadableDocument<N,E,T> doc, N node) { E elt = doc.asElement(node); return elt == null ? inText(node, doc.getData(doc.asText(node)).length()) : end(node); }
private void getInnerTextOfNode(N node, StringBuilder builder) { T maybeText = document.asText(node); if (maybeText != null) { getInnerTextOfText(maybeText, builder); } else { getInnerTextOfElement(document.asElement(node), builder); } }
/** * @param doc * @param point * @return Node before the given point */ public static <N, E extends N, T extends N> E elementBefore( ReadableDocument<N,E,T> doc, Point<N> point) { if (point.isInTextNode()) { if (point.getTextOffset() > 0) { return null; } else { return doc.asElement(doc.getPreviousSibling(point.getContainer())); } } else { return doc.asElement(nodeBefore(doc, point.asElementPoint())); } }
/** * Writes an entire subtree worth of operations to an initialization cursor. * NOTE(patcoleman): does not include annotations. * * @param doc Document the node resides within. * @param node Root of the subtree. * @param cursor Cursor to write results out to. */ public static <N, E extends N, T extends N> void buildDomInitializationFromSubtree( ReadableDocument<N, E, T> doc, N node, DocInitializationCursor cursor) { T text = doc.asText(node); if (text == null) { buildDomInitializationFromElement(doc, doc.asElement(node), cursor, true); } else { buildDomInitializationFromTextNode(doc, text, cursor); } }
/** * @param doc * @param point * @return Node before the given point */ @SuppressWarnings("unchecked") public static <N, E extends N, T extends N> E elementAfter( ReadableDocument<N,E,T> doc, Point<N> point) { if (point.isInTextNode()) { if (point.getTextOffset() < doc.getLength((T) point.getContainer())) { return null; } else { return doc.asElement(doc.getNextSibling(point.getContainer())); } } else { return doc.asElement(point.getNodeAfter()); } }
/** * Checks whether the current walk is the correct type of element. * @throws IllegalStateException if the element does not match the expected state. * @return the checked element. */ public E checkElement(String tagName, Map<String, String> attributes) { Preconditions.checkState(nodeWalker.hasNext(), "Tree Walker: no more nodes to walk, element expected"); progress(); E element = document.asElement(currentNode); Preconditions.checkState(element != null, "Tree Walker: At text node, element expected"); Preconditions.checkState(document.getTagName(element).equals(tagName), "Tree Walker: Incorrect tag name"); Preconditions.checkState(ValueUtils.equal(document.getAttributes(element), attributes), "Tree Walker: Incorrect attributes"); return element; }
/** Performs a walk against an iterable collection of nodes, returns true if all checks pass. */ public boolean checkWalk(ReadableDocument<N, E, T> doc, Iterable<N> nodes) { try { for (N node : nodes) { E elt = doc.asElement(node); if (elt != null) { checkElement(doc.getTagName(elt), doc.getAttributes(elt)); } else { checkTextNode(doc.getData(doc.asText(node))); } } return checkComplete(); } catch (IllegalStateException e) { return false; // assertion failed. } }
/** * Useful argument checking method for validating a point with respect * to a document * * @param doc * @param point * @param msgPrefix string to prepend to exception, if thrown */ public static <N, E extends N, T extends N> void checkPoint( ReadableDocument<N, E, T> doc, Point<N> point, String msgPrefix) { if (point.isInTextNode()) { checkOffset(doc, doc.asText(point.getContainer()), point.getTextOffset(), msgPrefix); } else { checkRelationship(doc, doc.asElement(point.getContainer()), point.getNodeAfter(), msgPrefix); } }
public void testNodeAtEnd() { TestDocumentContext<Node, Element, Text> cxt = createAnnotationContext(); MutableDocument<Node, Element, Text> doc = cxt.document(); Element e = doc.createChildElement(doc.getDocumentElement(), "p", Attributes.EMPTY_MAP); doc.insertText(Point.<Node>inElement(e, null), "Hi"); doc.setAnnotation(3, 4, "x", "1"); timerService.tick(100); ReadableDocument<Node, Element, Text> fullDoc = cxt.getFullRawDoc(); Element boundary = fullDoc.asElement(fullDoc.getLastChild(e)); assertNotNull(boundary); assertEquals("l:b", boundary.getTagName()); doc.setAnnotation(3, 4, "x", null); timerService.tick(100); boundary = fullDoc.asElement(fullDoc.getLastChild(e)); assertNull(boundary); }
public void testNodeAtEnd() { TestDocumentContext<Node, Element, Text> cxt = createAnnotationContext(); MutableDocument<Node, Element, Text> doc = cxt.document(); Element e = doc.createChildElement(doc.getDocumentElement(), "p", Attributes.EMPTY_MAP); doc.insertText(Point.<Node>inElement(e, null), "Hi"); doc.setAnnotation(3, 4, "x", "1"); timerService.tick(100); ReadableDocument<Node, Element, Text> fullDoc = cxt.getFullRawDoc(); Element boundary = fullDoc.asElement(fullDoc.getLastChild(e)); assertNotNull(boundary); assertEquals("l:b", boundary.getTagName()); doc.setAnnotation(3, 4, "x", null); timerService.tick(100); boundary = fullDoc.asElement(fullDoc.getLastChild(e)); assertNull(boundary); }