XML建模

哈喽大家好~我又又又又来啦~~

根据前几天的博客我们可以发现重复性的代码非常多!!!对于同一个xml文件需要重复的去解析

那我们今天教大家如何用建模去解决这个问题!!

目录

一,xml建模

2,ActionModel:

3,ForwardModel:

二、工厂模式

三,案例

ServletClassModel:代码如下:

ServletMappingModel:

ServletModel:

UrlPatternModel:

WebappModel:

ServletNameModel:

WebappModelFactory:


话不多说,我们接下来进入正题!!!

一,xml建模

建模的原理:以面向对象的思想去操作一个xml文件

1、ConfigModel

代码如下:

package com.zlp.model;

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

/**
 * 根标签对应的对象
 * @author zjjt
 *
 */
public class ConfigModel {
	private Map<String, ActionModel> aMap = new HashMap<String, ActionModel>();
	public void push(ActionModel actionMoel) {
		aMap.put(actionMoel.getPath(), actionMoel);
	}
	
	public ActionModel pop(String path) {
		return aMap.get(path);
	}
}

2,ActionModel:

代码如下:

package com.zlp.model;

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

/**
 * 对应Action标签
 * @author zjjt
 *
 */
public class ActionModel {

	//<action path="/regAction" type="test.RegAction">
	private String path;
	private String type;
	private Map<String, ForwardModel> fMap = new HashMap<String, ForwardModel>();
	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对象,查找forewModel对象
	//将一个新的forward标签加入容器
	 public void push(ForwardModel forwardModel) {
		fMap.put(forwardModel.getName(), forwardModel);
	 }
	public ForwardModel pop(String name) {
		return fMap.get(name);
	}
}

3,ForwardModel:

代码如下:

package com.zlp.model;
/**
 * 对应forward标签 
 * @author zjjt
 *
 */
public class ForwardModel {
	//<forward name="failed" path="/login.jsp" redirect="false" />
	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;
	}
	
	
}

4、ConfigModelFactory(可参考工厂模式)

二、工厂模式

23种设计模式之工厂模式
 sessionfactory
 ConfigModelFactory生产出来的configmodel对象
 生产出来的 ConfigModel对象就包含了config.xml中的配置内容

下面就是我们的代码展示啦~

package com.zlp.model;
 
import java.io.InputStream;
import java.util.List;
 
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
 
/**
 * 23种设计模式之工厂模式
 * ConfigModelFactory就是用来生产configmodel对象的
 *	 生产出来的configmodel对象就包含了config.xml的配置内容
 * 
 *	 此地生产configmodel有配置信息?
 * 1,解析config.xml中的配置信息
 * 2.将对应的配置信息分别加载进行不同的配置
 * @author zjjt
 *
 */
public class ConfigModelFactory {
	public static ConfigModel bulid(String path) throws Exception {
		String defaultPath="/config.xml";
		InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
		SAXReader sr=new SAXReader();
		Document doc=sr.read(in);
		ConfigModel configModel=new ConfigModel();
		List<Element> actionEls = doc.selectNodes("/config/action");
		for (Element actionEle : actionEls) {
			ActionModel actionModel = new ActionModel();
			actionModel.setPath(actionEle.attributeValue("path"));
			actionModel.setType(actionEle.attributeValue("type"));
//			将forwardModel赋值并且添加到Actionmodel中
			List<Element> frowardEles = actionEle.selectNodes("forward");
			for (Element element : frowardEles) {
				ForwardModel forwardModel = new ForwardModel();
				forwardModel.setName(element.attributeValue("name"));
				forwardModel.setPath(element.attributeValue("path"));
//				Redirect:只能是false|true,允许空,默认为true
				forwardModel.setRedirect("true".equals(element.attributeValue("redirect")));
				actionModel.push(forwardModel);
			}
			configModel.push(actionModel);
		}
		return configModel;
	}
	public static ConfigModel bulid() throws Exception{
		String defaultPath="/config.xml";
		return bulid(defaultPath);
	}
}

 

package com.zlp.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("success");
		System.out.println(forwardModel.getPath());
	}
}

三,案例

1、对web.xml进行建模并通过url-pattern读取到servlet-class的值

ServletClassModel:
代码如下:

package com.zlp.model;

public class ServletclassModel {
    private String bb;

    public String getBb() {
        return bb;
    }

    public void setBb(String bb) {
        this.nr = bb;
    }
    
}

ServletMappingModel:

package com.zlp.model;

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

public class ServletmappingModel {
	private ServletnameModel bbb;
	private List<UrlpatternModel> ls=new ArrayList<UrlpatternModel>();
	public ServletnameModel getBbb() {
		return bbb;
	}
	public void setBbb(ServletnameModel bbb) {
		this.bbb = bbb;
	}
	public List<UrlpatternModel> getLs() {
		return ls;
	}
	public void setLs(UrlpatternModel ll) {
		ls.add(ll);
	}
	
}

ServletModel:
 

package com.zlp.model;

public class ServletModel {
    private ServletnameModel bbb;
    private ServletclassModel ccc;
    public ServletnameModel getSnm() {
        return bbb;
    }
    public void setBbb(ServletnameModel bbb) {
        this.bbb= bbb;
    }
    public ServletclassModel getCcc() {
        return ccc;
    }
    public void setCcc(ServletclassModel ccc) {
        this.ccc= ccc;
    }
    
}

UrlPatternModel:

package com.zlp.model;

public class UrlpatternModel {
	private String bb;

	public String getBb() {
		return bb;
	}

	public void setBb(String bb) {
		this.bb= bb;
	}
	
}

WebappModel:

package com.zlp.model;

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

public class WebappModel {
	private List<ServletModel> ls=new ArrayList<ServletModel>();
	private List<ServletmappingModel> la=new ArrayList<>();
	public List<ServletModel> getLs() {
		return ls;
	}
	public void setLs(ServletModel servlet) {
		ls.add(servlet);
	}
	public List<ServletmappingModel> getLa() {
		return la;
	}
	public void setLa(ServletmappingModel mapp) {
		la.add(mapp);
	}
	
}

ServletNameModel:

package com.zlp.model;

public class ServletnameModel {
	private String bb;

	public String getBb() {
		return bb;
	}

	public void setBb(String bb) {
		this.bb= bb;
	}
	
}

WebappModelFactory:

package com.zlp.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 WebappModelFactory {
	public static WebappModel build() throws Exception {
		String path="/web.xml";
		return build(path);
	}
	public static WebappModel build(String path) throws Exception {
		InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
		SAXReader sr=new SAXReader();
		Document doc = sr.read(in);
		List<Element> servletEles = doc.selectNodes("/web-app/servlet");
		WebappModel wm=new WebappModel();
		for (Element servletEle : servletEles) {
			ServletModel sm=new ServletModel();
//			给sm填充xml的内容
			ServletnameModel snm=new ServletnameModel();
			ServletclassModel scm=new ServletclassModel();
			Element nEle = (Element)servletEle.selectSingleNode("servlet-name");
			Element cEle = (Element)servletEle.selectSingleNode("servlet-class");
			snm.setNr(nEle.getText());
			scm.setNr(cEle.getText());
			sm.setScm(scm);
			sm.setSnm(snm);
			wm.setLs(sm);
		}
//		将servlet-mapping的标签内容填充进webapp
		List<Element> servletma = doc.selectNodes("/web-app/servlet-mapping");
		for (Element servletEle : servletma) {
			ServletmappingModel smm=new ServletmappingModel();
//			给smm填充xml的内容
			Element nEle = (Element)servletEle.selectSingleNode("servlet-name");
			ServletnameModel am=new ServletnameModel();
			am.setNr(nEle.getText());
			smm.setSnm(am);
			List<Element> url=servletEle.selectNodes("url-pattern");
			for (Element urlpattern : url) {
				UrlpatternModel upm=new UrlpatternModel();
				upm.setNr(urlpattern.getText());
				smm.setLs(upm);
			}
			wm.setLa(smm);
		}
		return wm;
	}
	/**
	 * 通过浏览器输入的网址自动找到对应的后台处理类
	 * @param wam 建模后的实体类
	 * @param url 浏览器访问的网址
	 * @return
	 */
	public static String getServlet(WebappModel wam,String url) {
		String servletClass="";
//		找到浏览器网址对应的servlet-name
		String servletName="";
		List<ServletmappingModel> ls=wam.getLa();
		for (ServletmappingModel servletmappingModel : ls) {
			List<UrlpatternModel> urlpattern=servletmappingModel.getLs();
			for (UrlpatternModel urlpatternModel : urlpattern) {
				if(url.equals(urlpatternModel.getNr())) {
					ServletnameModel s=servletmappingModel.getSnm();
					servletName=s.getNr();
				}
			}
		}
//		找到servlet-name对应的后台处理类
		List<ServletModel> sm=wam.getLs();
		for (ServletModel servletModel : sm) {
			ServletnameModel a=servletModel.getSnm();
			if(servletName.equals(a.getNr())) {
				ServletclassModel scm=servletModel.getScm();
				servletClass=scm.getNr();
			}
		}
		return servletClass;
	}
	public static void main(String[] args) throws Exception {
		WebappModel wm=WebappModelFactory.build();
		String a=getServlet(wm, "/jrebelServlet");
		String b=getServlet(wm, "/jrebelServlet2");
		String c=getServlet(wm, "/jrebelServlet3");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
	}
}

今天的分享就到这啦~我们下期再见~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值