Skip to content

fuinorg/srcgen4j-core

Repository files navigation

srcgen4j-core

Source code generation for Java (Core)

Java Maven Build Coverage Status Maven Central LGPLv3 License Java Development Kit 11

Versions

  • 0.4.3 (or later) = Java 11 before namespace change from 'javax' to 'jakarta'
  • 0.4.2 (or previous) = Java 8

What is this?

The project provides some parsers and generators based on the (srcgen4j-common) project.

XtextParser

The parser is configured with the path where the model files with a dedicated extension can be found. The setup class attribute is used to instantiate the Xtext) parser itself.

<parser name="ptp" class="org.fuin.srcgen4j.core.xtext.XtextParser">
    <config>
        <xtext:xtext-parser-config modelPath="${testRes}" modelExt="xsdsl"
               setupClass="org.fuin.xsample.XSampleDslStandaloneSetup" />
    </config>
</parser>

A full blown example for the Xtext based DDD DSL can be found here.

EMFGenerator

The EMF generator requires setting up the different artifact factories that generate code for different EMF model elements.

<generator name="gen1" class="org.fuin.srcgen4j.core.emf.EMFGenerator" parser="ptp" project="current">
    <config>
        <emf:emf-generator-config>
            <emf:artifact-factory artifact="abstractHello" class="org.fuin.srcgen4j.core.emf.AbstractHelloTstGen">
                <variable name="package" value="a.b.c" />
            </emf:artifact-factory>
        </emf:emf-generator-config>
    </config>
    <artifact name="abstractHello" folder="testGenMainJava" />
</generator>

You can also define local variables that will be provided to the artifact factory.

ParameterizedTemplateParser

The parser is configured with the path where the model files can be found.

<parser name="ptp" class="org.fuin.srcgen4j.core.velocity.ParameterizedTemplateParser">
    <config>
        <velo:parameterized-template-parser modelPath="${testRes}" 
                                            modelFilter=".*\.ptg\.xml"
                                            templatePath="${testRes}" 
                                            templateFilter=".*\.ptg\.java" />
    </config>
</parser>

A model element always consists of two parts: An XML definition and a velocity template for code generation.

An example template definition (parameterized-template-1.ptg.xml):

<parameterized-template template="parameterized-template-1.ptg.java" xmlns="http://www.fuin.org/srcgen4j/core/velocity">
    
    <!-- Variables that can be used in the velocity template -->
    
    <arguments>
        <argument key="name" value="-" />
        <argument key="pkg" value="-" />
    </arguments>

    <!-- Files to be generated with constant values for the above defined variables -->
        
    <target-file path="a" name="A.java">
        <argument key="name" value="A" />
        <argument key="pkg" value="a" />
    </target-file>
    
    <target-file path="b" name="B.java">
        <argument key="name" value="B" />
        <argument key="pkg" value="b" />
    </target-file>
    
</parameterized-template>

An example velocity template (parameterized-template-1.ptg.java):

package ${pkg};

public class ${name} {
    // Whatever
}

It's also possible to create the variable values programmatically (See TestTFLProducer):

<parameterized-template template="parameterized-template-2.ptg.java" xmlns="http://www.fuin.org/srcgen4j/core/velocity">
    
    <arguments>
        <argument key="name" value="-" />
        <argument key="pkg" value="-" />
    </arguments>

    <target-file-list-producer class="org.fuin.srcgen4j.core.velocity.TestTFLProducer" />
    
</parameterized-template>

ParameterizedTemplateGenerator

The generator is simply configured with the path to the to the velocity templates (See topic Resource Management / file.resource.loader.path). xxx

<generator name="gen1" class="org.fuin.srcgen4j.core.velocity.ParameterizedTemplateGenerator" 
           parser="ptp" project="current" folder="testJava">
    <config>
        <velo:parameterized-template-generator templatePath="${testRes}" />
    </config>
    <artifact name="file" />
</generator>

Snapshots

Snapshots can be found on the OSS Sonatype Snapshots Repository.

Add the following to your .m2/settings.xml to enable snapshots in your Maven build:

<repository>
    <id>sonatype.oss.snapshots</id>
    <name>Sonatype OSS Snapshot Repository</name>
    <url>http://oss.sonatype.org/content/repositories/snapshots</url>
    <releases>
        <enabled>false</enabled>
    </releases>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
</repository>

About

Source code generation for Java (Core)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages