| 1 24 package org.riotfamily.components.component; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.io.StringWriter ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 import org.riotfamily.common.markup.TagWriter; 42 import org.riotfamily.components.Component; 43 import org.riotfamily.components.ComponentVersion; 44 import org.riotfamily.components.PropertyProcessor; 45 import org.springframework.util.Assert; 46 47 50 public abstract class AbstractComponent implements Component { 51 52 public static final String CONTAINER = AbstractComponent.class.getName() 53 + ".container"; 54 55 public static final String COMPONENT_ID = "componentId"; 56 57 public static final String THIS = "this"; 58 59 public static final String PARENT_ID = "parentId"; 60 61 public static final String POSITION_CLASS = "positionClass"; 62 63 protected Log log = LogFactory.getLog(AbstractComponent.class); 64 65 private List propertyProcessors = new ArrayList (); 66 67 public void setPropertyProcessors(List propertyProcessors) { 68 Assert.notNull(propertyProcessors); 69 this.propertyProcessors = propertyProcessors; 70 } 71 72 public void addPropertyProcessor(PropertyProcessor propertyProcessor) { 73 propertyProcessors.add(propertyProcessor); 74 } 75 76 public List getPropertyProcessors() { 77 return propertyProcessors; 78 } 79 80 public final void render(ComponentVersion componentVersion, 81 String positionClassName, HttpServletRequest request, 82 HttpServletResponse response) throws IOException { 83 84 try { 85 request.setAttribute(CONTAINER, componentVersion.getContainer()); 86 renderInternal(componentVersion, positionClassName, request, response); 87 } 88 catch (Exception e) { 89 log.error("Error rendering component", e); 90 91 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 92 StringWriter strace = new StringWriter (); 93 e.printStackTrace(new PrintWriter (strace)); 94 95 TagWriter pre = new TagWriter(response.getWriter()); 96 pre.start("pre") 97 .attribute("class", "riot-stacktrace") 98 .body(strace.toString()); 99 100 pre.end(); 101 } 102 } 103 104 public boolean isDynamic() { 105 return false; 106 } 107 108 public Map buildModel(ComponentVersion componentVersion) { 109 Map model = new HashMap (); 110 model.putAll(componentVersion.getProperties()); 111 Iterator it = propertyProcessors.iterator(); 112 while (it.hasNext()) { 113 PropertyProcessor pp = (PropertyProcessor) it.next(); 114 pp.resolveStrings(model); 115 } 116 return model; 117 } 118 119 public void updateProperties(ComponentVersion version, Map model) { 120 Iterator it = propertyProcessors.iterator(); 121 while (it.hasNext()) { 122 PropertyProcessor pp = (PropertyProcessor) it.next(); 123 pp.convertToStrings(model); 124 } 125 126 Map props = version.getProperties(); 127 128 it = model.entrySet().iterator(); 129 while (it.hasNext()) { 130 Map.Entry entry = (Map.Entry ) it.next(); 131 Assert.isInstanceOf(String .class, entry.getKey(), 132 "Map must only contain String keys."); 133 134 if (entry.getValue() == null) { 135 props.remove(entry.getKey()); 136 } 137 else { 138 Assert.isInstanceOf(String .class, entry.getValue(), 139 "Map must only contain String values."); 140 141 props.put(entry.getKey(), entry.getValue()); 142 } 143 } 144 version.setDirty(true); 145 } 146 147 protected abstract void renderInternal(ComponentVersion componentVersion, 148 String positionClassName, HttpServletRequest request, 149 HttpServletResponse response) throws Exception ; 150 151 public Collection getCacheTags(ComponentVersion version) { 152 ArrayList result = new ArrayList (); 153 Iterator it = propertyProcessors.iterator(); 154 while (it.hasNext()) { 155 PropertyProcessor pp = (PropertyProcessor) it.next(); 156 String [] tags = pp.getCacheTags(version.getProperties()); 157 for (int i = 0; tags != null && i < tags.length; i++) { 158 result.add(tags[i]); 159 } 160 } 161 return result; 162 } 163 164 } 165 | Popular Tags |