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

XML建模

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

XML建模

目录

一、以面向对象的思想封装config.xml文件

二、建模

三、案例:


一、以面向对象的思想封装config.xml文件

config.xml文件源码



	

	
	
		
		
	
	
		
		
	

封装的代码

package com.cdl.jm;

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


public class ConfigModel {
	private Map aMap = new HashMap<>();
	public void push(ActionModel  actionModel) {
		aMap.put(actionModel.getPath(), actionModel);
	}
	public ActionModel pop(String path) {
		return aMap.get(path);
	}
}
package com.cdl.jm;

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对象,查找forwardModel对象
	//将一个新的forwardModel标签对象加入容器
	public void push(ForwardModel forwardModel) {
		fMap.put(forwardModel.getName(), forwardModel);
	}
	public ForwardModel pop(String name) {
		return fMap.get(name);
	}

}
package com.cdl.jm;

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;
	}
	
	

}

二、建模

错误演示:
 

package com.cdl.jm;

public class Demo1 {
	public static void main(String[] args) {
		ConfigModel configModel = new ConfigModel();
		ActionModel actionModel = configModel.pop("/loginAction");
		System.out.println(actionModel.getType());
		
	}
}

现象:

 原因:因为new出来的对象中还没有任何xml中的信息

解决办法:创建一个ConfigModelFactory的类生产出来一个configmodel对象,其中就包含了config.xml的配置内容

ConfigModelFactory的类

第一种写法

package com.cdl.jm;

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() throws Exception {
		String defaultPath="/config.xml";
		InputStream in = ConfigModelFactory.class.getResourceAsStream(defaultPath);
		SAXReader sr = new SAXReader();
		Document doc = sr.read(in);
		List actionEles = doc.selectNodes("/config/action");
		ConfigModel configModel = new ConfigModel();
		for (Element actionEle : actionEles) {
			ActionModel actionModel = new ActionModel() ;
			actionModel.setPath(actionEle.attributeValue("path"));
			actionModel.setType(actionEle.attributeValue("type"));
//			将forwardModel赋值并且添加到actionModel中
			List forwardEles = actionEle.selectNodes("forward");
			for (Element element : forwardEles) {
				ForwardModel forwardModel =	new ForwardModel();
				forwardModel.setName(element.attributeValue("name"));
				forwardModel.setPath(element.attributeValue("path"));
				//redirect:只能是false|true,允许为空,默认值为false
				forwardModel.setRedirect("true".equals(element.attributeValue("redirect")));
				actionModel.push(forwardModel);
			}
			configModel.push(actionModel);
		}
		return configModel;
	}
}

第二种写法即优化后的写法

package com.cdl.jm;

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);
		List actionEles = doc.selectNodes("/config/action");
		ConfigModel configModel = new ConfigModel();
		for (Element actionEle : actionEles) {
			ActionModel actionModel = new ActionModel() ;
			actionModel.setPath(actionEle.attributeValue("path"));
			actionModel.setType(actionEle.attributeValue("type"));
//			将forwardModel赋值并且添加到actionModel中
			List forwardEles = actionEle.selectNodes("forward");
			for (Element element : forwardEles) {
				ForwardModel forwardModel =	new ForwardModel();
				forwardModel.setName(element.attributeValue("name"));
				forwardModel.setPath(element.attributeValue("path"));
				//redirect:只能是false|true,允许为空,默认值为false
				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.cdl.jm;

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("b");
		System.out.println(forwardModel.getPath());
	}
}

运行效果图:

 建模的原理:以面向对象的思维操作xml文件

三、案例:

对web.xml进性建模,写一个servlet,通过url-pattern读取到Servlet-class的值

web.xml的源代码:



  
  	jrebelServlet
  	com.cdl.xml.JrebelServlet
  
  
  
  	jrebelServlet
  	/jrebelServlet
  
  
  
  	jrebelServlet2
  	com.cdl.xml.JrebelServlet2
  
  
  
  	jrebelServlet2
  	/jrebelServlet2
  	/jrebelServlet3
  

建模:

package com.cdl.jm;

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.cdl.jm;

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 saxReader = new SAXReader();
		WebAppModel webAppModel = new WebAppModel();
		try {
			Document doc = saxReader.read(in);
			
			List servletEles = doc.selectNodes("/web-app/servlet");
			for (Element servletEle : servletEles) {
				ServletModel servletModel = new ServletModel();

				
				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);
			}

			
			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);
		
	}
}
package com.cdl.jm;

public class UrlPatternModel {
	private String context;

	public String getContext() {
		return context;
	}

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

}

servlet

package com.cdl.jm;

public class ServletClassModel {
	private String context;

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}
}
package com.cdl.jm;

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.cdl.jm;

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.cdl.jm;

public class ServletNameModel {
	private String context;

	public String getContext() {
		return context;
	}

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

运行结果:

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

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

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