5.4.1. Introduction

Writing any code instructions in MetaFactory starts in XML.

XML provides structure, consistency and out of the box functionality that works great, especially in generating Java and XML code. It also does have its limitations, which is why we recommend to complement your XML Code Instructions with Freemarker or Velocity snippets. This feature is extensively addressed in Code Instructions with snippets.

We assume that you already know about the structure of a MetaFactory project and how the file codeinstruction.xml is the base of all Code Instructions that you will write.

Glasses M MF Components and Structure.

5.4.1.1. Namespaces

We are in the transition of changing namespaces. These are currently used:

Listing 5.20 Project namespace
<personal-iom-project
        xmlns="http://www.firstbase.nl/xsd/personaliom/project"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.firstbase.nl/xsd/personaliom/project
                            http://www.firstbase.nl/xsd/personaliom/project.xsd"
        allowDeprecated="false">
Listing 5.21 Model namespace
<model
        xmlns="https://metafactory.io/xsd/v1/model"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://metafactory.io/xsd/v1/model
                            https://metafactory.io/xsd/v1/model.xsd">
Listing 5.22 Code instruction namespace
<code_instruction
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="https://metafactory.io/xsd/v1/codeinstruction"
        xsi:schemaLocation="https://metafactory.io/xsd/v1/codeinstruction
                            https://metafactory.io/xsd/v1/codeinstruction.xsd
                            https://metafactory.io/xsd/v1/java-codeinstruction
                            https://metafactory.io/xsd/v1/java-codeinstruction.xsd
                            https://metafactory.io/xsd/v1/typescript-codeinstruction
                            https://metafactory.io/xsd/v1/typescript-codeinstruction.xsd"
        xmlns:typescript="https://metafactory.io/xsd/v1/typescript-codeinstruction"
        xmlns:java="https://metafactory.io/xsd/v1/java-codeinstruction">
Listing 5.23 Pattern namespace (deprecating)
<pattern
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.firstbase.nl/xsd/personaliom/pattern"
        xsi:schemaLocation="http://www.firstbase.nl/xsd/personaliom/pattern
                            http://www.firstbase.nl/xsd/personaliom/pattern.xsd">
Listing 5.24 Java_package namespace
<java_package
        xmlns="https://metafactory.io/xsd/v1/java-codeinstruction"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://metafactory.io/xsd/v1/java-codeinstruction
                            https://metafactory.io/xsd/v1/java-codeinstruction.xsd">
Listing 5.25 Typescript namespace
<typescript
        xmlns="https://metafactory.io/xsd/v1/typescript-codeinstruction"
        xsi:schemaLocation="https://metafactory.io/xsd/v1/typescript-codeinstruction
                            https://metafactory.io/xsd/v1/typescript-codeinstruction.xsd">

5.4.1.2. The root element

Up till now the main Code Instruction file in MetaFactory was pattern.xml. It is validated by pattern.xsd and always begins with <pattern> as root element in the http://www.firstbase.nl/xsd/personaliom/pattern namespace.

Exclam M From now on all new implementations of MetaFactory will use a code_instruction root element in the namespace of Listing 5.22.

5.4.1.3. Child elements

Naturally MetaFactory requires a lot more instructions to generate code, which you can define through one or more child elements. From here you have two choices. Either define your code instructions directly within the root element or choose to include other code instruction files that contain one or more children of the root element. Note that these other code instruction files must be linked to the same xsd as codeinstruction.xml.

Below is a list of all children elements and a short explanation on how you should use them. More details can be obtained from the linked pages.

5.4.1.3.1. Generic elements

Table 5.27 Generic elements

Name

Description

<properties>

Use this element to define any variables or constants that you want to use in your pattern. Inside this element, define properties by creating new elements, e.g. <java.main.directory> src/main/java </java.main.directory> and fill them. You can later use this property elsewhere in the pattern with the following syntax: “${pattern.property.java.main.directory}“.

<file>

This element is a wrapper for the content of any file you would want to generate. It has attributes such as ‘name’ (name = “somename.html”), ‘path’ (path = “src/main/webapp”) and ‘package’. The latter refers to the package of model.xml that is currently active in MetaFactory. Please note that strings have been used for clarity here but it is better to refer to the pattern properties you have defined instead.

<xml>

This element is a wrapper for code instructions to generate XML files. It has different attributes than the <file> or <package> element. Loupe S XML files.

<freemarker>

Inside this element your can define namespaces that allow you to preset paths to your Freemarker snippet folders. Metafactory recommends to use Freemarker snippets because Freemarker has more advanced features than Velocity.

<velocity>

Inside this element your can define namespaces that allow you to preset paths to your Velocity snippet folders.

<externalLibraries>

An external library is a means by which code instructions and snippets may be shared between different MetaFactory projects. It can be used, for example, to make a library with code instructions for web applications and a library with code instructions and snippets for Swing applications.

<external.package>

An external package functions like an include. The usage of external package is to include a code instruction from a library with code instructions. Very useful when you wish to use your code instructions in multiple projects.

5.4.1.3.2. Java elements

Table 5.28 Java elements

Name

Description

<java_package>

This element marks the output location of a Java package.

It has attributes such as ‘name‘ (name = “org.yourdomain.yourapp.service“), ‘path‘ (path = “src/main/java“) and ‘package‘. The latter refers to “domain_model” of model.xml that is currently active in MetaFactory. Please note that strings have been used for clarity here but it’s better to refer to the pattern properties you have defined.

<class>

This element marks a Java class and is a child of the package element.

It has a ‘name’ and ‘condition’ attribute, both of which accept MetaFactory expression language.

<interface>

This element marks a Java interface and is a child of the package element.

It has a ‘name’ and ‘condition’ attribute, both of which accept MetaFactory expression language.

<field>

This element marks a Java property and is a child of either the class or interface element.

It has a ‘name’ and ‘condition’ attribute, both of which accept MetaFactory expression language.

<method>

This element marks a Java method and is a child of either the class or interface element.

It has a ‘name’ and ‘condition’ attribute, both of which accept MetaFactory expression language.

5.4.1.4. Complement XML patterns with snippets

We’ve already touched the subject of snippets briefly in the explanations of the Freemarker and Velocity elements. What we have not yet expressed is how powerful snippets can be for your patterns. When you have become comfortable writing some simple patterns without using snippets, then it will be time to unveil the power of snippets.

5.4.1.5. Code instruction base

This set of attributes can be applied for all code instruction elements.

Listing 5.26 <code instruction base> code example
 1<code_instruction>
 2        ...
 3        foreach = "package|object|attribute|reference| object.attribute|object.reference|
 4                   currentModelPackage.property|currentModelObject.property|
 5                   currentModelAttribute.property|currentModelReference.property"
 6        condition = ""
 7        package = ""
 8        object = ""
 9        var0 = ""
10        var1 = ""
11        var2 = ""
12        var3 = ""
13        var4 = ""
14        var5 = ""
15        var6 = ""
16        var7 = ""
17        var8 = ""
18        var9 = ""
19        var10 = ""
20        var11 = ""
21        var12 = ""
22        var13 = ""
23        var14 = ""
24        var15 = ""
25        var16 = ""
26        var17 = ""
27        var18 = ""
28        var19 = ""
29        var20 = ""
30        ...
31</code_instruction>
Table 5.29 code instruction base

Name

Description

foreach

Indicates whether the package must be created for each element in model (value of foreach determines what model element). Possible values: “package|object|attribute|reference”

package

This attribute points to the package element of the model.

object

The name of the model element used to apply the foreach attribute.

condition

An expression that evaluates to true or false. Generation of the current file wil precede if true and otherwise not.

condition="${object.name}=Person"

In this case, the package is generated only if the current model object has the name: Person. This works only if all model objects are iterated (foreach=”object”).

var0 - var20

Value that you wish to store in the pre-defined variable with the name var0..var20. Reference to this value can be made later by means of: ${var0}. This can be used in both a code instruction and in a freemarker or velocity snippet.

Defining properties

Repetition of code instructions can be prevented by defining code instruction properties (AKA pattern properties). They are not required but it is recommended to use them.

You can define zero or more properties and use a dot notation to indicate hierarchy. Properties are defined inside the XML tag <properties> while each property child has its own name tag with with enclosed content. In the example 2 properties are defined: “yourProperty1 and yourProperty2” with values “yourPropertyValue1 and yourPropertyValue2”.

Listing 5.27 <code instruction properties> code example
1<code_instruction>
2        ...
3        <properties>
4                <yourProperty1>yourPropertyValue1</yourProperty1>
5                <yourProperty2>yourPropertyValue2</yourProperty2>
6                ...
7        </properties>
8        ...
9</code_instruction>