xml建模

1、ConfigModel:

public class ConfigModel {
    //conifg节点下有多个action
    Map<String,ActionModel> actions = new HashMap<String, ActionModel>();
    
    //将解析到的actino数据,放到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:、ActionModel:

public class ActionModel {
    private String path;
    private String type;
    //action中有forward子元素
    private Map<String, ForwardModel> forwards = new HashMap<String, ForwardModel>();

    private static Pattern pattern = Pattern.compile("^/.+$");
    
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        Matcher matcher = pattern.matcher(path);
        if(!matcher.matches()) {
            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);
    }
}

3、ForwardModel:

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);
        if(!matcher.matches()) {
            throw new RuntimeException("ForwardModel.path[" + path + "]必须以/开头");
        }
        this.path = path;
    }

    public String getRedirect() {
        return redirect;
    }

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

4、ConfigFactory:

/**
 * 读取conifg.xml放入建好的模型中
 * @author zjjt
 *
 */
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 root = doc.getRootElement();
            List<Element> actions = root.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) {
            // TODO Auto-generated catch block
            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());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值