XML 建模
- 不同的人有不同的需求,那么是不是意味着每个人都需要对指定xml字符串进行解析呢
建模的好处:
- 将指定的xml字符串当作对象来操作,如果说当对一个指定的xml格式字符串完成了建模的操作,那么后期只需要调用指定的方法就可以完成预定的字符串获取
如何描述一个类(对象)
有属性和行为,方法。 如以下代码
<config>
<action path="/regAction" type="test.RegAction">
<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>
从以上代码中看forward这个类的属性为:name、path、redirect
action的"属性"为: path、type "行为"有:增加(因为在action中可以增加很多forword类)
查询(通过path拿到type) 删除等行为
建模步骤:
- 根据以上代码,首先我们分别先建action forward config 三个类
- 描述需要建模的xml文件(把每个标签描述为一个对象)
ForwardModel
package com.ljx.model;
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;
}
}
ActionModel
package com.ljx.model;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
//从里到外
private String path;
private String type;
//因为forward中一个值对应一个对象 所以用map来存
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;
}
//把东西存到map集合里面去
public void push(ForwardModel forwardModel) {
fMap.put(forwardModel.getName(),forwardModel);
}
//获取 (给一个名字返回一个对象)
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
ConfigModel
package com.ljx.model;
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);
}
public static void main(String[] args) {
ConfigModel configModel=new ConfigModel();//一个节点
ActionModel actionModel=configModel.pop("/loginAction");
System.out.println(actionModel.getType());
}
}
- 解析xml文件,将内容填充到实体内中
这里用到了23种设计模式(用来处理Java中所遇到的特定的一些问题)之一的工厂模式,它是一种解决方案,它用来将资源生产指定的实体类。它的好处在于提高了代码的复用性
package com.ljx.model;
import java.io.InputStream;
import java.util.Iterator;
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 build() throws DocumentException {
return ConfigModel("config.xml");
}
//以下代码为了生产出有内容的实体类configModel
private static ConfigModel ConfigModel(String xmlPath) throws DocumentException {
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对象填充xml中的action标签的内容
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
List<Element> forwardEles = actionEle.selectNodes("forward");
for (Element forwardEle : forwardEles) {
forwardModel=new ForwardModel();
//给forwardModel对象填充xml中的forward标签的内容
forwardModel.setName(forwardEle.attributeValue("name"));
forwardModel.setPath(forwardEle.attributeValue("path"));
forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
//redirect默认是true 重定向
//只有填了false才是转发 forwardEle.attributeValue("redirect")拿到的是xml中你所填的值
//不填就是重定向 "false".equals(forwardEle.attributeValue("redirect")是false
//填 true 重定向 "false".equals(forwardEle.attributeValue("redirect")是true
//填 false 转发 "false".equals(forwardEle.attributeValue("redirect")是true
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
public static void main(String[] args) throws DocumentException {
ConfigModel configModel=ConfigModelFactory.build();//一个节点
ActionModel actionModel=configModel.pop("/loginAction");
System.out.println(actionModel.getType());
}
}
对以上建模内容做一个完整的例子
要求:
- 对web.xml进行建模
- 写一个servlet
- 通过url-pattern读取到servlet-class的值
web.xml代码如下:
先写servlet
package com.ljx.model2;
public class ServletClassModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
package com.ljx.model2;
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;
}
}
package com.ljx.model2;
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.ljx.model2;
public class ServletNameModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
通过url-pattern读取到servlet-class的值
package com.ljx.model2;
public class UrlPatternModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
package com.ljx.model2;
import java.util.ArrayList;
import java.util.List;
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;
}
}
package com.ljx.model2;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
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.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);
}
}