1 /* DirectoryNode.java -- Structure for maintaining the cache directory tree.
2    Copyright (C) 2010 Red Hat, Inc.
3 
4 This file is part of IcedTea.
5 
6 IcedTea is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 2.
9 
10 IcedTea is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING.  If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19 
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library.  Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24 
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module.  An independent module is a module which is not derived from
32 or based on this library.  If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so.  If you do not wish to do so, delete this
35 exception statement from your version.
36 */
37 package net.sourceforge.jnlp.cache;
38 
39 import java.io.File;
40 import java.util.ArrayList;
41 
42 public class DirectoryNode {
43     private String name;
44     private File path;
45     private ArrayList<DirectoryNode> childNodes;
46     private DirectoryNode parent = null;
47     private File infoFile;
48 
49     /**
50      * Create a new instance of DirectoryNode.
51      *
52      * @param name Name representing this node.
53      * @param absPathToNode Absolute path to this node given as a String.
54      * @param parent The parent node.
55      */
DirectoryNode(String name, String absPathToNode, DirectoryNode parent)56     public DirectoryNode(String name, String absPathToNode, DirectoryNode parent) {
57         this(name, new File(absPathToNode), parent);
58     }
59 
60     /**
61      * Create a new instance of DirectoryNode.
62      *
63      * @param name Name representing this node.
64      * @param absPathToNode Absolute path to this node as a File.
65      * @param parent The parent node.
66      */
DirectoryNode(String name, File absPathToNode, DirectoryNode parent)67     public DirectoryNode(String name, File absPathToNode, DirectoryNode parent) {
68         this(name, absPathToNode, null, parent);
69     }
70 
71     /**
72      * Create a new instance of DirectoryNode.
73      *
74      * @param name Name representing this node.
75      * @param absPathToNode Absolute path to this node given as a File.
76      * @param childNodes List of children nodes.
77      * @param parent The parent node.
78      */
DirectoryNode(String name, File absPathToNode, ArrayList<DirectoryNode> childNodes, DirectoryNode parent)79     public DirectoryNode(String name, File absPathToNode, ArrayList<DirectoryNode> childNodes, DirectoryNode parent) {
80         this.name = name;
81         this.path = absPathToNode;
82         this.childNodes = childNodes;
83         if (this.childNodes == null)
84             this.childNodes = new ArrayList<DirectoryNode>();
85         this.parent = parent;
86         if (!isDir())
87             this.infoFile = new File(this.getFile().getAbsolutePath().concat(".info"));
88     }
89 
90     /**
91      * Append the given node to the list of child nodes.
92      *
93      * @param node Node to be appended.
94      */
addChild(DirectoryNode node)95     public void addChild(DirectoryNode node) {
96         try {
97             childNodes.add(node);
98         } catch (NullPointerException e) {
99             this.childNodes = new ArrayList<DirectoryNode>();
100             this.childNodes.add(node);
101         }
102     }
103 
104     /**
105      * Removes the node specified.
106      *
107      * @param node Node to be removed from the list of children
108      * @return true if this list of children contained the specified element
109      */
removeChild(DirectoryNode node)110     public boolean removeChild(DirectoryNode node) {
111         return this.childNodes.remove(node);
112     }
113 
114     /**
115      * Retrieve the name of this node.
116      *
117      * @return Name of this node.
118      */
getName()119     public String getName() {
120         return this.name;
121     }
122 
toString()123     public String toString() {
124         return this.name;
125     }
126 
127     /**
128      * Retrieve the file associated with this node.
129      *
130      * @return File that is associated with this node.
131      */
getFile()132     public File getFile() {
133         return path;
134     }
135 
136     /**
137      * Retrieve the parent node.
138      *
139      * @return DirectoryNode representing the parent of the current node.
140      */
getParent()141     public DirectoryNode getParent() {
142         return parent;
143     }
144 
145     /**
146      * Retrieves the list of child nodes.
147      *
148      * @return ArrayList of type DirectoryNode containing all the child nodes.
149      */
getChildren()150     public ArrayList<DirectoryNode> getChildren() {
151         return this.childNodes;
152     }
153 
154     /**
155      * Check if this node is a directory.
156      *
157      * @return True if node is directory.
158      */
isDir()159     public boolean isDir() {
160         return path.isDirectory();
161     }
162 
getInfoFile()163     public File getInfoFile() {
164         return this.infoFile;
165     }
166 
167 }
168