/** * Returns the formatted name for the node. * * @param node Node to format. * @param format Desired format (opening tag, closing tag, empty tag, or both). * @return Formatted name. */ public static String formatNodeName(Node node, TagFormat format) { StringBuilder sb = new StringBuilder((format == TagFormat.CLOSING ? "</" : "<") + node.getNodeName()); if (format != TagFormat.CLOSING) { sb.append(formatAttributes(node)); } sb.append(format == TagFormat.EMPTY ? " />" : ">"); if (format == TagFormat.BOTH) { sb.append(formatNodeName(node, TagFormat.CLOSING)); } return sb.toString(); }
@Override public Treenode render(Node node) { Treenode tnode = new Treenode(); if (node.getNodeType() == Node.TEXT_NODE) { setLabel(tnode, node.getNodeValue(), XMLConstants.STYLE_CONTENT); } else if (node.getNodeType() == Node.COMMENT_NODE) { setLabel(tnode, "<!--" + node.getNodeValue() + "-->", XMLConstants.STYLE_COMMENT); } else if (node.getParentNode() == null) { // Closing tag tnode.setAttribute("closing", true); setLabel(tnode, XMLUtil.formatNodeName(node, TagFormat.CLOSING), XMLConstants.STYLE_TAG); } else { boolean leaf = !node.hasChildNodes(); String label = "<" + node.getNodeName(); NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); label += " " + attr.getNodeName() + "='" + attr.getNodeValue() + "'"; } } setLabel(tnode, label + (leaf ? " />" : ">"), XMLConstants.STYLE_TAG); tnode.addEventListener("toggle", nodeListener); } return tnode; }