2 * Copyright (c) 2013 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.controller.config.yangjmxgenerator.plugin.ftl.directives;
10 import java.io.IOException;
11 import java.io.Writer;
12 import java.util.Collection;
15 import org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model.TypeDeclaration;
17 import com.google.common.base.Preconditions;
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;
27 * Add type declaration to freemarker template.
29 public class TypeDeclarationDirective implements TemplateDirectiveModel {
31 private static final String OBJECT = "object";
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");
39 object = ((StringModel) object).getWrappedObject();
40 Preconditions.checkArgument(
41 object instanceof TypeDeclaration,
42 "Type declaration should be instance of "
43 + TypeDeclaration.class + " but was "
46 TypeDeclaration type = (TypeDeclaration) object;
48 Writer out = env.getOut();
49 StringBuilder build = new StringBuilder("public ");
50 if (type.isAbstract())
51 build.append("abstract ");
53 build.append("final ");
54 build.append(type.getType() + " ");
55 build.append(type.getName() + " ");
57 generateExtendOrImplement(build, "extends", type.getExtended());
59 generateExtendOrImplement(build, "implements", type.getImplemented());
61 build.append(System.lineSeparator());
62 out.write(build.toString().toCharArray());
65 private void generateExtendOrImplement(StringBuilder build, String prefix,
66 Collection<String> elements) {
67 if (elements.isEmpty())
70 build.append(prefix + " ");
72 for (String extended : elements) {
73 build.append(extended);
76 build.deleteCharAt(build.length() - 1);
77 build.deleteCharAt(build.length() - 1);