Merged in f5soh/librepilot/LP-607_world_mag_model_2015v2 (pull request #526)
[librepilot.git] / ground / uavobjgenerator / generators / java / uavobjectgeneratorjava.cpp
blobb8229feecc81f1bd3524458f68da785858792938
1 /**
2 ******************************************************************************
4 * @file uavobjectgeneratorjava.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @brief produce java code for uavobjects
8 * @see The GNU Public License (GPL) Version 3
10 *****************************************************************************/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <QDebug>
28 #include "uavobjectgeneratorjava.h"
29 using namespace std;
31 bool UAVObjectGeneratorJava::generate(UAVObjectParser *parser, QString templatepath, QString outputpath)
33 fieldTypeStrCPP << "Byte" << "Short" << "Int" <<
34 "Short" << "Int" << "Long" << "Float" << "Byte";
36 fieldTypeStrCPPClass << "INT8" << "INT16" << "INT32"
37 << "UINT8" << "UINT16" << "UINT32" << "FLOAT32" << "ENUM";
39 javaCodePath = QDir(templatepath + QString(JAVA_TEMPLATE_DIR));
40 javaOutputPath = QDir(outputpath);
41 javaOutputPath.mkpath(javaOutputPath.absolutePath());
43 javaCodeTemplate = readFile(javaCodePath.absoluteFilePath("uavobject.java.template"));
44 QString javaInitTemplate = readFile(javaCodePath.absoluteFilePath("uavobjectsinit.java.template"));
46 if (javaCodeTemplate.isEmpty() || javaInitTemplate.isEmpty()) {
47 std::cerr << "Problem reading java code templates" << endl;
48 return false;
51 QString objInc;
52 QString javaObjInit;
54 for (int objidx = 0; objidx < parser->getNumObjects(); ++objidx) {
55 ObjectInfo *info = parser->getObjectByIndex(objidx);
56 process_object(info);
58 javaObjInit.append("\t\t\tobjMngr.registerObject( new " + info->name + "() );\n");
59 objInc.append("#include \"" + info->namelc + ".h\"\n");
62 // Write the gcs object inialization files
63 javaInitTemplate.replace(QString("$(OBJINC)"), objInc);
64 javaInitTemplate.replace(QString("$(OBJINIT)"), javaObjInit);
65 bool res = writeFileIfDifferent(javaOutputPath.absolutePath() + "/UAVObjectsInitialize.java", javaInitTemplate);
66 if (!res) {
67 cout << "Error: Could not write output files" << endl;
68 return false;
71 return true; // if we come here everything should be fine
75 /**
76 * Generate the java object files
78 bool UAVObjectGeneratorJava::process_object(ObjectInfo *info)
80 if (info == NULL) {
81 return false;
84 // Prepare output strings
85 QString outInclude = javaIncludeTemplate;
86 QString outCode = javaCodeTemplate;
88 // Replace common tags
89 replaceCommonTags(outInclude, info);
90 replaceCommonTags(outCode, info);
92 // Replace the $(DATAFIELDS) tag
93 QString type;
94 QString fields;
95 for (int n = 0; n < info->fields.length(); ++n) {
96 // Determine type
97 type = fieldTypeStrCPP[info->fields[n]->type];
98 // Append field
99 if (info->fields[n]->numElements > 1) {
100 fields.append(QString(" %1 %2[%3];\n").arg(type).arg(info->fields[n]->name)
101 .arg(info->fields[n]->numElements));
102 } else {
103 fields.append(QString(" %1 %2;\n").arg(type).arg(info->fields[n]->name));
106 outInclude.replace(QString("$(DATAFIELDS)"), fields);
108 // Replace the $(FIELDSINIT) tag
109 QString finit;
110 for (int n = 0; n < info->fields.length(); ++n) {
111 finit.append("\n");
113 // Setup element names
114 QString varElemName = info->fields[n]->name + "ElemNames";
115 finit.append(QString("\t\tList<String> %1 = new ArrayList<String>();\n").arg(varElemName));
116 QStringList elemNames = info->fields[n]->elementNames;
117 for (int m = 0; m < elemNames.length(); ++m) {
118 finit.append(QString("\t\t%1.add(\"%2\");\n")
119 .arg(varElemName)
120 .arg(elemNames[m]));
123 // Only for enum types
124 if (info->fields[n]->type == FIELDTYPE_ENUM) {
125 QString varOptionName = info->fields[n]->name + "EnumOptions";
126 finit.append(QString("\t\tList<String> %1 = new ArrayList<String>();\n").arg(varOptionName));
127 QStringList options = info->fields[n]->options;
128 for (int m = 0; m < options.length(); ++m) {
129 finit.append(QString("\t\t%1.add(\"%2\");\n")
130 .arg(varOptionName)
131 .arg(options[m]));
133 finit.append(QString("\t\tfields.add( new UAVObjectField(\"%1\", \"%2\", UAVObjectField.FieldType.ENUM, %3, %4) );\n")
134 .arg(info->fields[n]->name)
135 .arg(info->fields[n]->units)
136 .arg(varElemName)
137 .arg(varOptionName));
139 // For all other types
140 else {
141 finit.append(QString("\t\tfields.add( new UAVObjectField(\"%1\", \"%2\", UAVObjectField.FieldType.%3, %4, null) );\n")
142 .arg(info->fields[n]->name)
143 .arg(info->fields[n]->units)
144 .arg(fieldTypeStrCPPClass[info->fields[n]->type])
145 .arg(varElemName));
148 outCode.replace(QString("$(FIELDSINIT)"), finit);
150 // Replace the $(DATAFIELDINFO) tag
151 QString name;
152 QString enums;
153 for (int n = 0; n < info->fields.length(); ++n) {
154 enums.append(QString(" // Field %1 information\n").arg(info->fields[n]->name));
155 // Only for enum types
156 if (info->fields[n]->type == FIELDTYPE_ENUM) {
157 enums.append(QString(" /* Enumeration options for field %1 */\n").arg(info->fields[n]->name));
158 enums.append(" typedef enum { ");
159 // Go through each option
160 QStringList options = info->fields[n]->options;
161 for (int m = 0; m < options.length(); ++m) {
162 QString s = (m != (options.length() - 1)) ? "%1_%2=%3, " : "%1_%2=%3";
163 enums.append(s.arg(info->fields[n]->name.toUpper())
164 .arg(options[m].toUpper().replace(QRegExp(ENUM_SPECIAL_CHARS), ""))
165 .arg(m));
167 enums.append(QString(" } %1Options;\n")
168 .arg(info->fields[n]->name));
170 // Generate element names (only if field has more than one element)
171 if (info->fields[n]->numElements > 1 && !info->fields[n]->defaultElementNames) {
172 enums.append(QString(" /* Array element names for field %1 */\n").arg(info->fields[n]->name));
173 enums.append(" typedef enum { ");
174 // Go through the element names
175 QStringList elemNames = info->fields[n]->elementNames;
176 for (int m = 0; m < elemNames.length(); ++m) {
177 QString s = (m != (elemNames.length() - 1)) ? "%1_%2=%3, " : "%1_%2=%3";
178 enums.append(s.arg(info->fields[n]->name.toUpper())
179 .arg(elemNames[m].toUpper())
180 .arg(m));
182 enums.append(QString(" } %1Elem;\n")
183 .arg(info->fields[n]->name));
185 // Generate array information
186 if (info->fields[n]->numElements > 1) {
187 enums.append(QString(" /* Number of elements for field %1 */\n").arg(info->fields[n]->name));
188 enums.append(QString(" static const quint32 %1_NUMELEM = %2;\n")
189 .arg(info->fields[n]->name.toUpper())
190 .arg(info->fields[n]->numElements));
193 outInclude.replace(QString("$(DATAFIELDINFO)"), enums);
195 // Replace the $(INITFIELDS) tag
196 QString initfields;
197 for (int n = 0; n < info->fields.length(); ++n) {
198 if (!info->fields[n]->defaultValues.isEmpty()) {
199 // For non-array fields
200 if (info->fields[n]->numElements == 1) {
201 if (info->fields[n]->type == FIELDTYPE_ENUM) {
202 initfields.append(QString("\t\tgetField(\"%1\").setValue(\"%2\");\n")
203 .arg(info->fields[n]->name)
204 .arg(info->fields[n]->defaultValues[0]));
205 } else if (info->fields[n]->type == FIELDTYPE_FLOAT32) {
206 initfields.append(QString("\t\tgetField(\"%1\").setValue(%2);\n")
207 .arg(info->fields[n]->name)
208 .arg(info->fields[n]->defaultValues[0].toFloat()));
209 } else {
210 initfields.append(QString("\t\tgetField(\"%1\").setValue(%2);\n")
211 .arg(info->fields[n]->name)
212 .arg(info->fields[n]->defaultValues[0].toInt()));
214 } else {
215 // Initialize all fields in the array
216 for (int idx = 0; idx < info->fields[n]->numElements; ++idx) {
217 if (info->fields[n]->type == FIELDTYPE_ENUM) {
218 initfields.append(QString("\t\tgetField(\"%1\").setValue(\"%3\",%2);\n")
219 .arg(info->fields[n]->name)
220 .arg(idx)
221 .arg(info->fields[n]->defaultValues[idx]));
222 } else if (info->fields[n]->type == FIELDTYPE_FLOAT32) {
223 initfields.append(QString("\t\tgetField(\"%1\").setValue(%3,%2);\n")
224 .arg(info->fields[n]->name)
225 .arg(idx)
226 .arg(info->fields[n]->defaultValues[idx].toFloat()));
227 } else {
228 initfields.append(QString("\t\tgetField(\"%1\").setValue(%3,%2);\n")
229 .arg(info->fields[n]->name)
230 .arg(idx)
231 .arg(info->fields[n]->defaultValues[idx].toInt()));
238 outCode.replace(QString("$(INITFIELDS)"), initfields);
240 // Write the java code
241 bool res = writeFileIfDifferent(javaOutputPath.absolutePath() + "/" + info->name + ".java", outCode);
242 if (!res) {
243 cout << "Error: Could not write gcs output files" << endl;
244 return false;
247 return true;