HOME
UNTERNEHMEN
LEISTUNGEN
PRODUKTE
PROJEKTE
IQGEN
NEWS
KONTAKT
iQgen News
About iQgen
Whitepaper
Prerequisites
Download
Pricing & Licence
Services
Performance
User's Guide
Getting Started
FAQ
Resources

Getting Started

[Previous] [Overview] [Next]

How JSP are used in iQgen

iQgen uses JSP for two purposes:

  • the mapping of modelelements to templates
  • and the templates themselves.
  • From Modelelement to Template

    As you know, a model consists of different modelelements like classes, interfaces, associations, diagrams and so forth. Usually a single modelelement cannot be mapped one to one to a generated file. Instead often a single modelelement leads to the generation of multiple files, e.g. a SQL script, a configuration file, a deployment descriptor and a couple of classes. Therefore you need to specify which files should be generated for a given modelelement. To allow you to specify this in a very flexible and powerful way, this is done with a special JSP - the file main.jsp.

    There are three things you need to do, to write your own main.jsp:

  • Extend the class com.innoq.generator.jsp.JspMain
  • Implement the method public String[] getTemplates(MBase)
  • Implement the method public String getName()
  • So the most basic main.jsp looks like this:

    <%@ page extends="com.innoq.generator.jsp.JspMain"
    import="ru.novosoft.uml.MBase" %>

    <%!
        /**
         * For performance reasons this array is declared as static
         * final and can be reused at every invocation of getTemplates.
         */
        private static final String[] TEMPLATE = {"template.jsp"};

        /**
         * Returns the names of the templates to execute for a specific
         * modelelement.
         *
         * @param element The modelelement to return the templatenames for
         * @return Array of templatenames for a element
         */
        public String[] getTemplates(MBase element)  {
            return TEMPLATE;
        }

        /**
         * Name of this modelelement-template mapping.
         */
        public String getName() {
            return "My first main.jsp";
        }
    %>

    This main.jsp would return the name of the template template.jsp for every modelelement. Of course this only makes sense in the most simple case. You could not try to generate a SQL script and a Java class file for a persistent object with a single template. Our next example shows a slightly more complex, but a lot more realistic mapping:

    <%@ page extends="com.innoq.generator.jsp.JspMain"
    import="ru.novosoft.uml.MBase" %>

    <%!
        private static final String[] PERSISTENT = {"class.jsp",
            "drop_and_create.jsp"};
        private static final String[] INTERFACE = {"interface.jsp"};
        private static final String[] DEFAULT = {"class.jsp"};

        public String[] getTemplates(MBase element)  {
            String s = getMetaModel().getStereotype(element);
            String name = getMetaModel().getName(element);
            if ("Persistent".equals(s)) {
                return PERSISTENT;
            } else if (getMetaModel().isInterface(element)) {
                return INTERFACE;
            } else {
                return DEFAULT;
            }
        }

        public String getName() {
            return "My second main.jsp";
        }
    %>

    Instead of always returning the same template name for every modelelement, we return PERSISTENT for all modelelements marked with the stereotype Persistent, INTERFACE for all interfaces and DEFAULT for all other modelelements. This allows us to generate SQL scripts with the template drop_and_create.jsp and class files with class.jsp for all persistent modelelements. All other elements lead to the generation to either an interface or a class file.

    Let's look how this is done in detail. In every iQgen JSP you can obtain access to the model by invoking the method getMetaModel(). This will return an object of the type com.innoq.generator.metamodel.IMetaModelFacade, which can help you inspecting the current modelelement. The three methods we use in this example are getStereotype(MBase), getName(MBase), and isInterface(MBase). Additionally the metamodel offers many more methods to obtain detailed information about the current element. All methods are covered in the API reference section.

    So far we can invoke templates for and with specific modelelements. Often some templates need only be invoked once per model and even without a specific modelelement. Such cases can be covered with the method public String[] getPostprocessTemplates(). Just overwrite this method in your main.jsp to provide the names of all templates that should be executed after all modelelement-specific templates have been executed:

    <%@ page extends="com.innoq.generator.jsp.JspMain"
    import="ru.novosoft.uml.MBase" %>

    <%!
        [...]
        private static final String[] POST = {"build.jsp"};
        public String[] getPostprocessTemplates()  {
            return POST;
        }
    %>

    Our example always returns the name of the template build.jsp which generates an XML file that can be used as an Ant build script. Another useful application of this feature is a template for deployment descriptors.

    For a complete description of JspMain's API, take a look at the documentation page.

    We are now able to map modelelement to templates. You will learn how to write templates in the next section.

    Copyright © 2001-2003 innoQ. Alle Rechte vorbehalten. Rechtliche Hinweise