⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formfactory.java

📁 国外的一套开源CRM
💻 JAVA
字号:
/*
 * $Id: FormFactory.java,v 1.1 2003/08/17 08:40:13 ajzeneski Exp $
 *
 * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.ofbiz.content.widget.form;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.ParserConfigurationException;

import org.ofbiz.base.util.UtilCache;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilXml;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.service.LocalDispatcher;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;


/**
 * Widget Library - Form factory class
 *
 * @author     <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
 * @version    $Revision: 1.1 $
 * @since      2.2
 */
public class FormFactory {
    
    public static final String module = FormFactory.class.getName();

    public static final UtilCache formClassCache = new UtilCache("widget.form.classResource", 0, 0, false);
    public static final UtilCache formWebappCache = new UtilCache("widget.form.webappResource", 0, 0, false);
    
    public static ModelForm getFormFromClass(String resourceName, String formName, GenericDelegator delegator, LocalDispatcher dispatcher) 
            throws IOException, SAXException, ParserConfigurationException {
        Map modelFormMap = (Map) formClassCache.get(resourceName);
        if (modelFormMap == null) {
            synchronized (FormFactory.class) {
                modelFormMap = (Map) formClassCache.get(resourceName);
                if (modelFormMap == null) {
                    ClassLoader loader = Thread.currentThread().getContextClassLoader();
                    if (loader == null) {
                        loader = FormFactory.class.getClassLoader();
                    }
                    
                    URL formFileUrl = loader.getResource(resourceName);
                    Document formFileDoc = UtilXml.readXmlDocument(formFileUrl, true);
                    modelFormMap = readFormDocument(formFileDoc, delegator, dispatcher);
                    formClassCache.put(resourceName, modelFormMap);
                }
            }
        }
        
        ModelForm modelForm = (ModelForm) modelFormMap.get(formName);
        if (modelForm == null) {
            throw new IllegalArgumentException("Could not find form with name [" + formName + "] in class resource [" + resourceName + "]");
        }
        return modelForm;
    }
    
    public static ModelForm getFormFromWebappContext(String resourceName, String formName, HttpServletRequest request) 
            throws IOException, SAXException, ParserConfigurationException {
        String webappName = UtilHttp.getApplicationName(request);
        String cacheKey = webappName + "::" + resourceName;
        
        
        Map modelFormMap = (Map) formWebappCache.get(cacheKey);
        if (modelFormMap == null) {
            synchronized (FormFactory.class) {
                modelFormMap = (Map) formWebappCache.get(cacheKey);
                if (modelFormMap == null) {
                    ServletContext servletContext = (ServletContext) request.getAttribute("servletContext");
                    GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
                    LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
                    
                    URL formFileUrl = servletContext.getResource(resourceName);
                    Document formFileDoc = UtilXml.readXmlDocument(formFileUrl, true);
                    modelFormMap = readFormDocument(formFileDoc, delegator, dispatcher);
                    formWebappCache.put(cacheKey, modelFormMap);
                }
            }
        }
        
        ModelForm modelForm = (ModelForm) modelFormMap.get(formName);
        if (modelForm == null) {
            throw new IllegalArgumentException("Could not find form with name [" + formName + "] in webapp resource [" + resourceName + "] in the webapp [" + webappName + "]");
        }
        return modelForm;
    }
    
    public static Map readFormDocument(Document formFileDoc, GenericDelegator delegator, LocalDispatcher dispatcher) {
        Map modelFormMap = new HashMap();
        if (formFileDoc != null) {
            // read document and construct ModelForm for each form element
            Element rootElement = formFileDoc.getDocumentElement();
            List formElements = UtilXml.childElementList(rootElement, "form");
            Iterator formElementIter = formElements.iterator();
            while (formElementIter.hasNext()) {
                Element formElement = (Element) formElementIter.next();
                ModelForm modelForm = new ModelForm(formElement, delegator, dispatcher);
                modelFormMap.put(modelForm.getName(), modelForm);
            }
        }
        return modelFormMap;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -