This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
/
CppEXICodeGen.java
362 lines (307 loc) · 12.3 KB
/
CppEXICodeGen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/** 22.03.2012 19:11 */
package fabric.module.exi.cpp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Properties;
import java.util.Queue;
import java.util.ArrayList;
import java.util.LinkedList;
import de.uniluebeck.sourcegen.Workspace;
import de.uniluebeck.sourcegen.c.CCommentImpl;
import de.uniluebeck.sourcegen.c.CFun;
import de.uniluebeck.sourcegen.c.CFunSignature;
import de.uniluebeck.sourcegen.c.CParam;
import de.uniluebeck.sourcegen.c.CppSourceFile;
import fabric.wsdlschemaparser.schema.FElement;
import fabric.module.exi.FabricEXIModule;
import fabric.module.exi.base.EXICodeGen;
import fabric.module.exi.java.FixValueContainer.ElementData;
import fabric.module.exi.java.FixValueContainer.ArrayData;
import fabric.module.exi.java.FixValueContainer.ListData;
/**
* EXI code generator for C++.
*
* @author seidel
*/
public class CppEXICodeGen implements EXICodeGen
{
/** Logger object */
private static final Logger LOGGER = LoggerFactory.getLogger(CppEXICodeGen.class);
/** Workspace object for source code write-out */
private Workspace workspace;
/** Properties object with module configuration */
private Properties properties;
/** Name of the bean class */
private String beanClassName;
/** Name of the EXI de-/serializer class */
private String serializerClassName;
/** Name of the main application */
private String applicationName;
/** Source file with main application */
private CppSourceFile application;
/** Queue with metadata of XML elements */
private Queue<ElementMetadata> elementMetadata;
/** Factory to build schema-informed EXI grammars */
private EXISchemaInformedGrammarFactory grammarFactory;
/**
* Constructor sets various class names and creates source
* file for main application to do EXI serialization and
* deserialization.
*
* @param workspace Workspace object for source code write-out
* @param properties Properties object with module options
*/
public CppEXICodeGen(Workspace workspace, Properties properties) throws Exception
{
this.workspace = workspace;
this.properties = properties;
this.beanClassName = this.properties.getProperty(FabricEXIModule.MAIN_CLASS_NAME_KEY);
this.serializerClassName = "EXIConverter";
// Create source file for application
this.applicationName = this.properties.getProperty(FabricEXIModule.APPLICATION_CLASS_NAME_KEY);
this.application = this.createMainApplication(this.applicationName);
this.elementMetadata = new LinkedList<ElementMetadata>();
// Create factory for schema-informed EXI grammars
this.grammarFactory = new EXISchemaInformedGrammarFactory();
}
/**
* Generate code to serialize and deserialize Bean objects with EXI.
* The method arguments are used in the JavaEXICodeGen and should
* not be relevant for the CppEXICodeGen implementation.
*
* @param fixElements XML elements, where value-tags need to be fixed
* @param fixArrays XML arrays, where value-tags need to be fixed
* @param fixLists XML lists, where value-tags need to be fixed
*
* @throws Exception Error during code generation
*/
@Override
public void generateCode(ArrayList<ElementData> fixElements,
ArrayList<ArrayData> fixArrays,
ArrayList<ListData> fixLists) throws Exception
{
/*****************************************************************
* Create callback method that writes EXI stream to file
*****************************************************************/
CFun outputStream = this.generateOutputStreamFunction();
if (null != outputStream)
{
this.application.add(outputStream);
// Define name of EXI outfile
this.application.addBeforeDirective(String.format("define OUTFILE_NAME \"%s_serialized.exi\"", this.beanClassName.toLowerCase()));
}
/*****************************************************************
* Create callback method that reads EXI stream from file
*****************************************************************/
CFun inputStream = this.generateInputStreamFunction();
if (null != inputStream)
{
this.application.add(inputStream);
// Define name of EXI infile
this.application.addBeforeDirective(String.format("define INFILE_NAME \"%s_serialized.exi\"", this.beanClassName.toLowerCase()));
}
/*****************************************************************
* Create class and method calls for EXI converter
*****************************************************************/
CppEXIConverter exiConverter = new CppEXIConverter(this.properties);
// Create EXI de-/serializer class
exiConverter.generateSerializerClass(this.workspace, this.elementMetadata);
// Create method for EXI serialization
CFun exiSerialize = exiConverter.generateSerializeCall();
if (null != exiSerialize)
{
this.application.add(exiSerialize);
}
// Create method for EXI deserialization
CFun exiDeserialize = exiConverter.generateDeserializeCall();
if (null != exiDeserialize)
{
this.application.add(exiDeserialize);
}
/*****************************************************************
* Create main function for application
*****************************************************************/
CFun main = this.createMainFunction();
if (null != main)
{
this.application.add(main);
}
}
/**
* Create source file and write it to language-specific workspace.
* In CppEXICodeGen this is entirely done in the generateCode()
* method, because C++ main applications are no classes, but
* contain procedural code.
*
* @throws Exception Error during source file write-out
*/
@Override
public void writeSourceFile() throws Exception
{
// Empty implementation
}
/**
* Handle top level element from XML Schema document,
* i.e. build EXI grammar for element.
*
* @param element Top level element to handle
*/
@Override
public void handleTopLevelElement(final FElement element)
{
// Collect data of elements with simple type
if (element.getSchemaType().isSimple())
{
ElementMetadata d = new ElementMetadata(element);
this.elementMetadata.add(d);
// Build grammar
grammarFactory.addGlobalElement(d);
}
}
/**
* Handle local element from XML Schema document,
* i.e. build EXI grammar for element.
*
* @param element Local element to handle
* @param parentName Name of parent XML element
*/
@Override
public void handleLocalElement(final FElement element, final String parentName)
{
// Collect data of elements with simple type
if (element.getSchemaType().isSimple())
{
ElementMetadata d = new ElementMetadata(element, parentName);
this.elementMetadata.add(d);
// Build grammar
grammarFactory.addLocalElement(d);
}
}
/**
* Create main application in the C++ workspace and return
* source file. This method adds all necessary comments,
* includes and namespaces to the file.
*
* @param applicationName Desired application name
*
* @return CppSourceFile with main application
*
* @throws Exception Error during code generation
*/
private CppSourceFile createMainApplication(final String applicationName) throws Exception
{
CppSourceFile cppsf = workspace.getC().getCppSourceFile(applicationName);
cppsf.setComment(new CCommentImpl("The EXI application's main file."));
LOGGER.debug(String.format("Generated application source file '%s'.", cppsf.getFileName()));
// Add includes and namespace
cppsf.addLibInclude("cstdlib");
cppsf.addLibInclude("cstdio");
cppsf.addLibInclude("iostream");
cppsf.addInclude(this.beanClassName + ".hpp");
cppsf.addInclude("EXIStream.hpp");
cppsf.addInclude(this.serializerClassName + ".hpp");
cppsf.addUsingNamespace("std");
return cppsf;
}
/**
* Create callback method that is used as a function pointer,
* when the EXI converter wants to write data to an output
* stream.
*
* @return CFun with definition of callback method
*
* @throws Exception Error during code generation
*/
private CFun generateOutputStreamFunction() throws Exception
{
CParam buffer = CParam.factory.create("void*", "buffer");
CParam readSize = CParam.factory.create("mySize_t", "readSize");
CFunSignature cfs = CFunSignature.factory.create(buffer, readSize);
CFun writeFileOutputStream = CFun.factory.create("writeFileOutputStream", "mySize_t", cfs);
String methodBody =
"FILE *outfile = fopen(OUTFILE_NAME, \"wb\");\n\n" +
"mySize_t result = fwrite(buffer, 1, readSize, outfile);\n" +
"fclose(outfile);\n\n" +
"return result;";
writeFileOutputStream.appendCode(methodBody);
writeFileOutputStream.setComment(new CCommentImpl("Write EXI stream to an output file."));
return writeFileOutputStream;
}
/**
* Create callback method that is used as a function pointer,
* when the EXI converter wants to read data from an input
* stream.
*
* @return CFun with definition of callback method
*
* @throws Exception Error during code generation
*/
private CFun generateInputStreamFunction() throws Exception
{
CParam buffer = CParam.factory.create("void*", "buffer");
CParam readSize = CParam.factory.create("mySize_t", "readSize");
CFunSignature cfs = CFunSignature.factory.create(buffer, readSize);
CFun readFileInputStream = CFun.factory.create("readFileInputStream", "mySize_t", cfs);
String methodBody =
"FILE *infile = fopen(INFILE_NAME, \"rb\");\n\n" +
"mySize_t result = fread(buffer, 1, readSize, infile);\n" +
"fclose(infile);\n\n" +
"return result;";
readFileInputStream.appendCode(methodBody);
readFileInputStream.setComment(new CCommentImpl("Read EXI stream from an input file."));
return readFileInputStream;
}
/**
* Create main function for application. The method initializes
* the root container and an EXIStream object, so that one can
* easily test the C++ EXI module or create a new application
* based on the source file's code.
*
* @return CFun with definition of the main function
*
* @throws Exception Error during code generation
*/
private CFun createMainFunction() throws Exception
{
CParam argc = CParam.factory.create("int", "argc");
CParam argv = CParam.factory.create("char*", "argv[]");
CFunSignature mainSignature = CFunSignature.factory.create(argc, argv);
CFun mainMethod = CFun.factory.create("main", "int", mainSignature);
mainMethod.setComment(new CCommentImpl("Main function of the application."));
String methodBody = String.format(
"%s* %s = new %s();\n" +
"%s* exiConverter = new %s();\n" +
"EXIStream exiStream;\n\n" +
"try {\n" +
"\t// TODO: Add your custom initialization code here\n\n" +
"\t// Serialize bean object to EXI stream\n" +
"\ttoEXIStream(exiConverter, %s, &exiStream, writeFileOutputStream);\n\n" +
"\t// Deserialize bean object from EXI stream\n" +
"\tfromEXIStream(exiConverter, %s, &exiStream, readFileInputStream);\n" +
"}\n" +
"catch (const char* e) {\n" +
"\tcout << e << endl;\n" +
"}\n\n" +
"delete %s;\n" +
"delete exiConverter;\n\n" +
"return EXIT_SUCCESS;",
this.firstLetterCapital(this.beanClassName), this.beanClassName.toLowerCase(),
this.firstLetterCapital(this.beanClassName), this.serializerClassName,
this.serializerClassName, this.beanClassName.toLowerCase(),
this.beanClassName.toLowerCase(), this.beanClassName.toLowerCase());
mainMethod.appendCode(methodBody);
return mainMethod;
}
/**
* Private helper method to capitalize the first letter of a string.
* Function will return null, if argument was null.
*
* @param text Text to process
*
* @return Text with first letter capitalized or null
*/
private String firstLetterCapital(final String text)
{
return (null == text ? null : text.substring(0, 1).toUpperCase() + text.substring(1, text.length()));
}
}