当前位置:网站首页>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
边栏推荐
- Abnova丨DNA 标记高质量控制测试方案
- ts 之 属性的修饰符public、private、protect
- leetcode:1139. 最大的以 1 为边界的正方形
- Enclosed please find. Net Maui's latest learning resources
- MYSQL IFNULL使用功能
- Popular science | does poor English affect the NPDP exam?
- 王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
- 国外LEAD美国简称对照表
- Monorepo管理方法论和依赖安全
- phpstudy小皮的mysql点击启动后迅速闪退,已解决
猜你喜欢
珍爱网微服务底层框架演进从开源组件封装到自研
Maker education infiltrating the transformation of maker spirit and culture
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
Abnova fluorescent dye 620-m streptavidin scheme
浅聊我和一些编程语言的缘分
MySQL InnoDB架构原理
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
Prosci LAG-3 recombinant protein specification
显示屏DIN 4102-1 Class B1防火测试要求
Abnova丨 MaxPab 小鼠源多克隆抗体解决方案
随机推荐
AITM2-0002 12s或60s垂直燃烧试验
Abnova DNA marker high quality control test program
Norgen AAV提取剂盒说明书(含特色)
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
序列联配Sequence Alignment
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
Graph embedding learning notes
Simple getting started example of Web Service
XML建模
Abnova fluorescent dye 620-m streptavidin scheme
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
Talk about my fate with some programming languages
Abnova maxpab mouse derived polyclonal antibody solution
CareerCup它1.8 串移包括问题
Écrire une interface basée sur flask
五层网络协议
Abnova 环孢素A单克隆抗体,及其研究工具
示波器探头对信号源阻抗的影响
Analyze the knowledge transfer and sharing spirit of maker Education
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)