📄 recorderdealer.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package DownloadManager;import java.io.File;import java.io.FileOutputStream;import java.io.*;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;import javax.xml.*;import javax.xml.parsers.*;import javax.xml.transform.*;import javax.xml.transform.stream.*;import javax.xml.transform.dom.*;import javax.xml.xpath.*;import javax.xml.xpath.XPathConstants;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.xml.sax.*;import org.w3c.dom.*;/** * deal with the database--.xml file * @author Liuyuyang */public class RecorderDealer { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; File file; Document document; Element root; XPath xpath; Exception exception; FileOutputStream fileOutput; public RecorderDealer() { file = new File("d:/Project/files.xml"); try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } } public RecorderDealer(File file) { this.file = file; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } } private Node getNodeByInternetAddress(Document document, String internetAddress) { try { XPathFactory xpfactory = XPathFactory.newInstance(); xpath = xpfactory.newXPath(); Node childNode = (Node) xpath.evaluate("//File1[@internetAddress='" + internetAddress + "']", document, XPathConstants.NODE); return childNode; } catch (Exception e) { System.out.println(e); return null; } } private Node getNodeByName(Document document, String name) { try { XPathFactory xpfactory = XPathFactory.newInstance(); xpath = xpfactory.newXPath(); Node childNode = (Node) xpath.evaluate("//File1[@Name='" + name + "']", document, XPathConstants.NODE); return childNode; } catch (Exception e) { System.out.println(e); return null; } } /** this method is called when adding one node to parent node @param nodePath path of parent node */ public void addNode(String nodePath) { String parentPath; parentPath = "user1"; try { document = builder.parse(file); root = document.getDocumentElement(); String internetAddress = "http://168.131.152.215/CodeManager/upload/user1/" + nodePath; String localAddress = "D:/Project/upload/user1/" + nodePath; Element parentNode = (Element) getNodeByName(document, parentPath); if (this.getNodeByInternetAddress(document, nodePath) == null) { Element childNode = document.createElement("File2"); childNode.setAttribute("Type", "File"); childNode.setAttribute("Name", ""); childNode.setAttribute("internetAddress", internetAddress); childNode.setAttribute("localAddress", localAddress); parentNode.appendChild(childNode); this.saveChange(); } } catch (Exception e) { System.out.println(e); } } /** this method is called when remove a node @param nodePath value of node InternetAddress InternetAddress which is going to removed */ public void removeNode(String nodePath) { try { document = builder.parse(file); root = document.getDocumentElement(); Element childNode = (Element) getNodeByInternetAddress(document, nodePath); childNode.getParentNode().removeChild(childNode); this.saveChange(); } catch (Exception e) { System.out.println(e); } } /** This method is called when getting all URL records under the same user. In this method i should begin as 1, the first node is #test there should be some check mechanism, but time is not permitted. @param name user name @return pathList URL records */ public List getFilesPath(String name) { List pathList=new ArrayList(); NodeList nodeList; try { document = builder.parse(file); root = document.getDocumentElement(); Element parentNode = (Element) getNodeByName(document, name); nodeList=parentNode.getChildNodes(); for(int i=1;i<nodeList.getLength();i++) { Node node=nodeList.item(i); // System.out.println(node.getNodeName().toString()); NamedNodeMap map=node.getAttributes(); System.out.println( map.item(2).getNodeValue()); pathList.add(map.item(2).getNodeValue()); } return pathList; } catch (Exception e) { System.out.println(e); return null; } } private void saveChange() { try { Transformer t = TransformerFactory.newInstance().newTransformer(); fileOutput=new FileOutputStream(file); StreamResult streamResult=new StreamResult(fileOutput); DOMSource domSource=new DOMSource(document); t.transform(domSource,streamResult); } catch (Exception e) { System.out.println(e); } } /** close the file and dispose */ public void close() { try { fileOutput.close(); this.finalize(); } catch (Throwable e) { System.err.println(e); } } /** delete the file */ public void delete() { try { fileOutput.close(); this.file.delete(); } catch (IOException ex) { Logger.getLogger(RecorderDealer.class.getName()).log(Level.SEVERE, null, ex); } } /** public static void main(String args[]) { RecorderDealer recorderDealer = new RecorderDealer(); //recorderDealer.addNode("Trangle.class"); recorderDealer.getFilesPath("user1"); } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -