XML model component

This tutorial learn to you create XML component with XML model. It is able to generate a Java UserBean class with XML mapping Hibernate. Template used has doc parameter type of XML (the Hibernate mapping).

You can find next sample into usecases-catalog/Components/Model/XMLToJavaClass.akgncpt.xml catalog.

With XML mapping Hibernate :

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
    
<hibernate-mapping>

<class 
    name="net.sourceforge.akrogen.bean.UserBean" 
    table="T_USER"
>

    <id
        name="id"
        type="java.lang.Integer"
        column="USR_ID_N"
    >
        <generator class="identity" />
    </id>

    <property
        name="name"
        type="java.lang.String"
        column="USR_NAME_C"
        not-null="true"
        length="100"
    />
    <property
        name="lastName"
        type="java.lang.String"
        column="USR_LASTNAME_C"
        not-null="true"
        length="100"
    />
    <property
        name="birthday"
        type="java.util.Date"
        column="USR_BIRTHDAY_D"
        length="23"
    />
       
</class>
</hibernate-mapping>

We wish genrate class Java :

package net.sourceforge.akrogen.bean;

public class UserBean {

  private java.lang.Integer id; 
  private java.lang.String name;        
  private java.lang.String lastName;    
  private java.util.Date birthday;      

  public java.lang.Integer getId() {
          return this.id;
  }
  public void setId (java.lang.Integer id) {
          this.id = id;
  }
  public java.lang.String getName() {
          return this.name;
  }
  public void setName (java.lang.String name) {
          this.name = name;
  }
  public java.lang.String getLastName() {
          return this.lastName;
  }
  public void setLastName (java.lang.String lastName) {
          this.lastName = lastName;
  }
  public java.util.Date getBirthday() {
          return this.birthday;
  }
  public void setBirthday (java.util.Date birthday) {
          this.birthday = birthday;
  }
}  

By using this Wizard page :

Steps of this tutorial are :

Template

Here Freemarker template used to generate UserBean Java class :

<#assign classElement = doc["hibernate-mapping"].class />
package ${package};

public class ${className} {

<#list classElement.id as item>
    private ${item.@type} ${item.@name};        
</#list>        
<#list classElement.property as item>
    private ${item.@type} ${item.@name};        
</#list>        

<#list classElement.id as item>
    public ${item.@type} get${item.@name?cap_first}() {
            return this.${item.@name};
    }
    public void set${item.@name?cap_first} (${item.@type} ${item.@name}) {
            this.${item.@name} = ${item.@name};
    }
</#list>        
<#list classElement.property as item>
    public ${item.@type} get${item.@name?cap_first}() {
            return this.${item.@name};
    }
    public void set${item.@name?cap_first} (${item.@type} ${item.@name}) {
            this.${item.@name} = ${item.@name};
    }
</#list>        

}

Template has 3 parameters :

  • parameter doc type of XML which is Hibernate mapping.
  • parameter package type of String which is the class package.
  • parameter className type of String which is class name.

XML component

Here XML component which we must use :

<?xml version="1.0" encoding="UTF-8"?>
<component>
  <models>
    <!-- This component requires selection of XML File which is used into 
         Template context (Output) -->
    <model type="xml" key="doc" useIntoOutput="true" omitDocumentType="true" />
  </models>
  <input>
    <page title="XML context/Freemarker" >
      <description>Transform XML context to Java Class.</description>
      <box flex="1" id="" orient="vertical"
          xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">                                
        <hbox>
          <label value="Package :" />
          <textbox id="package" flex="1" />
          <button type="package" target="package" label="Browse..." />
        </hbox>                         
        <hbox>
          <label value="Class name :" />
          <textbox id="className" flex="1" />
        </hbox>                         
        <hbox>
          <label value="Output Base dir :" />
          <textbox id="outputBaseDir" flex="1" />
          <button type="folder" target="outputBaseDir" label="Browse..." />
        </hbox>
        <hbox>
          <label value="Output File name :" />
          <textbox id="outputFileName" flex="1" />
        </hbox> 
        <hbox>
            <button type="preview" label="Preview..." />
        </hbox>                 
      </box>                    
    </page>
  </input>
  <output>
    <file>
      <template uri="/ftl/Xml/XMLToJavaClass.ftl" />
    </file>
  </output>
</component>

Create XMLToJavaClass.akgncpt.xml XML component file into Components/Model/ directory of the MyAkrogenCatalog catalog.

Here explanation about this XML component :

model element

Model element define model to use into template (useIntoOutput="true"). You can use this model to generate XUL Dynamic Wizard page (with useIntoInput="true").

Here model is file type of type="xml" (Hibernate mapping) which is putted with key key="doc".

omitDocumentType="true" omit document type (DTD, Schema) linked to XML file when this document must be loaded. So you can load it without having DTD,... and without loading it of Internet (the XML loading is optimized in this case).

Input/page element

input/page element describes Wizard page eclipse :

  • XUL textbox with package id is the package template parameter.
  • XUL button type of package display a button which is able to open package selection dialog. Once package is selected, the XUL textbox package is updated, because XUL button define target="package".
  • XUL textbox with className id is the class name template parameter.

XML component use

Refresh catalog View. XMLToJavaClass must be appear into catalog.

Double click on XMLToJavaClass component to open Wizard page. This Wizard page is displayed before displaying XML component, because it required XML file (Hibernate mapping) :

Select Hibernate mapping XML file and click on Next button to display XML component Wizard page :

Fill fields of Wizard page :

  • Package: net.sourceforge.akrogen.bean
  • Class name: UserBean
  • Output base dir: src/net/sourceforge/akrogen/bean
  • Output file name: UserBean.java

Click on Finish button. Userbean class is generated into src folder.

Fill Output base dir and Output file name can be given boring when JAVA class is generated :

  • you must fill package and output base dir.
  • you must fill class name and output file name.

The next tutorial explains how add scripts in order to manage this problem.