KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > screen > HtmlWidget


1 /*
2  * $Id: HtmlWidget.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.widget.screen;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.GeneralException;
36 import org.ofbiz.base.util.UtilXml;
37 import org.ofbiz.base.util.collections.MapStack;
38 import org.ofbiz.base.util.string.FlexibleStringExpander;
39 import org.ofbiz.base.util.template.FreeMarkerWorker;
40 import org.w3c.dom.Element JavaDoc;
41
42 import freemarker.template.TemplateException;
43
44 /**
45  * Widget Library - Screen model HTML class
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @version $Rev: 5462 $
49  * @since 3.1
50  */

51 public class HtmlWidget extends ModelScreenWidget {
52     public static final String JavaDoc module = HtmlWidget.class.getName();
53     
54     protected ModelScreenWidget childWidget;
55     
56     public HtmlWidget(ModelScreen modelScreen, Element JavaDoc htmlElement) {
57         super(modelScreen, htmlElement);
58         List JavaDoc childElementList = UtilXml.childElementList(htmlElement);
59         Iterator JavaDoc childElementIter = childElementList.iterator();
60         while (childElementIter.hasNext()) {
61             Element JavaDoc childElement = (Element JavaDoc) childElementIter.next();
62             if ("html-template".equals(childElement.getNodeName())) {
63                 this.childWidget = new HtmlTemplate(modelScreen, childElement);
64             } else if ("html-template-decorator".equals(childElement.getNodeName())) {
65                 this.childWidget = new HtmlTemplateDecorator(modelScreen, childElement);
66             } else {
67                 throw new IllegalArgumentException JavaDoc("Tag not supported under the platform-specific -> html tag with name: " + childElement.getNodeName());
68             }
69         }
70     }
71
72     public void renderWidgetString(Writer JavaDoc writer, Map JavaDoc context, ScreenStringRenderer screenStringRenderer) throws GeneralException {
73         childWidget.renderWidgetString(writer, context, screenStringRenderer);
74     }
75
76     public String JavaDoc rawString() {
77         return "<html-widget>" + (this.childWidget==null?"":this.childWidget.rawString());
78     }
79     
80     public static void renderHtmlTemplate(Writer JavaDoc writer, FlexibleStringExpander locationExdr, Map JavaDoc context) {
81         String JavaDoc location = locationExdr.expandString(context);
82         //Debug.logInfo("Rendering template at location [" + location + "] with context: \n" + context, module);
83

84         if (location.endsWith(".ftl")) {
85             try {
86                 FreeMarkerWorker.renderTemplateAtLocation(location, context, writer);
87             } catch (MalformedURLException JavaDoc e) {
88                 String JavaDoc errMsg = "Error rendering included template at location [" + location + "]: " + e.toString();
89                 Debug.logError(e, errMsg, module);
90                 throw new RuntimeException JavaDoc(errMsg);
91             } catch (TemplateException e) {
92                 String JavaDoc errMsg = "Error rendering included template at location [" + location + "]: " + e.toString();
93                 Debug.logError(e, errMsg, module);
94                 throw new RuntimeException JavaDoc(errMsg);
95             } catch (IOException JavaDoc e) {
96                 String JavaDoc errMsg = "Error rendering included template at location [" + location + "]: " + e.toString();
97                 Debug.logError(e, errMsg, module);
98                 throw new RuntimeException JavaDoc(errMsg);
99             }
100         } else {
101             throw new IllegalArgumentException JavaDoc("Rending not yet support for the tempalte at location: " + location);
102         }
103     }
104     
105     public static class HtmlTemplate extends ModelScreenWidget {
106         protected FlexibleStringExpander locationExdr;
107         
108         public HtmlTemplate(ModelScreen modelScreen, Element JavaDoc htmlTemplateElement) {
109             super(modelScreen, htmlTemplateElement);
110             this.locationExdr = new FlexibleStringExpander(htmlTemplateElement.getAttribute("location"));
111         }
112
113         public void renderWidgetString(Writer JavaDoc writer, Map JavaDoc context, ScreenStringRenderer screenStringRenderer) {
114             renderHtmlTemplate(writer, this.locationExdr, context);
115         }
116
117         public String JavaDoc rawString() {
118             return "<html-template location=\"" + this.locationExdr.getOriginal() + "\"/>";
119         }
120     }
121
122     public static class HtmlTemplateDecorator extends ModelScreenWidget {
123         protected FlexibleStringExpander locationExdr;
124         protected Map JavaDoc sectionMap = new HashMap JavaDoc();
125         
126         public HtmlTemplateDecorator(ModelScreen modelScreen, Element JavaDoc htmlTemplateDecoratorElement) {
127             super(modelScreen, htmlTemplateDecoratorElement);
128             this.locationExdr = new FlexibleStringExpander(htmlTemplateDecoratorElement.getAttribute("location"));
129             
130             List JavaDoc htmlTemplateDecoratorSectionElementList = UtilXml.childElementList(htmlTemplateDecoratorElement, "html-template-decorator-section");
131             Iterator JavaDoc htmlTemplateDecoratorSectionElementIter = htmlTemplateDecoratorSectionElementList.iterator();
132             while (htmlTemplateDecoratorSectionElementIter.hasNext()) {
133                 Element JavaDoc htmlTemplateDecoratorSectionElement = (Element JavaDoc) htmlTemplateDecoratorSectionElementIter.next();
134                 String JavaDoc name = htmlTemplateDecoratorSectionElement.getAttribute("name");
135                 this.sectionMap.put(name, new HtmlTemplateDecoratorSection(modelScreen, htmlTemplateDecoratorSectionElement));
136             }
137         }
138
139         public void renderWidgetString(Writer JavaDoc writer, Map JavaDoc context, ScreenStringRenderer screenStringRenderer) {
140             // isolate the scope
141
if (!(context instanceof MapStack)) {
142                 context = MapStack.create(context);
143             }
144
145             MapStack contextMs = (MapStack) context;
146
147             // create a standAloneStack, basically a "save point" for this SectionsRenderer, and make a new "screens" object just for it so it is isolated and doesn't follow the stack down
148
MapStack standAloneStack = contextMs.standAloneChildStack();
149             standAloneStack.put("screens", new ScreenRenderer(writer, contextMs, screenStringRenderer));
150             SectionsRenderer sections = new SectionsRenderer(this.sectionMap, standAloneStack, writer, screenStringRenderer);
151             
152             // put the sectionMap in the context, make sure it is in the sub-scope, ie after calling push on the MapStack
153
contextMs.push();
154             context.put("sections", sections);
155
156             renderHtmlTemplate(writer, this.locationExdr, context);
157         }
158
159         public String JavaDoc rawString() {
160             return "<html-template-decorator location=\"" + this.locationExdr.getOriginal() + "\"/>";
161         }
162     }
163
164     public static class HtmlTemplateDecoratorSection extends ModelScreenWidget {
165         protected String JavaDoc name;
166         protected List JavaDoc subWidgets;
167         
168         public HtmlTemplateDecoratorSection(ModelScreen modelScreen, Element JavaDoc htmlTemplateDecoratorSectionElement) {
169             super(modelScreen, htmlTemplateDecoratorSectionElement);
170             this.name = htmlTemplateDecoratorSectionElement.getAttribute("name");
171             // read sub-widgets
172
List JavaDoc subElementList = UtilXml.childElementList(htmlTemplateDecoratorSectionElement);
173             this.subWidgets = ModelScreenWidget.readSubWidgets(this.modelScreen, subElementList);
174         }
175
176         public void renderWidgetString(Writer JavaDoc writer, Map JavaDoc context, ScreenStringRenderer screenStringRenderer) throws GeneralException {
177             // render sub-widgets
178
renderSubWidgetsString(this.subWidgets, writer, context, screenStringRenderer);
179         }
180
181         public String JavaDoc rawString() {
182             return "<html-template-decorator-section name=\"" + this.name + "\"/>";
183         }
184     }
185 }
186
187
Popular Tags