2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. 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.mdsal.binding.model.util;
10 import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.replaceAllIllegalChars;
11 import static org.opendaylight.mdsal.binding.model.util.FormattingUtils.formatToParagraph;
13 import com.google.common.annotations.Beta;
14 import com.google.common.escape.Escaper;
15 import com.google.common.escape.Escapers;
16 import java.util.Optional;
17 import java.util.regex.Pattern;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.opendaylight.mdsal.binding.model.api.TypeComment;
20 import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
23 * Utility methods for creating {@link TypeComment}s.
27 public final class TypeComments {
28 private static final Escaper ENTITY_ESCAPER = Escapers.builder()
29 .addEscape('<', "<")
30 .addEscape('>', ">")
31 .addEscape('&', "&")
32 .addEscape('@', "@").build();
33 private static final Pattern TAIL_COMMENT_PATTERN = Pattern.compile("*/", Pattern.LITERAL);
35 private TypeComments() {
40 * Create a {@link TypeComment} for a javadoc-compliant text snippet. This snippet must be eligible for direct
41 * inclusion in a Java comment without further escaping.
43 * @param javadoc Pre-formatted javadoc snippet
44 * @return {@link TypeComment}, or empty if the snippet was empty
46 public static Optional<TypeComment> javadoc(final String javadoc) {
47 return javadoc.isEmpty() ? Optional.empty() : Optional.of(() -> javadoc);
51 * Create a {@link TypeComment} for a {@link DocumentedNode}'s description string.
53 * @param node Documented node containing the description to be processed
54 * @return {@link TypeComment}, or empty if the node's description was empty or non-present.
56 public static Optional<TypeComment> description(final DocumentedNode node) {
57 final String description = node.getDescription().orElse("");
58 return description.isEmpty() ? Optional.empty() : Optional.of(() -> replaceAllIllegalChars(
60 TAIL_COMMENT_PATTERN.matcher(ENTITY_ESCAPER.escape(description)).replaceAll("*/"), 0)));