public static TraceNode decode(String str) {
        if (str == null || str.isEmpty()) {
            return new TraceNode();
        }
        TraceNode proxy = new TraceNode();
        TraceNode node = proxy;
        StringBuilder note = null;
        boolean inEscape = false;
        for (int i = 0, len = str.length(); i < len; ++i) {
            char c = str.charAt(i);
            if (note != null) {
                if (inEscape) {
                    note.append(c);
                    inEscape = false;
                } else if (c == '\\') {
                    inEscape = true;
                } else if (c == ']') {
                    node.addChild(note.toString());
                    note = null;
                } else {
                    note.append(c);
                }
            } else {
                if (c == '[') {
                    note = new StringBuilder();
                } else if (c == '(' || c == '{') {
                    node.addChild(new TraceNode());
                    node = node.getChild(node.getNumChildren() - 1);
                    node.setStrict(c == '(');
                } else if (c == ')' || c == '}') {
                    if (node == null) {
                        log.log(Level.WARNING, "Unexpected closing brace in trace '" + str + "' at position " + i + ".");
                        return new TraceNode();
                    }
                    if (node.isStrict() != (c == ')')) {
                        log.log(Level.WARNING, "Mismatched closing brace in trace '" + str + "' at position " + i + ".");
                        return new TraceNode();
                    }
                    node = node.parent;
                }
            }
        }
        if (note != null) {
            log.log(Level.WARNING, "Unterminated note in trace '" + str + "'.");
            return new TraceNode();
        }
        if (node != proxy) {
            log.log(Level.WARNING, "Missing closing brace in trace '" + str + "'.");
            return new TraceNode();
        }
        if (proxy.getNumChildren() == 0) {
            log.log(Level.WARNING, "No nodes found in trace '" + str + "'.");
            return new TraceNode();
        }
        if (proxy.getNumChildren() != 1) {
            return proxy; // best-effort recovery from malformed input
        }
        TraceNode ret = proxy.children.remove(0);
        ret.parent = null;
        return ret;
    }
Build a trace tree from the given string representation (possibly encoded using {@link #encode()}).

@param str The string to parse.
@return The corresponding trace tree, or an empty node if parsing failed.
messagebus/src/main/java/com/yahoo/messagebus/TraceNode.java