|
[Previous] [Overview] [Next]
JSP in Templates Part II
Let's finish our interface-example. We still need to generate the method section. Here's how it works:
<%@ page import="ru.novosoft.uml.MBase,java.util.Collection,java.util.Iterator"
extends="com.innoq.generator.jsp.JspBase" %>
<%@ taglib uri="iqgen" prefix="iqgen" %>
[...]
/**
* Interface <%=getClassname()%>
*/
public interface <%=getClassname()%><%=getExtendsString()%> {
<iqgen:foreach group="<%=getMetaModel().getOperations(getElement())%>" item="method">
<%
String methodName = getMetaModel().getName(method);
Object retType = getMetaModel().getReturnType(method);
String retTypeName = getMetaModel().getReturnTypeName(method);
String parList = getMetaModel().getParameterListAsString(method);
%>
/**
* Method <%=methodName%>
* <iqgen:foreach group="<%=getMetaModel().getParameterList(method)%>" item="parameter">
* @param <%=getMetaModel().getName(parameter)%> <%=getMetaModel().getDocumentation(parameter)%></iqgen:foreach><iqgen:if expr="<%=retType!=null%>">
* @return <%=retTypeName%></iqgen:if>
*/
public <%=retTypeName%> <%=methodName%>(<%=parList%>)
<%=getMetaModel().getExceptionListAsString(method)%>;
</iqgen:foreach>
} |
Basically we just have to iterate over all the declared methods and print out their documentation and signature. In our example this is done using the built-in foreach tag. As attribute we define a group of elements to iterate over. This is done with the JSP expression <%=getMetaModel().getOperations(getElement())%>. Additionally we need to specify a variable in which the items of the group are to be placed during the iteration. In our case this is method. So the whole iteration looks like this:
<iqgen:foreach group="<%=getMetaModel().getOperations(getElement())%>" item="method">
[...]
</iqgen:foreach> |
The rest is pretty much straight forward. First we declare a number of variables like methodName and retTypeName for easier access to the method modelelement. Then we print the documentation, which includes another foreach iteration over the method's parameters. Finally we print the method's signature. That's it.
Of course generating interfaces is pretty simple, because usually you don't need to add custom code. Read on in the next section to learn how to write more sophisticated templates.
|