Bharat saraswal | 870c56f | 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.utils.io.impl; |
| 18 | |
| 19 | import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax; |
| 20 | import org.onosproject.yangutils.utils.UtilConstants; |
| 21 | |
| 22 | /** |
| 23 | * Provides javadoc for the generated classes. |
| 24 | */ |
| 25 | public final class JavaDocGen { |
| 26 | |
| 27 | /** |
| 28 | * Default Constructor. |
| 29 | */ |
| 30 | private JavaDocGen() { |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * JavaDocs types. |
| 35 | */ |
| 36 | public static enum JavaDocType { |
| 37 | |
| 38 | /** |
| 39 | * For class. |
| 40 | */ |
| 41 | IMPL_CLASS, |
| 42 | |
| 43 | /** |
| 44 | * For builder class. |
| 45 | */ |
| 46 | BUILDER_CLASS, |
| 47 | |
| 48 | /** |
| 49 | * For interface. |
| 50 | */ |
| 51 | INTERFACE, |
| 52 | |
| 53 | /** |
| 54 | * For builder interface. |
| 55 | */ |
| 56 | BUILDER_INTERFACE, |
| 57 | |
| 58 | /** |
| 59 | * For package-info. |
| 60 | */ |
| 61 | PACKAGE_INFO, |
| 62 | |
| 63 | /** |
| 64 | * For getters. |
| 65 | */ |
| 66 | GETTER, |
| 67 | |
| 68 | /** |
| 69 | * For setters. |
| 70 | */ |
| 71 | SETTER, |
| 72 | |
| 73 | /** |
| 74 | * For default constructor. |
| 75 | */ |
| 76 | DEFAULT_CONSTRUCTOR, |
| 77 | |
| 78 | /** |
| 79 | * For constructor. |
| 80 | */ |
| 81 | CONSTRUCTOR, |
| 82 | |
| 83 | /** |
| 84 | * For build. |
| 85 | */ |
| 86 | BUILD |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns java docs. |
| 91 | * |
| 92 | * @param type java doc type |
| 93 | * @param name name of the YangNode |
| 94 | * @return javadocs. |
| 95 | */ |
| 96 | public static String getJavaDoc(JavaDocType type, String name) { |
| 97 | name = JavaIdentifierSyntax.getCamelCase(name); |
| 98 | String javaDoc = ""; |
| 99 | if (type.equals(JavaDocType.IMPL_CLASS)) { |
| 100 | javaDoc = generateForImplClass(name); |
| 101 | } else if (type.equals(JavaDocType.BUILDER_CLASS)) { |
| 102 | javaDoc = generateForBuilderClass(name); |
| 103 | } else if (type.equals(JavaDocType.INTERFACE)) { |
| 104 | javaDoc = generateForInterface(name); |
| 105 | } else if (type.equals(JavaDocType.BUILDER_INTERFACE)) { |
| 106 | javaDoc = generateForBuilderInterface(name); |
| 107 | } else if (type.equals(JavaDocType.PACKAGE_INFO)) { |
| 108 | javaDoc = generateForPackage(name); |
| 109 | } else if (type.equals(JavaDocType.GETTER)) { |
| 110 | javaDoc = generateForGetters(name); |
| 111 | } else if (type.equals(JavaDocType.SETTER)) { |
| 112 | javaDoc = generateForSetters(name); |
| 113 | } else if (type.equals(JavaDocType.DEFAULT_CONSTRUCTOR)) { |
| 114 | javaDoc = generateForDefaultConstructors(); |
| 115 | } else if (type.equals(JavaDocType.BUILD)) { |
| 116 | javaDoc = generateForBuild(name); |
| 117 | } else if (type.equals(JavaDocType.CONSTRUCTOR)) { |
| 118 | javaDoc = generateForConstructors(name); |
| 119 | } |
| 120 | return javaDoc; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Generate javaDocs for getter method. |
| 125 | * |
| 126 | * @param attribute attribute |
| 127 | * @return javaDocs |
| 128 | */ |
| 129 | private static String generateForGetters(String attribute) { |
| 130 | return (UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE |
| 131 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_GETTERS + attribute |
| 132 | + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION |
| 133 | + UtilConstants.NEW_LINE_ESTRIC + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN |
| 134 | + attribute + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION |
| 135 | + UtilConstants.JAVA_DOC_END_LINE); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Generates javaDocs for setter method. |
| 140 | * |
| 141 | * @param attribute attribute |
| 142 | * @return javaDocs |
| 143 | */ |
| 144 | private static String generateForSetters(String attribute) { |
| 145 | return (UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE |
| 146 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_SETTERS + attribute |
| 147 | + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION |
| 148 | + UtilConstants.NEW_LINE_ESTRIC + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM |
| 149 | + attribute + UtilConstants.SPACE + attribute + UtilConstants.NEW_LINE |
| 150 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN + UtilConstants.BUILDER_OBJECT |
| 151 | + attribute + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION |
| 152 | + UtilConstants.JAVA_DOC_END_LINE); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Generate javaDocs for the impl class. |
| 157 | * |
| 158 | * @param className class name |
| 159 | * @return javaDocs. |
| 160 | */ |
| 161 | private static String generateForImplClass(String className) { |
| 162 | return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.IMPL_CLASS_JAVA_DOC + className + UtilConstants.PERIOD |
| 163 | + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Generate javaDocs for the builder class. |
| 168 | * |
| 169 | * @param className class name |
| 170 | * @return javaDocs. |
| 171 | */ |
| 172 | private static String generateForBuilderClass(String className) { |
| 173 | return (UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_CLASS_JAVA_DOC |
| 174 | + className + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Generate javaDoc for the interface. |
| 179 | * |
| 180 | * @param interfaceName interface name |
| 181 | * @return javaDocs. |
| 182 | */ |
| 183 | private static String generateForInterface(String interfaceName) { |
| 184 | return (UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC |
| 185 | + interfaceName + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Generate javaDoc for the builder interface. |
| 190 | * |
| 191 | * @param builderforName builder for name |
| 192 | * @return javaDocs. |
| 193 | */ |
| 194 | private static String generateForBuilderInterface(String builderforName) { |
| 195 | return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_INTERFACE_JAVA_DOC + builderforName |
| 196 | + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Generate javaDocs for package-info. |
| 201 | * |
| 202 | * @param packageName package name |
| 203 | * @return javaDocs |
| 204 | */ |
| 205 | private static String generateForPackage(String packageName) { |
| 206 | return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.PACKAGE_INFO_JAVADOC + packageName |
| 207 | + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Generate javaDocs for default constructor. |
| 212 | * |
| 213 | * @return javaDocs |
| 214 | */ |
| 215 | private static String generateForDefaultConstructors() { |
| 216 | return (UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_DEFAULT_CONSTRUCTOR |
| 217 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Generate javaDocs for constructor with parameters. |
| 222 | * |
| 223 | * @param params list of parameters |
| 224 | * @param className class name |
| 225 | * @return javaDocs |
| 226 | */ |
| 227 | private static String generateForConstructors(String className) { |
| 228 | return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.FOUR_SPACE_INDENTATION |
| 229 | + UtilConstants.JAVA_DOC_SETTERS + className + UtilConstants.PERIOD + UtilConstants.NEW_LINE |
| 230 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC |
| 231 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM |
| 232 | + (className.substring(0, 1).toLowerCase() + className.substring(1)) + UtilConstants.OBJECT |
| 233 | + UtilConstants.SPACE + UtilConstants.BUILDER_OBJECT + UtilConstants.SPACE + className |
| 234 | + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Generate javaDocs for build. |
| 239 | * |
| 240 | * @return javaDocs |
| 241 | */ |
| 242 | private static String generateForBuild(String buildName) { |
| 243 | return (UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_FIRST_LINE |
| 244 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_BUILD + buildName + UtilConstants.PERIOD |
| 245 | + UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC |
| 246 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_RETURN |
| 247 | + UtilConstants.JAVA_DOC_BUILD_RETURN + buildName + UtilConstants.PERIOD + UtilConstants.NEW_LINE |
| 248 | + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_END_LINE); |
| 249 | } |
| 250 | } |