KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > form > FormFactory


1 /*
2  * $Id: FormFactory.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.widget.form;
25
26 import java.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import javax.servlet.ServletContext JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35
36 import org.ofbiz.base.location.FlexibleLocation;
37 import org.ofbiz.base.util.UtilHttp;
38 import org.ofbiz.base.util.UtilXml;
39 import org.ofbiz.base.util.cache.UtilCache;
40 import org.ofbiz.entity.GenericDelegator;
41 import org.ofbiz.service.LocalDispatcher;
42
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Element JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46
47
48 /**
49  * Widget Library - Form factory class
50  *
51  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
52  * @version $Rev: 5462 $
53  * @since 2.2
54  */

55 public class FormFactory {
56     
57     public static final String JavaDoc module = FormFactory.class.getName();
58
59     public static final UtilCache formLocationCache = new UtilCache("widget.form.locationResource", 0, 0, false);
60     public static final UtilCache formWebappCache = new UtilCache("widget.form.webappResource", 0, 0, false);
61     
62     public static ModelForm getFormFromLocation(String JavaDoc resourceName, String JavaDoc formName, GenericDelegator delegator, LocalDispatcher dispatcher)
63             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
64         Map JavaDoc modelFormMap = (Map JavaDoc) formLocationCache.get(resourceName);
65         if (modelFormMap == null) {
66             synchronized (FormFactory.class) {
67                 modelFormMap = (Map JavaDoc) formLocationCache.get(resourceName);
68                 if (modelFormMap == null) {
69                     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
70                     if (loader == null) {
71                         loader = FormFactory.class.getClassLoader();
72                     }
73                     
74                     URL JavaDoc formFileUrl = null;
75                     formFileUrl = FlexibleLocation.resolveLocation(resourceName); //, loader);
76
Document JavaDoc formFileDoc = UtilXml.readXmlDocument(formFileUrl, true);
77                     modelFormMap = readFormDocument(formFileDoc, delegator, dispatcher);
78                     formLocationCache.put(resourceName, modelFormMap);
79                 }
80             }
81         }
82         
83         ModelForm modelForm = (ModelForm) modelFormMap.get(formName);
84         if (modelForm == null) {
85             throw new IllegalArgumentException JavaDoc("Could not find form with name [" + formName + "] in class resource [" + resourceName + "]");
86         }
87         return modelForm;
88     }
89     
90     public static ModelForm getFormFromWebappContext(String JavaDoc resourceName, String JavaDoc formName, HttpServletRequest JavaDoc request)
91             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
92         String JavaDoc webappName = UtilHttp.getApplicationName(request);
93         String JavaDoc cacheKey = webappName + "::" + resourceName;
94         
95         
96         Map JavaDoc modelFormMap = (Map JavaDoc) formWebappCache.get(cacheKey);
97         if (modelFormMap == null) {
98             synchronized (FormFactory.class) {
99                 modelFormMap = (Map JavaDoc) formWebappCache.get(cacheKey);
100                 if (modelFormMap == null) {
101                     ServletContext JavaDoc servletContext = (ServletContext JavaDoc) request.getAttribute("servletContext");
102                     GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
103                     LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
104                     
105                     URL JavaDoc formFileUrl = servletContext.getResource(resourceName);
106                     Document JavaDoc formFileDoc = UtilXml.readXmlDocument(formFileUrl, true);
107                     modelFormMap = readFormDocument(formFileDoc, delegator, dispatcher);
108                     formWebappCache.put(cacheKey, modelFormMap);
109                 }
110             }
111         }
112         
113         ModelForm modelForm = (ModelForm) modelFormMap.get(formName);
114         if (modelForm == null) {
115             throw new IllegalArgumentException JavaDoc("Could not find form with name [" + formName + "] in webapp resource [" + resourceName + "] in the webapp [" + webappName + "]");
116         }
117         return modelForm;
118     }
119     
120     public static Map JavaDoc readFormDocument(Document JavaDoc formFileDoc, GenericDelegator delegator, LocalDispatcher dispatcher) {
121         Map JavaDoc modelFormMap = new HashMap JavaDoc();
122         if (formFileDoc != null) {
123             // read document and construct ModelForm for each form element
124
Element JavaDoc rootElement = formFileDoc.getDocumentElement();
125             List JavaDoc formElements = UtilXml.childElementList(rootElement, "form");
126             Iterator JavaDoc formElementIter = formElements.iterator();
127             while (formElementIter.hasNext()) {
128                 Element JavaDoc formElement = (Element JavaDoc) formElementIter.next();
129                 ModelForm modelForm = new ModelForm(formElement, delegator, dispatcher);
130                 modelFormMap.put(modelForm.getName(), modelForm);
131             }
132         }
133         return modelFormMap;
134     }
135 }
136
Popular Tags