用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轻量级开源框架 入门篇: 添加数据详解 ...
随机推荐
- HTML5 input placeholder 颜色 改动
David Murdoch:Chrome支持input=[type=text]占位文本属性,但下列CSS样式却不起作用: CSS input[placeholder], [placeholder], ...
- 使用BigDecimal来进行精确计算
在一些以金融等行业中的计算是需要十分精确的,即使我们使用像double这样的类型,由于浮点数的原因,会使得数据计算变得不精确,例如下面的例子: double a = 0.1; double b = 0 ...
- 慎得慌二u赫然共和任务i个屁
http://www.huihui.cn/share/8424421 http://www.huihui.cn/share/8424375 http://www.huihui.cn/share/842 ...
- 『openframeworks』shader制作三角形马赛克效果
不久前做了六边形马赛克的效果,很有意思,乘热打铁,弄了个三角形马赛克. 首先肯定是等边三角形,这样才能真正的无缝拼接.观察发现,三角形可以拼接成之前做个的六边形. 如下图: 我们可以发现6个三角形正好 ...
- 【Demo 0004】Java基础-类封装性
本章学习要点: 1. Java封装特性; 2. 掌握类的定义: 3. 掌握类的调用方法; 一.封装特性 Java 纯面向对象语言,面向对象语言遵 ...
- Java调用cmd命令 打开一个站点
使用Java程序打开一个站点 近期做了个东西使用SWT技术在一个client程序 须要升级时在提示升级 点击窗口上的一个连接 打开下载网页 花费了我非常长时间 用到了把它记录下来 怕是忘记,须要时能 ...
- Mysql ODBC 5.1 Driver免安装脚本
在使用Mysql 的时候,需要使用ODBC数据源的方式来连接mysql,所以常常需要用到免安装的驱动,自己参考官网的脚本, 修改了一个实用点的脚本,放出来大家一起分享: 安装mysql odbc 5. ...
- span标签可以使用hide()方法隐藏吗?
/获取li下的span var $span = $('ul.selector li span'); //span对象隐藏 $span.hide(); //或者 $span.css('display', ...
- HDU 1535 Invitation Cards(SPFA,及其优化)
题意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费. 有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1. ...
- 深度学习系列之CNN核心内容
导读 怎么样来理解近期异常火热的深度学习网络?深度学习有什么亮点呢?答案事实上非常简答.今年十月份有幸參加了深圳高交会的中科院院士论坛.IEEE fellow汤晓欧做了一场精彩的报告,这个问题被汤大神 ...