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建立仓库的逻辑层的操作的更多相关文章

  1. RapidIO 逻辑层IO操作与Message操作的原理和区别

    接上一篇 SRIO RapidIO (SRIO)协议介绍(一) 1     说明 查看协议手册时会发现,逻辑层的操作分成了IO和Message 2类动作,那么为什么要分成2类操作?从原理和应用角度来看 ...

  2. 小程序视图层(xx.xml)和逻辑层(xx.js)

    整个系统分为两块视图层(View)和逻辑层(App Service) 框架可以让数据与视图非常简单地保持同步.当做数据修改的时候,只需要在逻辑层修改数据,视图层就会做相应的更新. 通过这个简单的例子来 ...

  3. MyBatis知多少(6)表现层与业务逻辑层

    表现层 表现层负责向最终用户展示应用程序的控制方式以及数据.它还要负责所有信息的布局和格式.今天,商业应用程序最流行的表现方式应该算是Web前端了,它使用HTML和JavaScript并通 过Web浏 ...

  4. JSP业务逻辑层

    经典的三层架构:表示层.业务逻辑层和数据访问层 具体的区分方法 1:数据访问层:主要看你的数据层里面有没有包含逻辑处理,实际上他的各个函数主要完成各个对数据文件的操作.而不必管其他操作. 2:业务逻辑 ...

  5. 一个项目中说系统分为表现层、控制层、逻辑层、DAO层和最终数据库五层架构-转

    表现层就是看到的东西,比如你现在看到的当前页面控制层就将你的请求从页面传到后台代码逻辑层就是处理你的请求的代码DAO层就是将数据存到数据库中的代码数据库就是数据库了,存东西用的 ,DAO层就是将访问数 ...

  6. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(4)-业务逻辑层的封装

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(4)-业务逻辑层的封装 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    (2) ...

  7. 前端Vue 源码分析-逻辑层

    Vue 源码分析-逻辑层 预期的效果: 监听input的输入,input在输入的时候,会触发 watch与computed函数,并且会更新原始的input的数值.所以直接跟input相关的处理就有3处 ...

  8. Farseer.net轻量级开源框架 入门篇:逻辑层的选择

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 入门篇:增.删.改.查操作演示 下一篇:Farseer.net轻量级开源框架 入门 ...

  9. Farseer.net轻量级开源框架 入门篇:分类逻辑层

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 缓存逻辑层 下一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 ...

随机推荐

  1. Spring Uploading Files

    1,在servlet-dispatcher.xml中添加代码 <bean id="multipartResolver" class="org.springframe ...

  2. Hadoop应用开发实战(flume应用开发、搜索引擎算法、Pipes、集群、PageRank算法)

    Hadoop是2013年最热门的技术之一,通过北风网robby老师<深入浅出Hadoop实战开发>.<Hadoop应用开发实战>两套课程的学习,普通Java开发人员可以在最快的 ...

  3. hdu 5014 思维题/推理

    http://acm.hdu.edu.cn/showproblem.php?pid=5014 从小数開始模拟找方法规律,然后推广,尤其敢猜敢尝试,错了一种思路继续猜-----这是一种非常重要的方法啊 ...

  4. kernel hexdump分析

    驱动调试中,很多时候是二进制的,这个时候hexdump就是个非常有用的工具了. 不要再自己去实现类似的功能,kernel代码里面就有: 参考: kernel/lib/hexdump.c // 0Xxx ...

  5. PrimusUI

    小身材大用途,用PrimusUI驾驭你的页面 “PrimusUI”是自己在借鉴了如今网上很多开源的UI库,再经过自己整理加工的一个简单代码集合. 每个功能块的CSS代码都很少,力求简单易懂,低门槛,代 ...

  6. libcurl编程,整整17篇

    http://www.cnblogs.com/lidabo/category/639633.html

  7. RAID的解释(附购买链接)

     现在使用RAID6  早期 raid5+1hotspare   小型机有存储备份 就是这么多年没有恢复过~ 金融行业 数据万金  是的 磁盘阵列柜换控制器  型号太老 缓存数据问题 把存储都下线了 ...

  8. 设置程序版本等信息(可直接修改pro文件设置,但是更推荐使用rc文件设置)

    Qt版本:5.2.0 在.pro文件中设置版本等信息 VERSION = 1.2.3 QMAKE_TARGET_PRODUCT = 产品名称QMAKE_TARGET_COMPANY = 公司QMAKE ...

  9. 用ATL开发和部署ActiveX网页控件

    用ATL开发和部署ActiveX网页控件 摘 要 ActiveX插件技术广泛的运用于B/S系统中,本文通过一个项目实例,详细介绍用ATL开发和部署ActiveX网页控件的过程.学习使用ActiveX让 ...

  10. 使MYSQL能被外部访问_xeyuu_新浪博客

    使MYSQL能被外部访问_xeyuu_新浪博客 使MYSQL能被外部访问 (