2 * Copyright (c) 2021 PANTHEON.tech s.r.o. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.api.schema;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import java.util.Base64;
14 import java.util.Iterator;
15 import java.util.Locale;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.concepts.PrettyTree;
20 import org.opendaylight.yangtools.concepts.PrettyTreeAware;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.common.Revision;
26 public final class NormalizedNodePrettyTree extends PrettyTree implements Immutable {
27 private final @NonNull NormalizedNode node;
29 public NormalizedNodePrettyTree(final @NonNull NormalizedNode node) {
30 this.node = requireNonNull(node);
34 public void appendTo(final StringBuilder sb, final int depth) {
35 appendNode(sb, depth, null, node);
38 private static void appendNode(final StringBuilder sb, final int depth, final QNameModule parentNamespace,
39 final NormalizedNode node) {
40 final String simpleName = node.contract().getSimpleName();
41 appendIndent(sb, depth);
42 sb.append(simpleName.toLowerCase(Locale.ROOT).charAt(0)).append(simpleName, 1, simpleName.length()).append(' ');
44 final QName qname = node.name().getNodeType();
45 final QNameModule currentNamespace = qname.getModule();
46 appendNamespace(sb, parentNamespace, currentNamespace);
47 sb.append(qname.getLocalName()).append(' ');
49 if (node instanceof NormalizedNodeContainer) {
50 final NormalizedNodeContainer<?> container = (NormalizedNodeContainer<?>) node;
53 final Iterator<? extends NormalizedNode> it = container.body().iterator();
55 final int childIndent = depth + 1;
58 appendNode(sb, childIndent, currentNamespace, it.next());
59 } while (it.hasNext());
62 appendIndent(sb, depth);
65 } else if (node instanceof ValueNode) {
67 final Object value = node.body();
68 if (value instanceof byte[]) {
69 sb.append("(byte[])").append(Base64.getEncoder().encodeToString((byte[]) value));
70 } else if (value instanceof String) {
71 appendString(sb, (String) value);
75 } else if (node instanceof ForeignDataNode) {
76 final ForeignDataNode<?> data = (ForeignDataNode<?>) node;
77 final Object body = data.body();
78 if (body instanceof PrettyTreeAware) {
80 ((PrettyTreeAware) body).prettyTree().appendTo(sb, depth + 1);
81 appendIndent(sb, depth);
84 sb.append("= (").append(data.bodyObjectModel().getName()).append(')');
87 throw new IllegalStateException("Unhandled node " + node);
91 private static boolean appendNamespace(final StringBuilder sb, final QNameModule parent,
92 final QNameModule current) {
93 if (!current.equals(parent)) {
94 sb.append('(').append(current.getNamespace());
95 final Optional<Revision> rev = current.getRevision();
96 if (rev.isPresent()) {
97 sb.append('@').append(rev.orElseThrow());
105 private static void appendString(final StringBuilder sb, final String str) {
106 // TODO: do some escaping: '\r' '\n' '"' '\\' to make things even more zazzy
107 sb.append('"').append(str).append('"');