Fix license header violations in yang-jmx-generator-plugin
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / util / StringUtil.java
1 /*
2  * Copyright (c) 2013, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.config.yangjmxgenerator.plugin.util;
10
11 import com.google.common.base.Joiner;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Splitter;
14 import com.google.common.base.Strings;
15 import java.util.List;
16 import java.util.regex.Pattern;
17 import org.apache.commons.lang3.StringUtils;
18 import org.opendaylight.controller.config.yangjmxgenerator.plugin.java.FullyQualifiedName;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class StringUtil {
23     private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class);
24
25     /**
26      * @param list   of strings to be joined by ','
27      * @param prefix e.g. 'extends' or 'implements'
28      */
29     public static String prefixAndJoin(List<FullyQualifiedName> list, String prefix) {
30         if (list.isEmpty()) {
31             return "";
32         }
33         Joiner joiner = Joiner.on(",");
34         return " " + prefix + " " + joiner.join(list);
35     }
36
37     public static String addAsterixAtEachLineStart(String input) {
38         String s = Pattern.compile("^", Pattern.MULTILINE).matcher(input).replaceAll("* ");
39         // remove trailing spaces
40         s = Pattern.compile("\\s+$", Pattern.MULTILINE).matcher(s).replaceAll("");
41         s = ensureEndsWithSingleNewLine(s);
42         return s;
43     }
44
45     private static String ensureEndsWithSingleNewLine(String s) {
46         // .split Only trailing empty strings are skipped.
47         String[] split = s.split("\n");
48         s = Joiner.on("\n").join(split);
49         s = s + "\n";
50         return s;
51     }
52
53     public static String writeComment(String input, boolean isJavadoc) {
54         StringBuilder content = new StringBuilder();
55         content.append("/*");
56         if (isJavadoc) {
57             content.append("*");
58         }
59         content.append("\n");
60
61         content.append(addAsterixAtEachLineStart(input));
62         content.append("*/\n");
63         return content.toString();
64     }
65
66
67     public static Optional<String> loadCopyright() {
68         /*
69          * FIXME: BUG-980: this is a nice feature, but the copyright needs to come
70          *        from the project being processed, not this one.
71             try (InputStream in = StringUtil.class.getResourceAsStream("/copyright.txt")) {
72                 if (in != null) {
73                     return Optional.of(IOUtils.toString(in));
74                 }
75             } catch (IOException e) {
76                 LOG.warn("Cannot load copyright.txt", e);
77             }
78
79         */
80         return Optional.absent();
81     }
82
83     public static String formatJavaSource(String input) {
84         Iterable<String> split = Splitter.on("\n").trimResults().split(input);
85
86         int basicIndent = 4;
87         StringBuilder sb = new StringBuilder();
88         int intends = 0, empty = 0;
89         for (String line : split) {
90             intends -= StringUtils.countMatches(line, "}");
91             if (intends < 0) {
92                 intends = 0;
93             }
94             if (line.isEmpty() == false) {
95                 sb.append(Strings.repeat(" ", basicIndent * intends));
96                 sb.append(line);
97                 sb.append("\n");
98                 empty = 0;
99             } else {
100                 empty++; // one empty line is allowed
101                 if (empty < 2) {
102                     sb.append("\n");
103                 }
104             }
105             intends += StringUtils.countMatches(line, "{");
106         }
107         return ensureEndsWithSingleNewLine(sb.toString());
108     }
109
110 }