当前位置:网站首页>XML modeling
XML modeling
2022-07-28 23:30:00 【I love coriander TVT】
Catalog
The core idea : Operate in an object-oriented way xml file
One 、XML modeling
The core idea : Operate in an object-oriented way xml file
First we get one config.xml file As shown in the figure
Realize the idea : From inside to outside , first forwardModel, The second is actionModel, Last but not least ConfigModel, And a factory class ConfigModelFactory
1.forwardmodel
package com.zjy.Model;
/**
* Corresponding forward label
* @author Administrator
*
*/
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;
}
}
2.actionModel
package com.zjy.Model;
import java.util.HashMap;
import java.util.Map;
/**
* Corresponding action label
* @author Administrator
*
*/
public class ActionModel {
//<action path="/registerAction" type="test.action.RegisterAction">
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;
}
// Two behaviors increase forwardModel object , lookup forwardModel object
// Will a new forward Label objects are added to the container
public void push(ForwardModel forwardModel) {
fmap.put(forwardModel.getName(), forwardModel);
}
public ForwardModel pop(String name) {
return fmap.get(name);
}
}
3.configModel
package com.zjy.Model;
import java.util.HashMap;
import java.util.Map;
/**
* The object corresponding to the tag
* @author Administrator
*
*/
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);
}
}
4.configModelFactoryer
package com.zjy.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;
/**
* 23 Factory mode of design mode
* @author Administrator
*ConfigMOdelFactory It's for production configModel Object's
* manufactured ConfigModel Object contains config.xml Configuration information
*
* Produced here configModel There is configuration information
*1. analysis config.xml Configuration information
*2. Load the corresponding configuration information into different model objects
*
*
*/
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 configModel = new ConfigModel();
for (Element element : actionEles) {
ActionModel actionModel = new ActionModel();
actionModel.setPath(element.attributeValue("path"));
actionModel.setType(element.attributeValue("type"));
// take forwardmodel Assign and add to actionmodel in
List<Element> forwardEles = element.selectNodes("forward");
for (Element element2 : forwardEles) {
ForwardModel forwardModel = new ForwardModel();
forwardModel.setName(element2.attributeValue("name"));
forwardModel.setPath(element2.attributeValue("path"));
//redirect: Can only be false|true, Allow space , The default value is false
forwardModel.setRedirect("true".equals(element2.attributeValue("redirect")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
}
Two 、 expand
This time we got a web.xml file , It can be seen from the previous xml The difference between documents is web.xml Only labels , The label is embedded with a label , And only the text content , There is no attribute
Realize the idea : First, build the innermost url-patternmodel and servlet-namemodel as well as servlet-classmodel
Next is servletmodel and servlet-mappingmodel, Finally, the root tag Web-appmodel And the factory class Web-appmodelFactory
1、servlet-nameModel
package com.zjy.model;
/**
* Servletname The object corresponding to the label
* @author Administrator
*
*/
public class ServletNameModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
2、servlet-classModel
package com.zjy.model;
/**
* Servletclass The object corresponding to the label
* @author Administrator
*
*/
public class ServletClassModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
3、url-patternModel
package com.zjy.model;
/**
* urlpattern The object corresponding to the label
* @author Administrator
*
*/
public class UrlpatternModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
4、servletModel
package com.zjy.model;
/**
* Servlet The object corresponding to the label
* @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;
}
}
5、servlet-mappingModel
package com.zjy.model;
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 List<UrlpatternModel> getUrlPatternModels() {
return urlPatternModels;
}
public void pushUrlpatternModel(UrlpatternModel urlpatternModel) {
urlPatternModels.add(urlpatternModel);
}
}
6、web-appModel
package com.zjy.model;
import java.util.ArrayList;
import java.util.List;
public class WebAppModel {
private List<ServletModel> servletModels = new ArrayList<ServletModel>();
private List<ServletMappingModel> servletMappingModels = new ArrayList<>();
public List<ServletModel> getServletModels() {
return servletModels;
}
public void pushServletModels(ServletModel servletModel) {
servletModels.add(servletModel);
}
public List<ServletMappingModel> getServletMappingModels() {
return servletMappingModels;
}
public void pushServletMappingModels(ServletMappingModel servletMappingModel) {
servletMappingModels.add(servletMappingModel);
}
}
7、web-appmodelFactory Be careful : If you don't understand, you can look at the notes ,
package com.zjy.model;
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 bulid() throws Exception {
String path = "web.xml";
InputStream in = WebAppModelFactory.class.getResourceAsStream(path);
SAXReader sr = new SAXReader();
WebAppModel webAppModel = new WebAppModel();
try {
// take servlet The contents of the label are filled into webappmodel in
Document doc = sr.read(in);
// obtain servlet The node of
List<Element> servletEles = doc.selectNodes("/web-app/servlet");
for (Element element : servletEles) {
ServletModel servletModel = new ServletModel();
// to servletmodel Fill in the content
Element servletNameEles = (Element)element.selectSingleNode("servlet-name");
Element servletClassEles = (Element)element.selectSingleNode("servlet-class");
ServletNameModel servletNameModel = new ServletNameModel();
ServletClassModel servletClassModel = new ServletClassModel();
// to class and name Label assignment
servletClassModel.setContext(servletClassEles.getText());
servletNameModel.setContext(servletNameEles.getText());
// take class and name Label in servlet in
servletModel.setServletClassModel(servletClassModel);
servletModel.setServletNameModel(servletNameModel);
// take servlet In the webapp In the root label
webAppModel.pushServletModels(servletModel);
}
// to servletmapping Label filling
List<Element> serveltmappEles = doc.selectNodes("/web-app/servlet-mapping");
for (Element element : serveltmappEles) {
ServletMappingModel servletMappingModel = new ServletMappingModel();
// to ServletMappingModel Fill in the content
Element servletnameEle = (Element)element.selectSingleNode("servlet-name");
// Because a servletmapping There can be multiple labels url-pattern label , So here we use set to receive
List<Element> urlpatternEles = element.selectNodes("url-pattern");
ServletNameModel servletNameModel = new ServletNameModel();
// to name assignment
servletNameModel.setContext(servletnameEle.getText());
// take name Add to servletMapping in
servletMappingModel.setServletNameModel(servletNameModel);
// Traverse urlpatternModel aggregate
for (Element element2 : urlpatternEles) {
UrlpatternModel urlpatternModel = new UrlpatternModel();
// to Url assignment
urlpatternModel.setContext(element2.getText());
// take url Add to servletMapping in
servletMappingModel.pushUrlpatternModel(urlpatternModel);
}
// take servletMapping Add to webapp in
webAppModel.pushServletMappingModels(servletMappingModel);
}
} catch (Exception e) {
e.printStackTrace();
}
return webAppModel;
}
/**
*
* @param webAppModel webapp Modeling entity classes
* @param url website
* @return servletClass
*/
public static String getClassByUrl(WebAppModel webAppModel,String url) {
String servletClass = "";
String servletName = "";
List<ServletMappingModel> servletMappingModels = webAppModel.getServletMappingModels();
for (ServletMappingModel servletMappingModel : servletMappingModels) {
// find servletname The corresponding class
List<UrlpatternModel> urlPatternModels = servletMappingModel.getUrlPatternModels();
for (UrlpatternModel urlpatternModel : urlPatternModels) {
if(url.equals(urlpatternModel.getContext())) {
ServletNameModel servletNameModel = servletMappingModel.getServletNameModel();
servletName = servletNameModel.getContext();
}
}
}
// find servlet-name The corresponding class
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;
}
// test
public static void main(String[] args) throws Exception {
WebAppModel webAppModel = WebAppModelFactory.bulid();
String aa = getClassByUrl(webAppModel, "/jrebelServlet");
String bb = getClassByUrl(webAppModel, "/jrebelServlet2");
String cc = getClassByUrl(webAppModel, "/jrebelServlet3");
System.out.println(aa);
System.out.println(bb);
System.out.println(cc);
}
}
边栏推荐
- General principles of software quality
- Thesis reading (3) - googlenet of classification
- (important) first knowledge of C language -- function
- Retrofit Usage Summary
- 【C语言】三子棋小游戏实现
- Arduino UNO驱动合宙1.8‘TFT SPI屏幕示例演示(含资料包)
- 集火全屋智能“后装市场”,真正玩得转的没几个
- [copy] Internet terms, abbreviations, abbreviations
- Thesis reading (2) - vggnet of classification
- RouterOS 有限dns劫持及check
猜你喜欢
The front mounted ADAS camera in parking increased by 54.15% year-on-year, with TOP10 suppliers taking the lead
sql优化常用的几种方法
可视化全链路日志追踪
All aspect visual monitoring of istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
如何在VR全景中嵌入AI数字人功能?打造云端体验感
A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
以价换量却遭遇销量“六连跌”,不再安全的沃尔沃还能翻身吗?
Assembly analysis swift polymorphism principle
Cnpm installation steps
Thesis reading (3) - googlenet of classification
随机推荐
零念科技完成Pre-A轮融资,推动智能驾驶平台软件国产替代
22牛客多校day1 I - Chiitoitsu 概论dp
【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
recursion and iteration
Object object
After reading MySQL database advanced practice (SQL xiaoxuzhu)
[mongodb] basic use of mongodb database, special cases, and the installation and creation process of mongoose (including the installation of mongoose fixed version)
Typescript防止基类被实例化
Invest 145billion euros! EU 17 countries announce joint development of semiconductor technology
业界首创云原生安全检测双模型!安全狗重磅报告亮相数字中国建设峰会
【MongoDB】MongoDB数据库的基础使用,特殊情况以及Mongoose的安装和创建流程(含有Mongoose固定版本安装)
Performance optimized APK slimming
Typescript class method this pointer binding
Messages from students participating in the competition: memories of the 17th session
程序员成长第三十篇:识别真伪需求的神器
6 个超级良心的开源教程!
High quality subroutine 2 - high cohesion
安全狗入选《云安全全景图2.0》多个细项
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
[MySQL series] addition, deletion, modification and query of MySQL tables (Advanced)