XML建模(自定义Exception)

XML建模

本期类容⛵️

在这里插入图片描述

一、xml建模核心思想

xml建模的核心思想就是利用java面向对象的特性,用操作对象的方式操作xml。

二、xml建模的作用

1、节约资源

2、优化性能

3、更加便捷操作xml文件

三、建模案例

以下方config.xml为例

<?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>

一个标签就是一个对象,在上方xml文件中有三个标签config、action、forward。所以我们需要三个实体类来进行建模

ConfigModel类

在集合中增加ActionModel对象,通过path找到对应的ActionModel对象

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>();
	
 
	/**
	 * 将ActionModel对象放到map集合
	 * @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);
	}
	
	/**
	 * 通过action中的path从map结合中取出对应的action对象 若path填错则抛出自定义异常未找到
	 * @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类

在集合中增加ForwardModel对象,通过path找到对应的ForwardModel对象

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;
	}
	
	/**
	 * 将forwardModel对象放到map集合
	 * @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);
	}
	
	/**
	 * 通过forward中的name从map结合中取出对应的forward对象 若name填错则抛出自定义异常未找到
	 * @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 + "]";
	}
	
	
}

自定义异常

action重复异常定义

package com.zking.mymvc.framework;
 
public class ActionDuplicateDefinitionException extends RuntimeException{
 
	public ActionDuplicateDefinitionException() {
		super();
	}
	/**
	 * 报错信息
	 * @param msg
	 */
	public ActionDuplicateDefinitionException(String msg) {
		super(msg);
	}
	/**
	 * 报错信息、报错原因
	 * @param msg
	 * @param c
	 */
	public ActionDuplicateDefinitionException(String msg,Throwable c) {
		super(msg,c);
	}
	
}

action的path找不到异常

package com.zking.mymvc.framework;
 
public class ActionNotFoundException extends RuntimeException {
 
	public ActionNotFoundException() {
		super();
	}
	/**
	 * 报错信息
	 * @param msg
	 */
	public ActionNotFoundException(String msg) {
		super(msg);
	}
	/**
	 * 报错信息、报错原因
	 * @param msg
	 * @param c
	 */
	public ActionNotFoundException(String msg,Throwable c) {
		super(msg,c);
	}
	
}

ForwardModel类

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 + "]";
	}
	
	
	
}

自定义异常

forward重复定义

package com.zking.mymvc.framework;
 
public class ForwardDuplicateDefinitionException extends RuntimeException{
 
	public ForwardDuplicateDefinitionException() {
		super();
	}
	/**
	 * 报错信息
	 * @param msg
	 * 
	 */
	public ForwardDuplicateDefinitionException(String msg) {
		super(msg);
	}
	/**
	 * 报错信息、报错原因
	 * @param msg
	 * @param c
	 */
	public ForwardDuplicateDefinitionException(String msg,Throwable c) {
		super(msg,c);
	}
	
}

forward中的name找不到

package com.zking.mymvc.framework;
 
public class ForwardNotFoundException extends RuntimeException {
 
	public ForwardNotFoundException() {
		super();
	}
	/**
	 * 报错信息
	 * @param msg
	 */
	public ForwardNotFoundException(String msg) {
		super(msg);
	}
	/**
	 * 报错信息、报错原因
	 * @param msg
	 * @param c
	 */
	public ForwardNotFoundException(String msg,Throwable c) {
		super(msg,c);
	}
	
}

当三个都完成后,将他们装到ConfigModel中去,需要建一个ConfigFactory类

ConfigFactory类

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();
	
	//进行读取config.xml中的数据填充到模型中
	static {
		try {
			//1.获取io流
			InputStream in = XMLReader.class.getResourceAsStream("/config.xml");
		    //2.创建xml读取工具类SAXReader
			SAXReader sax = new SAXReader();
		    //3.读取配置文件,获得Document对象
			Document doc = sax.read(in);
		    //获取根元素
		    Element rootElement = doc.getRootElement();
		    //找到action节点
		    List<Element> actions = rootElement.selectNodes("action");
	        for(Element e: actions) {
	        	//获取属性值
	        	String path = e.attributeValue("path");
	            String type = e.attributeValue("type");
	            //实例化ActionModel对象
	            ActionModel action = new ActionModel();
	           //将属性值放到ActionModel中
	            action.setPath(path);
	            action.setType(type);
	            //找到forward节点
	            List<Element> forwards = e.selectNodes("forward");
	           for(Element f: forwards) {
	        	 //获取属性值
	        	   String name = f.attributeValue("name");
	               String fpath = f.attributeValue("path");
	               String redirect = f.attributeValue("redirect");
	               //实例化ForwardModel对象
	                ForwardModel forward = new ForwardModel();
	               //将属性值放入ForwardModel对象中去
	                forward.setName(name);
	               forward.setPath(fpath);
	               forward.setRedirect(redirect);
	               //将ForwardModel放到ActionModel集合中去
	              action.put(forward);   
	           }
	         //将ActionModel放到集合中去ConfigModel
	           config.put(action);
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
        
	}
	
	public static ConfigModel getConfig() {
		return config;
	}
	
	
}

测试效果

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);
	}
 
}

输出

在这里插入图片描述

当我们故意把action中的路径写错 如:

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);
	}
 
}

效果图如下:就会显示自定义异常该路径找不到

在这里插入图片描述

本期类容到此结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷亿!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值