KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > form > XmlComponent


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.form;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpSession JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.jaxen.JaxenException;
26 import org.jaxen.dom.DOMXPath;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
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 /**
39  * Implementation of a Comonent with support for NodeHandlers. The DOM Document
40  * is kept in memory.
41  * <p>
42  * The DOM may contain Elements with a handler attribute that contain the class
43  * names of NodeHandlers. Example
44  * <pre>
45  * &lt;myelem&gt;
46  * &lt;my-other-elem handler="my.pkg.ClassName"/&gt;
47  * &lt;/myelem&gt;
48  * </pre>
49  * XmlComponent will create an instance of my.pkg.ClassName and call its initialize()
50  * method with the my-other-elem Element as parameter. The NodeHandler may then choose
51  * to register itself as FormListener or RequestListener with the Environment.
52  *
53  * @author av
54  */

55 public class XmlComponent extends ComponentSupport {
56   private static Logger logger = Logger.getLogger(XmlComponent.class);
57   Document JavaDoc document;
58   Map JavaDoc elementHandlerMap;
59   Map JavaDoc idHandlerMap;
60   Map JavaDoc handlerElementMap;
61
62   /**
63    * creates a Component
64    * @param document the document to use.
65    */

66   public XmlComponent(String JavaDoc id, Component parent, Document JavaDoc document) {
67     super(id, parent);
68     this.document = document;
69   }
70
71   public void initialize(RequestContext context) throws Exception JavaDoc {
72     super.initialize(context);
73     hideSecuredElements(context, document);
74     installNodeHandlers(context, document);
75   }
76
77   public void destroy(HttpSession JavaDoc session) throws Exception JavaDoc {
78     for (Iterator JavaDoc it = elementHandlerMap.values().iterator(); it.hasNext();) {
79       NodeHandler nh = (NodeHandler) it.next();
80       nh.destroy(session);
81     }
82     super.destroy(session);
83   }
84
85   /**
86    * @see com.tonbeller.wcf.component.Component#render(RequestContext)
87    */

88   public Document JavaDoc render(RequestContext context) throws Exception JavaDoc {
89     for (Iterator JavaDoc it = elementHandlerMap.values().iterator(); it.hasNext();) {
90       NodeHandler nh = (NodeHandler) it.next();
91       nh.render(context);
92     }
93     return document;
94   }
95
96   /**
97    * an object that is attached to a DOM Element.
98    * <pre>
99    * &lt;myelem&gt;
100    * &lt;my-other-elem handler="my.pkg.ClassName"/&gt;
101    * &lt;/myelem&gt;
102    * </pre>
103    * install() will create an instance of my.pkg.ClassName and call its initialize()
104    * method with the my-other-elem Element as parameter.
105    * <p />
106    *
107    * @return Map maps the Elements to the created NodeHandlers (i.e. map.get(Element) returns NodeHandler)
108    */

109   void installNodeHandlers(RequestContext context, Document JavaDoc root) throws Exception JavaDoc {
110     try {
111       elementHandlerMap = new HashMap JavaDoc();
112       handlerElementMap = new HashMap JavaDoc();
113       idHandlerMap = new HashMap JavaDoc();
114       List JavaDoc elements = findHandlerElements(root);
115       for (Iterator JavaDoc it = elements.iterator(); it.hasNext();) {
116         Element JavaDoc elem = (Element JavaDoc) it.next();
117         String JavaDoc 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 JavaDoc e) {
128       logger.error(null, e);
129       throw new SoftException(e);
130     } catch (IllegalAccessException JavaDoc e) {
131       logger.error(null, e);
132       throw new SoftException(e);
133     } catch (ClassNotFoundException JavaDoc e) {
134       logger.error(null, e);
135       throw new SoftException(e);
136     }
137   }
138
139   private List JavaDoc findHandlerElements(Document JavaDoc 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 JavaDoc root, String JavaDoc xpath, String JavaDoc clazz) throws JaxenException {
151     DOMXPath xp = new DOMXPath(xpath);
152     List JavaDoc list = xp.selectNodes(root);
153     for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
154       Element JavaDoc e = (Element JavaDoc) it.next();
155       if (e.getAttribute("handler").length() == 0 && e.getAttribute("actionReference").length() == 0)
156         e.setAttribute("handler", clazz);
157     }
158   }
159   
160   /**
161    * sets the hidden attribute of elements that have a role attribute.
162    */

163   void hideSecuredElements(RequestContext context, Document JavaDoc root) throws Exception JavaDoc {
164     HttpServletRequest JavaDoc req = context.getRequest();
165     try {
166       DOMXPath xp = new DOMXPath("//*[@role]");
167       List JavaDoc elements = xp.selectNodes(root);
168       for (Iterator JavaDoc it = elements.iterator(); it.hasNext();) {
169         Element JavaDoc elem = (Element JavaDoc) it.next();
170         String JavaDoc role = elem.getAttribute("role");
171         if (context.isUserInRole(role))
172           XoplonCtrl.setHidden(elem, false); // DomUtils.removeAttribute(elem, "hidden");
173
else
174           XoplonCtrl.setHidden(elem, true); // elem.setAttribute("hidden", "true");
175
}
176     } catch (JaxenException e) {
177       logger.error(null, e);
178       throw new SoftException(e);
179     }
180   }
181
182   /**
183    * returns NodeHandler of the parent DOM Element
184    */

185   public NodeHandler getParent(NodeHandler handler) {
186     Element JavaDoc elem = (Element JavaDoc) handlerElementMap.get(handler);
187     Node JavaDoc 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 JavaDoc getHandlers(NodeHandler rootHandler, String JavaDoc xpathExpr) {
198     try {
199       List JavaDoc handlerList = new ArrayList JavaDoc();
200       DOMXPath xp = new DOMXPath(xpathExpr);
201       Element JavaDoc rootElem = (Element JavaDoc) handlerElementMap.get(rootHandler);
202       List JavaDoc elements = xp.selectNodes(rootElem);
203       for (Iterator JavaDoc it = elements.iterator(); it.hasNext();) {
204         Element JavaDoc element = (Element JavaDoc) 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 JavaDoc getChildren(NodeHandler handler) {
217     return getHandlers(handler, ".//*[@handler]");
218   }
219
220   /**
221    * returns the current DOM
222    */

223   public Document JavaDoc getDocument() {
224     return document;
225   }
226
227   /**
228    * return the NodeHandler associated with element or null, if there is no such handler
229    * @param element
230    * @return NodeHandler
231    */

232   public NodeHandler getHandler(Element JavaDoc element) {
233     return (NodeHandler) elementHandlerMap.get(element);
234   }
235
236   /**
237    * return the NodeHandler whose element contains an Attribute "id"
238    * @param id value of the id attribute
239    * @return NodeHandler associated with that element or null
240    */

241   public NodeHandler getHandler(String JavaDoc id) {
242     return (NodeHandler) idHandlerMap.get(id);
243   }
244
245   /**
246    * return the Element that NodeHandler associated with, or null if there is no such element
247    * @param element
248    * @return NodeHandler
249    */

250   public Element JavaDoc getElement(NodeHandler handler) {
251     return (Element JavaDoc) handlerElementMap.get(handler);
252   }
253
254   public Element JavaDoc getElement(String JavaDoc id) {
255     return DomUtils.findElementWithId(id, document.getDocumentElement());
256   }
257
258 }
259
Popular Tags