KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > view > TrivialViewFactory


1 /*
2  * $Id: TrivialViewFactory.java,v 1.5 2002/06/19 03:27:18 lhoriman Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/view/TrivialViewFactory.java,v $
4  */

5
6 package org.infohazard.maverick.view;
7
8 import org.infohazard.maverick.flow.*;
9 import java.io.IOException JavaDoc;
10 import java.io.Reader JavaDoc;
11 import javax.servlet.ServletException JavaDoc;
12 import javax.servlet.ServletConfig JavaDoc;
13 import javax.xml.transform.Source JavaDoc;
14 import org.jdom.Element;
15 import org.w3c.dom.Node JavaDoc;
16
17
18 /**
19  * <p>Creates simple views which expect the model itself to be the
20  * source of content. Controllers which use a trivial view should
21  * provide a model which is one of:</p>
22  *
23  * <ul>
24  * <li>java.lang.String</li>
25  * <li>java.io.Reader</li>
26  * <li>javax.xml.transform.Source</li>
27  * <li>org.w3c.dom.Node</li>
28  * </ul>
29  *
30  * <p>The model will be rendered to the output stream (or transform) unmolested.</p>
31  *
32  * <p>Trivial views can have transforms. Neither the view elements nor
33  * the factory elements have any additional parameters.</p>
34  *
35  * @author Jeff Schnitzer
36  */

37 public class TrivialViewFactory implements ViewFactory
38 {
39     /**
40      */

41     public void init(Element factoryNode, ServletConfig JavaDoc servletCfg) throws ConfigException
42     {
43         // Do nothing
44
}
45
46     /**
47      */

48     public View createView(Element viewNode) throws ConfigException
49     {
50         return new TrivialView();
51     }
52     
53     /**
54      */

55     protected static class TrivialView implements View
56     {
57         /**
58          */

59         public void go(ViewContext vctx) throws IOException JavaDoc, ServletException JavaDoc
60         {
61             TransformStep next = vctx.getNextStep();
62             
63             if (vctx.getModel() == null)
64             {
65                 // Nothing
66
}
67             else if (vctx.getModel() instanceof String JavaDoc)
68             {
69                 next.go((String JavaDoc)vctx.getModel());
70             }
71             else if (vctx.getModel() instanceof Reader JavaDoc)
72             {
73                 next.go((Reader JavaDoc)vctx.getModel());
74             }
75             else if (vctx.getModel() instanceof Source JavaDoc)
76             {
77                 next.go((Source JavaDoc)vctx.getModel());
78             }
79             else if (vctx.getModel() instanceof Node JavaDoc)
80             {
81                 Source JavaDoc src = new javax.xml.transform.dom.DOMSource JavaDoc((Node JavaDoc)vctx.getModel());
82                 next.go(src);
83             }
84             else
85             {
86                 throw new ServletException JavaDoc("TrivialView does not understand a model of type "
87                          + vctx.getModel().getClass().getName() + ", only String, "
88                          + "Reader, javax.xml.transform.Source, and org.w3c.dom.Node");
89             }
90         }
91     }
92 }
Popular Tags