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