当前位置:网站首页>XML modeling
XML modeling
2022-07-05 20:57:00 【Timely】
Catalog
One 、xml The core idea of modeling
One 、xml The core idea of modeling
xml The core idea of modeling is to use java Object oriented features , Operate in the way of operating objects xml.
Two 、xml Role of modeling
1、 Saving resource
2、 Optimize performance
3、 More convenient operation xml file
3、 ... and 、 Modeling cases
Below config.xml For example
<?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>
<action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
<forward name="students" path="/students/studentList.jsp" redirect="false"/>
</action>
</config>
- A tag is an object , On the top xml There are three labels in the file config、action、forward. So we need three entity classes to model
ConfigModel class
Add ActionModel object , adopt path Find the corresponding ActionModel object
package com.zking.mymvc.framework;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String, ActionModel> actionMap=new HashMap<String, ActionModel>();
/**
* take ActionModel Objects in the map aggregate
* @param forward
*/
public void put(ActionModel action) {
if(actionMap.containsKey(action.getPath())) {
throw new ActionDuplicateDefinitionException("Action path ="+ action.getPath()+" duplicate definition");
}
actionMap.put(action.getPath(), action);
}
/**
* adopt action Medium path from map Take out the corresponding action object if path If it is filled in incorrectly, a custom exception will be thrown and not found
* @param name
* @return
*/
public ActionModel find(String path) {
if(!actionMap.containsKey(path)) {
throw new ActionNotFoundException("Action path ="+ path +" not found");
}
return actionMap.get(path);
}
@Override
public String toString() {
return "ConfigModel [actionMap=" + actionMap + "]";
}
}
ActionModel class
Add ForwardModel object , adopt path Find the corresponding ForwardModel object
package com.zking.mymvc.framework;
import java.util.HashMap;
import java.util.Map;
public class ActionModel {
private String path;
private String type;
private Map<String, ForwardModel> forwardMap=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 Map<String, ForwardModel> getForwardMap() {
return forwardMap;
}
public void setForwardMap(Map<String, ForwardModel> forwardMap) {
this.forwardMap = forwardMap;
}
/**
* take forwardModel Objects in the map aggregate
* @param forward
*/
public void put(ForwardModel forward) {
if(forwardMap.containsKey(forward.getName())) {
throw new ForwardDuplicateDefinitionException("forward name= "+forward.getName()+"duplicate definition");
}
forwardMap.put(forward.getName(), forward);
}
/**
* adopt forward Medium name from map Take out the corresponding forward object if name If it is filled in incorrectly, a custom exception will be thrown and not found
* @param name
* @return
*/
public ForwardModel find(String name) {
if(!forwardMap.containsKey(name)) {
throw new ForwardNotFoundException("Forward name ="+name+" not found");
}
return forwardMap.get(name);
}
@Override
public String toString() {
return "ActionModel [path=" + path + ", type=" + type + ", forwardMap=" + forwardMap + "]";
}
}
Custom exception
- action Repeat the definition
package com.zking.mymvc.framework;
public class ActionDuplicateDefinitionException extends RuntimeException{
public ActionDuplicateDefinitionException() {
super();
}
/**
* Error message
* @param msg
*/
public ActionDuplicateDefinitionException(String msg) {
super(msg);
}
/**
* Error message 、 Error reason
* @param msg
* @param c
*/
public ActionDuplicateDefinitionException(String msg,Throwable c) {
super(msg,c);
}
}
- action Of path Can't find
package com.zking.mymvc.framework;
public class ActionNotFoundException extends RuntimeException {
public ActionNotFoundException() {
super();
}
/**
* Error message
* @param msg
*/
public ActionNotFoundException(String msg) {
super(msg);
}
/**
* Error message 、 Error reason
* @param msg
* @param c
*/
public ActionNotFoundException(String msg,Throwable c) {
super(msg,c);
}
}
ForwardModel class
package com.zking.mymvc.framework;
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;
}
public void setRedirect(String redirect) {
this.redirect = Boolean.valueOf(redirect);
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
Custom exception
- forward Repeat the definition
package com.zking.mymvc.framework;
public class ForwardDuplicateDefinitionException extends RuntimeException{
public ForwardDuplicateDefinitionException() {
super();
}
/**
* Error message
* @param msg
*
*/
public ForwardDuplicateDefinitionException(String msg) {
super(msg);
}
/**
* Error message 、 Error reason
* @param msg
* @param c
*/
public ForwardDuplicateDefinitionException(String msg,Throwable c) {
super(msg,c);
}
}
- forward Medium name Can't find
package com.zking.mymvc.framework;
public class ForwardNotFoundException extends RuntimeException {
public ForwardNotFoundException() {
super();
}
/**
* Error message
* @param msg
*/
public ForwardNotFoundException(String msg) {
super(msg);
}
/**
* Error message 、 Error reason
* @param msg
* @param c
*/
public ForwardNotFoundException(String msg,Throwable c) {
super(msg,c);
}
}
- When all three are finished , Put them in ConfigModel In the middle , Need to build a ConfigFactory class
ConfigFactory class
package com.zking.mymvc.framework;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.XMLReader;
@SuppressWarnings("unchecked")
public final class ConfigModelFactory {
private ConfigModelFactory() {
}
private static ConfigModel config=new ConfigModel();
// To read config.xml The data in is filled into the model
static {
try {
//1. obtain io flow
InputStream in = XMLReader.class.getResourceAsStream("/config.xml");
//2. establish xml Read tool class SAXReader
SAXReader sax = new SAXReader();
//3. Read configuration file , get Document object
Document doc = sax.read(in);
// Get the root element
Element rootElement = doc.getRootElement();
// find action node
List<Element> actions = rootElement.selectNodes("action");
for(Element e: actions) {
// Get attribute value
String path = e.attributeValue("path");
String type = e.attributeValue("type");
// Instantiation ActionModel object
ActionModel action = new ActionModel();
// Put the attribute value in ActionModel in
action.setPath(path);
action.setType(type);
// find forward node
List<Element> forwards = e.selectNodes("forward");
for(Element f: forwards) {
// Get attribute value
String name = f.attributeValue("name");
String fpath = f.attributeValue("path");
String redirect = f.attributeValue("redirect");
// Instantiation ForwardModel object
ForwardModel forward = new ForwardModel();
// Put the attribute value in ForwardModel Go to the object
forward.setName(name);
forward.setPath(fpath);
forward.setRedirect(redirect);
// take ForwardModel Put it in ActionModel Go to the assembly
action.put(forward);
}
// take ActionModel Put it in the assembly ConfigModel
config.put(action);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static ConfigModel getConfig() {
return config;
}
}
test
package com.zking.mymvc.framework;
public class Test {
public static void main(String[] args) {
ConfigModel config=ConfigModelFactory.getConfig();
ActionModel action = config.find("/studentAction");
System.out.println(action);
ForwardModel forward = action.find("students");
System.out.println(forward);
}
}
test result :
If we deliberately action The path in is misspelled
package com.zking.mymvc.framework;
public class Test {
public static void main(String[] args) {
ConfigModel config=ConfigModelFactory.getConfig();
ActionModel action = config.find("/studentction");
System.out.println(action);
ForwardModel forward = action.find("students");
System.out.println(forward);
}
}
The renderings are as follows : The custom exception will be displayed. The path cannot be found
边栏推荐
- Open source SPL eliminates tens of thousands of database intermediate tables
- Écrire une interface basée sur flask
- When steam education enters personalized information technology courses
- Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
- haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
- Abnova fluorescent dye 620-m streptavidin scheme
- MySQL fully parses json/ arrays
- 判断横竖屏的最佳实现
- 2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
- 教你自己训练的pytorch模型转caffe(三)
猜你喜欢
Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
Abnova丨血液总核酸纯化试剂盒预装相关说明书
珍爱网微服务底层框架演进从开源组件封装到自研
Typhoon is coming! How to prevent typhoons on construction sites!
教你自己训练的pytorch模型转caffe(一)
PHP反序列化+MD5碰撞
基于flask写一个接口
Duchefa丨P1001植物琼脂中英文说明书
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
随机推荐
Abnova CRISPR spcas9 polyclonal antibody protocol
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
实现浏览页面时校验用户是否已经完成登录的功能
启牛2980有没有用?开户安全吗、
Abbkine BCA法 蛋白质定量试剂盒说明书
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
Duchefa p1001 plant agar Chinese and English instructions
Learning notes of SAS programming and data mining business case 19
Popular science | does poor English affect the NPDP exam?
ProSci LAG-3 重组蛋白说明书
leetcode:1755. 最接近目标值的子序列和
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
Abnova CD81 monoclonal antibody related parameters and Applications
Prior knowledge of machine learning in probability theory (Part 1)
SYSTEMd resolved enable debug log
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
Duchefa MS medium contains vitamin instructions
Monorepo管理方法论和依赖安全
Abnova cyclosporin a monoclonal antibody and its research tools
CLion配置visual studio(msvc)和JOM多核编译