Taglibs are very useful to separate logical structures (e.g. iterations, conditions) and code. A custom taglib with the following tags is included in the iQgen release.
The IfTag enables you to generate code depending on conditions. The body of the tag will only be executed if the condition (stored in the tag attribute expr) is true. Syntax:
<iqgen:if expr="[true|false]"> ...body </iqgen:if>
Examples:
<iqgen:if expr="true"> ...something to write </iqgen:if>or
<iqgen:if expr="<%=!attrName.equals(\"name\")%>"> ...something to write </iqgen:if>or
<iqgen:if expr="<%=isProcessable(element)%%>"> ...something to write </iqgen:if>
The ForEachTag is used to iterate over a Collection, a List or with an Iterator.
<iqgen:foreach group="<%=attr.iterator()%>" item="element" type="ru.novosoft.uml.MBase">The parameter item allows you to access the current element of the loop.
String attrName = getMetaModel().getName(element);type is optional (default type="ru.novosoft.uml.MBase") and defines the type of the item. An incorrect type raises a ClassCastException.
The ImportTag is deprecated
The ImportTag is only useful if you are generating Java source code. This tag creates the necessary import statements, which usually is a pretty hard job. This encapsulation dramatically simplifies the development of templates that generate Java code.
<iqgen:import prefix="<%=getPrefix()%>" collectiontype="java.util.Collection"/> <iqgen:usercode id="imports"> // ... own imports </iqgen:usercode>See samples/getting_started/tutorial_1/templates/import.jsp
The parameter prefix is set to the path of the class. This is helpful for evaluating import statements because none of the classes in this path needs to be imported. The collectiontype parameter defines which collectiontype should be used for *-associations.
The PropertyTag is only useful in the context of Java, too. It encapsulates a list of attributes by generating getter and setter methods (see samples/getting_started/tutorial_1/templates/attribute.jsp):
<iqgen:properties>
Developers need to be able to extend generated code. To ensure this, iQgen recognizes protected areas which remain unchanged during re-generation. In the generated source code a protected area may look like this:
//==> Begin Protected Area global // ... //==> End Protected Area global
Developers have to write their code in these areas. During a re-generation iQgen merges generated and implemented areas.
In a template the protected area is marked with the usercode tag like this:
<iqgen:usercode id="global"> // ... </iqgen:usercode>
The id (here global) is used to identify a protected area within the generated code. The name for the id has to be unique.