自己的一点点思路,欢迎大家拍砖
1. 背景
需要为第三方提供数据接口
此数据接口的特点为:接口特别多,需要为每个功能都提供数据接口
2. 设计目标
1)以XML作为数据的载体
2)开发人员在源代码中可以清晰的获得数据接口的结构
3)数据接口具有较强的扩展性
3. 设计思路
1)开发前先定义好接口模板,程序启动时将接口模板加载到内存中
2)内存中构造出与接口模板想匹配的数据模型,见数据模型与接口模板进行数据绑定
4.
实现
Entity.java 实体类,与接口模板进行数据绑定
TemplateConfig.java 模板配置类,用于将模板文件加载到内存中
TemplateXML.java
XML模板处理类,生成数据接口
error.xml 数据接口模板 绑定规则 type属性默认为string;type=list 代表集合;type=entity代表实体;dataName 对应实体中的key
public final class Entity {
private Map<String, Object> genericValue = new HashMap<String, Object>();
public Entity put(String key, Object value){
genericValue.put(key, value);
return this;
}
public Object get(String key){
return genericValue.get(key);
}
public String getString(String key){
try {
return (String)genericValue.get(key);
} catch (ClassCastException e) {
return "Null";
}
}
public Entity getEnttiy(String key){
try {
return (Entity)genericValue.get(key);
} catch (ClassCastException e) {
return new Entity();
}
}
}
public class TemplateConfig {
public static Map<String, Document> templates = null;
private static File fileDirector = null;
private static InputStream is = null;
static {
templates = new HashMap<String, Document>();
fileDirector = new File(TemplateConfig.class.getClass().getResource("/config/template").getPath());
}
public static void init() throws TemplateException{
load(fileDirector);
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void load(File fileDirector) throws TemplateException{
if (fileDirector.isDirectory()){
for (File file : fileDirector.listFiles()){
if (file.isDirectory()){
load(file);
}else{
templates.put(file.getName(), getDocument(file));
}
}
}
}
public static Document getDocument(File file) throws TemplateException{
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
throw new TemplateException(e);
}
try {
return builder.parse(is);
} catch (SAXException e) {
e.printStackTrace();
throw new TemplateException(e);
} catch (IOException e) {
e.printStackTrace();
throw new TemplateException(e);
}
}
}
public class TemplateXML implements Template{
private InputStream is = null;
private Entity entity = null;
private Document templateDoc = null;
public TemplateXML(InputStream templateStream, Entity entity){
this.entity = entity;
this.is = templateStream;
}
public TemplateXML(Document templateDoc, Entity entity){
this.templateDoc = templateDoc;
this.entity = entity;
}
public void process() throws Exception{
Element templateRoot = templateDoc.getDocumentElement();
NodeList templateNodeList = templateRoot.getChildNodes();
parserNodeList(templateNodeList, entity);
}
private Document getDocument(InputStream is) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(is);
}
private void parserNodeList(NodeList nodeList, Entity entity) throws Exception{
for (int i =0; i< nodeList.getLength(); i++){
Node node = nodeList.item(i);
if ("#text".equals(node.getNodeName())) continue;
String nodeType = "";
String dataName = "";
try {
nodeType = node.getAttributes().getNamedItem("type").getNodeValue();
} catch (NullPointerException e) {
nodeType = Template.TYPE_STRING;
}
try {
dataName = node.getAttributes().getNamedItem("dataName").getNodeValue();
node.getAttributes().removeNamedItem("dataName");
} catch (NullPointerException e) {
dataName = node.getNodeName();
}
if (Template.TYPE_LIST.equals(nodeType)){
Entity nodeEntity = entity.getEnttiy(dataName);
this.parserNodeList(node.getChildNodes(), nodeEntity);
}else if (Template.TYPE_ENTITY.equals(nodeType)){
Entity nodeEntity = entity.getEnttiy(dataName);
this.parserNodeList(node.getChildNodes(), nodeEntity);
}else {
String nodeValue = entity.getString(dataName);
node.setTextContent(nodeValue);
}
}
}
public static void main(String [] args) throws Exception{
Entity e = new Entity();
e.put("errorTip", "a");
e.put("errorCode", "b");
e.put("errorMsg", "c");
e.put("dse_operationName", "d");
e.put("pkgId", "e");
e.put("contact", new Entity().put("phone", "123").put("address", "beijing"));
Entity ads = new Entity();
ads.put("ad", new Entity().put("simg", "simg1").put("bimg", "bimg1"));
e.put("ads", ads);
TemplateConfig.init();
TemplateXML tx = new TemplateXML(TemplateConfig.templates.get("error.xml"), e);
tx.process();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
t.transform(new DOMSource(tx.templateDoc), new StreamResult(bos));
System.out.println(bos.toString().replaceAll("\r\n|\t", ""));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<res>
<errorTip dataName="errorTip"/>
<errorCode dataName="errorCode"/>
<errorMsg dataName="errorMsg"/>
<operationName dataName="operationName"/>
<ads dataName="ads" type="list">
<ad dataName="ad" type="entity">
<simg dataName="simg"/>
<bimg dataName="bimg"/>
</ad>
</ads>
<contact type="entity">
<phone dataName="phone"/>
<address dataName="address"/>
</contact>
</res>
结果:
<?xml version="1.0" encoding="UTF-8"?><res><errorTip>a</errorTip><errorCode>b</errorCode><errorMsg>c</errorMsg><dse_operationName>d</dse_operationName><pkgId>e</pkgId><ads type="list"><ad type="entity"><simg>simg1</simg><bimg>bimg1</bimg></ad></ads><contact type="entity"><phone>123</phone><address>beijing</address></contact></res>
分享到:
相关推荐
本文为用Vue实现方式,另有一篇为用knockout.js的实现方法。 html代码 <table v-for=item v-bind:list=item></table> 组件模板代码 <script type=text/x-template id=table-component-tem
(1) 用于处理接收到LoRa无线数据; (2) 需在函数中添加解析无线数据的功能代码或函数。 OLED_InitView()函数说明 3 (1) 用于设置OLED屏的初始显示内容; (2) 函数内部调用了hal_oled.c内的接口函数;只支持英
大数据能力支撑平台接口规范(学习模板) 一、概述 本规范旨在为大数据能力支撑平台(以下简称“平台”)的接口定义、数据格式、接口调用等方面提供统一的标准和规范。通过制定本规范,旨在提高平台接口的可用性、...
历史数据导入 3.1.1 XX系统数据 3.1.1.1 数据清单… 3 3.1.1.2 关联规则… 3 3.1.1.3 界面… 3 3.1.1.4 输入输出… 3 3.1.1.5 处理逻辑… 3 3.1.1.6 异常处理… 3 3.2 增量数据导入 3.3 数据校验 3.4 数据导出 3.5 ...
大数据分析平台的需求报告模板(1)全文共4页,当前为第1页。大数据分析平台的需求报告模板(1)全文共4页,当前为第1页。大数据分析平台的需求报告 大数据分析平台的需求报告模板(1)全文共4页,当前为第1页。 大数据...
基于SpringBoot的金刚模板化接口自动化测试项目源码+项目详细说明.zip 主要模块:用例维护、配置管理、公共方法、数据管理、报告模块、告警通知 模块 支持的功能 备注 用例维护 静态数据用例、动态数据用例 动态数据...
3.1要求的状态和方式 5 3.2需求概述 5 3.2.1系统总体功能和业务结构 5 3.2.2硬件系统的需求 5 3.2.3软件系统的需求 5 3.2.4接口需求 5 3.3系统能力需求 5 3.3.x(系统能力) 5 3.4系统外部接口需求 6 3.4.1接口标识和...
WinHex 是一款以通用的 16 进制编辑器为核心,专门用来对付计算机取证、数据恢复、低级数据处理、以及 IT 安全性、各种日常紧急情况的高级工具: 用来检查和修复各种文件、恢复删除文件、硬盘损坏、数码相机卡损坏...
1、资源内容:为 YOLO c 版本添加接口以批量处理 TUM、KITTI 数据集并保存检测结果+源代码+文档说明 2、代码特点:内含运行结果,不会运行可私信,参数化编程、参数可方便更改、代码编程思路清晰、注释明细,都经过...
概要设计模板参考 1.引言1.1编写目的 [说明编写这份概要设计说明书的目的,指出预期的读者。]1.2背景 a.[待开发软件系统的名称;] b.[列出本项目的任务提出者、开发者、用户。]1.3定义 [列出本文件中用到的专门...
软件介绍: 网奇.NET网络商城系统是基于.Net平台开发的免费商城系统。功能强大,操作方便,设置简便。无需任何设置,上传到支持asp.net的主机...7、修改了处理缓存的方法,使后台更新资料后,前台可以及时更新数据
但是考虑到这种工作方式需要相关系统都在线的情况下才能进行计算处理,对开发调试的环境要求较高,并且在上线运行后如果出现故障,还需要相关系统调整到位的情况下才能重新运行,因此在源到目标的数据移动过程中不...
并通过简单方便的接口供上层程序员使用,具体的数据分层处理对上层程序员是黑盒的 8. 设计并开发整套缓存层使用的 KEY 规则,方便缓存更新 结合公司的数据订阅系统进行 Redis缓存层 + MongoDB 持久层数据更新功能 ...
- [X] 找数据、处理数据 -21\12\23 - [X] 构建知识图谱 -21\12\24 - [X] 定义关系问题,构建模板 -21\12\24 - [X] 编写问答脚本 -21\12\24 - [X] 重构,添加一点问题,完善问答的鲁棒性 -21\12\25 # 一,项目介绍 ...
则在定制报表模板的时候就教育报表引擎如何从XML文档获得数据,则对于所有的或大部分的XML文档无需编程,可减少报表开发量。 那么如何通用的处理具有复杂树状结构的XML文档呢? 大家知道,处理XML文档有两种模式,...
概要设计说明书 1引言 3 1.1编写目的 3 1.2背景 3 1.3定义 3 1.4参考资料 4 2总体设计 4 2.1功能描述 4 2.2运行环境 4 2.3基本设计概念和处理流程 ...5数据结构设计 19 6出错处理设计 22 6.1出错信息 22 6.2补救措施 23
本文总结介绍接口测试框架开发,环境使用python3+selenium3+unittest测试框架及ddt数据驱动,采用Excel管理测试用例等集成测试数据功能,以及使用HTMLTestRunner来生成测试报告,目前有开源的poman、Jmeter等接口...
毕设: 智能射频模块, 数据处理与SDR融合项目 任务目标 ● 从图像传感器中获取一幅较大分辨率的图片 ● 目标检测程序,识别图像中的目标区域,并标记 ● 对标记后的图像,通过射频通道,使用BPSK调制发送 ● 接收机...
该方法利用ATL(活动模板库)开发组件技术,在ADO(Active数据对象)接口的基础上设计新的数据处理接口,从而为数据库系统的开发者提供通用的数据处理方法;同时为一些特殊行业提供专业的接口,并将接口以dll文件...