1 24 package org.ofbiz.widget.screen; 25 26 import java.io.IOException ; 27 import java.io.Writer ; 28 import java.net.MalformedURLException ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 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 ; 41 42 import freemarker.template.TemplateException; 43 44 51 public class HtmlWidget extends ModelScreenWidget { 52 public static final String module = HtmlWidget.class.getName(); 53 54 protected ModelScreenWidget childWidget; 55 56 public HtmlWidget(ModelScreen modelScreen, Element htmlElement) { 57 super(modelScreen, htmlElement); 58 List childElementList = UtilXml.childElementList(htmlElement); 59 Iterator childElementIter = childElementList.iterator(); 60 while (childElementIter.hasNext()) { 61 Element childElement = (Element ) 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 ("Tag not supported under the platform-specific -> html tag with name: " + childElement.getNodeName()); 68 } 69 } 70 } 71 72 public void renderWidgetString(Writer writer, Map context, ScreenStringRenderer screenStringRenderer) throws GeneralException { 73 childWidget.renderWidgetString(writer, context, screenStringRenderer); 74 } 75 76 public String rawString() { 77 return "<html-widget>" + (this.childWidget==null?"":this.childWidget.rawString()); 78 } 79 80 public static void renderHtmlTemplate(Writer writer, FlexibleStringExpander locationExdr, Map context) { 81 String location = locationExdr.expandString(context); 82 84 if (location.endsWith(".ftl")) { 85 try { 86 FreeMarkerWorker.renderTemplateAtLocation(location, context, writer); 87 } catch (MalformedURLException e) { 88 String errMsg = "Error rendering included template at location [" + location + "]: " + e.toString(); 89 Debug.logError(e, errMsg, module); 90 throw new RuntimeException (errMsg); 91 } catch (TemplateException e) { 92 String errMsg = "Error rendering included template at location [" + location + "]: " + e.toString(); 93 Debug.logError(e, errMsg, module); 94 throw new RuntimeException (errMsg); 95 } catch (IOException e) { 96 String errMsg = "Error rendering included template at location [" + location + "]: " + e.toString(); 97 Debug.logError(e, errMsg, module); 98 throw new RuntimeException (errMsg); 99 } 100 } else { 101 throw new IllegalArgumentException ("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 htmlTemplateElement) { 109 super(modelScreen, htmlTemplateElement); 110 this.locationExdr = new FlexibleStringExpander(htmlTemplateElement.getAttribute("location")); 111 } 112 113 public void renderWidgetString(Writer writer, Map context, ScreenStringRenderer screenStringRenderer) { 114 renderHtmlTemplate(writer, this.locationExdr, context); 115 } 116 117 public String 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 sectionMap = new HashMap (); 125 126 public HtmlTemplateDecorator(ModelScreen modelScreen, Element htmlTemplateDecoratorElement) { 127 super(modelScreen, htmlTemplateDecoratorElement); 128 this.locationExdr = new FlexibleStringExpander(htmlTemplateDecoratorElement.getAttribute("location")); 129 130 List htmlTemplateDecoratorSectionElementList = UtilXml.childElementList(htmlTemplateDecoratorElement, "html-template-decorator-section"); 131 Iterator htmlTemplateDecoratorSectionElementIter = htmlTemplateDecoratorSectionElementList.iterator(); 132 while (htmlTemplateDecoratorSectionElementIter.hasNext()) { 133 Element htmlTemplateDecoratorSectionElement = (Element ) htmlTemplateDecoratorSectionElementIter.next(); 134 String name = htmlTemplateDecoratorSectionElement.getAttribute("name"); 135 this.sectionMap.put(name, new HtmlTemplateDecoratorSection(modelScreen, htmlTemplateDecoratorSectionElement)); 136 } 137 } 138 139 public void renderWidgetString(Writer writer, Map context, ScreenStringRenderer screenStringRenderer) { 140 if (!(context instanceof MapStack)) { 142 context = MapStack.create(context); 143 } 144 145 MapStack contextMs = (MapStack) context; 146 147 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 contextMs.push(); 154 context.put("sections", sections); 155 156 renderHtmlTemplate(writer, this.locationExdr, context); 157 } 158 159 public String rawString() { 160 return "<html-template-decorator location=\"" + this.locationExdr.getOriginal() + "\"/>"; 161 } 162 } 163 164 public static class HtmlTemplateDecoratorSection extends ModelScreenWidget { 165 protected String name; 166 protected List subWidgets; 167 168 public HtmlTemplateDecoratorSection(ModelScreen modelScreen, Element htmlTemplateDecoratorSectionElement) { 169 super(modelScreen, htmlTemplateDecoratorSectionElement); 170 this.name = htmlTemplateDecoratorSectionElement.getAttribute("name"); 171 List subElementList = UtilXml.childElementList(htmlTemplateDecoratorSectionElement); 173 this.subWidgets = ModelScreenWidget.readSubWidgets(this.modelScreen, subElementList); 174 } 175 176 public void renderWidgetString(Writer writer, Map context, ScreenStringRenderer screenStringRenderer) throws GeneralException { 177 renderSubWidgetsString(this.subWidgets, writer, context, screenStringRenderer); 179 } 180 181 public String rawString() { 182 return "<html-template-decorator-section name=\"" + this.name + "\"/>"; 183 } 184 } 185 } 186 187 | Popular Tags |