Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 19 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK; |
| 20 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK; |
| 21 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS; |
| 22 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK; |
| 23 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 24 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getExtendsList; |
| 25 | import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.isExtendsList; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.utils.UtilConstants.BUILDER; |
| 27 | import static org.onosproject.yangutils.utils.UtilConstants.CLASS; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 28 | import static org.onosproject.yangutils.utils.UtilConstants.COMMA; |
| 29 | import static org.onosproject.yangutils.utils.UtilConstants.EXTEND; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.utils.UtilConstants.FINAL; |
| 31 | import static org.onosproject.yangutils.utils.UtilConstants.IMPL; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS; |
| 33 | import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE; |
| 34 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 35 | import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET; |
| 36 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 37 | import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC; |
| 38 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 39 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 40 | |
| 41 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 42 | * Represents generator for class definition of generated files. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 43 | */ |
| 44 | public final class ClassDefinitionGenerator { |
| 45 | |
| 46 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 47 | * Creates an instance of class definition generator. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 48 | */ |
| 49 | private ClassDefinitionGenerator() { |
| 50 | } |
| 51 | |
| 52 | /** |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 53 | * Based on the file type and the YANG name of the file, generate the class |
| 54 | * / interface definition start. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 55 | * |
| 56 | * @param genFileTypes generated file type |
| 57 | * @param yangName class name |
| 58 | * @return class definition |
| 59 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 60 | public static String generateClassDefinition(int genFileTypes, String yangName) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 61 | |
| 62 | /** |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 63 | * based on the file type and the YANG name of the file, generate the |
| 64 | * class / interface definition start. |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 65 | */ |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 66 | if ((genFileTypes & INTERFACE_MASK) != 0) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 67 | |
| 68 | return getInterfaceDefinition(yangName); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 69 | } else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 70 | |
| 71 | return getBuilderClassDefinition(yangName); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 72 | } else if ((genFileTypes & IMPL_CLASS_MASK) != 0) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 73 | |
| 74 | return getImplClassDefinition(yangName); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 75 | } else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 76 | |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 77 | return getBuilderInterfaceDefinition(yangName); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 78 | } else if ((genFileTypes & GENERATE_TYPEDEF_CLASS) != 0) { |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 79 | |
| 80 | return getTypeDefClassDefinition(yangName); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 81 | } |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns interface file class definition. |
| 87 | * |
| 88 | * @param yangName file name |
| 89 | * @return definition |
| 90 | */ |
| 91 | private static String getInterfaceDefinition(String yangName) { |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 92 | if (!isExtendsList()) { |
| 93 | return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
| 94 | } |
| 95 | String def = PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE; |
| 96 | for (String extend : getExtendsList()) { |
| 97 | def = def + extend + COMMA; |
| 98 | } |
| 99 | def = trimAtLast(def, COMMA); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 100 | |
Bharat saraswal | cc1cdab | 2016-04-16 02:28:25 +0530 | [diff] [blame^] | 101 | return def + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns builder interface file class definition. |
| 106 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 107 | * @param yangName java class name, corresponding to which the builder class |
| 108 | * is being generated |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 109 | * @return definition |
| 110 | */ |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 111 | private static String getBuilderInterfaceDefinition(String yangName) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 112 | return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns builder file class definition. |
| 117 | * |
| 118 | * @param yangName file name |
| 119 | * @return definition |
| 120 | */ |
| 121 | private static String getBuilderClassDefinition(String yangName) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 122 | return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD |
| 123 | + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns impl file class definition. |
| 128 | * |
| 129 | * @param yangName file name |
| 130 | * @return definition |
| 131 | */ |
| 132 | private static String getImplClassDefinition(String yangName) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 133 | return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + IMPL + SPACE + IMPLEMENTS + SPACE + yangName |
| 134 | + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 135 | } |
| 136 | |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 137 | /** |
| 138 | * Returns typeDef file class definition. |
| 139 | * |
| 140 | * @param yangName file name |
| 141 | * @return definition |
| 142 | */ |
| 143 | private static String getTypeDefClassDefinition(String yangName) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 144 | return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE; |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 145 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 146 | } |