KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > util > core > VirtualFolder


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.util.core;
17
18 import com.blandware.atleap.common.util.Folder;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 /**
27  * <p>Contains information about page folder, which is virtual and does not exist in database.</p>
28  * <p><a HREF="VirtualFolder.java.htm"><i>View Source</i></a></p>
29  *
30  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
31  * @version $Revision: 1.4 $ $Date: 2005/08/03 15:39:04 $
32  */

33 public class VirtualFolder implements Folder {
34
35     protected transient final Log log = LogFactory.getLog(VirtualFolder.class);
36
37     /**
38      * Name of this folder. In form, for example, baz
39      */

40     protected String JavaDoc name;
41
42     /**
43      * Path to this folder from the root. In form, for example, /foo/bar,
44      * so full path to this folder will be /foo/bar/baz
45      */

46     protected String JavaDoc path;
47
48     /**
49      * Delimiter between folder names
50      */

51     protected char delimiter = '/';
52
53     /**
54      * Creates new instance of VirtualFolder with a given name
55      *
56      * @param name Name of this folder
57      */

58     public VirtualFolder(String JavaDoc name) {
59         this(name, '/');
60     }
61
62     /**
63      * Creates new instance of VirtualFolder with a given name and path
64      *
65      * @param name Name of this folder
66      * @param path Path to this folder
67      */

68     public VirtualFolder(String JavaDoc name, String JavaDoc path) {
69         this(name, path, '/');
70     }
71
72     /**
73      * Creates new instance of VirtualFolder with a given name and folder delimiter
74      *
75      * @param name Name of this folder
76      * @param delimiter Delimiter between folder names
77      */

78     public VirtualFolder(String JavaDoc name, char delimiter) {
79         this.name = name;
80         this.delimiter = delimiter;
81     }
82
83     /**
84      * Creates new instance of VirtualFolder with a given name, path and a folder
85      * delimiter
86      *
87      * @param name Name of this folder
88      * @param path Path to this folder
89      * @param delimiter Delimiter between folder names
90      */

91     public VirtualFolder(String JavaDoc name, String JavaDoc path, char delimiter) {
92         this.name = name;
93         this.path = path;
94         this.delimiter = delimiter;
95     }
96
97     /**
98      * Gets name of this virtual folder
99      *
100      * @return name
101      */

102     public String JavaDoc getName() {
103         return name;
104     }
105
106     /**
107      * Sets name of this virtual folder
108      *
109      * @param name name to set
110      */

111     public void setName(String JavaDoc name) {
112         this.name = name;
113     }
114
115     /**
116      * Gets path of this virtual folder
117      *
118      * @return path
119      */

120     public String JavaDoc getPath() {
121         return path;
122     }
123
124     /**
125      * Sets path of this virtual folder
126      *
127      * @param path path to set
128      */

129     public void setPath(String JavaDoc path) {
130         this.path = path;
131     }
132
133     /**
134      * Gets delimiter of this virtual folder
135      *
136      * @return delimiter
137      */

138     public char getDelimiter() {
139         return delimiter;
140     }
141
142     /**
143      * Sets delimiter of this virtual folder
144      *
145      * @param delimiter delimiter to set
146      */

147     public void setDelimiter(char delimiter) {
148         this.delimiter = delimiter;
149     }
150
151     /**
152      * Returns full path to the folder which is concatenation of path and name fields
153      *
154      * @return Full path to the folder
155      */

156     public String JavaDoc getFullPath() {
157         String JavaDoc fullPath = path;
158         if ( !name.trim().equals("..") ) {
159             fullPath += (path.length() > 1 ? String.valueOf(delimiter) : "") + name;
160         }
161         return fullPath;
162     }
163
164     /**
165      * Splits path by specified delimiter and returns list of virtual folders.
166      *
167      * @param path Path to split
168      * @param delimiter Delimiter to split by
169      * @return List of virtual folders
170      */

171     public static List JavaDoc splitPath(String JavaDoc path, char delimiter) {
172         return splitPath(path, delimiter, true);
173     }
174
175     /**
176      * Splits path by specified delimiter and returns list of virtual folders.
177      * For example, if "foo/bar/baz" is given as <code>path</code>, '/' as
178      * <code>delimiter</code> and <code>startFromDelim</code> is
179      * <code>false</code>, wi'll get <code>{"foo", "foo/bar", "foo/bar/baz"}</code>.
180      *
181      * @param path Path to split
182      * @param delimiter Delimiter to split by
183      * @param startFromDelim Whether or not to begin string with delimiter
184      * @return List of virtual folders
185      */

186     public static List JavaDoc splitPath(String JavaDoc path, char delimiter, boolean startFromDelim) {
187         String JavaDoc delimiterString = String.valueOf(delimiter);
188         List JavaDoc folders = new ArrayList JavaDoc();
189         if ( path != null && path.length() > 0 ) {
190             StringTokenizer JavaDoc pathElements = new StringTokenizer JavaDoc(path, delimiterString, false);
191             String JavaDoc folderPath = startFromDelim ? delimiterString : "";
192             while ( pathElements.hasMoreTokens() ) {
193                 String JavaDoc folderName = pathElements.nextToken();
194                 VirtualFolder virtualFolder = new VirtualFolder(folderName, folderPath, delimiter);
195                 folders.add(virtualFolder);
196                 folderPath += (folderPath.length() > 1 ? delimiterString : "") + folderName;
197             }
198         }
199         return folders;
200     }
201
202     /**
203      * Splits path by '/' and returns list of virtual folders.
204      *
205      * @param path Path to split
206      * @return List of virtual folders
207      */

208     public static List JavaDoc splitPath(String JavaDoc path) {
209         return splitPath(path, '/');
210     }
211
212
213     public boolean equals(Object JavaDoc o) {
214         if ( this == o ) {
215             return true;
216         }
217         if ( !(o instanceof Folder) ) {
218             return false;
219         }
220
221         final Folder folder = (Folder) o;
222
223         if ( name != null ? !name.equals(folder.getName()) : folder.getName() != null ) {
224             return false;
225         }
226         if ( path != null ? !path.equals(folder.getPath()) : folder.getPath() != null ) {
227             return false;
228         }
229
230         return true;
231     }
232
233     /**
234      * Compares this folder to another. If specified object does not implement
235      * <code>com.blandware.atleap.common.util.Folder</code>, ClassCastException will be thrown
236      *
237      * @param o Object to compare with
238      * @return Negative integer if this folder is less than specified one, zero value if this folder is equal to specified and
239      * positive integer if this folder is greater that specified one
240      * @see java.lang.Comparable#compareTo(Object)
241      * @see com.blandware.atleap.common.util.Folder
242      */

243     public int compareTo(Object JavaDoc o) {
244         if ( !(o instanceof Folder) ) {
245             throw new ClassCastException JavaDoc("Cannot compare instance of " + getClass().getName() + " to the instance of " + o.getClass().getName());
246         }
247         Folder f = (Folder) o;
248         return name.equalsIgnoreCase(f.getName()) ? path.compareToIgnoreCase(f.getPath()) : name.compareToIgnoreCase(f.getName());
249     }
250
251     public int hashCode() {
252         int result;
253         result = (name != null ? name.hashCode() : 0);
254         result = 29 * result + (path != null ? path.hashCode() : 0);
255         return result;
256     }
257
258     public String JavaDoc toString() {
259         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("VirtualFolder [");
260         sb.append("name=").append(this.name);
261         sb.append(", path=").append(this.path);
262         sb.append("]");
263         return sb.toString();
264     }
265 }
266
Popular Tags