Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
DirectoryTreeFilterChildren.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.directorytree;
20 
21 import java.util.List;
22 import java.util.logging.Level;
24 import org.openide.nodes.Children;
26 import org.openide.nodes.FilterNode;
27 import org.openide.nodes.Node;
44 
50 class DirectoryTreeFilterChildren extends FilterNode.Children {
51 
52  private final ShowItemVisitor showItemV = new ShowItemVisitor();
53  private final IsLeafItemVisitor isLeafItemV = new IsLeafItemVisitor();
54  private final static Logger logger = Logger.getLogger(DirectoryTreeFilterChildren.class.getName());
55 
59  public DirectoryTreeFilterChildren(Node arg) {
60  super(arg);
61  }
62 
63  @Override
64  protected Node copyNode(Node arg0) {
65  return new DirectoryTreeFilterNode(arg0, true);
66  }
67 
68  protected Node copyNode(Node arg0, boolean createChildren) {
69  return new DirectoryTreeFilterNode(arg0, createChildren);
70  }
71 
72 
73 
74  /* This method takes in a node as an argument and will create
75  * a new one if it should be displayed in the tree. If it is
76  * to be displayed, it also figures out if it is a leaf or not
77  * (i.e. should it have a + sign in the tree).
78  *
79  * It does NOT create children nodes
80  */
81  @Override
82  protected Node[] createNodes(Node origNode) {
83  if (origNode == null || !(origNode instanceof DisplayableItemNode)) {
84  return new Node[]{};
85  }
86 
87  // Shoudl this node be displayed in the tree or not
88  final DisplayableItemNode diNode = (DisplayableItemNode) origNode;
89  if (diNode.accept(showItemV) == false) {
90  //do not show
91  return new Node[]{};
92  }
93 
94  // If it is going to be displayed, then determine if it should
95  // have a '+' next to it based on if it has children of itself.
96  // We will filter out the "." and ".." directories
97  final boolean isLeaf = diNode.accept(isLeafItemV);
98 
99  return new Node[]{this.copyNode(origNode, !isLeaf)};
100  }
101 
109  private static boolean isLeafDirectory(DirectoryNode node) {
110  Directory dir = node.getLookup().lookup(Directory.class);
111  boolean ret = true;
112  try {
113  for (Content c : dir.getChildren()) {
114  if (c instanceof Directory && (!((Directory) c).getName().equals(".")
115  && !((Directory) c).getName().equals(".."))) {
116  ret = false;
117  break;
118  } else if (c.hasChildren()) {
119  //fie has children, such as derived files
120  ret = false;
121  break;
122  }
123  }
124  } catch (TskException ex) {
125  Logger.getLogger(DirectoryTreeFilterChildren.class.getName())
126  .log(Level.WARNING, "Error getting directory children", ex); //NON-NLS
127  return false;
128  }
129  return ret;
130  }
131 
132  private static boolean isLeafVolume(VolumeNode node) {
133  Volume vol = node.getLookup().lookup(Volume.class);
134  boolean ret = true;
135 
136  try {
137  for (Content c : vol.getChildren()) {
138  if (!(c instanceof LayoutFile
139  || c instanceof VirtualDirectory
140  )
141 
142  ) {
143  ret = false;
144  break;
145  }
146  }
147 
148  } catch (TskException ex) {
149  Logger.getLogger(DirectoryTreeFilterChildren.class.getName())
150  .log(Level.WARNING, "Error getting volume children", ex); //NON-NLS
151  return false;
152  }
153  return ret;
154  }
155 
159  private static boolean isDotDirectory(DirectoryNode dir) {
160  String name = dir.getDisplayName();
161  return name.equals(DirectoryNode.DOTDIR) || name.equals(DirectoryNode.DOTDOTDIR);
162  }
163 
172  public static Children createInstance(Node arg, boolean createChildren) {
173  if (createChildren) {
174  return new DirectoryTreeFilterChildren(arg);
175  } else {
176  return Children.LEAF;
177  }
178  }
179 
180  private static class IsLeafItemVisitor extends DisplayableItemNodeVisitor.Default<Boolean> {
181 
182  @Override
183  protected Boolean defaultVisit(DisplayableItemNode c) {
184  return c.isLeafTypeNode();
185  }
186 
187  @Override
188  public Boolean visit(DirectoryNode dn) {
189  return isLeafDirectory(dn);
190  }
191 
193  //is a leaf if has no children, or children are files not dirs
194  boolean hasChildren = node.hasContentChildren();
195  if (!hasChildren) {
196  return true;
197  }
198  List<Content> derivedChildren = node.getContentChildren();
199  //child of a file, must be a (derived) file too
200  for (Content childContent : derivedChildren) {
201  if (((AbstractFile) childContent).isDir()) {
202  return false;
203  }
204  else {
205  try {
206  if (childContent.hasChildren()) {
207  return false;
208  }
209  } catch (TskCoreException e) {
210  logger.log(Level.SEVERE, "Error checking if file node is leaf.", e); //NON-NLS
211  }
212  }
213  }
214  return true;
215  }
216 
217  @Override
218  public Boolean visit(FileNode fn) {
219  return visitDeep(fn);
220  }
221 
222 
223  @Override
224  public Boolean visit(LocalFileNode lfn) {
225  return visitDeep(lfn);
226  }
227 
228  @Override
229  public Boolean visit(LayoutFileNode fn) {
230  return visitDeep(fn);
231  }
232 
233 
234  @Override
235  public Boolean visit(VolumeNode vn) {
236  return isLeafVolume(vn);
237  }
238 
239  @Override
240  public Boolean visit(VirtualDirectoryNode vdn) {
241  return visitDeep(vdn);
242  //return ! vdn.hasContentChildren();
243  }
244  }
245 
246  private static class ShowItemVisitor extends DisplayableItemNodeVisitor.Default<Boolean> {
247 
248  @Override
249  protected Boolean defaultVisit(DisplayableItemNode c) {
250  return true;
251  }
252 
253  @Override
254  public Boolean visit(DirectoryNode dn) {
255  if (isDotDirectory(dn)) {
256  return false;
257  }
258  return true;
259  }
260 
261  @Override
262  public Boolean visit(FileNode fn) {
263  return fn.hasContentChildren();
264  }
265 
266  @Override
267  public Boolean visit(LocalFileNode lfn) {
268  return lfn.hasContentChildren();
269  }
270 
271  @Override
272  public Boolean visit(LayoutFileNode ln) {
273  return ln.hasContentChildren();
274  }
275 
276  @Override
277  public Boolean visit(VirtualDirectoryNode vdn) {
278  return true;
279  //return vdn.hasContentChildren();
280  }
281  }
282 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.