blob: f854f9973330899b3cbd11f87bf132838de57426 [file] [log] [blame]
Gaurav Agrawal3b57f362016-09-07 13:16:35 +05301/*
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
17package org.onosproject.yangutils.translator.tojava.utils;
18
19import org.onosproject.yangutils.datamodel.LocationInfo;
20import org.onosproject.yangutils.datamodel.YangNode;
21import org.onosproject.yangutils.datamodel.YangSchemaNode;
22import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
23import org.onosproject.yangutils.translator.tojava.TempJavaBeanFragmentFiles;
24import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
25import org.onosproject.yangutils.translator.tojava.TempJavaTypeFragmentFiles;
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053026
27import java.io.IOException;
28
Bharat saraswal9fab16b2016-09-23 23:27:24 +053029import static org.onosproject.yangutils.utils.UtilConstants.AT;
30import static org.onosproject.yangutils.utils.UtilConstants.IN;
31
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053032/**
33 * Represents common translator utilities.
34 */
35public final class TranslatorUtils {
36
Gaurav Agrawal3b57f362016-09-07 13:16:35 +053037 // 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 saraswal54e4bab2016-10-05 23:32:14 +0530121 public static TempJavaTypeFragmentFiles getTypeFiles(YangNode curNode) {
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530122 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 Agrawal3b57f362016-09-07 13:16:35 +0530132 * @return default constructor for class
133 * @throws IOException when fails to append to file
134 */
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530135 static String addDefaultConstructor(YangNode node, String modifier,
136 String toAppend)
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530137 throws IOException {
138 return ((TempJavaCodeFragmentFilesContainer) node)
139 .getTempJavaCodeFragmentFiles()
Bharat saraswal9fab16b2016-09-23 23:27:24 +0530140 .addDefaultConstructor(modifier, toAppend);
Gaurav Agrawal3b57f362016-09-07 13:16:35 +0530141 /*
142 * TODO update addDefaultConstructor, it doesn't need YANG node as an
143 * input.
144 */
145 }
146}