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.StringReader;
12 import java.io.Writer;
15 import com.google.common.collect.Maps;
17 import freemarker.core.Environment;
18 import freemarker.template.Configuration;
19 import freemarker.template.Template;
20 import freemarker.template.TemplateDirectiveBody;
21 import freemarker.template.TemplateDirectiveModel;
22 import freemarker.template.TemplateException;
23 import freemarker.template.TemplateModel;
26 * Add fields to freemarker template.
28 public class FieldsDirectiveTemplate implements TemplateDirectiveModel {
30 private static final String OBJECT = "object";
33 public void execute(Environment env, Map params, TemplateModel[] loopVars,
34 TemplateDirectiveBody body) throws TemplateException, IOException {
35 Object object = params.get(OBJECT);
39 String templateStr = " <#list fields as field>"
40 + "private <#if field.final==true>final </#if> <#if field.static==true>static </#if>"
41 + "${field.type} ${field.name}<#if field.definition??> = ${field.definition}</#if>;"
42 + System.lineSeparator() + " </#list>";
43 Template t = new Template("name", new StringReader(templateStr),
47 Map<String, Object> map = Maps.newHashMap();
48 map.put("fields", object);
49 Writer out = env.getOut();
51 } catch (TemplateException e) {
52 throw new IllegalStateException(
53 "Template error while generating fields" + e);