MVC框架实现3---Smart框架配置文件解析

Smart框架配置文件解析解析用的是dom4j工具,所以编写代码前需要将依赖的jar包导入ClassPath

ConfigResolver.java ,解析xml配置文件

配置文件模板:

<?xml version="1.0" encoding="UTF-8" ?>

<Smart-Config>

	
	<!-- 
		<RequestProcessor class="com.smart.servlet.helper.DefaultRequestProcessor"/>
		<Converter target="java.util.Date" class="com.smart.converter.UtilDateConverter" />
 	-->
	<Actions>
		<!-- Action 可配置多个,每个Action同样可以配置多个Result -->
		<Action name="helloAction" class="com.app.HelloAction" method="execute1">
			<Result name="index">/index.jsp</Result>
		</Action>
	</Actions>
	
	
</Smart-Config>



package com.smart.config;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

import com.smart.common.SmartConstants;
import com.smart.context.ConfigContext;
import com.smart.context.SmartContext;
import com.smart.exception.ConfigResolverException;


/**
 * 解析smart配置文件
 * 用dom4j为工具来解析
 * @author issuser
 *
 */
public class ConfigResolver {
	
	@SuppressWarnings("unchecked")
	public static void configResolving( File cfgFile){
		ConfigContext configContext = null;
		
		try {
			//将文档读取到内存
			SAXReader reader = new SAXReader();	
			Document doc = reader.read(cfgFile);
			
			//读取配置文件根节点Smart-Config
			Node root = doc.selectSingleNode("/"+SmartConstants.ROOT_NODE_CONFIG);
			configContext = SmartContext.getContext().getConfigContext();
			
			//解析RequestProcessor节点
			Node processorNode = root.selectSingleNode(SmartConstants.PROCESSOR_NODE_CONFIG);
			if( processorNode != null ){
				Element e = (Element)processorNode;
				configContext.setProcessorDefine(
						new RequestProcessorDefine(e.attributeValue(SmartConstants.CLASS_ATTR_CONFIG)));
			}
			
			//解析Converter节点
			List<ConverterDefine> cds = configContext.getCds();
			List<Element> cs = (List<Element>)root.selectNodes(SmartConstants.CONVERTER_NODE_CONFIG);
			for( Element e : cs){
				ConverterDefine cd = new ConverterDefine();
				cd.setClazz(e.attributeValue(SmartConstants.CLASS_ATTR_CONFIG));
				cd.setTarget(e.attributeValue(SmartConstants.TARGET_ATTR_CONFIG));
				cds.add(cd);
			}
			
			//解析Actions节点
			Node actionsNode = root.selectSingleNode(SmartConstants.ACTIONS_NOAD_CONFIG);
			if( actionsNode != null ){
				Map<String,ActionDefine> actions = configContext.getActions();
				
				//解析Actions中的每个Action节点
				List<Element> list = (List<Element>)actionsNode.selectNodes(SmartConstants.ACTION_NOAD_CONFIG);
				for( Element e : list ){
					
					ActionDefine actionDefine = new ActionDefine();
					actionDefine.setName(e.attributeValue(SmartConstants.NAME_ATTR_CONFIG));
					actionDefine.setClazz(e.attributeValue(SmartConstants.CLASS_ATTR_CONFIG));
					if(e.attributeValue(SmartConstants.METHOD_ATTR_CONFIG) != null ){
						actionDefine.setMethod(e.attributeValue(SmartConstants.METHOD_ATTR_CONFIG));
					}
						
					//解析Action节点中的每个Result节点
					List<ForwarderDefine> fds = new ArrayList<ForwarderDefine>();
					List<Element> rs = e.selectNodes(SmartConstants.RESULT_NOAD_CONFIG);
					for( Element r : rs ){
						ForwarderDefine fd = new ForwarderDefine();
						fd.setName(r.attributeValue(SmartConstants.NAME_ATTR_CONFIG));
						fd.setPath(r.getStringValue());
						
						fds.add(fd);
					}
					
					actionDefine.setFdList(fds);
					
					actions.put(actionDefine.getClazz(), actionDefine);
				}
			}
			
			
		} catch (Exception e){
			throw new ConfigResolverException(e);
		}
		
	}
	
}

ActionDefine.java

package com.smart.config;

import java.util.List;

import com.smart.common.SmartConstants;

/**封装配置文件中Action节点数据**/
public class ActionDefine {
	
	private String name; // Action的名字
	private String clazz;	//Action的类名
	private String method = SmartConstants.METHOD_DEFAULT_ACTION;	//Action的调用方法,默认为execute
	private List<ForwarderDefine> fdList;	
	
	public ActionDefine(){}

	public String getName() {
		return name;
	}

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

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public List<ForwarderDefine> getFdList() {
		return fdList;
	}

	public void setFdList(List<ForwarderDefine> fdList) {
		this.fdList = fdList;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}
}

Converter.java

package com.smart.config;

public class ConverterDefine {
	
	private String target;
	private String clazz;
	
	public ConverterDefine(){}
	public ConverterDefine( String target ,String clazz ){
		this.target = target;
		this.clazz = clazz;
	}
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public String getClazz() {
		return clazz;
	}
	public void setClazz(String clazz) {
		this.clazz = clazz;
	}
}
Forwarder.java

package com.smart.config;

/**
 * 封装Result节点数据
 * @author qiaoyupeng
 *
 */
public class ForwarderDefine {
	
	private String name;
	private String path;	//jsp页面路径,以“/”开头
	private boolean response = false;
	
	public ForwarderDefine(){}

	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 isResponse() {
		return response;
	}

	public void setResponse(boolean response) {
		this.response = response;
	}
}

RequestProcessor.java

package com.smart.config;

public class RequestProcessorDefine {
	
	private String clazz;
	
	public RequestProcessorDefine( String clazz ){
		this.clazz = clazz;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值