1. How you use HuGo

Aus Hugo Doku

(Weitergeleitet von 1.1. A simple example)
Wechseln zu: Navigation, Suche

This section is about how you use HuGo. To read some general informations about how a generator works in principle, please refer to Section 1.1. What does a generator do.

To explain the fundamental principle of how to use HuGo, we show a very simple example, where a generator for a data class is written.

Look at Example 1.1. This file shows a possible XML specification of a data class.

Inhaltsverzeichnis

Example 1.1. A simple example

simple.model:

<model>
   <class name="MyFirstClass" >
   	<item name="value1" type="int" default="5"/>
    	<item name="value2" type="double" default="9"/>
   </class>	
</model> 

Out of this XML specification a data class like following should be generated.

MyFirstClass.java:

import java.io.*;
public class MyFirstClass
{
  //-----------------------------------------------sk--
  int value1=5;
  //-------------------------------------------------
  double value2=9;
  //-------------------------------------------------
  /** getter for item value1
  */
  public int getValue1()
  {
      return value1;
  }
  /** setter for item value1
  */
  final public void setValue1(int value1)
  {
    this.value1=value1;
  }
  /** getter for item value2
  */
  public double getValue2()
  {
      return value2;
  }
  /** setter for item value2
  */
  final public void setValue2(double value2)
  {
    this.value2=value2;
  }
}

In listing you see, that for each specified item (<item>), there has to be a data delaration and a corresponding set( ) and get( ) method.

This logical rule can be placed as templated code snippets within the so called HuGo Frames. You write a Frame class for each XML tag in the .model file. The following listings shows the Frame class for the tag <class> (defined in _class.jxgp) and the Frame class for the tag <item> (defined in item.jxgp.

_class.jxgp:

package java_frames;
public class _class extends base
{
  @#slot item_decl;
  @#slot item_getter_setter;   
  
  public @#String name;
  
  
  @#constructor   
  {
     
  }
 
  @#gen JCLASS<!name+".java","src","JAVA"!>
   <#java
       import java.io.*;
       public class <!name!> 
       {
          @#item_decl
          //-------------------------------------------------   
          @#item_getter_setter
       }
   #>
}

item.jxgp:

package java_frames;
public class item extends base
{
  public @#String name;
  public @#String type="int";
  public @#String _default="0";
  @#constructor   {   }
  
  @#gen item_decl
  <#java 
       //-------------------------------------------------   
       <!type!> <!name!>=<!_default!>;
   #>
   @#gen item_getter_setter
  <#java 
       /** getter for item <!name!>
       */
  
       public <!type!> get<!upper(name)!>()
       {
           return <!name!>;
       }   
       /** setter for item <!name!>
       */
       final public void set<!upper(name)!>(<!type!> <!name!>)
       {
         this.<!name!>=<!name!>;
       }
       
   #>
}

The following subchapter explain the HuGo fundamental usage on base of this simple example.


Next: 1.1.1. Correlation Model <-> Frame
Persönliche Werkzeuge