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.sal.java.api.generator;
10 import static org.opendaylight.controller.sal.java.api.generator.Constants.*;
12 import java.io.IOException;
13 import java.io.StringWriter;
14 import java.io.Writer;
15 import java.util.List;
18 import org.opendaylight.controller.sal.binding.model.api.*;
20 public final class InterfaceGenerator implements CodeGenerator {
22 private Map<String, String> imports;
24 private String generateEnums(List<Enumeration> enums) {
27 EnumGenerator enumGenerator = new EnumGenerator();
28 for (Enumeration en : enums) {
30 result = result + (enumGenerator.generateInnerEnumeration(en, TAB).toString() + NL);
31 } catch (IOException e) {
39 private String generateConstants(List<Constant> constants, String pkgName) {
41 if (constants != null) {
42 for (Constant c : constants) {
43 result = result + GeneratorUtil.createConstant(c, TAB, imports, pkgName) + NL;
50 public String generateMethods(List<MethodSignature> methods, String pkgName) {
53 if (methods != null) {
54 for (MethodSignature m : methods) {
55 result = result + GeneratorUtil.createMethodDeclaration(m, TAB, imports, pkgName) + NL;
62 public Writer generate(Type type) throws IOException {
63 Writer writer = new StringWriter();
64 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
65 final GeneratedType genType = (GeneratedType) type;
66 imports = GeneratorUtil.createImports(genType);
68 final String currentPkg = genType.getPackageName();
69 final List<Constant> constants = genType.getConstantDefinitions();
70 final List<MethodSignature> methods = genType.getMethodDefinitions();
71 final List<Enumeration> enums = genType.getEnumerations();
73 writer.write(GeneratorUtil.createPackageDeclaration(genType.getPackageName()));
76 final List<String> importLines = GeneratorUtil.createImportLines(imports);
77 for (String line : importLines) {
78 writer.write(line + NL);
81 writer.write(GeneratorUtil.createIfcDeclaration(genType, "", imports));
84 writer.write(generateEnums(enums));
85 writer.write(generateConstants(constants, currentPkg));
86 writer.write(generateMethods(methods, currentPkg));