XML建模

建模的由来:
就是将指定的xml字符串当作对象来操作
如果说当对一个指定的xml格式字符串完成了建模操作,
好处在于,只需要调用指定的方法就可以完成预定的字符串获取。

建模的好处:
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、定义对象从小到大(从里到外)
4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象
好处:
提高代码的复用性

建模分为两步:
1、以面向对象的编程思想,描述xml资源文件
2、将xml文件中内容封装进model实体对象。

我们通过案例来了解建模。
案例1:
对(mvc.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" />
	</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>

ConfigModel
将xml资源文件的config中的子标签对象化。

package com.LHJ;

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

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

ForwardModel
将xml资源文件中的forward标签对象化。

package com.LHJ;

public class ForwardModel {
	private String name;
	private String path;
	private boolean redirect =true;
	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;
	}

}

ActionModel
将xml资源文件中的action标签对象化。

package com.LHJ;

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

public class ActionModel {
	private String path;
	private String type;
	private Map<String, ForwardModel> 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;
	}

	public void push(ForwardModel forwardModel) {
		fMap.put(forwardModel.getName(), forwardModel);
	}
	
	public ForwardModel pop(String name) {
		return fMap.get(name);
	}
}

ConfigModelFactory
工厂模式:
java23种设计模式中的一种(设计模式:是一种解决方案,是为了处理java中所遇到的特定的一些问题)
把资源文件生产成指定的的实体类
能够提高代码的复用性

package com.LHJ;

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

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

/**工厂模式:
 * java23种设计模式中的一种(设计模式:是一种解决方案,是为了处理java中所遇到的特定的一些问题)
 * 把资源文件生产成指定的的实体类
 * 能够提高代码的复用性 
 * @author Administrator
 *
 */
public class ConfigModelFactory {
	public static ConfigModel build() throws Exception {
		return ConfigModel("mvc.xml");
	}

	private static ConfigModel ConfigModel(String xmlPath) throws Exception {
		ConfigModel configModel = new ConfigModel();
		ActionModel actionModel = null;
		ForwardModel forwardModel = null;
		InputStream in = ConfigModelFactory.class.getResourceAsStream(xmlPath);
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(in);
		List<Element> actionEles = doc.selectNodes("config/action");
		for (Element actionEle : actionEles) {
			actionModel = new ActionModel();
//		            填充actionModel
			actionModel.setPath(actionEle.attributeValue("path"));
			actionModel.setType(actionEle.attributeValue("type"));
			List<Element> forwardEles = actionEle.selectNodes("forward");
			for (Element forwardEle : forwardEles) {
				forwardModel = new ForwardModel();
//				填充forwardModel
				forwardModel.setName(forwardEle.attributeValue("name"));
				forwardModel.setPath(forwardEle.attributeValue("path"));
				forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
				actionModel.push(forwardModel);
			}
			configModel.push(actionModel);
		}
		
		return configModel;
	}
	
	public static void main(String[] args) throws Exception {
		ConfigModel configModel = ConfigModelFactory.build();
//		获取到第二个action中的type值
		ActionModel actionModel = configModel.pop("/loginAction");
//		获取到第二个action下的第二个forward的path值
		System.out.println("获取到第二个action中的type值:"+actionModel.getType());
		System.out.println("获取到第二个action下的第二个forward的path值:"+actionModel.pop("success").getPath());
	}

}

结果如下:
在这里插入图片描述
案例2:
对(web.xml)文件进行建模。
文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
  	<servlet-name>jrebelServlet</servlet-name>
  	<servlet-class>com.LHJ.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.LHJ.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>

ServletClassModel 将servlet-class标签封装成对象

package com.LHJ;

/**   ServletClassModel 将servlet-class标签封装成对象
 * @author Administrator
 *
 */
public class ServletClassModel {
          private String context;

		public String getContext() {
			return context;
		}

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

ServletModel 将servlet标签封装成对象

package com.LHJ;

/**   ServletModel 将servlet标签封装成对象
 * @author Administrator
 *
 */
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;
	}
      
      
}

ServletMappingModel 将servlet-mapping标签封装成对象

package com.LHJ;

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

/**  ServletMappingModel 将servlet-mapping标签封装成对象
 * @author Administrator
 *
 */
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;
 	}
}

WebAppModel 将web-app标签封装成对象

package com.LHJ;

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

/**  WebAppModel 将web-app标签封装成对象
 * @author Administrator
 *
 */
public class WebAppModel {
     private List<ServletModel> servletModels=new ArrayList<>();
     private List<ServletMappingModel> servletMappingModels=new ArrayList<>();
     
     public void pushServletModel(ServletModel servletModel) {
 		servletModels.add(servletModel);
 	}
 	
 	public List<ServletModel> getServletModels() {
 		return servletModels;
 	}
 	
 	public void pushServletMappingModel(ServletMappingModel servletMappingModel) {
 		servletMappingModels.add(servletMappingModel);
 	}
 	
 	public List<ServletMappingModel> getServletMappingModels() {
 		return servletMappingModels;
 	}
     
     
}

UrlPatternModel 将url-pattern标签封装成对象

package com.LHJ;

/**  UrlPatternModel 将url-pattern标签封装成对象
 * @author Administrator
 *
 */
public class UrlPatternModel {
      private String context;

	public String getContext() {
		return context;
	}

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

ServletNameModel 将servlet-name标签封装成对象

package com.LHJ;

/**  ServletNameModel 将servlet-name标签封装成对象
 * @author Administrator
 *
 */
public class ServletNameModel {
      private String context;

	public String getContext() {
		return context;
	}

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

WebAppModelFactory 实行工厂模式对web.xml进行建模

package com.LHJ;

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;

/**  WebAppModelFactory 实行工厂模式对web.xml进行建模
 * @author Administrator
 *
 */
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();

				/*
				 * 给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(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);
		
	}

}

运行结果如下:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值