XML建模与自定义异常

1、什么是XML建模

将XML配置文件中的元素,属性,文本信息 转换成对象的过程叫XML建模。

2、XML建模

根据XML配置文件元素节点创建元素,节点,实体类
ConfigModel ActionModel ForwardModel
利用dom4j+xpath技术实现XML建模ConfigModelFactory(提高代码的复用性)

3、xml建模的思路

1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、定义对象从小到大(从里到外)
4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象

4、建模的好处

提高代码的复用性

5、下面是今天所用到的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>

6、他一共有3个标签我们就要建3个基础类 config action forward

ConfigModel类

package com.zking.mymvc.framework;

import java.util.HashMap;
import java.util.Map;

public class ConfigModel {
	
	private Map<String,ActionModel> actionMap = new HashMap<>();	
	
	public void put(ActionModel action) {
		if(actionMap.containsKey(action.getPath())) {
			 throw new ActionDulicateDefinitionException("Action path ="+action.getPath()+"duplicate definition");
		}
		actionMap.put(action.getPath(),action);
	}
	
	public ActionModel find(String path) {
		if(!actionMap.containsKey(path)) {
			throw new ActionNotFoundException("Action path ="+path+"not found");
		}
		return actionMap.get(path);
	}

}

ActionModel类

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

	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;
	}
	
	
	
	@Override
	public String toString() {
		return "ActionModel [path=" + path + ", type=" + type + ", forwardMap=" + forwardMap + "]";
	}

	public void put(ForwardModel forward) {
		if(forwardMap.containsKey(forward.getName())) {
			throw new ForwardDulicateDefinitionException("forward name="+forward.getName()+"duplicate definition");
		}
		forwardMap.put(forward.getName(), forward);
	}
	public ForwardModel find(String name) {
		if(!forwardMap.containsKey(name)) {
			throw new ForwardNotFoundException("forward name="+name+"not found");
		}
		return forwardMap.get(name);
	}
	
}

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

7、自定义异常代码

ActionDulicateDefinitionException类

package com.zking.mymvc.framework;

public class ActionDulicateDefinitionException extends RuntimeException{
	
	public ActionDulicateDefinitionException() {
		super();
	}
	
	public ActionDulicateDefinitionException(String msg) {
		super(msg);
	}
	
	public ActionDulicateDefinitionException(String msg,Throwable c) {
		super(msg,c);
	}
	
}

ActionNotFoundException类

package com.zking.mymvc.framework;

public class ActionNotFoundException extends RuntimeException{
	
	public ActionNotFoundException() {
		super();
	}
	
	public ActionNotFoundException(String msg) {
		super(msg);
	}
	
	public ActionNotFoundException(String msg,Throwable c) {
		super(msg,c);
	}
	
}

ForwardDulicateDefinitionException类

package com.zking.mymvc.framework;

public class ForwardDulicateDefinitionException extends RuntimeException{
	
	public ForwardDulicateDefinitionException() {
		super();
	}
	
	public ForwardDulicateDefinitionException(String msg) {
		super(msg);
	}
	
	public ForwardDulicateDefinitionException(String msg,Throwable c) {
		super(msg,c);
	}
	
}

ForwardNotFoundException 类

package com.zking.mymvc.framework;

public class ForwardNotFoundException extends RuntimeException{
	
	public ForwardNotFoundException() {
		super();
	}
	
	public ForwardNotFoundException(String msg) {
		super(msg);
	}
	
	public ForwardNotFoundException(String msg,Throwable c) {
		super(msg,c);
	}
	
}

8、基础类建完了,就到我们的俗称是工厂模式的类了

package com.zking.mymvc.framework;

import java.io.InputStream;
import java.util.List;

import javax.sql.rowset.spi.XmlReader;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

@SuppressWarnings("unchecked")
public final class ConfigModelFactory {
	
	private ConfigModelFactory() {}
	
	private static ConfigModel config = new ConfigModel();
	
	//读取config.xml中的数据,填充到模型中
	static {
		try {
			InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
			SAXReader reader = new SAXReader();
			Document doc = reader.read(in);
			
			Element rootElement = doc.getRootElement();
			List<Element> actions = rootElement.selectNodes("action");
			
			for(Element e:  actions) {
				String path = e.attributeValue("path");
				String type = e.attributeValue("type");
				
				ActionModel action = new ActionModel();
				action.setPath(path);
				action.setType(type);
				
				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 forward = new ForwardModel();
					forward.setName(name);
					forward.setPath(fPath);
					forward.setRedirect(redirect);
					
					action.put(forward);
				}
				
				config.put(action);
			}
		} catch (Exception e) {
			
		}
		
	}
	
	public static ConfigModel getConfig() {
		return config;
	}
	
	
	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);
	}

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值