blob: 529a93d88eed9b72faf15c7b7a3d77e2831a6927 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal870c56f2016-02-20 21:57:16 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.translator.tojava.utils;
18
Bharat saraswale2d51d62016-03-23 19:40:35 +053019import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
20import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
21import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
Gaurav Agrawal338735b2016-04-18 18:53:11 +053022import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053023import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
24import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053025import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getExtendsList;
26import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.isExtendsList;
Bharat saraswale2d51d62016-03-23 19:40:35 +053027import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
28import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053029import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
30import static org.onosproject.yangutils.utils.UtilConstants.EXTEND;
Bharat saraswale2d51d62016-03-23 19:40:35 +053031import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
32import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
33import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
34import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
35import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
36import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
37import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
38import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
39import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053040import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
Bharat saraswal870c56f2016-02-20 21:57:16 +053041
42/**
Bharat saraswald9822e92016-04-05 15:13:44 +053043 * Represents generator for class definition of generated files.
Bharat saraswal870c56f2016-02-20 21:57:16 +053044 */
45public final class ClassDefinitionGenerator {
46
47 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053048 * Creates an instance of class definition generator.
Bharat saraswal870c56f2016-02-20 21:57:16 +053049 */
50 private ClassDefinitionGenerator() {
51 }
52
53 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053054 * Based on the file type and the YANG name of the file, generate the class
55 * / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053056 *
57 * @param genFileTypes generated file type
Gaurav Agrawal338735b2016-04-18 18:53:11 +053058 * @param yangName class name
Bharat saraswal870c56f2016-02-20 21:57:16 +053059 * @return class definition
60 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053061 public static String generateClassDefinition(int genFileTypes, String yangName) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053062
63 /**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053064 * based on the file type and the YANG name of the file, generate the
65 * class / interface definition start.
Bharat saraswal870c56f2016-02-20 21:57:16 +053066 */
Bharat saraswale2d51d62016-03-23 19:40:35 +053067 if ((genFileTypes & INTERFACE_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053068 return getInterfaceDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053069 } else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053070 return getBuilderClassDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053071 } else if ((genFileTypes & IMPL_CLASS_MASK) != 0) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053072 return getImplClassDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053073 } else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) {
Vinod Kumar Sc4216002016-03-03 19:55:30 +053074 return getBuilderInterfaceDefinition(yangName);
Bharat saraswale2d51d62016-03-23 19:40:35 +053075 } else if ((genFileTypes & GENERATE_TYPEDEF_CLASS) != 0) {
Gaurav Agrawal338735b2016-04-18 18:53:11 +053076 return getTypeClassDefinition(yangName);
77 } else if ((genFileTypes & GENERATE_UNION_CLASS) != 0) {
78 return getTypeClassDefinition(yangName);
Bharat saraswal870c56f2016-02-20 21:57:16 +053079 }
80 return null;
81 }
82
83 /**
84 * Returns interface file class definition.
85 *
86 * @param yangName file name
87 * @return definition
88 */
89 private static String getInterfaceDefinition(String yangName) {
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053090 if (!isExtendsList()) {
91 return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
92 }
93 String def = PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE;
94 for (String extend : getExtendsList()) {
95 def = def + extend + COMMA;
96 }
97 def = trimAtLast(def, COMMA);
Bharat saraswal870c56f2016-02-20 21:57:16 +053098
Bharat saraswalcc1cdab2016-04-16 02:28:25 +053099 return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530100 }
101
102 /**
103 * Returns builder interface file class definition.
104 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530105 * @param yangName java class name, corresponding to which the builder class
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530106 * is being generated
Bharat saraswal870c56f2016-02-20 21:57:16 +0530107 * @return definition
108 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530109 private static String getBuilderInterfaceDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530110 return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530111 }
112
113 /**
114 * Returns builder file class definition.
115 *
116 * @param yangName file name
117 * @return definition
118 */
119 private static String getBuilderClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530120 return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
121 + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530122 }
123
124 /**
125 * Returns impl file class definition.
126 *
127 * @param yangName file name
128 * @return definition
129 */
130 private static String getImplClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530131 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + IMPL + SPACE + IMPLEMENTS + SPACE + yangName
132 + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal870c56f2016-02-20 21:57:16 +0530133 }
134
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530135 /**
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530136 * Returns type file class definition.
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530137 *
138 * @param yangName file name
139 * @return definition
140 */
Gaurav Agrawal338735b2016-04-18 18:53:11 +0530141 private static String getTypeClassDefinition(String yangName) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530142 return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530143 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530144}