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