当前位置:网站首页>Modeling of XML
Modeling of XML
2022-07-24 06:27:00 【zsm030616】
One 、xml modeling
Modeling is divided into two steps :
1、 With the idea of object-oriented programming , describe xml Resource file
2、 take xml The contents of the document are encapsulated in model Entity object .
characteristic : Just call the specified method to complete the predetermined string acquisition ;
The idea of modeling :
1、 analysis xml Properties and behaviors in the content contained in , And then encapsulate it ;
2、 When encapsulating, it is necessary to judge whether the attribute is single or multiple , And then sort it out ;
3、 When packaging, pay attention to from the inside to the outside ( From small to large ) encapsulate ;
4、xml Put the file in the root directory , Otherwise, a null pointer exception will occur
Case study
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>jrebelServlet</servlet-name>
<servlet-class>com.zsm.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.zsm.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>
ForwardModel.java
package com.zsm.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.java
package com.zsm.model;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
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;
}
// Two behaviors , increase forwardModel object , lookup forwardModel object
// Will a new forward Label objects are added to the container
public void push(ForwardModel forwoardModel) {
fMap.put(forwoardModel.getName(), forwoardModel);
}
public ForwardModel pop(String name) {
return fMap.get(name);
}
}
ConfigModel.java
package com.zsm.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);
}
}
Two 、 Factory mode
1. Why use factory mode
It can improve the reusability of code
2. How to use factory mode
Just build a method , To produce the specified object you need
3. Where to use factory mode
To produce the specified object you need , For reuse , Repeat the operation xml file , Repeatedly analyze xml character string
Case study :
package com.zsm.model;
import java.io.InputStream;
import java.util.List;
import com.cxy.model.ForwardModel;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigModelFactory {
public static ConfigModel build(String path) throws Exception {
//String defaultPath="/config.xml";
InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
SAXReader sr=new SAXReader();
Document doc = sr.read(in);
List<Element> actionEles = doc.selectNodes("/config/action");
ConfigModel configModel=new ConfigModel();
for (Element actionEle : actionEles) {
ActionModel actionModel=new ActionModel();
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
// take forwardModel Assign and add to ActionModel in
List<Element> forwardEles = actionEle.selectNodes("forward");
for (Element element : forwardEles) {
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")));
actionModel.push(forwardModel);
}
configModel.push(actionModel);
}
return configModel;
}
public static ConfigModel build() throws Exception {
String defaultPath="/config.xml";
return build(defaultPath);
}
public static void main(String[] args) throws Exception {
//ConfigModel configModel=new ConfigModel();
ConfigModel configModel=ConfigModelFactory.build();
ActionModel actionModel = configModel.pop("/loginAction");
System.out.println(actionModel.getType());
ForwardModel forwardModel = actionModel.pop("success");
System.out.println(forwardModel.getPath());
}
}
3、 ... and 、 Case study :web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>jrebelServlet</servlet-name>
<servlet-class>com.zsm.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.zsm.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>
ServletClassModel .java
package com.zsm.modelzsm;
/**
* servlet Entity class
* @author zjjt
*
*/
public class ServletClassModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public ServletClassModel() {
// TODO Auto-generated constructor stub
}
public ServletClassModel(String context) {
super();
this.context = context;
}
@Override
public String toString() {
return "ServletClassModel [context=" + context + "]";
}
}
ServletNameModel .java
package com.zsm.modelzsm;
/**
* servletname Entity class
* @author zjjt
*
*/
public class ServletNameModel {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public ServletNameModel() {
// TODO Auto-generated constructor stub
}
public ServletNameModel(String content) {
super();
this.content = content;
}
@Override
public String toString() {
return "ServletNameModel [content=" + content + "]";
}
}
UrlPatternModel.java
package com.zsm.modelzsm;
/**
* url—pattern Entity class
* @author zjjt
*
*/
public class UrlPatternModel {
private String context;
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public UrlPatternModel() {
// TODO Auto-generated constructor stub
}
public UrlPatternModel(String context) {
super();
this.context = context;
}
@Override
public String toString() {
return "UrlPatternModel [context=" + context + "]";
}
}
ServletModel .java
package com.zsm.modelzsm;
/**
* servlet Entity class
* @author zjjt
*
*/
public class ServletModel {
private ServletNameModel servletname;
private ServletClassModel servletclass;
public ServletNameModel getServletname() {
return servletname;
}
public void setServletname(ServletNameModel servletname) {
this.servletname = servletname;
}
public ServletClassModel getServletclass() {
return servletclass;
}
public void setServletclass(ServletClassModel servletclass) {
this.servletclass = servletclass;
}
public ServletModel() {
// TODO Auto-generated constructor stub
}
public ServletModel(ServletNameModel servletname, ServletClassModel servletclass) {
super();
this.servletname = servletname;
this.servletclass = servletclass;
}
@Override
public String toString() {
return "ServletModel [servletname=" + servletname + ", servletclass=" + servletclass + "]";
}
}
ServletMapingModel .java
package com.zsm.modelzsm;
import java.util.ArrayList;
import java.util.List;
/**
* servletmapping Entity class
* @author zjjt
*
*/
public class ServletMapingModel {
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;
}
}
WepAppModel .java
package com.zsm.modelzsm;
import java.util.ArrayList;
import java.util.List;
/**wep-app Entity class
* @author zjjt
*
*/
public class WepAppModel {
private List<ServletModel> servletModels = new ArrayList<>();
private List<ServletMapingModel> servletMappingModels = new ArrayList<>();
public void push(ServletModel servletModel) {
servletModels.add(servletModel);
}
public List<ServletModel> getServletModels() {
return servletModels;
}
public void push(ServletMapingModel servletMappingModel) {
servletMappingModels.add(servletMappingModel);
}
public List<ServletMapingModel> getServletMappingModels() {
return servletMappingModels;
}
}
WebAppModelFactory .java
package com.zsm.modelzsm;
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 {
/**
* xml
* @return
*/
public static WepAppModel buildWebAppModel() {
String xmlPath = "/web.xml";
return buildWebAppModel(xmlPath);
}
/**
* modeling
* @param path xml name
* @return
*/
public static WepAppModel buildWebAppModel(String path) {
InputStream in = WebAppModelFactory.class.getResourceAsStream(path);
SAXReader sr = new SAXReader();
WepAppModel wam = new WepAppModel();
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.setContent(servletNameEle.getText());
servletClassModel.setContext(servletClassEle.getText());
servletModel.setServletname(servletNameModel);
servletModel.setServletclass(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) {
ServletMapingModel servletMappingModel = new ServletMapingModel();
/*
* to servletmapingmodel fill xml The content of
*/
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.setContext(urlPatternEle.getText());
servletMappingModel.pushUrlPatternModel(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(WepAppModel webAppModel, String url) {
String servletClass = "";
/*
* Find the corresponding URL of the browser servlet-name
*/
String servletName = "";
List<ServletMapingModel> servletMapingModels = webAppModel.getServletMappingModels();
for (ServletMapingModel servletMappingModel : servletMapingModels) {
List<UrlPatternModel> urlPatternModels = servletMappingModel.getUrlPatternModels();
for (UrlPatternModel urlPatternModel : urlPatternModels) {
if(url.equals(urlPatternModel.getContext())) {
ServletNameModel servletNameModel = servletMappingModel.getServletNameModel();
servletName = servletNameModel.getContent();
}
}
}
/*
* find servlet-name Corresponding background processing class
*/
List<ServletModel> servletModels = webAppModel.getServletModels();
for (ServletModel servletModel : servletModels) {
ServletNameModel servletNameModel = servletModel.getServletname();
if(servletName.equals(servletNameModel.getContent())) {
ServletClassModel servletClassModel = servletModel.getServletclass();
servletClass = servletClassModel.getContext();
}
}
return servletClass;
}
public static void main(String[] args) {
WepAppModel 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);
}
}
边栏推荐
- IP笔记(10)
- 第二周作业
- Do not rent servers, build your own personal business website (4)
- 配置固定的远程桌面地址【内网穿透、无需公网IP】
- Simple but easy to use: using keras 2 to realize multi-dimensional time series prediction based on LSTM
- IP notes (10)
- Sorting of common AR and MR head mounted display devices
- MySQL from basic to entry to high availability
- Basic knowledge of unity and the use of some basic APIs
- 一个测试经理/测试主管/测试总监的工作总结
猜你喜欢

Do not rent servers, build your own personal business website (4)

A batch of interview questions and answers_ 20180403 latest arrangement

MySQL from basic to entry to high availability

The public network uses Microsoft Remote Desktop remote desktop to work remotely at any time

【217】#!/usr/bin/env 的意义

项目上复盘引导问题清单

公网访问内网IIS网站服务器【无需公网IP】

Using keras and LSTM to realize time series prediction of long-term trend memory -lstnet

IA课总结(2)

Mysql database - SQL summary (remember to pay attention to me! Come on in China!)
随机推荐
一批面试题及答案_20180403最新整理
快速简单搭建FTP服务器,并内网穿透实现公网访问【无需公网IP】
Unity 3D frame rate statistics script
IP笔记(7)
Do not rent servers, build your own personal business website (2)
MySQL数据库—SQL汇总(记得关注我!中国加油!)
Machine learning & deep learning introduction information sharing summary
List of problems in the re disk guidance of the project
Heap overflow of kernel PWN basic tutorial
[226] instructions for Wireshark parameters
[301] grotesque behavior - predictable irrationality
IP作业(2)RIP
[214] what is an automation framework
Summary of ten common vulnerabilities (principle, harm, defense)
How to build a website full of ritual sense and publish it on the public website 2-2
Work summary of a test Manager / Test Supervisor / test director
配置固定的远程桌面地址【内网穿透、无需公网IP】
Hololens 2 development: development environment deployment
Flink checkpoint configuration details
不租服务器,自建个人商业网站(如何购买域名)