用xml建立仓库的逻辑层的操作
package com.repositoryclient.xml; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.StringBufferInputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import com.repositoryclient.treeview.FileNode; public class RepositoryConstructionOption {
//------------------------------------------------------------------------------------- private DocumentBuilderFactory factory;
private Element node;
private Element model;
private DocumentBuilder documentBuilder;
private Document document;
private String filePath="./xml/Test1.xml";
private String nodeType="Node";
private String modelType="Model";
private List wishList=new ArrayList<>();
private StringBufferInputStream inputStream;
private String xmlFileContent;
//------------------------------------------------------------------------------------- public RepositoryConstructionOption(String xmlFileContent){
this.xmlFileContent=xmlFileContent;
setUpDomFactory();
} private void setUpDomFactory(){
factory=DocumentBuilderFactory.newInstance();
try{
inputStream=new StringBufferInputStream(xmlFileContent);
factory.setIgnoringElementContentWhitespace(true);
documentBuilder=factory.newDocumentBuilder();
document=documentBuilder.parse(inputStream); }catch(Exception e){
e.printStackTrace();
}
}
//-----------------------------------------------------------------------------------
public void addNode(String targetNodePath,String newNodeName){
Element newChild=document.createElement(newNodeName);
newChild.setTextContent("\n");
newChild.setAttribute("type", "Node");
newChild.setAttribute("NodeName", newNodeName);
newChild.setAttribute("NodePath", targetNodePath);
newChild.setAttribute("NodeAuther", "Administrator");
newChild.setAttribute("NodeSize", String.valueOf());
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
newChild.setAttribute("CreateData", date.format(new Date()));
Element targetElement=(Element) selectSingleNode(targetNodePath, document);
targetElement.appendChild(newChild);
saveXml("./xml/Test1.xml", document);
}
public void deleteNode(String targetNodePath){
Element targetElement=(Element) selectSingleNode(targetNodePath, document);
targetElement.getParentNode().removeChild(targetElement); saveXml("./xml/Test1.xml", document);
}
public void renameNode(String targetNodePath,String newNodeName){
Node targetNode=selectSingleNode(targetNodePath, document);
document.renameNode(targetNode, null, newNodeName);
saveXml("./xml/Test1.xml", document);
}
//------------------------------------------------------------------------------------- public void addModel(String targetNodePath,FileNode newModel,String[] Tags){
Element newChild=document.createElement(newModel.getFileName()); //设置Model的属性信息
newChild.setAttribute("type", "Model");
newChild.setAttribute("NodeName", newModel.getFileName());
newChild.setAttribute("NodePath", newModel.getPath());
newChild.setAttribute("NodeAuther", newModel.getAuthor());
newChild.setAttribute("NodeSize", String.valueOf(newModel.getSize()));
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
newChild.setAttribute("CreateData", date.format(newModel.getDate())); //设置Model的Tag(标签),以后可能会添加通过Tag搜索的功能时会用到
newChild.setAttribute("Tag0", Tags[]);
newChild.setAttribute("Tag1", Tags[]);
newChild.setAttribute("Tag2", Tags[]); newChild.setNodeValue("Model");
Element targetElement=(Element) selectSingleNode(targetNodePath, document);
//添加Model到xml
targetElement.appendChild(newChild);
saveXml("./xml/Test1.xml", document);
}
public void deleteModel(String modelName){
Element targetElement=(Element) selectSingleNode(modelName, document);
targetElement.getParentNode().removeChild(targetElement); saveXml("./xml/Test1.xml", document);
}
//-------------------------------------------------------------------------------------
public List getTheTree(String rootNodeName){
List resultList=new ArrayList();
Element targetNode=(Element) document.getElementsByTagName(rootNodeName).item();
FileNode fatherNode=new FileNode("root", "Repository", , "Administrator", , new Date(), null, "Node");
searchTheTree(targetNode,fatherNode);
resultList.add(fatherNode); return resultList; }
public void searchTheTree(Node rootNode,FileNode fatherNode){
try {
List childList=new ArrayList();
Node node;
NodeList childNodesList=rootNode.getChildNodes();
for(int i=;i<childNodesList.getLength();i++){
node=childNodesList.item(i);
if(node.getNodeName()!="#text"){
//System.out.println("here: "+node.getAttributes().item(0));
NamedNodeMap nodeMap=node.getAttributes();
if(nodeMap.getNamedItem("type").getNodeValue().equals(nodeType)){
//String property[]=new String[8];
String property[]=handleModelProperty(node);
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
FileNode subNode; subNode = new FileNode(property[], property[], , property[], Long.valueOf(property[]),date.parse(property[]) , null, "Node"); searchTheTree(node, subNode);
childList.add(subNode);
}else{
//String property[]=new String[8];
String property[]=handleModelProperty(node);
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
FileNode subNode=new FileNode(property[], property[], , property[], Long.valueOf(property[]),date.parse(property[]) , null, "Model");
childList.add(subNode);
}
}
}
fatherNode.setChildren(childList);
}
catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[] handleModelProperty(Node node){
String modelProperty[] = new String[];;
NamedNodeMap nodeMap=node.getAttributes();
//System.out.println(nodeMap.getNamedItem("NodeName").getNodeValue());
modelProperty[]=nodeMap.getNamedItem("NodeName").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("NodePath").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("NodeAuther").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("NodeSize").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("CreateData").getNodeValue();
if(nodeMap.getNamedItem("type").getNodeValue().equals(modelType))
{
modelProperty[]=nodeMap.getNamedItem("Tag0").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("Tag1").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("Tag2").getNodeValue();
}
return modelProperty; }
public List searchModelByKeyWords(String rootNodeName,String keywords){
Element targetNode=(Element) document.getElementsByTagName(rootNodeName).item();
wishList.clear();
getTheList(targetNode,,keywords); return wishList;
} public List getUsersOwnedFiles(String rootNodeName,String author){
Element targetNode=(Element) document.getElementsByTagName(rootNodeName).item();
wishList.clear();
getTheList(targetNode,-,author); return wishList; }
/**
*
* @param rootNode
* @param key (-1:getUsersOwnedFiles 0:searchModelByKeyWords)
*/
public void getTheList(Node rootNode,int key,String requirement){
try{
Node node;
NodeList childNodesList=rootNode.getChildNodes();
for(int i=;i<childNodesList.getLength();i++){
node=childNodesList.item(i);
if(node.getNodeName()!="#text"){
NamedNodeMap nodeMap=node.getAttributes();
if(nodeMap.getNamedItem("type").getNodeValue().equals(nodeType)){
getTheList(node, key,requirement);
}else{
String property[]=handleModelProperty(node);
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
FileNode subNode=new FileNode(property[], property[], , property[], Long.valueOf(property[]),date.parse(property[]) , null, "Model");
switch (key) {
case -://getUsersOwnedFiles
if(requirement.equals(subNode.getAuthor())){
wishList.add(subNode);
}
break; default://searchModelByKeyWords
if(requirement.contains(property[]) || requirement.contains(property[]) || requirement.contains(property[]) || property[].contains(requirement) || property[].contains(requirement) || property[].contains(requirement)){
wishList.add(subNode);
}
break;
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
//-------------------------------------------------------------------------------------
public void saveXml(String fileName, Document doc) {//将Document输出到文件
TransformerFactory transFactory=TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("indent", "yes"); DOMSource source=new DOMSource();
source.setNode(doc);
StreamResult result=new StreamResult();
result.setOutputStream(new FileOutputStream(fileName)); transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public Node selectSingleNode(String express, Object source) {//查找节点,并返回第一个符合条件节点
Node result=null;
XPathFactory xpathFactory=XPathFactory.newInstance();
XPath xpath=xpathFactory.newXPath();
try {
result=(Node) xpath.evaluate(express, source, XPathConstants.NODE);
} catch (XPathExpressionException e) {
e.printStackTrace();
} return result;
}
//------------------------------------------------------------------------------------- }
用xml建立仓库的逻辑层的操作的更多相关文章
- RapidIO 逻辑层IO操作与Message操作的原理和区别
接上一篇 SRIO RapidIO (SRIO)协议介绍(一) 1 说明 查看协议手册时会发现,逻辑层的操作分成了IO和Message 2类动作,那么为什么要分成2类操作?从原理和应用角度来看 ...
- 小程序视图层(xx.xml)和逻辑层(xx.js)
整个系统分为两块视图层(View)和逻辑层(App Service) 框架可以让数据与视图非常简单地保持同步.当做数据修改的时候,只需要在逻辑层修改数据,视图层就会做相应的更新. 通过这个简单的例子来 ...
- MyBatis知多少(6)表现层与业务逻辑层
表现层 表现层负责向最终用户展示应用程序的控制方式以及数据.它还要负责所有信息的布局和格式.今天,商业应用程序最流行的表现方式应该算是Web前端了,它使用HTML和JavaScript并通 过Web浏 ...
- JSP业务逻辑层
经典的三层架构:表示层.业务逻辑层和数据访问层 具体的区分方法 1:数据访问层:主要看你的数据层里面有没有包含逻辑处理,实际上他的各个函数主要完成各个对数据文件的操作.而不必管其他操作. 2:业务逻辑 ...
- 一个项目中说系统分为表现层、控制层、逻辑层、DAO层和最终数据库五层架构-转
表现层就是看到的东西,比如你现在看到的当前页面控制层就将你的请求从页面传到后台代码逻辑层就是处理你的请求的代码DAO层就是将数据存到数据库中的代码数据库就是数据库了,存东西用的 ,DAO层就是将访问数 ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(4)-业务逻辑层的封装
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(4)-业务逻辑层的封装 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1):框架搭建 (2) ...
- 前端Vue 源码分析-逻辑层
Vue 源码分析-逻辑层 预期的效果: 监听input的输入,input在输入的时候,会触发 watch与computed函数,并且会更新原始的input的数值.所以直接跟input相关的处理就有3处 ...
- Farseer.net轻量级开源框架 入门篇:逻辑层的选择
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 入门篇:增.删.改.查操作演示 下一篇:Farseer.net轻量级开源框架 入门 ...
- Farseer.net轻量级开源框架 入门篇:分类逻辑层
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 缓存逻辑层 下一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 ...
随机推荐
- uvc摄像头代码解析7
13.uvc视频初始化 13.1 uvc数据流控制 struct uvc_streaming_control { __u16 bmHint; __u8 bFormatIndex; //视频格式索引 _ ...
- SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...
- QQ邮箱中转站文件即将过期时如何转存到微云
今天QQ邮箱提示我的中转站有个文件即将过期,然后我看看了那个文件然后我想永久保存这个文件,腾讯有个微云网盘(好像有10T),想知道能不能保存到微云已变永久保存 结果发现在文件中转站这个界面竟然没有续期 ...
- 关于Opengl中将24位BMP图片加入一个alpha通道并实现透明的问题
#include <windows.h>#include <GL/glut.h>#include <GL/glaux.h>#include <stdio.h& ...
- 0 and 1
Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think ab ...
- [置顶] Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
SQLite 是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...
- Skype无法显示登录界面
Skype升级之后突然抽风,双击运行程序之后,输入用户名和密码的窗口都没了,截图如下(本机为Windows 7 32bit版本): 卸载重新安装,也无济于事.删除注册表中的Skype的相关信息后问题依 ...
- vc 按钮自绘
按钮自绘,将按钮区域分成三部分,左边.右边.中间都由贴图绘制,可用于手动进度条按钮,或者左右选择项按钮 cpp代码部分: // LRSkinButton.cpp : implementation fi ...
- jquery的ajax提交form表单方式总结
方法一: function AddHandlingFeeToRefund() { var AjaxURL= "../OrderManagement/AjaxModifyOrderServic ...
- 8086 CPU 寻址方式
8086 CPU 寻址方式灵活.有以下几种 idata 表示常量 1. [ idata ] 用一个常量来表示地址,可用于直接定位内存单元,但是在 MASM中要显实在的说明 ds 段寄存器, 比如 ...