我是靠谱客的博主 愉快饼干,最近开发中收集的这篇文章主要介绍XML建模,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

    目录:

一.XML建模

        1.什么是建模?

        2.建模思路

        3.建模步骤

        核心思想

二.工厂模式


 一.XML建模:

什么是建模?

核心思想:以面向对象的思想去操作xml文件

建模思路:

1.要分析需要被建模的文件中有哪几个对象

2.每个对象拥有的行为以及属性

3.定义一个从里到外的对象

4.通过23种设计模式中的工厂模式,解析xml生产指定对象

建模步骤:

1.以面向对象的编程思想,描述xml资源文件

2.将xml文件中的内容封装到model实体对象中

作用:减少重复代码,将代码反复使用

建模方式:由内到外

config.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<!-- action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 type:字符串,非空 -->
	<action path="/regAction" type="test.RegAction">
		<!-- forward标签:没有子标签; name:字符串,同一action标签下的forward标签name值不能相同 ; path:以/开头的字符串 
			redirect:只能是false|true,允许空,默认值为false -->
		<forward name="failed" path="/reg.jsp" redirect="false" />
		<forward name="success" path="/login.jsp" redirect="true" />
	</action>

	<action path="/loginAction" type="test.LoginAction">
		<forward name="failed" path="/login.jsp" redirect="false" />
		<forward name="success" path="/main.jsp" redirect="true" />
	</action>
</config>

第一个实体类:ForwardModel( 由里到外)

package com.ljj.model;

/**
 * 对应forward标签
 * @author Administrator 刘俊杰
 *下午6:27:10
 */
public class ForwardModel {
//<forward name="failed" path="/login.jsp" redirect="false" />
private String name;
private String path;
private boolean readirect;
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 isReadirect() {
	return readirect;
}
public void setReadirect(boolean readirect) {
	this.readirect = readirect;
}
}

第二个实体类:ActionModel

          定义了两个方法:一个是压栈(就是将数据加入相对应的对象中)

         第二个是弹栈(弹栈就相当于获取值的方法)

package com.ljj.model;

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

/**
 * 对应action标签
 * @author Administrator 刘俊杰
 *下午6:25:56
 */
public class ActionModel {
//	<action path="/loginAction" type="test.LoginAction">
	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对象,查找forwardModel对象
	public void push(ForwardModel forwardModel ) {
		fMap.put(forwardModel.getName(), forwardModel);
	}
	public ForwardModel pop(String name) {
		return fMap.get(name);
	}
	
}

第三个实体类:ConfigModel 

package com.ljj.model;

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

/**
 * 根标签对应的对象
 * @author Administrator 刘俊杰
 *下午6:24:17
 */
public class ConfigModel {

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

二.工厂模式:

工厂类:ConfigModelFactory

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

 
 此地生产configmodel有配置信息?

 1、解析config.xml中的配置信息
 2、将对应的配置信息分别加载进行不同的模型信息

代码展示:

第一种方法:

package com.ljj.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;

/**
 * 23种设计模式之工厂模式
 * sessionfactory
 * ConfigModelFactory就是用来生成configmodel对象的
 * 生产出来的ConfigModel对象就包含了config.xml中的配置内容
 * 
 * 此地生产configmodel有配置信息?
 * 1.解析config.xml中的配置信息
 * 2.将对应的配置信息分别加载进不同的模型对象中
 * @author Administrator 刘俊杰
 *下午7:11:48
 */
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<Element> 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<Element> forwardEles = actionEle.selectNodes("forward");
			for (Element element : forwardEles) {
				ForwardModel forwardModel = new ForwardModel();
				forwardModel.setName(element.attributeValue("name"));
				forwardModel.setPath(element.attributeValue("path"));
//				redirect:只能是true|false,允许空,默认为false
				forwardModel.setReadirect("true".equals(element.attributeValue("readirec")));
				actionModel.push(forwardModel);
			}
			configModel.push(actionModel);
		}
		return configModel;
	}
	
	public static ConfigModel bulid() throws Exception {
		String defaultpath="/config.xml";
		return bulid(defaultpath);
	}
}

第二种方法:

package com.ljj.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;

/**
 * 23种设计模式之工厂模式
 * sessionfactory
 * ConfigModelFactory就是用来生成configmodel对象的
 * 生产出来的ConfigModel对象就包含了config.xml中的配置内容
 * 
 * 此地生产configmodel有配置信息?
 * 1.解析config.xml中的配置信息
 * 2.将对应的配置信息分别加载进不同的模型对象中
 * @author Administrator 刘俊杰
 *下午7:11:48
 */
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<Element> 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<Element> forwardEles = actionEle.selectNodes("forwaard");
			for (Element element : forwardEles) {
				ForwardModel forwardModel = new ForwardModel();
				forwardModel.setName(element.attributeValue("name"));
				forwardModel.setPath(element.attributeValue("path"));
//				redirect:只能是true|false,允许空,默认为false
				forwardModel.setReadirect("true".equals(element.attributeValue("readirec")));
				actionModel.push(forwardModel);
			}
			configModel.push(actionModel);
		}
		return configModel;
	}
}

执行测试代码:

package com.ljj.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());
	}
}

运行结果:

 扩展案例:对web.xml进行建模

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
  	<servlet-name>jrebelServlet</servlet-name>
  	<servlet-class>com.zking.xml.JrebelServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>jrebelServlet</servlet-name>
  	<url-pattern>/jrebelServlet</url-pattern>
  </servlet-mapping>
  
  <servlet>
  	<servlet-name>jrebelServlet2</servlet-name>
  	<servlet-class>com.zking.xml.JrebelServlet2</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>jrebelServlet2</servlet-name>
  	<url-pattern>/jrebelServlet2</url-pattern>
  	<url-pattern>/jrebelServlet3</url-pattern>
  </servlet-mapping>
</web-app>

 ForwardModel.java

package com.ljj.model;

/**
 * 对应forward标签
 * @author Administrator 刘俊杰
 *下午6:27:10
 */
public class ForwardModel {
//<forward name="failed" path="/login.jsp" redirect="false" />
private String name;
private String path;
private boolean readirect;
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 isReadirect() {
	return readirect;
}
public void setReadirect(boolean readirect) {
	this.readirect = readirect;
}
}

ServletClassModel.java

package com.ljj.model;

public class ServletClassModel {

	private String context;

	public String getContext() {
		return context;
	}

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

ServeltMapping.java

package com.ljj.model;

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

public class ServletMappingModel {

	private ServletNameModel servletNameModel;
	private List<UrlPatternModel> 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<UrlPatternModel> getUrlPatternModels() {
		return urlPatternModels;
	}
}

ServletModel.java

package com.ljj.model;

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

ServletNameModel.java

package com.ljj.model;

public class ServletNameModel {
	private String context;
	
	public String getContext() {
		return context;
	}
	public void setContext(String context) {
		this.context=context;
	}
}

UrlPatternModel.java

package com.ljj.model;

public class UrlPatternModel {
	private String context;

	public String getContext() {
		return context;
	}

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

WebAppModel.java

package com.ljj.model;

public class UrlPatternModel {
	private String context;

	public String getContext() {
		return context;
	}

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

WebAppModelFactory.java

package com.ljj.model;

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

	/**
	 * 建模
	 * @param xmlPath
	 * @return
	 */
	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);
			/**
			 * 将servlet的标签内容填充进WebApp
			 */
			List<Element> servletEles = doc.selectNodes("/web-app/servlet");
			for (Element servletEle : servletEles) {
				ServletModel servletModel = new ServletModel();
				/**
				 * 给servlet填充xml的内容
				 */
				Element servletNameEle =(Element) servletEle.selectSingleNode("servlet-name");
				Element servletClassEle = (Element) servletEle.selectSingleNode("select-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) {
			e.printStackTrace();
		}
		return webAppModel;
	}

	/**
	 * 通过浏览器输入的网址自动找到对应的后台处理类
	 * @param webAppModel	建模后的实体类
	 * @param url	浏览器访问的网址
	 * @return
	 */
	public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
		String servletClass = "";
		/*
		 * 找到浏览器网址对应的servlet-name
		 */
		String servletName = "";
		List<ServletMappingModel> servletMappingModels = webAppModel.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 = 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);
		
	}
	
}

 测试结果:

 

最后

以上就是愉快饼干为你收集整理的XML建模的全部内容,希望文章能够帮你解决XML建模所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部