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 | |
| 19 | import org.onosproject.yangutils.translator.GeneratedFileType; |
| 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 | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 47 | if ((genFileTypes & GeneratedFileType.INTERFACE_MASK) != 0) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 48 | |
| 49 | return getInterfaceDefinition(yangName); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 50 | } else if ((genFileTypes & GeneratedFileType.BUILDER_CLASS_MASK) != 0) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 51 | |
| 52 | return getBuilderClassDefinition(yangName); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 53 | } else if ((genFileTypes & GeneratedFileType.IMPL_CLASS_MASK) != 0) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 54 | |
| 55 | return getImplClassDefinition(yangName); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 56 | } else if ((genFileTypes & GeneratedFileType.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); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 59 | } |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns interface file class definition. |
| 65 | * |
| 66 | * @param yangName file name |
| 67 | * @return definition |
| 68 | */ |
| 69 | private static String getInterfaceDefinition(String yangName) { |
| 70 | |
| 71 | return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE + UtilConstants.SPACE + yangName |
| 72 | + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns builder interface file class definition. |
| 77 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 78 | * @param yangName java class name, corresponding to which the builder class |
| 79 | * is being generated |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 80 | * @return definition |
| 81 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 82 | private static String getBuilderInterfaceDefinition(String yangName) { |
| 83 | return UtilConstants.INTERFACE + UtilConstants.SPACE + yangName + UtilConstants.BUILDER + UtilConstants.SPACE |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 84 | + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns builder file class definition. |
| 89 | * |
| 90 | * @param yangName file name |
| 91 | * @return definition |
| 92 | */ |
| 93 | private static String getBuilderClassDefinition(String yangName) { |
| 94 | |
| 95 | return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.CLASS + UtilConstants.SPACE + yangName |
| 96 | + UtilConstants.BUILDER + UtilConstants.SPACE + UtilConstants.IMPLEMENTS + UtilConstants.SPACE |
| 97 | + yangName + UtilConstants.PERIOD + UtilConstants.BUILDER + UtilConstants.SPACE |
| 98 | + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns impl file class definition. |
| 103 | * |
| 104 | * @param yangName file name |
| 105 | * @return definition |
| 106 | */ |
| 107 | private static String getImplClassDefinition(String yangName) { |
| 108 | |
| 109 | return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE |
| 110 | + UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.IMPL + UtilConstants.SPACE |
| 111 | + UtilConstants.IMPLEMENTS + UtilConstants.SPACE + yangName + UtilConstants.OPEN_CURLY_BRACKET |
| 112 | + UtilConstants.SPACE + UtilConstants.NEW_LINE; |
| 113 | } |
| 114 | |
| 115 | } |