当前位置:网站首页>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 
边栏推荐
- 树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
- 当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
- Duchefa MS medium contains vitamin instructions
- Abnova CRISPR spcas9 polyclonal antibody protocol
- Abnova丨培养细胞总 RNA 纯化试剂盒中英文说明书
- 解析创客教育的知识迁移和分享精神
- Norgen AAV提取剂盒说明书(含特色)
- Abnova blood total nucleic acid purification kit pre installed relevant instructions
- Abnova丨DNA 标记高质量控制测试方案
- Abnova丨荧光染料 620-M 链霉亲和素方案
猜你喜欢

示波器探头对测量带宽的影响

基于AVFoundation实现视频录制的两种方式

Typhoon is coming! How to prevent typhoons on construction sites!

珍爱网微服务底层框架演进从开源组件封装到自研

Enclosed please find. Net Maui's latest learning resources

10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践

CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)

台风来袭!建筑工地该如何防范台风!

教你自己训练的pytorch模型转caffe(一)

基於flask寫一個接口
随机推荐
MYSQL IFNULL使用功能
Analysis of steam education mode under the integration of five Education
LeetCode: Distinct Subsequences [115]
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
《SAS编程和数据挖掘商业案例》学习笔记# 19
使用WebAssembly在浏览器端操作Excel
Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
Implementation of redis unique ID generator
LeetCode: Distinct Subsequences [115]
When steam education enters personalized information technology courses
清除app data以及获取图标
最长摆动序列[贪心练习]
Abnova cyclosporin a monoclonal antibody and its research tools
解析创客教育的知识迁移和分享精神
木板ISO 5660-1 热量释放速率摸底测试
The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
ViewRootImpl和WindowManagerService笔记
Duchefa low melting point agarose PPC Chinese and English instructions
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口