4.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.

4.4.1.1. Namespaces

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

Listing 4.19 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 4.20 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 4.21 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 4.22 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 4.23 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 4.24 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">

4.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 4.21.

4.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.

4.4.1.3.1. Generic elements

Table 4.18 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.

4.4.1.3.2. Java elements

Table 4.19 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.

4.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.

4.4.1.5. Code instruction base

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

Listing 4.25 <code instruction base> code example
 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
<code_instruction>
        ...
        foreach = "package|object|attribute|reference| object.attribute|object.reference|
                   currentModelPackage.property|currentModelObject.property|
                   currentModelAttribute.property|currentModelReference.property"
        condition = ""
        package = ""
        object = ""
        var0 = ""
        var1 = ""
        var2 = ""
        var3 = ""
        var4 = ""
        var5 = ""
        var6 = ""
        var7 = ""
        var8 = ""
        var9 = ""
        var10 = ""
        var11 = ""
        var12 = ""
        var13 = ""
        var14 = ""
        var15 = ""
        var16 = ""
        var17 = ""
        var18 = ""
        var19 = ""
        var20 = ""
        ...
</code_instruction>
Table 4.20 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 4.26 <code instruction properties> code example
1
2
3
4
5
6
7
8
9
<code_instruction>
        ...
        <properties>
                <yourProperty1>yourPropertyValue1</yourProperty1>
                <yourProperty2>yourPropertyValue2</yourProperty2>
                ...
        </properties>
        ...
</code_instruction>