xml建模
xml建模就是用面向对象的思维去操作xml。
将xml文件中的每一个标签都封装成对象,封装顺序是从里到外
建模步骤
- 获取xml文件资源
- 解析出对象,分析对象属性,行为
- 将资源赋值进对象,注意对象的包含关系也要做好
实例
例题:为config.xml建模
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
config标签:可以包含0~N个action标签
-->
<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>
分析:config.xml中,有config,action,forword三个标签
所以,从最小的开始分析出对象
ForwordModel
package com.csf.model;
public class ForwardModel {
//<forward name="failed" path="/reg.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;
}
}
ActionModel
package com.csf.model;
import java.util.HashMap;
import java.util.Map;
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;
}
//压栈
public void push(ForwardModel forwardModel) {
fMap.put(forwardModel.getName(), forwardModel);
}
//弹栈
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
ConfigModel
package com.csf.model;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String, ActionModel> acMap=new HashMap<String, ActionModel>();
//压栈
public void push(ActionModel actionModel) {
acMap.put(actionModel.getPath(), actionModel);
}
//弹栈
public ActionModel pop(String path) {
return acMap.get(path);
}
}
那么,对象就抽取完毕了,现在再利用工厂模式,完成建模
首先,建一个'厂',建一个ConfigModelFactory,
建‘厂’思路:
* 1.将原有的config.xml进行解析
* 2.对应标签的内容,将其封装赋值给相应的对象
* forward标签赋值给ForwardModel对象
* action标签赋值给ActionModel对象
* config标签赋值给ConfigModel对象
package com.csf.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 ConfigModelFactory {
public static ConfigModel bulid() throws DocumentException {
return build("config.xml");
}
public static ConfigModel build(String resourpath) throws DocumentException {
InputStream in = ConfigModelFactory.class.getResourceAsStream(resourpath);
SAXReader sr=new SAXReader();
Document doc = sr.read(in);
ConfigModel cm=new ConfigModel();
List<Element> actionEles = doc.selectNodes("/config/action");
for (Element actEle : actionEles) {
ActionModel am=new ActionModel();
//将xml文件解析得来的值,赋值给模型的path属性
am.setPath(actEle.attributeValue("path"));
am.setType(actEle.attributeValue("type"));
List<Element> forwardEles = actEle.selectNodes("forward");
for (Element forEle : forwardEles) {
ForwardModel fm=new ForwardModel();
fm.setName(forEle.attributeValue("name"));
fm.setPath(forEle.attributeValue("path"));
fm.setRedirect(!"false".equals(forEle.attributeValue("redirect")));
am.push(fm);
}
cm.push(am);
}
return cm;
}
}
这就建好了,试一下用这个模型去取数据试一下
package com.csf.model;
import org.dom4j.DocumentException;
public class Test {
public static void main(String[] args) throws DocumentException {
ConfigModel cm=ConfigModelFactory.bulid();
ActionModel am = cm.pop("/loginAction");
System.out.println(am.getPath());
ForwardModel fm = am.pop("success");
System.out.println(fm.getPath());
}
}
输出结果是
ok现在可以通过ConfigModel轻松取值了。
练习
1.对web.xml进行建模
2.写一个servlet
3.通过url-pattern读取到servlet-class的值
<?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>
这就是给出的web.xml文件
这里我觉得建模过程中,工厂类的编写最为复杂,而其他对象类很容易,就不放出来了,
工厂类:
package com.csf.model2;
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();
/*
* 给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.setContent(servletNameEle.getText());
servletClassModel.setContent(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.setContent(servletNameEle.getText());
servletMappingModel.setServletNameModel(servletNameModel);
List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
for (Element urlPatternEle : urlPatternEles) {
UrlPatternModel urlPatternModel = new UrlPatternModel();
urlPatternModel.setContent(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.popServletMappingModels();
for (ServletMappingModel servletMappingModel : servletMappingModels) {
List<UrlPatternModel> urlPatternModels = servletMappingModel.getUrlPatternModels();
for (UrlPatternModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getContent())) {
ServletNameModel servletNameModel = servletMappingModel.getServletNameModel();
servletName = servletNameModel.getContent();
}
}
}
/*
* 找到servlet-name对应的后台处理类
*/
List<ServletModel> servletModels = webAppModel.popServletModels();
for (ServletModel servletModel : servletModels) {
ServletNameModel servletNameModel = servletModel.getServletNameModel();
if(servletName.equals(servletNameModel.getContent())) {
ServletClassModel servletClassModel = servletModel.getServletClassModel();
servletClass = servletClassModel.getContent();
}
}
return servletClass;
}
}
注意:这当中附带一个通过url-pattern得到servlet-class
这就是为什么平时我们在使用servlet时,只需要输入我们配置的url就可以运行servlet的原理
总结:
xml建模是为了让我们以面向对象的思维去操作xml文件,而目前的学习,是为了让我们感受框架的底层原理。
建模的步骤,
分析xml文件,提取相应的对象,建立工厂,给相应对象赋值,然后就可以用建好后的对象进行操作了,就比如上面的通过url-pattern得到servlet-class。
over.