前言
上篇(http://t.csdn.cn/I7hoF)中我们已经认识了XML解析,这篇我们将学习XML的建模
目录
一、XML建模
A、什么时候用建模?
重复性的代码比较多,对于同一个xml文件需要重复解析的时候
B、建模的原理
以面向对象的思想操作xml文件
C、建模思路
- 1.要分析需要被建模的文件中有哪几个对象
- 2.每个对象拥有的行为以及属性
- 3.定义一个从里到外的对象
- 4.通过23种设计模式中的工厂模式,解析xml生产指定对象
D、建模步骤
- 以面向对象的编程思想,描述xml资源文件
- 将xml文件中的内容封装到model实体对象中
作用:
减少重复代码,将代码反复使用
建模方式:
由内到外
config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<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.oyang.model;
/**
* 对应forward标签
* @author yang
*
* @date 2022年6月15日上午9:05:38
*/
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 getRedirect() {
return redirect;
}
public void setRedirect(boolean b) {
this.redirect = b;
}
}
第二个实体类:ActionModel:
package com.oyang.model;
import java.util.HashMap;
import java.util.Map;
/**
* 对应action标签
* @author yang
*
* @date 2022年6月15日上午9:04:55
*/
public class ActionModel {
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;
}
//两个行为,增加fowwardModel对象,查找forwardModel对象
//将一个新的forward标签对象加入容器
public void push(ForwardModel forwardModel) {
fMap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
定义了两个方法:一个是压栈(就是将数据加入相对应的对象中)
第三个实体类:ConfigModel :
package com.oyang.model;
import java.util.HashMap;
import java.util.Map;
/**
* 根标签对应的对象
* @author yang
*
* @date 2022年6月15日上午9:04:05
*/
public class ConfigModel {
private Map<String, ActionModel> aMap=new HashMap<String, ActionModel>();
public void push(ActionModel actionModel) {
aMap.put(actionModel.getPath(), actionModel);
}
public ActionModel pop(String path) {
return aMap.get(path);
}
}
二、工厂模式
23种设计模式之工厂模式
ConfigModelFactory 就是用来生产configmodel对象的
生产出来的Configmodel对象就包含了config.xml中的配置内容
第一种方式:
package com.oyang.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;
/**
* @author yang
* @date 2022年6月15日上午9:55:56
*/
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 con=new ConfigModel();
for (Element actionM : actionEles) {
ActionModel actionModel = new ActionModel();
actionModel.setPath(actionM.attributeValue("path"));
actionModel.setType(actionM.attributeValue("type"));
//将forwardmodel赋值并且添加到actionmodel中
List<Element> forwardEles = actionM.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);
}
con.push(actionModel);
}
return con;·
}
}
优化后第二种方式:
package com.oyang.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;
/**
* @author yang
*
* @date 2022年6月15日上午9:55:56
*/
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 con=new ConfigModel();
for (Element actionM : actionEles) {
ActionModel actionModel = new ActionModel();
actionModel.setPath(actionM.attributeValue("path"));
actionModel.setType(actionM.attributeValue("type"));
//将forwardmodel赋值并且添加到actionmodel中
List<Element> forwardEles = actionM.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);
}
con.push(actionModel);
}
return con;
}
public static ConfigModel bulid() throws Exception {
String defaultPath="config.xml";
return bulid(defaultPath);
}
}
执行代码:
package com.oyang.model;
public class Demo1 {
public static void main(String[] args) throws Exception {
ConfigModel con = ConfigModelFactory.bulid();
ActionModel pop = con.pop("/loginAction");
System.out.println(pop.getType());
ForwardModel forwardModel = pop.pop("success");
System.out.println(forwardModel.getPath());
}
}
执行结果:
三、案例
对web.xml进行建模,写一个servlet,通过url-pattern读取到Servlet-class的值
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app >
<servlet>
<servlet-name>jrebelServlet</servlet-name>
<servlet-class>com.oyang.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.oyang.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>
开始建模
ClassModel:
package com.oyang.Modelz;
public class ClassModel {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
MappingModel:
package com.oyang.Modelz;
import java.util.ArrayList;
import java.util.List;
public class MappingModel {
private NameModel namemodels;
private List<UrlpatternModel> urlpatternmodels=new ArrayList<UrlpatternModel>();
public NameModel getNamemodel() {
return namemodels;
}
public void setNamemodel(NameModel namemodel) {
this.namemodels=namemodel;
}
public void pushUrlPatternModel(UrlpatternModel urlpatternModel) {
urlpatternmodels.add(urlpatternModel);
}
public List<UrlpatternModel> getUrlpatternmodels() {
return urlpatternmodels;
}
}
NameModel:
package com.oyang.Modelz;
public class NameModel {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
ServletModel:
package com.oyang.Modelz;
public class ServletModel {
private NameModel NameModel;
private ClassModel classModel;
public NameModel getNameModel() {
return NameModel;
}
public void setNameModel(NameModel nameModel) {
NameModel = nameModel;
}
public ClassModel getClassModel() {
return classModel;
}
public void setClassModel(ClassModel classModel) {
this.classModel = classModel;
}
}
UrlpatternModel:
package com.oyang.Modelz;
public class UrlpatternModel {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
XmlApp:
package com.oyang.Modelz;
import java.util.ArrayList;
import java.util.List;
public class XmlApp {
private List<ServletModel> servletModels=new ArrayList<ServletModel>();
private List<MappingModel> servletMappingModels =new ArrayList<>();
public List<ServletModel> getServletModel() {
return servletModels;
}
public void pushsetServletModel(ServletModel servletModel) {
servletModels.add(servletModel);
}
public List<MappingModel> getServletMappingModels() {
return servletMappingModels;
}
public void pushsetServletMappingModels(MappingModel servletMappingModel) {
servletMappingModels.add(servletMappingModel);
}
}
WebAppModelFactory:
package com.oyang.Modelz;
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 XmlApp bulidWebAddModel(){
String xmlPath="web.xml";
return buildWebAppModel(xmlPath);
}
/**
* 建模
* @param xmlPath
* @return
* @throws Exception
*/
public static XmlApp buildWebAppModel(String xmlPath){
InputStream in = WebAppModelFactory.class.getResourceAsStream(xmlPath);
SAXReader sr=new SAXReader();
XmlApp xmlapp=new XmlApp();
try {
Document doc = sr.read(in);
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");
NameModel nameModel=new NameModel();
ClassModel classModel=new ClassModel();
nameModel.setText(ServletNameEle.getText());
classModel.setText(servletClassEle.getText());
servletmodel.setNameModel(nameModel);
servletmodel.setClassModel(classModel);
xmlapp.pushsetServletModel(servletmodel);
}
// 将servlet-mapping的标签内容填充进WebApp
List<Element> servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
for (Element servletMappingEle : servletMappingEles) {
MappingModel mappingmodel=new MappingModel();
// 给mappingmodel填充xml的内容
Element selectNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
NameModel nameModel=new NameModel();
nameModel.setText(selectNameEle.getText());
mappingmodel.setNamemodel(nameModel);
List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
for (Element urlPatternEle : urlPatternEles) {
UrlpatternModel urlpattermodel=new UrlpatternModel();
urlpattermodel.setText(urlPatternEle.getText());
mappingmodel.pushUrlPatternModel(urlpattermodel);
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return xmlapp;
}
/**
* 通过浏览器输入的网址自动找到对应的后台处理类
* @param xmlapp 建模后的实体类
* @param url 浏览器访问的网址
* @return
*/
public static String getServletClassByUrl(XmlApp xmlapp, String url) {
String servletClass ="";
//找到浏览器网址对应的servlet-name
String servletName = "";
List<MappingModel> servletMappingModels=xmlapp.getServletMappingModels();
for (MappingModel mappingModel : servletMappingModels) {
List<UrlpatternModel> urlmodel=mappingModel.getUrlpatternmodels();
for (UrlpatternModel urlpatternModel : urlmodel) {
if(url.equals(urlpatternModel.getText())) {
NameModel namemodel=mappingModel.getNamemodel();
servletName=namemodel.getText();
}
}
}
//找到servlet-name对应的后台处理类
List<ServletModel> servletModels=xmlapp.getServletModel();
for (ServletModel servletModel : servletModels) {
NameModel namemodel=servletModel.getNameModel();
if(servletName.equals(namemodel.getText())) {
ClassModel classmodel=servletModel.getClassModel();
servletClass=classmodel.getText();
}
}
return servletClass;
}
public static void main(String[] args) {
XmlApp XmlApp = WebAppModelFactory.bulidWebAddModel();
String res = getServletClassByUrl(XmlApp, "/jrebelServlet");
String res2 = getServletClassByUrl(XmlApp, "/jrebelServlet2");
String res3 = getServletClassByUrl(XmlApp, "/jrebelServlet3");
System.out.println(res);
System.out.println(res2);
System.out.println(res3);
}
}
运行结果:
OK,今日的学习就到此结束啦,如果对个位看官有帮助的话可以留下免费的赞哦(收藏或关注也行),如果文章中有什么问题或不足以及需要改正的地方可以私信博主,博主会做出改正的。个位看官,小陽在此跟大家说拜拜啦!