当前位置:网站首页>XML modeling
XML modeling
2022-07-23 11:26:00 【_ Leaf1217】
Catalog
Two 、23 Factory mode of three modeling methods
One 、 Learn to XML modeling
Today we'll see how XML modeling , First of all, we should model it first XML file 【config.xml】 Put it here :
<?xml version="1.0" encoding="UTF-8"?>
<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>Let's first analyze this xml file :
1) Analyze tags
A root tag :config
A sub tag :action
action There are sub tags inside :forward
2) Analysis properties
action:path、type
forward:name、path、redirect
According to the above analysis, we can know that there are three different tags in this configuration file :
config: There are sub tags 、 There is no attribute
action: There are sub tags
forward: No sub tags
So we started our The first step of modeling ,
utilize Object oriented programming idea For different labels Construction entity :
ForwardModel:
package com.leaf.mode;
/**
* forward label
* @author Leaf
*
* 2022 year 6 month 15 Japan In the morning 8:40:48
*/
public class ForwardModel {
//<forward name="success" path="/main.jsp" redirect="true" />
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;
}
public ForwardModel() {
}
public ForwardModel(String name, String path, boolean redirect) {
this.name = name;
this.path = path;
this.redirect = redirect;
}
@Override
public String toString() {
return "ForwardMode [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}ActionModel: There are sub tags , need Two behavior methods :
1) add to forward label
2) Inquire about forward label
package com.leaf.mode;
import java.util.HashMap;
import java.util.Map;
/**
* action label
* @author Leaf
*
* 2022 year 6 month 15 Japan In the morning 8:41:08
*/
public class ActionModel {
//<action path="/loginAction" type="test.LoginAction">
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 ActionModel() {
// TODO Auto-generated constructor stub
}
public ActionModel(String path, String type) {
super();
this.path = path;
this.type = type;
}
/**
* Two behaviors : increase forward object | lookup forward object
* @param forward object
*/
// Will a new forward Label objects are added to the container
public void push(ForwardModel f) {
fMap.put(f.getName(), f);
}
// Inquire about forward label
public ForwardModel pop(String name) {
return fMap.get(name);
}
@Override
public String toString() {
return "ActionMode [path=" + path + ", type=" + type + "]";
}
}ConfigModel: There is no attribute 、 There are sub tags 、 need Two behavior methods :
1) add to action label
2) Inquire about action label
package com.leaf.mode;
/**
* config label
* @author Leaf
*
* 2022 year 6 month 15 Japan In the morning 8:41:23
*/
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String, ActionModel> aMap = new HashMap<String, ActionModel>();
public void push(ActionModel am) {
aMap.put(am.getPath(), am);
}
public ActionModel pop(String path) {
return aMap.get(path);
}
}Two 、23 Factory mode of three modeling methods
But after we build entity classes for each tag and write the corresponding behavior methods ,
Need Create a factory pattern class :ConfigModelFactory
First take a look at the comments about this factory schema class :
/**
* 23 Factory mode of design mode
* ConfigModelFactory It's for production ConfigModel Object's
* manufactured ConfigModel Object contains Config.xml Configuration content in
*
* Produced here ConfigModel There is configuration information
* 1、 analysis Config.xml Configuration information in
* 2、 Load the corresponding configuration information into different model objects
* @author Leaf
*
* 2022 year 6 month 15 Japan In the morning 9:00:42
*/
Then I put it directly Factory class code La :
package com.leaf.mode;
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
* ConfigModelFactory It's for production ConfigModel Object's
* manufactured ConfigModel Object contains Config.xml Configuration content in
*
* Produced here ConfigModel There is configuration information
* 1、 analysis Config.xml Configuration information in
* 2、 Load the corresponding configuration information into different model objects
* @author Leaf
*
* 2022 year 6 month 15 Japan In the morning 9:00:42
*/
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 cm = new ConfigModel();
for (Element actionEle : actionEles) {
ActionModel am = new ActionModel();
am.setPath(actionEle.attributeValue("path"));
am.setType(actionEle.attributeValue("type"));
// take forwardmodel Assign and add to Actionmodel in
List<Element> forwardEle = actionEle.selectNodes("forward");
for (Element element : forwardEle) {
ForwardModel forwardModel = new ForwardModel();
forwardModel.setName(element.attributeValue("name"));
forwardModel.setPath(element.attributeValue("path"));
//redirect: Can only be false|true, Allow space , The default value is false
forwardModel.setRedirect("true".equals(element.attributeValue("redirect")));
am.push(forwardModel);
}
cm.push(am);
}
return cm;
}
// Only test here , It can be called directly where it is used bulid(" File path ") Of ;
public static ConfigModel bulid() throws Exception {
String defaultPath = "config.xml";
return bulid(defaultPath);
}
}When all these are built, we can create another class Call tests Use it :
public static void main(String[] args) throws Exception {
// Call the method of the factory class to get the tag config
ConfigModel cm = ConfigModelFactory.bulid();
// call config How to query
ActionModel am = cm.pop("/loginAction");
// Print test
System.out.println("Type:\t"+am.getType()+"\n");
// Inquire about
ForwardModel fm = am.pop("success");
// Print test
System.out.println("Path:\t"+fm.getPath());
}
Running results :

3、 ... and 、 Practice cases
Case study : Yes web.xml Modeling , Write a servlet adopt url-pattern Read servlet-class Value ;
First of all, put XML file :web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>jrebelServlet</servlet-name>
<servlet-class>com.leaf.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.leaf.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>Then let's go analysis Look at the whole XML file Of structure :
1) Analyze tags
A root tag :web-app
Two sub tags :servlet、servlet-mapping
servlet There are sub tags inside :servlet-name、servlet-class
servlet-mapping There are sub tags inside :servlet-name、url-pattern
According to the above analysis, we can know that there are six different tags in this configuration file :
servlet-name、servlet-class、url-pattern、servlet、servlet-mapping、web-app
except servlet、servlet-mapping、web-app outside , Other tags have values ;
therefore , Old rules , Let's give them six different labels first Object oriented idea Build entity classes :
ServletNameModel、ServletClassModel、UrlPatternModel、ServletModel、ServletMappingModel、WebAppModel;
There have been examples above , There is no more code for putting entity classes here ;
After we build the entity class, we need to create a factory class , Put it here Factory class code :WebAppFactory
package com.leaf.modezuoye;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* WebApp Factory
* @author Leaf
*
* 2022 year 6 month 15 Japan In the morning 11:34:52
*/
public class WebAppFactory {
/**
* modeling
* @param path xml name
* @return
*/
public static WebAppModel build(String path) {
InputStream in = WebAppFactory.class.getResourceAsStream(path);
SAXReader sr = new SAXReader();
WebAppModel wam = new WebAppModel();
try {
Document doc = sr.read(in);
// take servlet The contents of the label are filled in web-app
List<Element> servletEles = doc.selectNodes("/web-app/servlet");
for (Element servletEle : servletEles) {
ServletModel servletModel = new ServletModel();
// to servlet fill xml The content of
Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
ServletNameModel servletNameModel = new ServletNameModel();
ServletClassModel servletClassModel = new ServletClassModel();
servletNameModel.setNr(servletNameEle.getText());
servletClassModel.setNr(servletClassEle.getText());
servletModel.setSnm(servletNameModel);
servletModel.setScm(servletClassModel);
wam.push(servletModel);
}
/*
* take servlet-mapping The contents of the label are filled in web_app
*/
List<Element> servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
for (Element servletMappingEle : servletMappingEles) {
ServletMappingModel servletMappingModel = new ServletMappingModel();
/*
* to servletmapingmodel fill xml The content of
*/
Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
ServletNameModel servletNameModel = new ServletNameModel();
servletNameModel.setNr(servletNameEle.getText());
servletMappingModel.setSnm(servletNameModel);
List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
for (Element urlPatternEle : urlPatternEles) {
UrlPatternModel urlPatternModel = new UrlPatternModel();
urlPatternModel.setNr(urlPatternEle.getText());
servletMappingModel.push(urlPatternModel);
}
wam.push(servletMappingModel);
}
} catch (Exception e) {
e.printStackTrace();
}
return wam;
}
/**
* Automatically find the corresponding background processing class through the web address entered by the browser
* @param webAppModel The modeled entity class
* @param url The web address visited by the browser
* @return
*/
public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
String servletClass = "";
/*
* Find the corresponding URL of the browser servlet-name
*/
String servletName = "";
List<ServletMappingModel> servletMappingModels = webAppModel.pop();
for (ServletMappingModel servletMappingModel : servletMappingModels) {
List<UrlPatternModel> urlPatternModels = servletMappingModel.getUrlPatternModels();
for (UrlPatternModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getNr())) {
ServletNameModel servletNameModel = servletMappingModel.getSnm();
servletName = servletNameModel.getNr();
}
}
}
/*
* find servlet-name Corresponding background processing class
*/
List<ServletModel> servletModels = webAppModel.getServletModels();
for (ServletModel servletModel : servletModels) {
ServletNameModel servletNameModel = servletModel.getSnm();
if(servletName.equals(servletNameModel.getNr())) {
ServletClassModel servletClassModel = servletModel.getScm();
servletClass = servletClassModel.getNr();
}
}
return servletClass;
}
// test
public static void main(String[] args) {
WebAppModel webAppModel = WebAppFactory.build("web.xml");
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);
}
}Running results :

OK, today Leaf That's all for learning notes sharing , See you next time !!!
边栏推荐
- Simple implementation of rectangular area block
- NepCTF 2022 MISC <签到题>(极限套娃)
- 解决手动查询Oracle数据库时间格式不正确的问题(DATE类型)
- D2dengine edible tutorial (1) -- the simplest program
- 【uiautomation】键指令大全(以及三种调用方式)+常用鼠标动作+SendKeys+Inspect学习
- XML建模
- 自定义公式输入框
- Sorting out common SQL interview questions and answers
- Resizeobserver ignoring buried point records - loop limit exceeded
- xtu-ctf Challenges-Reverse 1、2
猜你喜欢

Burpsuite learning notes

TypeScript 高级类型
![[监控部署实操]基于granfana展示Prometheus的图表和loki+promtail的图表](/img/34/b7a05bff05e1d3a1daef4fb2b98a92.png)
[监控部署实操]基于granfana展示Prometheus的图表和loki+promtail的图表

MySQL索引&&执行计划

First meet flask
[email protected] ‘] failed with code 1"/>NPM init vite app < project name > error install for[‘ [email protected] ‘] failed with code 1

Oracle创建数据库“监听程序未启动或数据库服务未注册”错误处理
D2DEngine食用教程(1)———最简单的程序

简单实现矩形面积块

Install enterprise pycharm and Anaconda
随机推荐
D2dengine edible tutorial (1) -- the simplest program
Application of higher-order functions: handwritten promise source code (4)
构造函数,原型链,instanceOf
Solve the problem that the time format of manually querying Oracle database is incorrect (date type)
Getting started with RPC and thrift
Application of higher-order functions: handwritten promise source code (I)
npm init vite-app <project-name> 报错 Install for [‘[email protected]‘] failed with code 1
Flex+js realizes that the height of the internal box follows the maximum height
Understanding of closures of JS
Goodbye if else
Keras saves the best model in the training process
Py program can run, but the packaged exe prompts an error: recursion is detected when loading the "CV2" binary extension. Please check the opencv installation.
Request data acquisition and response
Anti shake and throttling of JS
pycharm如何正确打包ocr且让打包出来的exe尽量小
XML建模
Uscd pedestrian anomaly data set user guide | quick download
[Doris]配置和基本使用contens系统(有时间继续补充内容)
some、every、find、findIndex的用法
Inheritance mode of JS