|
|
|
|
|
Java Value Object
A Java Value Object might not be a real life application for SERF since there are other programs or parts of programs better suited to generate them but it serves as a good first example and a simple introduction to SERFs template script instructions.
NOTE! This example works in the pre-alpha (not released) version of SERF and the template script instructions may change in coming releases.
Object model and translation table.
The object model is a very straight forward representation of the value object and it's properties. The translation table shown below is the minimum required. In this particular example it could have been left out since there is only one target.
01: <?xml version="1.0"?>
02:
03: <value-object>
04:
05: <name>Person</name>
06: <properties>
07: <property name="name" type="string"/>
08: <property name="age" type="int"/>
09: <property name="email" type="string"/>
10: </properties>
11:
12: </value-object>
01: <?xml version="1.0"?>
02:
03: <translation-table version="1.0">
04:
05: <mappings target="java">
06: <translate value="string" to="String"/>
07: <translate value="integer" to="int"/>
08: </mappings>
09:
10: </translation-table>
Template
The template contains Java code for a public constructor (with all properties as arguments) as well as public get and set methods for each of the properties.
01: <?xml version="1.0"?>
02:
03: <template version="1.0" target="java">
04:
05: <source use="value-object">
06: <![CDATA[
07:
08: public class <?= name?> {
09: <?for-each properties/property?>
10: private <?= \translate @type?> <?= @name?>;<?end for-each?>
11:
12: public <?= name?> (<?for-each \trim:2 properties/property?>
13: <?= \translate @type?> <?= @name?>, <?end for-each?>) {
14: <?for-each properties/property?>
15: this.<?= @name?>= <?= @name?>;<?end for-each?>
16: }
17:
18: <?for-each properties/property?>
19: public get<?= \u @name?>() {
20: return <?= @name?>;
21: }
22:
23: public set<?= \u @name?>(<?= \translate @type?> <?= @name?>) {
24: this.<?= @name?>= <?= @name?>;
25: }
26: <?end for-each?>
27: }
28:
29: ]]>
30: </source>
31:
32: </template>
Person.java
This is the resulting Java source code. All of the whitespace is kept as in the template so you must think of how and where to place your template script instructions if you wish to keep the code tidy.
01: public class Person {
02:
03: private String name;
04: private int age;
05: private String email;
06:
07: public Person (
08: String name,
09: int age,
10: String email) {
11:
12: this.name= name;
13: this.age= age;
14: this.email= email;
15: }
16:
17:
18: public getName() {
19: return name;
20: }
21:
22: public setName(String name) {
23: this.name= name;
24: }
25:
26: public getAge() {
27: return age;
28: }
29:
30: public setAge(int age) {
31: this.age= age;
32: }
33:
34: public getEmail() {
35: return email;
36: }
37:
38: public setEmail(String email) {
39: this.email= email;
40: }
41:
42: }
|
|
|