001: /*
002: * $Id: FormFactory.java,v 1.1 2003/08/17 08:40:13 ajzeneski Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.content.widget.form;
025:
026: import java.io.IOException;
027: import java.net.URL;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032:
033: import javax.servlet.ServletContext;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.xml.parsers.ParserConfigurationException;
036:
037: import org.ofbiz.base.util.UtilCache;
038: import org.ofbiz.base.util.UtilHttp;
039: import org.ofbiz.base.util.UtilXml;
040: import org.ofbiz.entity.GenericDelegator;
041: import org.ofbiz.service.LocalDispatcher;
042: import org.w3c.dom.Document;
043: import org.w3c.dom.Element;
044: import org.xml.sax.SAXException;
045:
046: /**
047: * Widget Library - Form factory class
048: *
049: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
050: * @version $Revision: 1.1 $
051: * @since 2.2
052: */
053: public class FormFactory {
054:
055: public static final String module = FormFactory.class.getName();
056:
057: public static final UtilCache formClassCache = new UtilCache(
058: "widget.form.classResource", 0, 0, false);
059: public static final UtilCache formWebappCache = new UtilCache(
060: "widget.form.webappResource", 0, 0, false);
061:
062: public static ModelForm getFormFromClass(String resourceName,
063: String formName, GenericDelegator delegator,
064: LocalDispatcher dispatcher) throws IOException,
065: SAXException, ParserConfigurationException {
066: Map modelFormMap = (Map) formClassCache.get(resourceName);
067: if (modelFormMap == null) {
068: synchronized (FormFactory.class) {
069: modelFormMap = (Map) formClassCache.get(resourceName);
070: if (modelFormMap == null) {
071: ClassLoader loader = Thread.currentThread()
072: .getContextClassLoader();
073: if (loader == null) {
074: loader = FormFactory.class.getClassLoader();
075: }
076:
077: URL formFileUrl = loader.getResource(resourceName);
078: Document formFileDoc = UtilXml.readXmlDocument(
079: formFileUrl, true);
080: modelFormMap = readFormDocument(formFileDoc,
081: delegator, dispatcher);
082: formClassCache.put(resourceName, modelFormMap);
083: }
084: }
085: }
086:
087: ModelForm modelForm = (ModelForm) modelFormMap.get(formName);
088: if (modelForm == null) {
089: throw new IllegalArgumentException(
090: "Could not find form with name [" + formName
091: + "] in class resource [" + resourceName
092: + "]");
093: }
094: return modelForm;
095: }
096:
097: public static ModelForm getFormFromWebappContext(
098: String resourceName, String formName,
099: HttpServletRequest request) throws IOException,
100: SAXException, ParserConfigurationException {
101: String webappName = UtilHttp.getApplicationName(request);
102: String cacheKey = webappName + "::" + resourceName;
103:
104: Map modelFormMap = (Map) formWebappCache.get(cacheKey);
105: if (modelFormMap == null) {
106: synchronized (FormFactory.class) {
107: modelFormMap = (Map) formWebappCache.get(cacheKey);
108: if (modelFormMap == null) {
109: ServletContext servletContext = (ServletContext) request
110: .getAttribute("servletContext");
111: GenericDelegator delegator = (GenericDelegator) request
112: .getAttribute("delegator");
113: LocalDispatcher dispatcher = (LocalDispatcher) request
114: .getAttribute("dispatcher");
115:
116: URL formFileUrl = servletContext
117: .getResource(resourceName);
118: Document formFileDoc = UtilXml.readXmlDocument(
119: formFileUrl, true);
120: modelFormMap = readFormDocument(formFileDoc,
121: delegator, dispatcher);
122: formWebappCache.put(cacheKey, modelFormMap);
123: }
124: }
125: }
126:
127: ModelForm modelForm = (ModelForm) modelFormMap.get(formName);
128: if (modelForm == null) {
129: throw new IllegalArgumentException(
130: "Could not find form with name [" + formName
131: + "] in webapp resource [" + resourceName
132: + "] in the webapp [" + webappName + "]");
133: }
134: return modelForm;
135: }
136:
137: public static Map readFormDocument(Document formFileDoc,
138: GenericDelegator delegator, LocalDispatcher dispatcher) {
139: Map modelFormMap = new HashMap();
140: if (formFileDoc != null) {
141: // read document and construct ModelForm for each form element
142: Element rootElement = formFileDoc.getDocumentElement();
143: List formElements = UtilXml.childElementList(rootElement,
144: "form");
145: Iterator formElementIter = formElements.iterator();
146: while (formElementIter.hasNext()) {
147: Element formElement = (Element) formElementIter.next();
148: ModelForm modelForm = new ModelForm(formElement,
149: delegator, dispatcher);
150: modelFormMap.put(modelForm.getName(), modelForm);
151: }
152: }
153: return modelFormMap;
154: }
155: }
|