当前位置:网站首页>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 marker high quality control test program
- hdu2377Bus Pass(构建更复杂的图+spfa)
- 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
- 基于flask写一个接口
- php中explode函数存在的陷阱
- Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
- Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
- 获取前一天的js(时间戳转换)
- Abnova fluorescent dye 620-m streptavidin scheme
- LeetCode: Distinct Subsequences [115]
猜你喜欢
随机推荐
Abnova CD81 monoclonal antibody related parameters and Applications
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
Is Kai Niu 2980 useful? Is it safe to open an account
驱动壳美国测试UL 2043 符合要求有哪些?
AITM 2-0003 水平燃烧试验
Write an interface based on flask
Chemical properties and application instructions of prosci Lag3 antibody
培养机器人教育创造力的前沿科技
国外LEAD美国简称对照表
PHP反序列化+MD5碰撞
获取前一天的js(时间戳转换)
基于AVFoundation实现视频录制的两种方式
Abnova maxpab mouse derived polyclonal antibody solution
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
示波器探头对信号源阻抗的影响
Talk about my fate with some programming languages
Specification of protein quantitative kit for abbkine BCA method
Binary search
PHP deserialization +md5 collision
matplotlib绘图润色(如何形成高质量的图,例如设如何置字体等)