栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

xml建模

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

xml建模

目录

一、xml建模

二、工厂建模

三、案例解析

 【案例1】


一、xml建模

常用到:

举例一个XML------config.xml


        
        
    

    
        
        
    

ConfigModel

package com.lucy.model;

import java.util.HashMap;
import java.util.Map;

public class configModel {
        private Map amap=new HashMap();
        //增加action标签对象
        public void push(ActionModel a) {
            amap.put(a.getPath(), a);
        }
    
    public  ActionModel pop(String path) {
        return  amap.get(path);
    }  
}
 

ActionModel

package com.lucy.model;

import java.util.HashMap;
import java.util.Map;

public class ActionModel {
    //    

            private String  path;
            private String type;
            private Map fmap=new HashMap();
            public String getPath() {
                return path;
            }
            public void setPath(String path) {
                this.path = path;
            }
            public String getType() {
                return type;
            }
            public void setType(String type) {
                this.type = type;
            }
            //两个行为  增加、查找forwardModel对象
            //将一个新的forward标签对象加入容器
            public  void push(ForwardModel f) {//增加
            fmap.put(f.getName(),f);//加入属性
            }
            //根据属性名查找标签对象
            public ForwardModel pop(String name) {
                return fmap.get(name);
            }  
            
}
 

ForwardModel

package com.lucy.model;

public class ForwardModel {
    //
    
        private String  name;
        private String  path;
        private boolean redirect;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPath() {
            return path;
        }
        public void setPath(String path) {
            this.path = path;
        }
        public boolean isRedirect() {
            return redirect;
        }
        public void setRedirect(boolean redirect) {
            this.redirect = redirect;
        }
}
 

ConfigModelFactory

public static configModel bulid(String path)throws Exception {
            InputStream in=ConfigModelFactory.class.getResourceAsStream(path);
            SAXReader sr=new SAXReader();
            Document doc=sr.read(in);
            //将actionmodel赋值并且添加configmodel里
        List actionEles=doc.selectNodes("/config/action");
        configModel  configModel=new configModel();
            for (Element actionEle : actionEles) {
                //实例化action对象
                ActionModel actionModel=new ActionModel();
                //加属性
                actionModel.setPath(actionEle.attributeValue("path"));
                actionModel.setType(actionEle.attributeValue("type"));
//                将forwardmodel赋值并且添加actionmodel里
            List forwardEles=actionEle.selectNodes("forward");
            for (Element s : forwardEles) {
                ForwardModel forwardModel=new ForwardModel();
                forwardModel.setName(s.attributeValue("name"));
                forwardModel.setPath(s.attributeValue("path"));
//                redirect 赋值并且增加actionModel里
                forwardModel.setRedirect("true".equals(s.attributeValue("redirect")));
                actionModel.push(forwardModel);
            }
                configModel.push(actionModel);//把action标签加进去
            }
            return configModel;
        }
    public static configModel bulid() throws Exception {
            String defaultPath="config.xml";
            return bulid(defaultPath); 
    }


二、工厂建模

23种设计模式之工厂 Factory 模式:
    configModelFactory是用来生产configModel对象的
     生产出来的 configModel对象就包含了config.xml的配置内容
     1.此地生产configModel有配置信息
     2.将对应的配置信息分别加载进行不同的模型对象里 
 

package com.lucy.model;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class ConfigModelFactory {
        public static configModel bulid(String path)throws Exception {
            InputStream in=ConfigModelFactory.class.getResourceAsStream(path);
            SAXReader sr=new SAXReader();
            Document doc=sr.read(in);
            //将actionmodel赋值并且添加configmodel里
        List actionEles=doc.selectNodes("/config/action");
        configModel  configModel=new configModel();
            for (Element actionEle : actionEles) {
                //实例化action对象
                ActionModel actionModel=new ActionModel();
                //加属性
                actionModel.setPath(actionEle.attributeValue("path"));
                actionModel.setType(actionEle.attributeValue("type"));
//                将forwardmodel赋值并且添加actionmodel里
            List forwardEles=actionEle.selectNodes("forward");
            for (Element s : forwardEles) {
                ForwardModel forwardModel=new ForwardModel();
                forwardModel.setName(s.attributeValue("name"));
                forwardModel.setPath(s.attributeValue("path"));
//                redirect 赋值并且增加actionModel里
                forwardModel.setRedirect("true".equals(s.attributeValue("redirect")));
                actionModel.push(forwardModel);
            }
                configModel.push(actionModel);//把action标签加进去
            }
            return configModel;
        }
    public static configModel bulid() throws Exception {
            String defaultPath="config.xml";
            return bulid(defaultPath); 
    }
    
}
 


三、案例解析

package com.lucy.model;

public class Demo1 {//建模后使用的案例
        
            public static void main(String[] args) throws Exception {

//                configModel configModel=new configModel();
                configModel configModel    =ConfigModelFactory.bulid();
                ActionModel actionModel=configModel.pop("/loginAction");
//                System.out.println(actionModel.getType());
                    
                ForwardModel forwardModel=actionModel.pop("failed");
                System.out.println(forwardModel.getPath());
             
            }
}
 

 效果图;

 【案例1】

对web.xml进行建模

    



 
      jrebelServlet
      com.zking.xml.JrebelServlet
 

  
 
      jrebelServlet
      /jrebelServlet
 

  
 
      jrebelServlet2
      com.zking.xml.JrebelServlet2
 

  
 
      jrebelServlet2
      /jrebelServlet2
      /jrebelServlet3
 

 【注:UrlPatternModel,ServletClassModel和ServletNameModel 类内容一致】

package com.lucy.zy;

public class ServletNameModel {
            private String context;

            public String getContext() {
                return context;
            }

            public void setContext(String context) {
                this.context = context;
            };
            
    
}
 

 

package com.lucy.zy;

import java.util.ArrayList;
import java.util.List;

public class ServletMappingModel {
    private ServletNameModel servletNameModel;
    private List urlPatternModels = new ArrayList<>();
    public ServletNameModel getServletNameModel() {
        return servletNameModel;
    }
    public void setServletNameModel(ServletNameModel servletNameModel) {
        this.servletNameModel = servletNameModel;
    }
    
    public void pushUrlPatternModel(UrlPatternModel urlPatternModel) {
        urlPatternModels.add(urlPatternModel);
    }
    
    public List getUrlPatternModels() {
        return urlPatternModels;
    
    }
    
    
}
 

 

 

package com.lucy.zy;

public class ServletModel {
        
    
    private ServletNameModel servletNameModel ;
    private ServletClassModel servletClassModel;
    public ServletNameModel getServletNameModel() {
        return servletNameModel;
    }
    public void setServletNameModel(ServletNameModel servletNameModel) {
        this.servletNameModel = servletNameModel;
    }
    public ServletClassModel getServletClassModel() {
        return servletClassModel;
    }
    public void setServletClassModel(ServletClassModel servletClassModel) {
        this.servletClassModel = servletClassModel;
    } 
    
    
}
 

package com.lucy.zy;

import java.util.ArrayList;
import java.util.List;

public class WebappModel {

    private List servletModels = new ArrayList<>();
    private List servletMappingModels = new ArrayList<>();

    public void pushServletModel(ServletModel servletModel) {
        servletModels.add(servletModel);
    }
    
    public List getServletModels() {
        return servletModels;
    }
    
    public void pushServletMappingModel(ServletMappingModel servletMappingModel) {
        servletMappingModels.add(servletMappingModel);
    }
    
    public List getServletMappingModels() {
        return servletMappingModels;
    }
    
}
 

 

package com.lucy.zy;

import java.io.InputStream;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class WebAppModelFactory {
    
            public  static  WebappModel buildWebAppModel() {
                String xmlPath="/web.xml";
                return  buildWebAppModel(xmlPath);
        
    }
            public static WebappModel buildWebAppModel(String  xmlPath)  {
                InputStream in =WebAppModelFactory.class.getResourceAsStream(xmlPath);
                SAXReader sr=new SAXReader();
                WebappModel webappmodel=new WebappModel();
            try {
                
                 Document doc=sr.read(in);
                 //把servlet填充到web-app
                    List servletEles=doc.selectNodes("/web-app/servlet");
                for (Element servletEle : servletEles) {
                    ServletModel servletmodel=new ServletModel();
                    
                    //给servetModel填充xml内容
                Element  servletnameEle=(Element) servletEle.selectSingleNode("servlet-name");
                Element  servletclassEle=(Element) servletEle.selectSingleNode("servlet-class");
                //实例化ServletNameModel    ,ServletClassModel
                ServletNameModel servletnamemodel=new ServletNameModel();
                    ServletClassModel servletClassModel=new ServletClassModel(); 
                    //填充值
                    servletnamemodel.setContext(servletnameEle.getText());
                    servletClassModel.setContext(servletclassEle.getText());
                    //给servletModel赋值
                    servletmodel.setServletNameModel(servletnamemodel); 
                    servletmodel.setServletClassModel(servletClassModel);
                }
//                    将servlet-mapping的标签内填充入webapp 
//                    开始
                    
                    List servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
                    for (Element servletMappingEle : servletMappingEles) {
                        ServletMappingModel servletMappingModel = new ServletMappingModel();

                        
                        Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
                        ServletNameModel servletNameModel = new ServletNameModel();
                        servletNameModel.setContext(servletNameEle.getText());
                        servletMappingModel.setServletNameModel(servletNameModel);
                        
                        List 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;
            }
            
            
            public static String getServletClassByUrl(WebappModel webAppModel, String url) {
                String servletClass = "";
                
                String servletName = "";
                List servletMappingModels = webAppModel.getServletMappingModels();
                for (ServletMappingModel servletMappingModel : servletMappingModels) {
                    List urlPatternModels = servletMappingModel.getUrlPatternModels();
                    for (UrlPatternModel urlPatternModel : urlPatternModels) {
                        if(url.equals(urlPatternModel.getContext())) {
                            ServletNameModel servletNameModel = servletMappingModel.getServletNameModel();
                            servletName = servletNameModel.getContext();
                        }
                    }
                }
                
                
                List servletModels = webAppModel.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) {
                WebappModel webAppModel = WebAppModelFactory.buildWebAppModel();
                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);
                
            }
                    
         
}
 

 

通过url-pattern读取到servlet-calss的值

package com.lucy.zy;

import java.io.InputStream;

import org.dom4j.io.SAXReader;

public class Demo1 {
        public static void main(String[] args) {
//            
            WebappModel webappModel=WebAppModelFactory.buildWebAppModel();
            ServletModel servletmodel=webappModel.pushServletMappingModel();
            
            
        }
}
 

 ===========================不得转载和抄袭=============================

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/972826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号