1 5 6 package org.infohazard.maverick.view; 7 8 import javax.servlet.ServletConfig ; 9 10 import org.infohazard.maverick.flow.ConfigException; 11 import org.infohazard.maverick.flow.View; 12 import org.infohazard.maverick.flow.ViewContext; 13 import org.infohazard.maverick.flow.ViewFactory; 14 import org.infohazard.maverick.util.XML; 15 import org.jdom.Element; 16 17 59 public class DocumentViewFactory implements ViewFactory 60 { 61 64 protected static final String DEFAULT_DEFAULT_BEAN_NAME = "model"; 65 66 71 protected static final String ATTR_DEFAULT_BEAN_NAME = "default-bean-name"; 72 73 82 protected static final String ATTR_BEAN_NAME = "bean"; 83 84 93 protected static final String ATTR_SCOPE = "scope"; 94 95 103 protected static final String SCOPE_APP = "application"; 104 105 113 protected static final String SCOPE_SESSION = "session"; 114 115 123 protected static final String SCOPE_REQUEST = "request"; 124 125 131 protected String defaultBeanName = DEFAULT_DEFAULT_BEAN_NAME; 132 133 143 public void init(Element factoryNode, ServletConfig servletCfg) throws 144 ConfigException 145 { 146 if (factoryNode != null) 147 { 148 String value = XML.getValue(factoryNode, ATTR_DEFAULT_BEAN_NAME); 149 if (value != null && value.length() > 0) 150 this.defaultBeanName = value; 151 } 152 } 153 154 160 public View createView(Element viewNode) throws ConfigException 161 { 162 String beanName = XML.getValue(viewNode, ATTR_BEAN_NAME); 163 if (beanName == null) 164 beanName = this.defaultBeanName; 165 166 String scope = XML.getValue(viewNode, ATTR_SCOPE); 167 168 if (SCOPE_APP.equals(scope)) 170 { 171 return new DocumentView(beanName) { 172 protected void setAttribute(ViewContext vctx) { 173 vctx.getServletContext().setAttribute(this.beanName, vctx.getModel()); 174 } 175 }; 176 } 177 else if (SCOPE_SESSION.equals(scope)) 178 { 179 return new DocumentView(beanName) { 180 protected void setAttribute(ViewContext vctx) { 181 vctx.getRequest().getSession().setAttribute(this.beanName, vctx.getModel()); 182 } 183 }; 184 } 185 else if (SCOPE_REQUEST.equals(scope) || scope == null) 186 { 187 return new DocumentView(beanName) { 188 protected void setAttribute(ViewContext vctx) { 189 vctx.getRequest().setAttribute(this.beanName, vctx.getModel()); 190 } 191 }; 192 } 193 else 194 { 195 throw new ConfigException("Illegal scope specified: " + XML.toString(viewNode)); 196 } 197 } 198 } | Popular Tags |