Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.datamodel.LocationInfo; |
| 20 | import org.onosproject.yangutils.datamodel.YangNode; |
| 21 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
| 22 | import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; |
| 23 | import org.onosproject.yangutils.translator.tojava.TempJavaBeanFragmentFiles; |
| 24 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer; |
| 25 | import org.onosproject.yangutils.translator.tojava.TempJavaTypeFragmentFiles; |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 26 | |
| 27 | import java.io.IOException; |
| 28 | |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.utils.UtilConstants.AT; |
| 30 | import static org.onosproject.yangutils.utils.UtilConstants.IN; |
| 31 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 32 | /** |
| 33 | * Represents common translator utilities. |
| 34 | */ |
| 35 | public final class TranslatorUtils { |
| 36 | |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 37 | // No instantiation |
| 38 | private TranslatorUtils() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns translator error message string with location information for |
| 43 | * YANG schema node with localized message. |
| 44 | * |
| 45 | * @param errorType type of translator error |
| 46 | * @param node YANG schema node |
| 47 | * @param localizedMsg localized message |
| 48 | * @return translator error message |
| 49 | */ |
| 50 | public static String getErrorMsg(TranslatorErrorType errorType, |
| 51 | YangSchemaNode node, |
| 52 | String localizedMsg) { |
| 53 | return getErrorMsg(errorType, node) + localizedMsg; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns translator error message string with location information for |
| 58 | * YANG schema node. |
| 59 | * |
| 60 | * @param errorType type of translator error |
| 61 | * @param node YANG schema node |
| 62 | * @return translator error message |
| 63 | */ |
| 64 | public static String getErrorMsg(TranslatorErrorType errorType, |
| 65 | YangSchemaNode node) { |
| 66 | return errorType.prefix() + IN + node.getName() + getLocationMsg(node); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns translator error message string with location information for |
| 71 | * JAVA code generator info. |
| 72 | * |
| 73 | * @param errorType type of translator error |
| 74 | * @param location node with location info |
| 75 | * @return translator error message |
| 76 | */ |
| 77 | public static String getErrorMsgForCodeGenerator(TranslatorErrorType errorType, |
| 78 | LocationInfo location) { |
| 79 | return errorType.prefix() + getLocationMsg(location); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns location message string. |
| 84 | * |
| 85 | * @param location location info node |
| 86 | * @return location message string |
| 87 | */ |
| 88 | private static String getLocationMsg(LocationInfo location) { |
| 89 | return AT + location.getLineNumber() + AT + |
| 90 | location.getCharPosition() + IN + location.getFileName(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns bean temp files for YANG node. |
| 95 | * |
| 96 | * @param curNode current YANG node |
| 97 | * @return bean files |
| 98 | */ |
| 99 | public static TempJavaBeanFragmentFiles getBeanFiles(YangNode curNode) { |
| 100 | return ((TempJavaCodeFragmentFilesContainer) curNode) |
| 101 | .getTempJavaCodeFragmentFiles().getBeanTempFiles(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns bean temp files for JAVA code generator info. |
| 106 | * |
| 107 | * @param info JAVA code generator info |
| 108 | * @return bean files |
| 109 | */ |
| 110 | public static TempJavaBeanFragmentFiles getBeanFiles(JavaCodeGeneratorInfo |
| 111 | info) { |
| 112 | return info.getTempJavaCodeFragmentFiles().getBeanTempFiles(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns type temp files for YANG node. |
| 117 | * |
| 118 | * @param curNode current YANG node |
| 119 | * @return type files |
| 120 | */ |
Bharat saraswal | 54e4bab | 2016-10-05 23:32:14 +0530 | [diff] [blame] | 121 | public static TempJavaTypeFragmentFiles getTypeFiles(YangNode curNode) { |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 122 | return ((TempJavaCodeFragmentFilesContainer) curNode) |
| 123 | .getTempJavaCodeFragmentFiles().getTypeTempFiles(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Adds default constructor to a given YANG node. |
| 128 | * |
| 129 | * @param node YANG node |
| 130 | * @param modifier modifier for constructor. |
| 131 | * @param toAppend string which need to be appended with the class name |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 132 | * @return default constructor for class |
| 133 | * @throws IOException when fails to append to file |
| 134 | */ |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 135 | static String addDefaultConstructor(YangNode node, String modifier, |
| 136 | String toAppend) |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 137 | throws IOException { |
| 138 | return ((TempJavaCodeFragmentFilesContainer) node) |
| 139 | .getTempJavaCodeFragmentFiles() |
Bharat saraswal | 9fab16b | 2016-09-23 23:27:24 +0530 | [diff] [blame] | 140 | .addDefaultConstructor(modifier, toAppend); |
Gaurav Agrawal | 3b57f36 | 2016-09-07 13:16:35 +0530 | [diff] [blame] | 141 | /* |
| 142 | * TODO update addDefaultConstructor, it doesn't need YANG node as an |
| 143 | * input. |
| 144 | */ |
| 145 | } |
| 146 | } |