目录
前言
上次分享了Xml的解析,这次要分享的内容是xml建模
一、XML建模
当重复性的代码较多,对同一个xml文件需要重复的解析时,我们可以用建模
以config.xml为例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
<!ELEMENT config (action*)>
<!ELEMENT action (forward*)>
<!ELEMENT forward EMPTY>
<!ATTLIST action
path CDATA #REQUIRED
type CDATA #REQUIRED
>
<!ATTLIST forward
name CDATA #REQUIRED
path CDATA #REQUIRED
redirect (true|false) "false"
>
]>
<!-- 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>
1、forward建模
package com.zhw.xml;
/**
* @author zhw
*
*/
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 getRedirect() {
return redirect;
}
public void setRedirect(Boolean redirect) {
this.redirect = redirect;
}
}
2、action建模
package com.zhw.xml;
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;
}
// 增加forwardmodel
public void push(ForwardModel forwardModel) {
fMap.put(forwardModel.getName(), forwardModel);
}
// 查找forwardmodel
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
3、config建模
package com.zhw.xml;
import java.util.HashMap;
import java.util.Map;
/**
* 对应标签
* @author zhw
*
*/
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);
}
}
二、工厂模式
同样是以config为例:
package com.zhw.xml;
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
*
* @author zhw
*
*/
public class ConfigModelFactory {
public static ConfigModel bulid() throws Exception {
String defaultPath = "/config.xml";
InputStream in = ConfigModelFactory.class.getResourceAsStream(defaultPath);
SAXReader s = new SAXReader();
Document doc = s.read(in);
ConfigModel configModel = new ConfigModel();
List<Element> actionEles = doc.selectNodes("/config/action");
for (Element ele : actionEles) {
ActionModel actionModel = new ActionModel();
actionModel.setPath(ele.attributeValue("path"));
actionModel.setType(ele.attributeValue("type"));
//将forwardModel 赋值并添加到ActionModel
List<Element> forwardEles = ele.selectNodes("forward");
for (Element fele : forwardEles) {
ForwardModel forwardModel = new ForwardModel();
forwardModel.setName(fele.attributeValue("name"));
forwardModel.setPath(fele.attributeValue("path"));
forwardModel.setRedirect("true".equals(fele.attributeValue("redirect")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
}
三、案例
以web.xml为例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app[
<!ELEMENT web-app (servlet,servlet-mapping)+>
<!ELEMENT servlet (servlet-name,servlet-class)>
<!ELEMENT servlet-mapping (servlet-name,url-pattern+)>
<!ELEMENT servlet-name (#PCDATA)>
<!ELEMENT servlet-class (#PCDATA)>
<!ELEMENT url-pattern (#PCDATA)>
]>
<web-app>
<servlet>
<servlet-name>jrebelServlet</servlet-name>
<servlet-class>com.zhw.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.zhw.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>
以下是各标签的建模
package com.zhw.demo;
public class ServeltnameModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
package com.zhw.demo;
public class ServeltclassModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
package com.zhw.demo;
public class UrlModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
package com.zhw.demo;
public class ServeltModel {
private ServeltnameModel servletNameModel;
private ServeltclassModel servletClassModel;
public ServeltnameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServeltnameModel servletNameModel) {
this.servletNameModel = servletNameModel;
}
public ServeltclassModel getServletClassModel() {
return servletClassModel;
}
public void setServletClassModel(ServeltclassModel servletClassModel) {
this.servletClassModel = servletClassModel;
}
}
package com.zhw.demo;
import java.util.ArrayList;
import java.util.List;
public class MappingModel {
private ServeltnameModel servletNameModel;
private static List<UrlModel> urlModels = new ArrayList<>();
public ServeltnameModel getServletNameModel() {
return servletNameModel;
}
public void setServletNameModel(ServeltnameModel servletNameModel) {
this.servletNameModel = servletNameModel;
}
public static void pushUrlModel(UrlModel urlPatternModel) {
urlModels.add(urlPatternModel);
}
public List<UrlModel> getUrlModels() {
return urlModels;
}
}
package com.zhw.demo;
import java.util.ArrayList;
import java.util.List;
public class WebModel {
private static List<ServeltModel> servletModels = new ArrayList<>();
private static List<MappingModel> servletMappingModels = new ArrayList<>();
public static void pushServletModel(ServeltModel servletModel) {
servletModels.add(servletModel);
}
public List<ServeltModel> getServletModels() {
return servletModels;
}
public static void pushServletMappingModel(MappingModel servletMappingModel) {
servletMappingModels.add(servletMappingModel);
}
public List<MappingModel> getServletMappingModels() {
return servletMappingModels;
}
}
最后是工厂模式
package com.zhw.demo;
import java.io.InputStream;
import java.util.List;
import org.apache.catalina.servlet4preview.http.ServletMapping;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class WebFactory {
public static WebModel build() throws Exception {
String xmlPath = "/web.xml";
InputStream in = WebFactory.class.getResourceAsStream(xmlPath);
SAXReader s = new SAXReader();
WebModel webModel = new WebModel();
Document doc = s.read(in);
List<Element> servletEles = doc.selectNodes("/web-app/servlet");
for (Element servletEle : servletEles) {
ServeltModel servletModel = new ServeltModel();
/*
* 给ServletModel填充xml的内容
*/
Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
ServeltnameModel servletNameModel = new ServeltnameModel();
ServeltclassModel servletClassModel = new ServeltclassModel();
servletNameModel.setContext(servletNameEle.getText());
servletClassModel.setContext(servletClassEle.getText());
servletModel.setServletNameModel(servletNameModel);
servletModel.setServletClassModel(servletClassModel);
WebModel.pushServletModel(servletModel);
}
/*
* 将servlet-mapping的标签内容填充进WebApp
*/
List<Element> servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
for (Element servletMappingEle : servletMappingEles) {
MappingModel servletMappingModel = new MappingModel();
/*
* 给ServletMappingModel填充xml的内容
*/
Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
ServeltnameModel servletNameModel = new ServeltnameModel();
servletNameModel.setContext(servletNameEle.getText());
servletMappingModel.setServletNameModel(servletNameModel);
List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
for (Element urlPatternEle : urlPatternEles) {
UrlModel urlPatternModel = new UrlModel();
urlPatternModel.setContext(urlPatternEle.getText());
servletMappingModel.pushUrlModel(urlPatternModel);
}
WebModel.pushServletMappingModel(servletMappingModel);
}
return webModel;
}
public static String getServletClassByUrl(WebModel webAppModel, String url) {
String servletClass = "";
/*
* 找到浏览器网址对应的servlet-name
*/
String servletName = "";
List<MappingModel> servletMappingModels = webAppModel.getServletMappingModels();
for (MappingModel servletMappingModel : servletMappingModels) {
List<UrlModel> urlPatternModels = servletMappingModel.getUrlModels();
for (UrlModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getContext())) {
ServeltnameModel servletNameModel = servletMappingModel.getServletNameModel();
servletName = servletNameModel.getContext();
}
}
}
/*
* 找到servlet-name对应的后台处理类
*/
List<ServeltModel> servletModels = webAppModel.getServletModels();
for (ServeltModel servletModel : servletModels) {
ServeltnameModel servletNameModel = servletModel.getServletNameModel();
if(servletName.equals(servletNameModel.getContext())) {
ServeltclassModel servletClassModel = servletModel.getServletClassModel();
servletClass = servletClassModel.getContext();
}
}
return servletClass;
}
public static void main(String[] args) throws Exception {
WebModel webAppModel = WebFactory.build();
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建模,希望对你能有帮助,下期预告反射。
我是九歌,一个喜欢编程的程序员。
如有错误还望指正,谢谢啦。