`

用xml建立仓库的逻辑层的操作

    博客分类:
  • 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(0));
        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[0]);
        newChild.setAttribute("Tag1", Tags[1]);
        newChild.setAttribute("Tag2", Tags[2]);
        
        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(0);
        FileNode fatherNode=new FileNode("root", "Repository", 0, "Administrator", 0, 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=0;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[0], property[1], 0, property[2], Long.valueOf(property[3]),date.parse(property[4]) , 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[0], property[1], 0, property[2], Long.valueOf(property[3]),date.parse(property[4]) , 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[8];;
        NamedNodeMap nodeMap=node.getAttributes();
        //System.out.println(nodeMap.getNamedItem("NodeName").getNodeValue());
        modelProperty[0]=nodeMap.getNamedItem("NodeName").getNodeValue();
        modelProperty[1]=nodeMap.getNamedItem("NodePath").getNodeValue();
        modelProperty[2]=nodeMap.getNamedItem("NodeAuther").getNodeValue();
        modelProperty[3]=nodeMap.getNamedItem("NodeSize").getNodeValue();
        modelProperty[4]=nodeMap.getNamedItem("CreateData").getNodeValue();
    if(nodeMap.getNamedItem("type").getNodeValue().equals(modelType))
    {
        modelProperty[5]=nodeMap.getNamedItem("Tag0").getNodeValue();
        modelProperty[6]=nodeMap.getNamedItem("Tag1").getNodeValue();
        modelProperty[7]=nodeMap.getNamedItem("Tag2").getNodeValue();
    }
        return modelProperty;
        
    }
    public List searchModelByKeyWords(String rootNodeName,String keywords){
        Element targetNode=(Element) document.getElementsByTagName(rootNodeName).item(0);
        wishList.clear();
        getTheList(targetNode,0,keywords);
        
        return wishList;
    }
    
    public List getUsersOwnedFiles(String rootNodeName,String author){
        Element targetNode=(Element) document.getElementsByTagName(rootNodeName).item(0);
        wishList.clear();
        getTheList(targetNode,-1,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=0;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[0], property[1], 0, property[2], Long.valueOf(property[3]),date.parse(property[4]) , null, "Model");
                    switch (key) {
                    case -1://getUsersOwnedFiles
                        if(requirement.equals(subNode.getAuthor())){
                            wishList.add(subNode);
                        }
                        break;

                    default://searchModelByKeyWords
                        if(requirement.contains(property[5]) || requirement.contains(property[6]) || requirement.contains(property[7]) || property[5].contains(requirement) || property[6].contains(requirement) || property[7].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;
    }
    //-------------------------------------------------------------------------------------

}

 

分享到:
评论

相关推荐

    地区电力调度数据仓库建模与数据处理

    并对地区电力调度中心数据仓库的测点、日期与时间维度表进行了设计,说明了ETL数据仓库中的相关数据是操作环境对应数据,首先将逻辑数据模型转换为物理数据模型后,进行扩展性标识语言(XML)描述实现异构系统的数据共享...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    它使用服务层框架可以将JavaBeans从Jsp/Servlet中分离出来,而使用表现层框架则可以将Jsp中剩余的JavaBeans完全分离,这部分JavaBeans主要负责显示相关信息,一般是通过标签库(Taglib)实现,不同框架有不同自己的...

    蓝焰设计站图文管理系统

    数据层是通过ADO.NET操纵数据为事务逻辑层提供数据服务,如存储数据操作结果、返回数据检索结果等。 1.2 ADO.NET访问数据库的原理 与数据库相连,ADO.NET提供了如下3种方式:通过ODBC相连;通过OLEDB相连;直接与SQL...

    电子商务网站设计原理.pdf

    数据链路层:是建立在物理传输能力基础上的, 以帧为单位传输数据,它的主要任务就是进行数据 封装和数据连接的建立。在封装的数据信息中地址 段含有发送结点和接收结点的地址,控制段用来表 示数格 18.中间件:是一...

    maven的设置和介绍

    maven在导入jar包的时候,不直接将jar包导入到工程中,而是建立了一个专门存放jar包的参考,当需要使用jar包时,在pom.xml文件中添加所依赖的jar坐标,maven会根据pom.xml中添加的依赖坐标到仓库中找到jar。...

    超级有影响力霸气的Java面试题大全文档

    Stateless Session Bean 虽然也是逻辑组件,但是他却不负责记录使用者状态,也就是说当使用者呼叫 Stateless Session Bean 的时候,EJB Container 并不会找寻特定的 Stateless Session Bean 的实体来执行这个 method...

    java 面试题 总结

    Stateless Session Bean 虽然也是逻辑组件,但是他却不负责记录使用者状态,也就是说当使用者呼叫 Stateless Session Bean 的时候,EJB Container 并不会找寻特定的 Stateless Session Bean 的实体来执行这个 method...

    SQL Server 2008管理员必备指南(超高清PDF)Part1

    10.7.4 使用XML索引 10.7.5 使用筛选索引 10.7.6 确定应当索引哪些列 10.7.7 索引计算列和视图 10.7.8 查看索引属性 10.7.9 创建索引 10.7.10 管理索引 10.7.11 使用数据库引擎优化顾问 10.8 列约束和规则 10.8.1 ...

    SQL Server 2008管理员必备指南(超高清PDF)Part3

    10.7.4 使用XML索引 10.7.5 使用筛选索引 10.7.6 确定应当索引哪些列 10.7.7 索引计算列和视图 10.7.8 查看索引属性 10.7.9 创建索引 10.7.10 管理索引 10.7.11 使用数据库引擎优化顾问 10.8 列约束和规则 10.8.1 ...

    SQL Server 2008管理员必备指南(超高清PDF)Part2

    10.7.4 使用XML索引 10.7.5 使用筛选索引 10.7.6 确定应当索引哪些列 10.7.7 索引计算列和视图 10.7.8 查看索引属性 10.7.9 创建索引 10.7.10 管理索引 10.7.11 使用数据库引擎优化顾问 10.8 列约束和规则 10.8.1 ...

    asp学习相关资料大全

    在这种结构下,用户工作界面是通过www浏览器来实现,极少部分事务逻辑在前端(Browser)实现,但是主要事务逻辑在服务器端(Server)实现,形成所谓三层3-tier结构。这样就大大简化了客户端电脑载荷,减轻了系统维护...

    数据挖掘论文合集-242篇(part1)

    Web使用模式研究中的数据挖掘.caj Web数据挖掘技术及工具研究.kdh Web数据挖掘技术探讨.kdh Web数据挖掘的BN实现方案.kdh XML与面向Web的数据挖掘技术.caj 一个新的数据挖掘模型与算法.caj 一个面向电子商务的数据...

    数据挖掘论文合集-242篇(part2)

    Web使用模式研究中的数据挖掘.caj Web数据挖掘技术及工具研究.kdh Web数据挖掘技术探讨.kdh Web数据挖掘的BN实现方案.kdh XML与面向Web的数据挖掘技术.caj 一个新的数据挖掘模型与算法.caj 一个面向电子商务的数据...

    数据挖掘论文合集-242篇(part3)

    Web使用模式研究中的数据挖掘.caj Web数据挖掘技术及工具研究.kdh Web数据挖掘技术探讨.kdh Web数据挖掘的BN实现方案.kdh XML与面向Web的数据挖掘技术.caj 一个新的数据挖掘模型与算法.caj 一个面向电子商务的数据...

    数据挖掘在各行业的应用论文

    Web使用模式研究中的数据挖掘.caj 数据挖掘中决策树算法的探讨.caj 数据挖掘工具和应用中的问题.caj 基于Web的数据仓库与数据挖掘技术.caj 一种新的高效关联规则数据挖掘算法.caj 数据挖掘技术及在电子商务中的应用....

Global site tag (gtag.js) - Google Analytics