@Override public Map<String, String> getAttributes(E element) { return inner.getAttributes(element); }
/** * Worker to get the attributes from the element as a string * @param element * @return string of attributes, e.g., " name1='value1' name1='value2'" */ private String getAttributesString(E element) { // TODO(danilatos): Investigate if it's worth optimising for ContentElement // StringMap attributes Map<String, String> attributes = view.getAttributes(element); String soFar = ""; for (String key : attributes.keySet()) { soFar += " " + key + "=\"" + attrEscape(attributes.get(key)) + "\""; } return soFar; }
/** * Appends element's attributes * * @param doc * @param element */ private <E extends N, T extends N> void appendAttributes( ReadableDocument<N, E, T> doc, E element) { try { Map<String, String> attributes = doc.getAttributes(element); for (Map.Entry<String, String> attribute : attributes.entrySet()) { append(" " + attribute.getKey() + "='" + attribute.getValue() + "'"); } } catch (Exception e) { // TODO(user): remove this when + if HtmlViewImpl implements getAttributes for (String name : new String[] {"class", "src", "id", "type", "name", "for", "href", "target"}) { String value = doc.getAttribute(element, name); if (value != null && value.length() > 0) { append(" " + name + "='" + value + "'"); } } } }
/** * 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. } }
/** * Writes an element's information out to an initialization cursor, optionally recursing * to do likewise for its children. * * @param doc Document the node resides within. * @param element Element containing information to be written. * @param cursor Cursor to write results out to. * @param recurse Whether or not to write children to the operation. */ public static <N, E extends N, T extends N> void buildDomInitializationFromElement( ReadableDocument<N, E, T> doc, E element, DocInitializationCursor cursor, boolean recurse) { cursor.elementStart(doc.getTagName(element), new AttributesImpl(doc.getAttributes(element))); if (recurse) { for (N child = doc.getFirstChild(element); child != null; child = doc.getNextSibling(child)) { buildDomInitializationFromSubtree(doc, child, cursor); } } cursor.elementEnd(); } }