用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轻量级开源框架 入门篇: 添加数据详解 ...
随机推荐
- 高仿精仿快播应用android源码下载
今天给大家在网上找到的一款高仿精仿快播应用android源码,分享给大家,希望大家功能喜欢. 说明源码更新中.... 源码即将上传 也可以到这个网站下载:download
- Appium Server 传递iOS参数
Appium server iOS Capabilities 参数 iOS Only Capability Description Values calendarFormat (Sim-only) ...
- nginx的反向代理和负载均衡的区别是什么 - 开源中国社区
nginx的反向代理和负载均衡的区别是什么 - 开源中国社区 nginx的反向代理和负载均衡的区别是什么 #user nobody;worker_processes 1;#error_log ...
- viewDidLoad、viewDidUnload、viewWillAppear、viewDidAppear、viewWillDisappear 和 -viewDidDisappear的区别和使用
首先看一下官方解释: - (void)loadView; // This is where subclasses should create their custom view hierarchy i ...
- C嵌入汇编
概述:linux内核源码中,有很多C语言中嵌入了汇编语句,如何理解这些汇编语句,对理解内核有很重要的作用. 具有输入和输出参数的嵌入式汇编语句的基本格式为: asm("汇编语句" ...
- spring获取bean 实例
ApplicationContext ctx = new ClassPathXmlApplication("applicationContext.xml"); DataSource ...
- iOS UIScrollView 停止滑动 减速
1.UIScrollView 减速 可能通过decelerationRate的属性来设置,它的值域是(0.0,1.0),当decelerationRate设置为0.1时,当手指touch up时就会很 ...
- perl 异步请求和JS对比
perl 异步和js对比: /js************** $(function(){ function isPhone(str){ var regex = /[0-9]{11,11}/; ret ...
- 关于iptables的u32匹配
前面一篇文章----阐释了iptables最新的bpf match,说它将多个matches并成了一个经过编译的解释型bytecode bpf match,早在bpf match之前,u32 matc ...
- vs2013 IntelliSense: "const char *" 类型的实參与 "LPCWSTR" 类型的形參不兼容
错误例如以下: 解决方法: project字符集选择多字符