42556c243e76e5f5f001018e0943299044ea991a
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / ftl / directives / TypeDeclarationDirective.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.directives;
9
10 import java.io.IOException;
11 import java.io.Writer;
12 import java.util.Collection;
13 import java.util.Map;
14
15 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.TypeDeclaration;
16
17 import com.google.common.base.Preconditions;
18
19 import freemarker.core.Environment;
20 import freemarker.ext.beans.StringModel;
21 import freemarker.template.TemplateDirectiveBody;
22 import freemarker.template.TemplateDirectiveModel;
23 import freemarker.template.TemplateException;
24 import freemarker.template.TemplateModel;
25
26 /**
27  * Add type declaration to freemarker template.
28  */
29 public class TypeDeclarationDirective implements TemplateDirectiveModel {
30
31     private static final String OBJECT = "object";
32
33     @Override
34     public void execute(Environment env, Map params, TemplateModel[] loopVars,
35             TemplateDirectiveBody body) throws TemplateException, IOException {
36         Object object = params.get(OBJECT);
37         Preconditions.checkNotNull(object, "Null type declaration");
38
39         object = ((StringModel) object).getWrappedObject();
40         Preconditions.checkArgument(
41                 object instanceof TypeDeclaration,
42                 "Type declaration should be instance of "
43                         + TypeDeclaration.class + " but was "
44                         + object.getClass());
45
46         TypeDeclaration type = (TypeDeclaration) object;
47
48         Writer out = env.getOut();
49         StringBuilder build = new StringBuilder("public ");
50         if (type.isAbstract())
51             build.append("abstract ");
52         if (type.isFinal())
53             build.append("final ");
54         build.append(type.getType() + " ");
55         build.append(type.getName() + " ");
56
57         generateExtendOrImplement(build, "extends", type.getExtended());
58
59         generateExtendOrImplement(build, "implements", type.getImplemented());
60
61         build.append(System.lineSeparator());
62         out.write(build.toString().toCharArray());
63     }
64
65     private void generateExtendOrImplement(StringBuilder build, String prefix,
66             Collection<String> elements) {
67         if (elements.isEmpty())
68             return;
69
70         build.append(prefix + " ");
71
72         for (String extended : elements) {
73             build.append(extended);
74             build.append(", ");
75         }
76         build.deleteCharAt(build.length() - 1);
77         build.deleteCharAt(build.length() - 1);
78     }
79
80 }