+ static void appendSnippet(final StringBuilder sb, final GeneratedType type) {
+ type.getYangSourceDefinition().ifPresent(def -> {
+ sb.append('\n');
+
+ if (def instanceof Single) {
+ final DocumentedNode node = ((Single) def).getNode();
+
+ sb.append("<p>\n")
+ .append("This class represents the following YANG schema fragment defined in module <b>")
+ .append(def.getModule().argument().getLocalName()).append("</b>\n")
+ .append("<pre>\n");
+ appendYangSnippet(sb, def.getModule(), ((EffectiveStatement<?, ?>) node).getDeclared());
+ sb.append("</pre>");
+
+ if (node instanceof SchemaNode) {
+ final SchemaNode schema = (SchemaNode) node;
+ sb.append("The schema path to identify an instance is\n");
+ appendPath(sb.append("<i>"), def.getModule(), schema.getPath().getPathFromRoot());
+ sb.append("</i>\n");
+
+ if (hasBuilderClass(schema)) {
+ final String builderName = type.getName() + BindingMapping.BUILDER_SUFFIX;
+
+ sb.append("\n<p>To create instances of this class use {@link ").append(builderName)
+ .append("}.\n")
+ .append("@see ").append(builderName).append('\n');
+ if (node instanceof ListSchemaNode) {
+ final var keyDef = ((ListSchemaNode) node).getKeyDefinition();
+ if (!keyDef.isEmpty()) {
+ sb.append("@see ").append(type.getName()).append(BindingMapping.KEY_SUFFIX);
+ }
+ sb.append('\n');
+ }
+ }
+ }
+ } else if (def instanceof Multiple) {
+ sb.append("<pre>\n");
+ for (SchemaNode node : ((Multiple) def).getNodes()) {
+ appendYangSnippet(sb, def.getModule(), ((EffectiveStatement<?, ?>) node).getDeclared());
+ }
+ sb.append("</pre>\n");
+ }
+ });
+ }
+
+ static String encodeJavadocSymbols(final String description) {
+ // FIXME: Use String.isBlank()?
+ return description == null || description.isEmpty() ? description
+ : TAIL_COMMENT_PATTERN.matcher(AMP_MATCHER.replaceFrom(description, "&")).replaceAll("*/");
+ }
+
+ private static void appendYangSnippet(final StringBuilder sb, final ModuleEffectiveStatement module,
+ final DeclaredStatement<?> stmt) {
+ for (String str : YANG_FORMATTER.toYangTextSnippet(module, stmt)) {
+ sb.append(replaceAllIllegalChars(encodeAngleBrackets(encodeJavadocSymbols(str))));
+ }
+ }
+
+ private static void appendPath(final StringBuilder sb, final ModuleEffectiveStatement module,
+ final List<QName> path) {
+ if (!path.isEmpty()) {
+ // FIXME: this is module name, while when we switch, we end up using QName.toString() -- which is weird
+ sb.append(module.argument().getLocalName());
+ XMLNamespace currentNamespace = path.get(0).getNamespace();
+
+ for (QName pathElement : path) {
+ final XMLNamespace elementNamespace = pathElement.getNamespace();
+ if (!elementNamespace.equals(currentNamespace)) {
+ sb.append(pathElement);
+ currentNamespace = elementNamespace;
+ } else {
+ sb.append(pathElement.getLocalName());
+ }
+ }
+ }
+ }
+
+ private static boolean hasBuilderClass(final SchemaNode schemaNode) {
+ return schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode
+ || schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition;
+ }
+