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