XML建模

 

 

 

文章目录

 


前言

创建实体模型就是建模, 建模的原因是为了在内存中使用方便;


提示:以下是本篇文章正文内容,下面案例可供参考

一、XML建模是什么?

ConfigModel ActionModel
ForwardModel
ConfigModelFactory  
ActionModel

二、使用步骤

1.ConfigModel

代码如下(示例):

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

/**
 * config.xml中的config节点模型
 * @author L
 *
 */
public class ConfigModel {
    
    //config节点下会有多个action
        Map<String,ActionModel> actions = new HashMap<>();
        
        //讲解析到的action数据,放入actions集合中去
    public void addAction(ActionModel action) {
        if(actions.containsKey(action.getPath())) {
            throw new RuntimeException("ActionModel[" + action.getPath() + "]");
        }
        actions.put(action.getPath(), action);
    }
    
    //通过指定的path,从actions集合中获取Action对象
    public ActionModel findAction(String path) {
        
        if(!actions.containsKey(path)) {
            throw new RuntimeException("ActionModel[" + path + "]");
        }
        
        return actions.get(path);
    }
    
}

2.ForwardModel

代码如下(示例):

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ForwardModel {
    
    private String name;
    
    private String path;
    
    private String redirect;
    
    private static Pattern pattern = Pattern.compile("^/.+$");

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        
        Matcher matcher = pattern.matcher(path);
        boolean b = matcher.matches();
        if(!b) {
            throw new RuntimeException("ForwardModel.path[" + path + "]");
        }
        
        this.path = path;
    }

    public String getRedirect() {
        return redirect;
    }

    public void setRedirect(String redirect) {
        this.redirect = redirect;
    }

}

3.ConfigFactory

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

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

/*
 * 读取config.xml放入建好的模型中
 */
public class ConfigFactory {
	
	private static String CONFIG_NAME = "/config.xml";
	
	private static ConfigModel configModel = new ConfigModel();
	
	//在static语句块中读取config.xml,static块只会在类加载的过程中执行一次
		static {
		InputStream in = ConfigFactory.class.getResourceAsStream(CONFIG_NAME);
		SAXReader reader = new SAXReader();
		
		try {
			Document doc = reader.read(in);
			Element rootElement = doc.getRootElement();
			
			List<Element> actions = rootElement.selectNodes("action");
			for(Element action: actions) {
				String path = action.attributeValue("path");
				String type = action.attributeValue("type");
				
				ActionModel actionModel = new ActionModel();
				actionModel.setPath(path);
				actionModel.setType(type);
				
				List<Element> forwards = action.selectNodes("forward");
				for(Element forward: forwards) {
					String name = forward.attributeValue("name");
					String fpath = forward.attributeValue("path");
					String redirect = forward.attributeValue("redirect");
					
					ForwardModel forwardModel = new ForwardModel();
					forwardModel.setName(name);
					forwardModel.setPath(fpath);
					forwardModel.setRedirect(redirect);
					
					actionModel.addForward(forwardModel);
				}
				
				configModel.addAction(actionModel);
			}
			
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}



	public static ConfigModel getConfigModel() {
		return configModel;
	}


	public static void main(String[] args) {
		ConfigModel configModel = getConfigModel();
		ActionModel actionModel = configModel.findAction("/loginAction");
		ForwardModel forwardModel = actionModel.findForward("success");
		System.out.println(forwardModel.getPath());
	}

}

 

4.ActionModel

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ActionModel {
	
	private String path;
	
	private String type;
	

	private Map<String,ForwardModel> forwards = new HashMap<>();
	
	private static Pattern pattern = Pattern.compile("^/.+$");

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		
		Matcher matcher = pattern.matcher(path);
		boolean b = matcher.matches();
		if(!b) {
			throw new RuntimeException("ActionModel.path[" + path + "]");
		}
		
		this.path = path;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public void addForward(ForwardModel forward) {
		if(forwards.containsKey(forward.getName())) {
			throw new RuntimeException("ActionModel[" + path + "]/forwardModel.name[" + forward.getName() + "]");
		}
		forwards.put(forward.getName(), forward);
	}
	
	public ForwardModel findForward(String name) {
		if(!forwards.containsKey(name)) {
			throw new RuntimeException("ActionModel[" + path + "]/forwardModel.name[" + name + "]涓嶅瓨鍦�");
		}
		return forwards.get(name);
	}

}

总结

以上就是今天要讲的内容,今天主要讲了建模,以及将xml文件解析;

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值