KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > xmlform > ModifyDomViewHandler


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: ModifyDomViewHandler.java,v 1.4 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.xmlform;
21
22 import java.io.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 import org.w3c.dom.*;
28 import org.w3c.dom.html.*;
29 import org.enhydra.xml.xmlc.*;
30
31 import org.enhydra.barracuda.core.comp.*;
32 import org.enhydra.barracuda.core.util.dom.*;
33 import org.enhydra.barracuda.core.event.*;
34 import org.enhydra.barracuda.core.event.helper.*;
35 import org.enhydra.barracuda.core.helper.servlet.*;
36 import org.enhydra.barracuda.core.util.*;
37 import org.enhydra.barracuda.plankton.exceptions.*;
38 import org.enhydra.barracuda.core.view.*;
39 import org.enhydra.barracuda.core.util.http.*;
40 import org.enhydra.barracuda.core.comp.renderer.*;
41 import org.apache.log4j.*;
42
43 /** Pretty similar class to {@link org.enhydra.barracuda.core.event.helper.BTemplateViewHandler
44  * }. The main differences are
45  * <ul>
46  * <li>A <CODE>ViewContext</CODE> is passed to {@link #getTemplateModels } and
47  * {@link #getTemplateClass}
48  * <li>provides a hook for modifying a XMLC class <B>before</B> the whole
49  * Model/Component rendering is performed
50  * </ul>
51  * @author Stefan Armbruster
52  * @version $Id: ModifyDomViewHandler.java,v 1.4 2004/02/01 05:16:27 christianc Exp $
53  */

54 public abstract class ModifyDomViewHandler extends DefaultViewHandler {
55
56     protected static Logger logger = Logger.getLogger(ModifyDomViewHandler.class.getName());
57
58     protected BTemplate templateComp = null;
59
60     /** abstract method for getting als required models
61      * @param vc the ViewContext of the current RenderEvent
62      * @throws EventException
63      * @throws ServletException
64      * @return either a single model or a {@link java.util.List} of models
65      */

66     public abstract Object JavaDoc getTemplateModels(ViewContext vc) throws EventException, ServletException, IOException;
67
68     /** Return an instance of the template class (must implement Document) */
69     public abstract Class JavaDoc getTemplateClass(ViewContext vc) throws EventException, ServletException, IOException;
70
71     /**
72      * Provide a handle to the underlying BTemplate component
73      */

74     public BTemplate getBTemplate() {
75         return templateComp;
76     }
77
78     /** Placeholder for DOM-modifications prior to directive mappings. The default
79      * implementation is empty.
80      * @param page a instance of the class returned by {@link #getTemplateClass}
81      * @param vc the context of the current event
82      */

83     protected void modifyDOM(Document page, ViewContext vc) throws EventException, ServletException, IOException {
84     }
85
86     /**
87      * Generate the view
88      */

89     public Document handleViewEvent(BComponent root) throws EventException, ServletException, IOException {
90
91         ViewContext vc = getViewContext();
92         Document page = DefaultDOMLoader.getGlobalInstance().getDOM(getTemplateClass(vc), vc.getViewCapabilities().getClientLocale());
93         modifyDOM(page, vc);
94
95         //create a template component and bind it to the views
96
templateComp = new BTemplate();
97         Object JavaDoc o = getTemplateModels(vc);
98         if (o instanceof TemplateModel) templateComp.addModel((TemplateModel) o);
99         else if (o instanceof List) templateComp.addModels((List) o);
100         else if (o == null) { }
101         else throw new RuntimeException JavaDoc("Fatal err: Model must return either TemplateModel or List");
102         templateComp.setView(new DefaultTemplateView(page.getDocumentElement()));
103         root.addChild(templateComp);
104
105         return page;
106     }
107 }
108
Popular Tags