Hi,

I created a small example to illustrate things...

Take this configuration file:

(maverick.xml)
----------------------------------------------------------------------------
--------------------------
    <?xml version="1.0"?>
    <maverick version="2.0" default-view-type="document"
default-transform-type="document">
        <modules>
            <view-factory type="document"
provider="org.infohazard.maverick.view.DocumentViewFactory">
                <default-bean-name value="model"/>
            </view-factory>
            <transform-factory type="xslt"
provider="org.infohazard.maverick.transform.XSLTransformFactory">
                <!-- caching can be 'preload', 'lazy' or 'disabled' -->
                <template-caching value="disabled"/>
            </transform-factory>
        </modules>

        <commands>

            <command name="getxmldoc">
                <controller
class="nl.openedge.maverick.example.ctrl.XMLCtrl" />
                    <view name="view1" type="trivial">
                        <transform type="xslt" path="transform1.xsl"/>
                    </view>
                    <view name="view2" type="trivial">
                        <transform type="xslt" path="transform2.xsl"/>
                    </view>
            </command>

        </commands>
    </maverick>
----------------------------------------------------------------------------
--------------------------

and then this control:

(XMLCtrl)
----------------------------------------------------------------------------
--------------------------
package nl.openedge.maverick.example.ctrl;

import java.util.HashMap;
import java.util.Map;

import org.infohazard.maverick.ctl.ThrowawayBean2;
import org.infohazard.maverick.flow.ControllerContext;

/**
 * Simple XML test for Maverick.
 * @author Eelco Hillenius
 */
public class XMLCtrl extends ThrowawayBean2
{
      // dummy doc 1
    private static final String xml1 =
        "<?xml version=\"1.0\" ?>"
            + "<page>"
            + "      <title>Hi there</title>"
            + "      <content>"
            + "         <p>"
            + "               Page1"
            + "         </p>"
            + "      </content>"
            + "</page>";

      // dummy doc 2
    private static final String xml2 =
        "<?xml version=\"1.0\" ?>"
            + "<page>"
            + "      <title>Again</title>"
            + "      <content>"
            + "         <p>"
            + "               Page2"
            + "         </p>"
            + "      </content>"
            + "</page>";

    /** simple 'xml db' */
    private static Map xmldb = new HashMap(2);
    static
    {
          xmldb.put("doc1", xml1);
          xmldb.put("doc2", xml2);
    }

    /**
     * this parameter is set by Maverick when provided as a request
     * parameter (like http://myserver/myapp/mycmd.m?view=myview);
     * we use it to get the proper view.
     */
    private String view;
    /**
     * the id of the xml document to use.
     */
    private String docid;

    /*
     * @see org.infohazard.maverick.ctl.ThrowawayBean2#perform()
     */
    protected String perform() throws Exception
    {
      // note that by now the properties 'view' and 'docid' should be
populated
      // from the request
            ControllerContext cctx = getCtx();
            String xml = getXML(docid);
            cctx.setModel(xml); // will serve as concrete view for the
transforms
        return view;
    }

    /**
     * Get the test XML document; this could also be of type
       * <ul>
       *   <li>java.lang.String</li>
       *   <li>java.io.Reader</li>
       *   <li>javax.xml.transform.Source</li>
       *   <li>org.w3c.dom.Node</li>
       * </ul>
     * @param docid id of the document to get
     * @return the xml doc as a String
     */
    private String getXML(String docid)
    {
        return (String)xmldb.get(docid);
    }

      // we need the getters and setters in order to be able to populate the
      // properties from the view
    public String getDocid() { return docid; }
    public String getView() { return view; }
    public void setDocid(String string) { docid = string; }
    public void setView(String string) { view = string; }
}

----------------------------------------------------------------------------
--------------------------

And finally, these XSLT templates:

(transform1.xsl)
----------------------------------------------------------------------------
--------------------------

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
      <xsl:output method="html" indent="yes" encoding="ISO-8859-1"/>

      <xsl:template match="@*|node()">
            <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
      </xsl:template>

      <xsl:template match="/page">
            <html>
                  <head>
                        <title><xsl:value-of select="title" /></title>
                        <xsl:apply-templates select="head/*" />
                  </head>

                  <body bgcolor="#FFFFCC">
                        <table cellspacing="0" cellpadding="0" width="100%">
                              <tr>
                                    <td class="pageTitle" colspan="2"> <h1>
<xsl:value-of select="title" /> </h1> </td>
                              </tr>
                        </table>

                        <xsl:apply-templates select="content/*" />

                        <h4>Created with transform1.xsl</h4>
                  </body>
            </html>
      </xsl:template>

</xsl:stylesheet>

----------------------------------------------------------------------------
--------------------------

(transform2.xsl)
----------------------------------------------------------------------------
--------------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
      <xsl:output method="html" indent="yes" encoding="ISO-8859-1"/>

      <xsl:template match="@*|node()">
            <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
      </xsl:template>

      <xsl:template match="/page">
            <html>
                  <head>
                        <title><xsl:value-of select="title" /></title>
                        <xsl:apply-templates select="head/*" />
                  </head>

                  <body bgcolor="#CCCCFF">
                        <table cellspacing="0" cellpadding="0" width="100%">
                              <tr>
                                    <td class="pageTitle" colspan="2"> <h1>
<xsl:value-of select="title" /> </h1> </td>
                              </tr>
                        </table>

                        <xsl:apply-templates select="content/*" />

                        <h4>Created with transform2.xsl</h4>
                  </body>
            </html>
      </xsl:template>

</xsl:stylesheet>
----------------------------------------------------------------------------
--------------------------

Now, get this stuff into your webapp and test urls like (e.g.)
http://localhost:8080/yourwebapp/getxmldoc.m?view=view2&docid=doc1,
http://localhost:8080/yourwebapp/getxmldoc.m?view=view2&docid=doc2 or
http://localhost:8080/yourwebapp/getxmldoc.m?view=view1&docid=doc2

That should give you an idea I hope ;)

There's tons of other possibilities (like coupling views and xml's somewhere
in a seperate registry) and you should ofcourse get real xml docs from your
database in your controller (or delegate that to some business class), but
this is basically how you could do it.

Good luck with it (I will be out for a few days, so for any other questions
you have to either wait until next week, or hope someone else will help you
out further)!

Eelco



-------------------------------------------------------
This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference
Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer
Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA
REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code NWMGYKND
[INVALID FOOTER]

Reply via email to