zoukankan      html  css  js  c++  java
  • 用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("
    ");
            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;
        }
        //-------------------------------------------------------------------------------------
    
    }
  • 相关阅读:
    完全分布式安装HBase
    HDFS常用的文件API操作
    启动HBase后遇到的一个问题
    HBase常用的数据库API操作
    HBase数据库常用操作命令
    Hive安装
    eclipse中配置hadoop开发环境
    Hadoop小程序倒排索引
    我学习设计模式的一些所想所得
    架构之路实战项目记录(二) 忘记数据库 开始抽象
  • 原文地址:https://www.cnblogs.com/wangjiyuan/p/luojiceng.html
Copyright © 2011-2022 走看看