com.easyjf.web.config.XMLConfigFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.easyjf.web.config.XMLConfigFactory.java

Source

/*
 * Copyright 2006-2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easyjf.web.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.easyjf.util.I18n;
import com.easyjf.util.StringUtils;
import com.easyjf.util.XmlElementUtil;
import com.easyjf.web.FormConfig;
import com.easyjf.web.FormProperty;
import com.easyjf.web.Globals;
import com.easyjf.web.Module;
import com.easyjf.web.Page;

/**
 * 
 * <p>
 * Title:XML????
 * </p>
 * <p>
 * Description:?XML?EasyJWeb??
 * </p>
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * <p>
 * Company: www.easyjf.com
 * </p>
 * 
 * @author ?
 * @version 1.0
 */

public class XMLConfigFactory implements IConfigFactory {

    private Document doc;

    private static final Logger logger = Logger.getLogger(XMLConfigFactory.class);

    /**
     * @deprecated
     * 
     */
    public XMLConfigFactory() {
        try {
            File file = new File(Globals.CONFIG_FILE_FULL_PATH);
            if (file.exists()) {
                FileInputStream in = new FileInputStream(file);
                SAXReader reader = new SAXReader();
                doc = reader.read(in);
            } else {
                logger.warn(I18n.getLocaleMessage("core.web.unable.to.find.configuration.file")
                        + Globals.CONFIG_FILE_FULL_PATH + I18n.getLocaleMessage(
                                "core.web.EasyJWEB.will.automatically.use.the.default.path.processing.Action"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.warn(I18n.getLocaleMessage("core.web.configuration.file.errors") + e);
        }
    }

    public XMLConfigFactory(InputStream in) {
        try {

            SAXReader reader = new SAXReader();
            doc = reader.read(in);
        } catch (Exception e) {
            e.printStackTrace();
            //         logger.warn("??" + e);
            logger.warn(I18n.getLocaleMessage("core.web.unable.to.find.configuration.file")
                    + Globals.CONFIG_FILE_FULL_PATH + ","
                    + I18n.getLocaleMessage("core.web.configuration.file.errors") + I18n.getLocaleMessage(
                            "core.web.EasyJWEB.will.automatically.use.the.default.path.processing.Action"));
        }
    }

    private Element getRootElement() {
        if (doc == null)
            return null;
        return doc.getRootElement();
    }

    private Element findElement(String name, Element el) {
        return XmlElementUtil.findElement(name, el);
    }

    private List findElements(String name, Element el) {
        return XmlElementUtil.findElements(name, el);
    }

    public void initForm(Map forms) {
        Element root = findElement("forms", getRootElement());
        if (root == null)
            return;
        List nodes = root.elements();
        // ??forms
        // formsform??
        // FromConfig?forms
        for (int i = 0; i < nodes.size(); i++) {
            Element e = (Element) nodes.get(i);
            FormConfig fc = new FormConfig();
            // ???js?page????EasyJWeb??
            fc.setAlertType(e.attributeValue("alertType"));
            // Formbean??WebForm
            fc.setBean(e.attributeValue("bean"));
            // ???
            fc.setClientValidate(e.attributeValue("clientValidate"));
            // form??
            fc.setName(e.attributeValue("name"));
            // ????
            fc.setServerValidate(e.attributeValue("serverValidate"));
            // Form
            String events = e.attributeValue("event");
            if (events != null && (!events.equals(""))) {
                String[] s = events.split(",");
                if (s != null) {
                    List event = new ArrayList();
                    for (int t = 0; t < s.length; t++) {
                        if (!s[t].equals(""))
                            event.add(s[t]);
                    }
                    fc.setEvent(event);
                }
            }
            // form
            List lPage = findElements("property", e);
            Map pages = new HashMap();
            for (int j = 0; j < lPage.size(); j++) {

                Element p = (Element) lPage.get(j);
                FormProperty prop = new FormProperty();
                prop.setInitial(p.attributeValue("initial"));
                prop.setName(p.attributeValue("name"));
                prop.setNotNull(p.attributeValue("notNull"));
                prop.setSize(p.attributeValue("size"));
                prop.setType(p.attributeValue("type"));
                events = p.attributeValue("event");
                if (events != null && (!events.equals(""))) {
                    String[] s = events.split(",");
                    if (s != null) {
                        List event = new ArrayList();
                        for (int t = 0; t < s.length; t++) {
                            if (!s[t].equals(""))
                                event.add(s[t]);
                        }
                        prop.setEvent(event);
                    }
                }
                pages.put(prop.getName(), prop);
            }
            fc.setPropertys(pages);
            forms.put(fc.getName(), fc);
        }
    }

    public void initModule(Map modules) {
        Element root = findElement("modules", getRootElement());
        if (root == null)
            return;

        // ??modules
        // modulesmodule??
        // FromConfig?forms
        String injectType = "none";
        if (root.attributeValue("inject") != null)
            injectType = root.attributeValue("inject");
        List nodes = root.elements();
        for (int i = 0; i < nodes.size(); i++) {
            Element e = (Element) nodes.get(i);
            Module mc = new Module();
            // ??action
            mc.setAction(e.attributeValue("action"));
            // ?token      
            if (e.attributeValue("autoToken") != null)
                mc.setAutoToken(Boolean.valueOf(e.attributeValue("autoToken")));
            if (e.attributeValue("alias") != null) {
                mc.setAlias(e.attributeValue("alias"));
            }
            // ?
            if (e.attributeValue("defaultPage") != null)
                mc.setDefaultPage(e.attributeValue("defaultPage"));
            // ??         
            mc.setForm(e.attributeValue("form"));
            // ? /hello.ejf /hello
            mc.setPath(e.attributeValue("path"));
            // bean
            // singleton
            if (e.attributeValue("scope") != null)
                mc.setScope(e.attributeValue("scope"));
            if (e.attributeValue("method") != null)
                mc.setMethod(e.attributeValue("method"));
            if (e.attributeValue("inject") != null)
                mc.setInject(e.attributeValue("inject"));
            else
                mc.setInject(injectType);
            if (e.attributeValue("views") != null)
                mc.setViews(e.attributeValue("views"));
            if (e.attributeValue("view") != null)
                mc.setViews(e.attributeValue("view"));
            if (e.attributeValue("validate") != null)
                mc.setValidate(Boolean.parseBoolean(e.attributeValue("validate")));
            if (e.attributeValue("messageResource") != null) {
                mc.setMessageResource(e.attributeValue("messageResource"));
            }
            List lPage = findElements("page", e);
            for (int j = 0; j < lPage.size(); j++) {
                Element p = (Element) lPage.get(j);
                Page page = new Page();
                // Page?? modulePage??
                page.setName(p.attributeValue("name"));
                if (mc.getDefaultPage() == null || mc.getDefaultPage().equals("")) {
                    mc.setDefaultPage(page.getName());
                }
                // Page html/template
                // ?,html
                page.setType(p.attributeValue("type"));
                // Pageurl,
                page.setUrl(p.attributeValue("url"));
                if (p.attributeValue("contentType") != null)
                    page.setContentType(p.attributeValue("contentType"));
                // pages.put(page.getName(), page);
                mc.getPages().put(page.getName(), page);
            }
            // ,??
            List interceptors = findElements("interceptor", e);
            for (int j = 0; j < interceptors.size(); j++) {
                Element p = (Element) interceptors.get(j);
                String interceptorClazz = p.attributeValue("class");
                try {
                    mc.getInterceptors().add(Class.forName(interceptorClazz).newInstance());
                } catch (Exception ex) {
                }
            }
            // ?modulepropertyproperty
            mc.setPropertyValues(BeanConfigReader.parsePropertyValues(findElements("property", e)));
            modules.put(mc.getPath(), mc);

        }
    }

    public void initPage(Map pages) {
        Element root = findElement("pages", getRootElement());
        if (root == null)
            return;
        List nodes = root.elements();
        for (int i = 0; i < nodes.size(); i++) {
            Element e = (Element) nodes.get(i);
            Page page = new Page();
            page.setName(e.attributeValue("name"));
            page.setType(e.attributeValue("type"));
            page.setUrl(e.attributeValue("url"));
            if (e.attributeValue("contentType") != null)
                page.setContentType(e.attributeValue("contentType"));
            pages.put(page.getName(), page);
        }
    }

    public Map initOther() {
        if (doc == null)
            return null;
        Map result = new HashMap();
        Element root = findElement("framework-setting", getRootElement());
        if (root == null) {
            root = findElement("frame-setting", getRootElement());
        }
        if (root != null) {
            // 
            Element node = findElement("template-base", root);
            if (node != null)
                result.put("TemplateBasePath", node.getText());
            // ???
            node = findElement("init-app", root);
            List appList = findElements("app-class", node);
            List applist = new ArrayList();
            for (int i = 0; i < appList.size(); i++) {
                Element e = (Element) appList.get(i);
                Map app = new HashMap();
                app.put("init-method", e.attributeValue("init-method"));
                app.put("destroy-method", e.attributeValue("destroy-method"));
                app.put("class", e.getText());
                applist.add(app);
            }
            result.put("initApp", applist);

            // ?
            node = findElement("interceptors", root);
            List interceptors = findElements("app-class", node);
            List appinterceptorslist = new ArrayList();
            if (interceptors != null) {
                for (int i = 0; i < interceptors.size(); i++) {
                    Element e = (Element) interceptors.get(i);
                    Map app = new HashMap();
                    app.put("name", e.attributeValue("name"));
                    app.put("method", e.attributeValue("method"));
                    app.put("class", e.getText());
                    appinterceptorslist.add(app);
                }
            }
            result.put("interceptors", appinterceptorslist);

            // ???
            node = findElement("error-handler", root);
            List errorHandlers = findElements("app-class", node);
            if (errorHandlers != null) {
                for (int i = 0; i < errorHandlers.size(); i++) {
                    Element e = (Element) errorHandlers.get(i);
                    Map app = new HashMap();
                    app.put("exception", e.attributeValue("exception"));
                    app.put("path", e.attributeValue("path"));
                    app.put("class", e.getText());
                    errorHandlers.set(i, app);
                }
            }
            result.put("errorHandlers", errorHandlers);
            // ????   
            List list = findElements("property", root);
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    Element el = (Element) list.get(i);
                    result.put(el.attributeValue("name"), el.getText());
                }
            }
        }
        List importResources = new java.util.ArrayList();
        List list = findElements("import", getRootElement());
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                Element el = (Element) list.get(i);
                String resource = el.attributeValue("resource");
                if (resource != null && !"".equals(resource)) {
                    //?/WEB-INFclasspath
                    if (resource.toLowerCase().indexOf("web-inf") < 0 && resource.indexOf("classpath") != 0)
                        resource = "/WEB-INF/" + resource;
                    importResources.add(resource);
                }
            }
        }
        result.put("importResources", importResources);
        // EasyJWebBean??bean beanDefinitions
        result.put("beanDefinitions", BeanConfigReader.parseBeansFromDocument(doc));
        return result;
    }

    public Document parse(String fileName) throws DocumentException {
        SAXReader reader = new SAXReader();
        File file = new File(fileName);
        Document document = file.exists() ? reader.read(fileName) : null;
        return document;
    }

    public Document getDoc() {
        return doc;
    }

}