Resolve Bug:445 Remove freemarker from config code generator.
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / java / GeneratedObjectBuilder.java
1 package org.opendaylight.controller.config.yangjmxgenerator.plugin.java;
2
3 import com.google.common.base.Optional;
4 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil;
5
6 import static org.opendaylight.controller.config.yangjmxgenerator.plugin.util.StringUtil.prefixAndJoin;
7
8 public class GeneratedObjectBuilder {
9     private final JavaFileInput input;
10
11     public GeneratedObjectBuilder(JavaFileInput input) {
12         this.input = input;
13     }
14
15
16     public GeneratedObject toGeneratedObject() {
17         FullyQualifiedName fqn = input.getFQN();
18         StringBuilder content = new StringBuilder();
19
20
21         content.append(maybeAddComment(input.getCopyright()));
22         content.append(maybeAddComment(input.getHeader()));
23
24         if (input.getFQN().getPackageName().isEmpty() == false) {
25             content.append("package ");
26             content.append(input.getFQN().getPackageName());
27             content.append(";\n");
28         }
29         content.append(maybeAddComment(input.getClassJavaDoc(), true));
30
31         for (String classAnnotation : input.getClassAnnotations()) {
32             content.append(classAnnotation);
33             content.append("\n");
34         }
35
36         content.append("public ");
37         content.append(input.getType());
38         content.append(" ");
39         content.append(input.getFQN().getTypeName());
40         content.append(prefixAndJoin(input.getExtends(), "extends"));
41         content.append(prefixAndJoin(input.getImplements(), "implements"));
42         content.append(" {\n");
43
44         for (String method : input.getBodyElements()) {
45             content.append(method);
46             content.append("\n");
47         }
48
49         content.append("\n}\n");
50
51         return new GeneratedObject(fqn, content.toString());
52     }
53
54     private static String maybeAddComment(Optional<String> comment) {
55         return maybeAddComment(comment, false);
56     }
57
58     private static String maybeAddComment(Optional<String> comment, boolean isJavadoc) {
59
60         if (comment.isPresent()) {
61             String input = comment.get();
62             return StringUtil.writeComment(input, isJavadoc);
63         } else {
64             return "";
65         }
66     }
67
68 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.