MVC框架实现3---Smart框架配置文件解析
2012-03-09 16:12
337 查看
Smart框架配置文件解析解析用的是dom4j工具,所以编写代码前需要将依赖的jar包导入ClassPath
ConfigResolver.java ,解析xml配置文件
配置文件模板:
ActionDefine.java
Converter.java
RequestProcessor.java
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; } }
相关文章推荐
- 框架中web.xml中配置文件解析
- 实训ssm框架学习配置文件初步解析(一)
- MVC框架实现9---Smart框架Context实现
- 在python中实现自定义配置文件解析及应用技巧[yaml and ConfigParser] 推荐
- 让Apache支持shtml实现include文件解析的配置方法
- Mybatis实现【3】-初始化(配置文件读取和解析)
- spring 框架中的依赖注入(IOC--设值注入)---使用xml简单配置文件---的具体实例的简单实现
- 让Apache支持shtml实现include文件解析的配置方法
- python实现的解析crontab配置文件代码
- 用C语言实现解析简单配置文件的小工具
- 用C语言实现解析简单配置文件的小工具
- XML形式的配置文件解析框架以及自动代码生成(一)
- MVC框架实现6---Smart框架ActionServlet控制器组件实现
- php INI配置文件的解析实现分析
- MVC框架实现10---Smart框架打包、下载、使用
- 如何开发一个java开源框架-----Jvn框架之实现用注解代替配置文件
- webx框架 实现文件上传保存服务器 解析
- mybatis实现物理分页准备之mybatis配置文件解析及实例
- 让Apache支持shtml实现include文件解析的配置方法