📄 virtualfilesystemnode.java
字号:
If this node does not have realPath, then it is not the graft root. */ Enumeration childrenColl = children(); for (int i = depth; i > 0; i--) System.out.print("\t"); System.out.println(super.toString()); while (childrenColl.hasMoreElements()){ ((VirtualFileSystemNode)childrenColl.nextElement()).printTree(depth + 1); } } public Vector graftPoints(String filePrefix){ Vector pathMappings = new Vector(); Vector childPathMappings, filePathMappings; VirtualFileSystemNode aNode; StringBuffer sb = new StringBuffer(); PathMapping pathMapping; if (filePrefix != null){ /* This means this node represents a file. Since it is stored * in 'files' variable and NOT as a regular child node of the tree, * it does not know where in the virtual file system tree it belongs * so, to indicate the graft mapping, it needs to know the path where * it is present. */ sb.append(filePrefix); sb.append("\"" + getUserObject() + "\""); pathMapping = new PathMapping(realPath(), sb.toString()); pathMappings.add(pathMapping); } else { /* Receiver is a directory. So, after creating path mapping for this node, see whether * it has any files in it and create path mappings for them too. */ Object path[] = getUserObjectPath(); for(int i = 1; i < path.length; i++){ sb.append("/"); sb.append((String)path[i]); }; sb.append("/"); if (realPath != null){ /* realPath is null for top most root node */ pathMapping = new PathMapping(realPath(), sb.toString()); pathMappings.add(pathMapping); } if (files != null) { Iterator anIterator = files.iterator(); while (anIterator.hasNext()){ aNode = (VirtualFileSystemNode)anIterator.next(); if (aNode.realPath() != null) { filePathMappings = ((VirtualFileSystemNode)aNode).graftPoints(sb.toString()); if (filePathMappings != null) pathMappings.addAll(filePathMappings); } } } } Enumeration childrenColl = children(); while (childrenColl.hasMoreElements()){ childPathMappings = ((VirtualFileSystemNode)childrenColl.nextElement()).graftPoints(null); if (childPathMappings != null) pathMappings.addAll(childPathMappings); } return pathMappings; } public Vector excludedPaths(String prefix){ Vector exclPaths = new Vector(); File f; Enumeration childrenColl = children(); String newPrefix; if (this.isVirtualFileSystemRoot()) { /* anything deleted under root dir is actually not important */ } else { if (prefix == null) f = new File(this.realPath()); else f = new File(prefix + "/" + (String)getUserObject()); if(f.isDirectory()){ if (deletedFiles != null) { Iterator i = deletedFiles.iterator(); while (i.hasNext()) { exclPaths.add(f.getAbsolutePath() + "\\" + ((VirtualFileSystemNode)i.next()).getUserObject()); } } } } if (this.realPath() == null) { if (prefix == null) newPrefix = null; else newPrefix = prefix + "/" + getUserObject(); } else { newPrefix = this.realPath(); } while (childrenColl.hasMoreElements()){ exclPaths.addAll(((VirtualFileSystemNode)childrenColl.nextElement()).excludedPaths(newPrefix)); } // System.out.println(totalSize); return exclPaths; } public boolean isNameAlreadyTaken(String aName){ boolean nameAlreadyTaken = false; Enumeration childrenColl; childrenColl = children(); while(childrenColl.hasMoreElements() && (nameAlreadyTaken == false)) { if(aName.equals((String)((VirtualFileSystemNode)childrenColl.nextElement()).name())) nameAlreadyTaken = true; }; if(nameAlreadyTaken) return true; if (files != null) { Iterator i = files.iterator(); while (i.hasNext()) { if(aName.equals((String)((VirtualFileSystemNode)i.next()).getUserObject())) nameAlreadyTaken = true; } } return nameAlreadyTaken; } public void printGraftPoints(){ Vector pathMappings = graftPoints(null); for(int i = 0; i < pathMappings.size(); i++){ System.out.println(((PathMapping)pathMappings.get(i)).toString()); } } public String graftPath(String aName, String aPathStr, boolean graftRoot){ /* aPathStr represents either a file or directory from the filesystem. * Add a node/tree to represent it. * First insert a node to represent aPathStr and if aPathStr represents a * directory, then insert nodes to represent them recursively. If graftRoot * is true, then realPath must be set. */ VirtualFileSystemNode newNode; boolean nameAlreadyTaken = false; Enumeration childrenColl; if (aPathStr == null) return null; childrenColl = children(); while(childrenColl.hasMoreElements() && (nameAlreadyTaken == false)) { if(aName.equals((String)((VirtualFileSystemNode)childrenColl.nextElement()).name())) nameAlreadyTaken = true; }; if(nameAlreadyTaken) return "Name '" + aName + "' is already taken."; if (files != null) { Iterator i = files.iterator(); while (i.hasNext()) { if(aName.equals((String)((VirtualFileSystemNode)i.next()).getUserObject())) nameAlreadyTaken = true; } } if(nameAlreadyTaken) return "Name '" + aName + "' is already taken."; File aFileOrDir = new File(aPathStr); if (aFileOrDir != null){ if (graftRoot) newNode = new VirtualFileSystemNode(aName, aPathStr); else newNode = new VirtualFileSystemNode(aName); if (aFileOrDir.isDirectory() && aFileOrDir.canRead()){ add(newNode); File [] dirContents; dirContents = aFileOrDir.listFiles(); for (int i = 0; i < dirContents.length; i++){ if (dirContents[i].isDirectory()) newNode.graftPath(dirContents[i].getName(), dirContents[i].getAbsolutePath(), false); else newNode.addFileNode(new VirtualFileSystemNode(dirContents[i].getName())); } } else{ addFileNode(newNode); } } return null; } public int compare(Object obj1, Object obj2) { return compare((VirtualFileSystemNode)obj1, (VirtualFileSystemNode)obj2); } public int compare(VirtualFileSystemNode node1, VirtualFileSystemNode node2) { System.out.println(node1.toString() + " == " + node2.toString() + " = " + node1.getUserObject().toString().compareTo(node1.getUserObject().toString())); return(node1.getUserObject().toString().compareTo(node1.getUserObject().toString())); } public int childCount(){ if (files == null) return (super.getChildCount()); else return (super.getChildCount() + files.size()); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -