2 * Copyright (c) 2017 Cisco Systems, Inc. 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.javav2.generator.impl.util;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.CharMatcher;
12 import com.google.common.base.Strings;
13 import com.google.common.collect.Iterables;
14 import java.util.StringTokenizer;
15 import java.util.regex.Pattern;
16 import javax.annotation.RegEx;
17 import org.opendaylight.yangtools.yang.common.QName;
23 public final class YangTextTemplate {
24 private static final CharMatcher NEWLINE_OR_TAB = CharMatcher.anyOf("\n\t");
26 private static final String SPACES_REGEX = " +";
27 private static final Pattern SPACES_PATTERN = Pattern.compile(SPACES_REGEX);
29 private YangTextTemplate() {
30 throw new UnsupportedOperationException("Util class");
33 public static String formatSchemaPath(final String moduleName, final Iterable<QName> schemaPath) {
34 final StringBuilder sb = new StringBuilder();
35 sb.append(moduleName);
37 QName currentElement = Iterables.getFirst(schemaPath, null);
38 for (final QName pathElement : schemaPath) {
40 if (!currentElement.getNamespace().equals(pathElement.getNamespace())) {
41 currentElement = pathElement;
42 sb.append(pathElement);
44 sb.append(pathElement.getLocalName());
51 * Used in #yangtemplateformodule.scala.txt for formating revision description
53 * @param text Content of tag description
54 * @param nextLineIndent Number of spaces from left side default is 12
55 * @return formatted description
57 public static String formatToParagraph(final String text, final int nextLineIndent) {
58 if (Strings.isNullOrEmpty(text)) {
61 boolean isFirstElementOnNewLineEmptyChar = false;
62 final StringBuilder sb = new StringBuilder();
63 final StringBuilder lineBuilder = new StringBuilder();
64 final String lineIndent = Strings.repeat(" ", nextLineIndent);
65 final String textToFormat = NEWLINE_OR_TAB.removeFrom(text);
66 final String formattedText = SPACES_PATTERN.matcher(textToFormat).replaceAll(" ");
67 final StringTokenizer tokenizer = new StringTokenizer(formattedText, " ", true);
69 while (tokenizer.hasMoreElements()) {
70 final String nextElement = tokenizer.nextElement().toString();
72 if (lineBuilder.length() + nextElement.length() > 80) {
73 // Trim trailing whitespace
74 for (int i = lineBuilder.length() - 1; i >= 0 && lineBuilder.charAt(i) != ' '; --i) {
75 lineBuilder.setLength(i);
77 // Trim leading whitespace
78 while (lineBuilder.charAt(0) == ' ') {
79 lineBuilder.deleteCharAt(0);
81 sb.append(lineBuilder).append('\n');
82 lineBuilder.setLength(0);
84 if (nextLineIndent > 0) {
85 sb.append(lineIndent);
88 if (" ".equals(nextElement)) {
89 isFirstElementOnNewLineEmptyChar = true;
92 if (isFirstElementOnNewLineEmptyChar) {
93 isFirstElementOnNewLineEmptyChar = false;
95 lineBuilder.append(nextElement);
98 return sb.append(lineBuilder).append('\n').toString();
102 * Used in all yangtemplates for formating augmentation target
104 * @param schemaPath path to augmented node
105 * @return path in string format
107 public static String formatToAugmentPath(final Iterable<QName> schemaPath) {
108 final StringBuilder sb = new StringBuilder();
109 for (final QName pathElement : schemaPath) {
110 sb.append('/').append(pathElement.getLocalName());
112 return sb.toString();