我们为什么要进行建模呢?
我们可以通过获取资源文件xml,拿到指定的xml字符串,那么不同的人,有不同的需求,那就意味着每一个人都需要对指定的xml文件进行解析,这样我们耗费的时间将大大增加,所以我们要将xml字符串存储到内存当中方便拿取,这就是我们为什么要进行一个建模的操作
建模说白了就是将指定的xml字符串当作对象来操作
如果一个xml文件完成了建模,那么我们就可以调用指定的方法直接拿到数据,不需要再走一遍io流的操作,节省了大部分时间
建模的思路:
- 分析需要被建模的文件中有哪几个对象
- 每个对象拥有的行为和属性
- 定义对象从小到大(从里到外)
- 通过工厂模式,解析xml生产出指定对象(提高代码的复用性)
建模分两步:
- 以面向对象的编程思路,描述xml资源文件
- 将xml文件中的内容封装进model实体对象
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>
<action path="/studentAction02" type="org.lisen.mvc.action.StudentAction">
<forward name="students02" path="/students/studentList.jsp" redirect="false"/>
action>
config>
package com.mymvc;
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 + "]";
}
}
package com.mymvc;
public class ForwardDuplicateDefinitionException extends RuntimeException {
public ForwardDuplicateDefinitionException() {
super();
}
public ForwardDuplicateDefinitionException(String msg) {
super(msg);
}
/**
* @param msg
* @param c(异常原因)
*/
public ForwardDuplicateDefinitionException(String msg ,Throwable c) {
super(msg ,c);
}
}
package com.mymvc;
public class ForwardNotFoundException extends RuntimeException{
public ForwardNotFoundException() {
super();
}
public ForwardNotFoundException(String msg) {
super(msg);
}
/**
* @param msg
* @param c(异常原因)
*/
public ForwardNotFoundException(String msg ,Throwable c) {
super(msg ,c);
}
}
package com.mymvc;
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 void put(ForwardModel forward) {
if(forwardMap.containsKey(forward.getName())) {
throw new ForwardDuplicateDefinitionException("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);
}
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 + "]";
}
}
package com.mymvc;
public class ActionDuplicateDefinitionException extends RuntimeException {
public ActionDuplicateDefinitionException() {
super();
}
public ActionDuplicateDefinitionException(String msg) {
super(msg);
}
/**
* @param msg
* @param c(异常原因)
*/
public ActionDuplicateDefinitionException(String msg ,Throwable c) {
super(msg ,c);
}
}
package com.mymvc;
public class ActionNotFoundException extends RuntimeException{
public ActionNotFoundException() {
super();
}
public ActionNotFoundException(String msg) {
super(msg);
}
/**
* @param msg
* @param c(异常原因)
*/
public ActionNotFoundException(String msg ,Throwable c) {
super(msg ,c);
}
}
package com.mymvc;
import java.util.HashMap;
import java.util.Map;
public class ConfigModel {
private Map<String ,ActionModel> actionMap = new HashMap<String ,ActionModel>();
public void put(ActionModel action) {
if(actionMap.containsKey(action.getPath())) {
throw new ActionDuplicateDefinitionException("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);
}
}
package com.mymvc;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigModelFactory {
/**
* 单例模式
*/
private ConfigModelFactory() {}
private static ConfigModel config = new ConfigModel();
/**
* 使用单例模式获得ConfigModel
* @return ConfigModel
*/
public static ConfigModel getConfig() {
return config;
}
static {
try {
InputStream in = ConfigModelFactory.class.getResourceAsStream("/config.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
Element root = doc.getRootElement();
List<Element> actions = root.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 fe : forwards) {
String name = fe.attributeValue("name");
String fpath = fe.attributeValue("path");
String redirect = fe.attributeValue("redirect");
ForwardModel forward = new ForwardModel();
forward.setName(name);
forward.setPath(path);
forward.setRedirect(redirect);
action.put(forward);
}
config.put(action);
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ActionModel actionModel = ConfigModelFactory.getConfig().find("/studentAction");
System.out.println(actionModel);
System.out.println(actionModel.find("students"));
}
}