• xml建模


    1.什么是XML建模
    将XML配置文件中的元素,属性,文本信息 转换成对象的过程叫XML建模。
    2.XML建模
    根据XML配置文件元素节点创建元素,节点,实体类
    ConfigModel ActionModel ForwardModel
    利用dom4j+xpath技术实现XML建模ConfigModelFactory(提高代码的复用性)

    建模思路

    1、分析需要被建模的文件中有那几个对象
    2、每个对象拥有的行为以及属性
    3、根据XML中元素节点 情况 ConfigModel , ActionModel , ForwardModel 对象模型。 (定义对象从里到外)

    package com.model;
    
    import java.io.InputStream;
    import java.util.List;
    
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    public class webModelFactory {
    
        public static webmodel buildwebmodel() {
            String xmlPath="/web.xml";
            return buildWebAppModel(xmlPath);
        }
        /**
         * 建模
         * 
         * @param xmlPath
         * @return
         */
        public static webmodel buildWebAppModel(String xmlPath) {
            InputStream in = webModelFactory.class.getResourceAsStream(xmlPath);
            SAXReader saxReader = new SAXReader();
            webmodel webAppModel = new webmodel();
            try {
                Document doc = saxReader.read(in);
                /*
                 * 将servlet的标签内容填充进WebApp
                 */
                List<Element> servletEles = doc.selectNodes("/web-app/servlet");
                for (Element servletEle : servletEles) {
                    servletmodel servletModel = new servletmodel();
    
                    /*
                     * 给ServletModel填充xml的内容
                     */
                    Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
                    Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
                    servletNamemodel servletNameModel = new servletNamemodel();
                    servletclassmodel servletClassModel = new servletclassmodel();
                    servletNameModel.setContext(servletNameEle.getText());
                    servletClassModel.setContext(servletClassEle.getText());
                    
                    servletModel.setServletNameModel(servletNameModel);
                    servletModel.setServletClassModel(servletClassModel);
    
                    webAppModel.pushservletModel(servletModel);
                }
    
                /*
                 * 将servlet-mapping的标签内容填充进WebApp
                 */
                List<Element> servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
                for (Element servletMappingEle : servletMappingEles) {
                    servletmappingmodel servletMappingModel = new servletmappingmodel();
    
                    /*
                     * 给ServletMappingModel填充xml的内容
                     */
                    Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
                    servletNamemodel servletNameModel = new servletNamemodel();
                    servletNameModel.setContext(servletNameEle.getText());
                    servletMappingModel.setServletNamemodel(servletNameModel);
                    
                    List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
                    for (Element urlPatternEle : urlPatternEles) {
                        urlpatternmodel urlPatternModel = new urlpatternmodel();
                        urlPatternModel.setContext(urlPatternEle.getText());
                        servletMappingModel.pushUrlPatternModel(urlPatternModel);
                    }
    
                    webAppModel.pushservletMappingModel(servletMappingModel);
                }
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return webAppModel;
        }
        
        /**
         * 通过浏览器输入的网址自动找到对应的后台处理类
         * @param webAppModel    建模后的实体类
         * @param url    浏览器访问的网址
         * @return
         */
        public static String getServletClassByUrl(webmodel webModel, String url) {
            String servletClass = "";
            /*
             * 找到浏览器网址对应的servlet-name
             */
            String servletName = "";
            List<servletmappingmodel> servletMappingModels = webModel.getServletmappingmodels();
            for (servletmappingmodel servletMappingModel : servletMappingModels) {
                List<urlpatternmodel> urlPatternModels = servletMappingModel.getUrlpatternmodels();
                for (urlpatternmodel urlPatternModel : urlPatternModels) {
                    if(url.equals(urlPatternModel.getContext())) {
                        servletNamemodel servletNameModel = servletMappingModel.getServletNameModel();
                        servletName = servletNameModel.getContext();
                    }
                }
            }
            
            /*
             * 找到servlet-name对应的后台处理类
             */
            List<servletmodel> servletModels = webModel.getServletmodels();
            for (servletmodel servletModel : servletModels) {
                servletNamemodel servletNameModel = servletModel.getServletNameModel();
                if(servletName.equals(servletNameModel.getContext())) {
                    servletclassmodel servletClassModel = servletModel.getServletClassModel();
                    servletClass = servletClassModel.getContext();
                }
            }
            return servletClass;
        }
        
        public static void main(String[] args) {
            webmodel webAppModel = webModelFactory.buildwebmodel();
            String res = getServletClassByUrl(webAppModel, "/jrebelServlet");
            String res2 = getServletClassByUrl(webAppModel, "/jrebelServlet2");
            String res3 = getServletClassByUrl(webAppModel, "/jrebelServlet3");
            System.out.println(res);
            System.out.println(res2);
            System.out.println(res3);
            
        }
    }
    

      

  • 相关阅读:
    webstorm
    呐,这是某蒟蒻幼稚的博客 ~~Welcome
    CSP-S 2021 补题记录
    CSP-S 2021 游记
    Tarjan 算法小结
    FHQ Treap 浅析
    2048游戏 (C++ Windows)
    线段树 算法分析
    树状数组 算法分析
    数学期望(ξ) 浅析
  • 原文地址:https://www.cnblogs.com/omji0030/p/11005760.html
Copyright © 2020-2023  润新知