1 13 package com.tonbeller.wcf.form; 14 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpSession ; 23 24 import org.apache.log4j.Logger; 25 import org.jaxen.JaxenException; 26 import org.jaxen.dom.DOMXPath; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 31 import com.tonbeller.wcf.component.Component; 32 import com.tonbeller.wcf.component.ComponentSupport; 33 import com.tonbeller.wcf.controller.RequestContext; 34 import com.tonbeller.wcf.ui.XoplonCtrl; 35 import com.tonbeller.wcf.utils.DomUtils; 36 import com.tonbeller.wcf.utils.SoftException; 37 38 55 public class XmlComponent extends ComponentSupport { 56 private static Logger logger = Logger.getLogger(XmlComponent.class); 57 Document document; 58 Map elementHandlerMap; 59 Map idHandlerMap; 60 Map handlerElementMap; 61 62 66 public XmlComponent(String id, Component parent, Document document) { 67 super(id, parent); 68 this.document = document; 69 } 70 71 public void initialize(RequestContext context) throws Exception { 72 super.initialize(context); 73 hideSecuredElements(context, document); 74 installNodeHandlers(context, document); 75 } 76 77 public void destroy(HttpSession session) throws Exception { 78 for (Iterator it = elementHandlerMap.values().iterator(); it.hasNext();) { 79 NodeHandler nh = (NodeHandler) it.next(); 80 nh.destroy(session); 81 } 82 super.destroy(session); 83 } 84 85 88 public Document render(RequestContext context) throws Exception { 89 for (Iterator it = elementHandlerMap.values().iterator(); it.hasNext();) { 90 NodeHandler nh = (NodeHandler) it.next(); 91 nh.render(context); 92 } 93 return document; 94 } 95 96 109 void installNodeHandlers(RequestContext context, Document root) throws Exception { 110 try { 111 elementHandlerMap = new HashMap (); 112 handlerElementMap = new HashMap (); 113 idHandlerMap = new HashMap (); 114 List elements = findHandlerElements(root); 115 for (Iterator it = elements.iterator(); it.hasNext();) { 116 Element elem = (Element ) it.next(); 117 String clazz = elem.getAttribute("handler"); 118 NodeHandler nh = (NodeHandler) Class.forName(clazz).newInstance(); 119 nh.initialize(context, this, elem); 120 elementHandlerMap.put(elem, nh); 121 handlerElementMap.put(nh, elem); 122 idHandlerMap.put(elem.getAttribute("id"), nh); 123 } 124 } catch (JaxenException e) { 125 logger.error(null, e); 126 throw new SoftException(e); 127 } catch (InstantiationException e) { 128 logger.error(null, e); 129 throw new SoftException(e); 130 } catch (IllegalAccessException e) { 131 logger.error(null, e); 132 throw new SoftException(e); 133 } catch (ClassNotFoundException e) { 134 logger.error(null, e); 135 throw new SoftException(e); 136 } 137 } 138 139 private List findHandlerElements(Document root) throws JaxenException { 140 installDefaultHandler(root, "//xtree", "com.tonbeller.wcf.tree.TreeHandler"); 141 installDefaultHandler(root, "//xtable", "com.tonbeller.wcf.table.TableHandler"); 142 installDefaultHandler(root, "//xtabbed", "com.tonbeller.wcf.tabbed.TabbedHandler"); 143 installDefaultHandler(root, "//button", "com.tonbeller.wcf.form.ButtonHandler"); 144 installDefaultHandler(root, "//imgButton", "com.tonbeller.wcf.form.ButtonHandler"); 145 146 DOMXPath xp = new DOMXPath("//*[@handler]"); 147 return xp.selectNodes(root); 148 } 149 150 private void installDefaultHandler(Document root, String xpath, String clazz) throws JaxenException { 151 DOMXPath xp = new DOMXPath(xpath); 152 List list = xp.selectNodes(root); 153 for (Iterator it = list.iterator(); it.hasNext();) { 154 Element e = (Element ) it.next(); 155 if (e.getAttribute("handler").length() == 0 && e.getAttribute("actionReference").length() == 0) 156 e.setAttribute("handler", clazz); 157 } 158 } 159 160 163 void hideSecuredElements(RequestContext context, Document root) throws Exception { 164 HttpServletRequest req = context.getRequest(); 165 try { 166 DOMXPath xp = new DOMXPath("//*[@role]"); 167 List elements = xp.selectNodes(root); 168 for (Iterator it = elements.iterator(); it.hasNext();) { 169 Element elem = (Element ) it.next(); 170 String role = elem.getAttribute("role"); 171 if (context.isUserInRole(role)) 172 XoplonCtrl.setHidden(elem, false); else 174 XoplonCtrl.setHidden(elem, true); } 176 } catch (JaxenException e) { 177 logger.error(null, e); 178 throw new SoftException(e); 179 } 180 } 181 182 185 public NodeHandler getParent(NodeHandler handler) { 186 Element elem = (Element ) handlerElementMap.get(handler); 187 Node node = elem.getParentNode(); 188 while (node.getNodeType() == Node.ELEMENT_NODE) { 189 NodeHandler h = (NodeHandler) elementHandlerMap.get(node); 190 if (h != null) 191 return h; 192 node = node.getParentNode(); 193 } 194 return null; 195 } 196 197 List getHandlers(NodeHandler rootHandler, String xpathExpr) { 198 try { 199 List handlerList = new ArrayList (); 200 DOMXPath xp = new DOMXPath(xpathExpr); 201 Element rootElem = (Element ) handlerElementMap.get(rootHandler); 202 List elements = xp.selectNodes(rootElem); 203 for (Iterator it = elements.iterator(); it.hasNext();) { 204 Element element = (Element ) it.next(); 205 NodeHandler handler = (NodeHandler) elementHandlerMap.get(element); 206 if (handler != null) 207 handlerList.add(handler); 208 } 209 return handlerList; 210 } catch (JaxenException e) { 211 logger.error(null, e); 212 throw new SoftException(e); 213 } 214 } 215 216 public List getChildren(NodeHandler handler) { 217 return getHandlers(handler, ".//*[@handler]"); 218 } 219 220 223 public Document getDocument() { 224 return document; 225 } 226 227 232 public NodeHandler getHandler(Element element) { 233 return (NodeHandler) elementHandlerMap.get(element); 234 } 235 236 241 public NodeHandler getHandler(String id) { 242 return (NodeHandler) idHandlerMap.get(id); 243 } 244 245 250 public Element getElement(NodeHandler handler) { 251 return (Element ) handlerElementMap.get(handler); 252 } 253 254 public Element getElement(String id) { 255 return DomUtils.findElementWithId(id, document.getDocumentElement()); 256 } 257 258 } 259 | Popular Tags |