KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > ftp > FTPPath


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.ftp;
18
19 import org.alfresco.filesys.server.filesys.*;
20 import org.alfresco.filesys.server.core.*;
21
22 /**
23  * FTP Path Class
24  * <p>
25  * Converts FTP paths to share/share relative paths.
26  *
27  * @author GKSpencer
28  */

29 public class FTPPath
30 {
31
32     // FTP directory seperator
33

34     private static final String JavaDoc FTP_SEPERATOR = "/";
35     private static final char FTP_SEPERATOR_CHAR = '/';
36
37     // Share relative path directory seperator
38

39     private static final String JavaDoc DIR_SEPERATOR = "\\";
40     private static final char DIR_SEPERATOR_CHAR = '\\';
41
42     // FTP path
43

44     private String JavaDoc m_ftpPath;
45
46     // Share name nad share relative path
47

48     private String JavaDoc m_shareName;
49     private String JavaDoc m_sharePath;
50
51     // Shared device
52

53     private DiskSharedDevice m_shareDev;
54
55     // Flag to indicate if this is a directory or file path
56

57     private boolean m_dir = true;
58
59     /**
60      * Default constructor
61      */

62     public FTPPath()
63     {
64         try
65         {
66             setFTPPath(null);
67         }
68         catch (Exception JavaDoc ex)
69         {
70         }
71     }
72
73     /**
74      * Class constructor
75      *
76      * @param ftpPath String
77      * @exception InvalidPathException
78      */

79     public FTPPath(String JavaDoc ftpPath) throws InvalidPathException
80     {
81         setFTPPath(ftpPath);
82     }
83
84     /**
85      * Class constructor
86      *
87      * @param shrName String
88      * @param shrPath String
89      * @exception InvalidPathException
90      */

91     public FTPPath(String JavaDoc shrName, String JavaDoc shrPath) throws InvalidPathException
92     {
93         setSharePath(shrName, shrPath);
94     }
95
96     /**
97      * Copy constructor
98      *
99      * @param ftpPath FTPPath
100      */

101     public FTPPath(FTPPath ftpPath)
102     {
103         try
104         {
105             setFTPPath(ftpPath.getFTPPath());
106             m_shareDev = ftpPath.getSharedDevice();
107         }
108         catch (Exception JavaDoc ex)
109         {
110         }
111     }
112
113     /**
114      * Determine if the current FTP path is the root path
115      *
116      * @return boolean
117      */

118     public final boolean isRootPath()
119     {
120         return m_ftpPath.compareTo(FTP_SEPERATOR) == 0 ? true : false;
121     }
122
123     /**
124      * Determine if the path is for a directory or file
125      *
126      * @return boolean
127      */

128     public final boolean isDirectory()
129     {
130         return m_dir;
131     }
132
133     /**
134      * Check if the FTP path is valid
135      *
136      * @return boolean
137      */

138     public final boolean hasFTPPath()
139     {
140         return m_ftpPath != null ? true : false;
141     }
142
143     /**
144      * Return the FTP path
145      *
146      * @return String
147      */

148     public final String JavaDoc getFTPPath()
149     {
150         return m_ftpPath;
151     }
152
153     /**
154      * Check if the share name is valid
155      *
156      * @return boolean
157      */

158     public final boolean hasShareName()
159     {
160         return m_shareName != null ? true : false;
161     }
162
163     /**
164      * Return the share name
165      *
166      * @return String
167      */

168     public final String JavaDoc getShareName()
169     {
170         return m_shareName;
171     }
172
173     /**
174      * Check if the share path is the root path
175      *
176      * @return boolean
177      */

178     public final boolean isRootSharePath()
179     {
180         if (m_sharePath == null || m_sharePath.compareTo(DIR_SEPERATOR) == 0)
181             return true;
182         return false;
183     }
184
185     /**
186      * Check if the share path is valid
187      *
188      * @reutrn boolean
189      */

190     public final boolean hasSharePath()
191     {
192         return m_sharePath != null ? true : false;
193     }
194
195     /**
196      * Return the share relative path
197      *
198      * @reutrn String
199      */

200     public final String JavaDoc getSharePath()
201     {
202         return m_sharePath;
203     }
204
205     /**
206      * Check if the shared device has been set
207      *
208      * @return boolean
209      */

210     public final boolean hasSharedDevice()
211     {
212         return m_shareDev != null ? true : false;
213     }
214
215     /**
216      * Return the shared device
217      *
218      * @return DiskSharedDevice
219      */

220     public final DiskSharedDevice getSharedDevice()
221     {
222         return m_shareDev;
223     }
224
225     /**
226      * Set the paths using the specified FTP path
227      *
228      * @param path String
229      * @exception InvalidPathException
230      */

231     public final void setFTPPath(String JavaDoc path) throws InvalidPathException
232     {
233
234         // Check for a null path or the root path
235

236         if (path == null || path.length() == 0 || path.compareTo(FTP_SEPERATOR) == 0)
237         {
238             m_ftpPath = FTP_SEPERATOR;
239             m_shareName = null;
240             m_sharePath = null;
241             m_shareDev = null;
242             return;
243         }
244
245         // Check if the path starts with the FTP seperator
246

247         if (path.startsWith(FTP_SEPERATOR) == false)
248             throw new InvalidPathException("Invalid FTP path, should start with " + FTP_SEPERATOR);
249
250         // Save the FTP path
251

252         m_ftpPath = path;
253
254         // Get the first level directory from the path, this maps to the share name
255

256         int pos = path.indexOf(FTP_SEPERATOR, 1);
257         if (pos != -1)
258         {
259             m_shareName = path.substring(1, pos);
260             if (path.length() > pos)
261                 m_sharePath = path.substring(pos).replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR);
262             else
263                 m_sharePath = DIR_SEPERATOR;
264         }
265         else
266         {
267             m_shareName = path.substring(1);
268             m_sharePath = DIR_SEPERATOR;
269         }
270
271         // Check if the share has changed
272

273         if (m_shareDev != null && m_shareName != null && m_shareDev.getName().compareTo(m_shareName) != 0)
274             m_shareDev = null;
275     }
276
277     /**
278      * Set the paths using the specified share and share relative path
279      *
280      * @param shr String
281      * @param path String
282      * @exception InvalidPathException
283      */

284     public final void setSharePath(String JavaDoc shr, String JavaDoc path) throws InvalidPathException
285     {
286
287         // Save the share name and path
288

289         m_shareName = shr;
290         m_sharePath = path != null ? path : DIR_SEPERATOR;
291
292         // Build the FTP style path
293

294         StringBuffer JavaDoc ftpPath = new StringBuffer JavaDoc();
295
296         ftpPath.append(FTP_SEPERATOR);
297         if (hasShareName())
298             ftpPath.append(getShareName());
299
300         if (hasSharePath())
301         {
302
303             // Convert the share relative path to an FTP style path
304

305             String JavaDoc ftp = getSharePath().replace(DIR_SEPERATOR_CHAR, FTP_SEPERATOR_CHAR);
306             ftpPath.append(ftp);
307         }
308         else
309             ftpPath.append(FTP_SEPERATOR);
310
311         // Update the FTP path
312

313         m_ftpPath = ftpPath.toString();
314     }
315
316     /**
317      * Set the shared device
318      *
319      * @param shareList SharedDeviceList
320      * @param sess FTPSrvSession
321      * @return boolean
322      */

323     public final boolean setSharedDevice(SharedDeviceList shareList, FTPSrvSession sess)
324     {
325
326         // Clear the current shared device
327

328         m_shareDev = null;
329
330         // Check if the share name is valid
331

332         if (hasShareName() == false || shareList == null)
333             return false;
334
335         // Find the required disk share
336

337         SharedDevice shr = shareList.findShare(getShareName());
338
339         if (shr != null && shr instanceof DiskSharedDevice)
340             m_shareDev = (DiskSharedDevice) shr;
341
342         // Return the status
343

344         return m_shareDev != null ? true : false;
345     }
346
347     /**
348      * Set the shared device
349      *
350      * @param share DiskSharedDevice
351      * @return boolean
352      */

353     public final boolean setSharedDevice(DiskSharedDevice share)
354     {
355         m_shareDev = share;
356         return true;
357     }
358     
359     /**
360      * Build an FTP path to the specified file
361      *
362      * @param fname String
363      * @return String
364      */

365     public final String JavaDoc makeFTPPathToFile(String JavaDoc fname)
366     {
367
368         // Build the FTP path to a file
369

370         StringBuffer JavaDoc path = new StringBuffer JavaDoc(256);
371         path.append(m_ftpPath);
372         if (m_ftpPath.endsWith(FTP_SEPERATOR) == false)
373             path.append(FTP_SEPERATOR);
374         path.append(fname);
375
376         return path.toString();
377     }
378
379     /**
380      * Build a share relative path to the specified file
381      *
382      * @param fname String
383      * @return String
384      */

385     public final String JavaDoc makeSharePathToFile(String JavaDoc fname)
386     {
387
388         // Build the share relative path to a file
389

390         StringBuilder JavaDoc path = new StringBuilder JavaDoc(256);
391         path.append(m_sharePath);
392         if (m_sharePath.endsWith(DIR_SEPERATOR) == false)
393             path.append(DIR_SEPERATOR);
394         path.append(fname);
395
396         return path.toString();
397     }
398
399     /**
400      * Add a directory to the end of the current path
401      *
402      * @param dir String
403      */

404     public final void addDirectory(String JavaDoc dir)
405     {
406
407         // Check if the directory has a trailing seperator
408

409         if (dir.length() > 1 && dir.endsWith(FTP_SEPERATOR) || dir.endsWith(DIR_SEPERATOR))
410             dir = dir.substring(0, dir.length() - 1);
411
412         // Append the directory to the FTP path
413

414         StringBuilder JavaDoc str = new StringBuilder JavaDoc(256);
415         str.append(m_ftpPath);
416
417         if (m_ftpPath.endsWith(FTP_SEPERATOR) == false)
418             str.append(FTP_SEPERATOR);
419         str.append(dir);
420         if (m_ftpPath.endsWith(FTP_SEPERATOR) == false)
421             str.append(FTP_SEPERATOR);
422
423         m_ftpPath = str.toString();
424
425         // Check if there are any incorrect seperators in the FTP path
426

427         if (m_ftpPath.indexOf(DIR_SEPERATOR) != -1)
428             m_ftpPath = m_ftpPath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR);
429
430         // Append the directory to the share relative path
431

432         str.setLength(0);
433         str.append(m_sharePath);
434         if (m_sharePath.endsWith(DIR_SEPERATOR) == false)
435             str.append(DIR_SEPERATOR);
436         str.append(dir);
437
438         m_sharePath = str.toString();
439
440         // Check if there are any incorrect seperators in the share relative path
441

442         if (m_sharePath.indexOf(FTP_SEPERATOR) != -1)
443             m_sharePath = m_sharePath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR);
444
445         // Indicate that the path is to a directory
446

447         setDirectory(true);
448     }
449
450     /**
451      * Add a file to the end of the current path
452      *
453      * @param file String
454      */

455     public final void addFile(String JavaDoc file)
456     {
457
458         // Append the file name to the FTP path
459

460         StringBuilder JavaDoc str = new StringBuilder JavaDoc(256);
461         str.append(m_ftpPath);
462
463         if (m_ftpPath.endsWith(FTP_SEPERATOR) == false)
464             str.append(FTP_SEPERATOR);
465         str.append(file);
466
467         m_ftpPath = str.toString();
468
469         // Check if there are any incorrect seperators in the FTP path
470

471         if (m_ftpPath.indexOf(DIR_SEPERATOR) != -1)
472             m_ftpPath = m_ftpPath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR);
473
474         // Append the file name to the share relative path
475

476         str.setLength(0);
477         str.append(m_sharePath);
478         if (m_sharePath.endsWith(DIR_SEPERATOR) == false)
479             str.append(DIR_SEPERATOR);
480         str.append(file);
481
482         m_sharePath = str.toString();
483
484         // Check if there are any incorrect seperators in the share relative path
485

486         if (m_sharePath.indexOf(FTP_SEPERATOR) != -1)
487             m_sharePath = m_sharePath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR);
488
489         // Indicate that the path is to a file
490

491         setDirectory(false);
492     }
493
494     /**
495      * Remove the last directory from the end of the path
496      */

497     public final void removeDirectory()
498     {
499
500         // Check if the FTP path has a directory to remove
501

502         if (m_ftpPath != null && m_ftpPath.length() > 1)
503         {
504
505             // Find the last directory in the FTP path
506

507             int pos = m_ftpPath.length() - 1;
508             if (m_ftpPath.endsWith(FTP_SEPERATOR))
509                 pos--;
510
511             while (pos > 0 && m_ftpPath.charAt(pos) != FTP_SEPERATOR_CHAR)
512                 pos--;
513
514             // Set the new FTP path
515

516             m_ftpPath = m_ftpPath.substring(0, pos);
517
518             // Indicate that the path is to a directory
519

520             setDirectory(true);
521
522             // Reset the share/share path
523

524             try
525             {
526                 setFTPPath(m_ftpPath);
527             }
528             catch (InvalidPathException ex)
529             {
530             }
531         }
532     }
533
534     /**
535      * Set/clear the directory path flag
536      *
537      * @param dir boolean
538      */

539     protected final void setDirectory(boolean dir)
540     {
541         m_dir = dir;
542     }
543
544     /**
545      * Check if an FTP path string contains multiple directories
546      *
547      * @param path String
548      * @return boolean
549      */

550     public final static boolean hasMultipleDirectories(String JavaDoc path)
551     {
552         if (path == null)
553             return false;
554
555         if (path.startsWith(FTP_SEPERATOR))
556             return true;
557         return false;
558     }
559
560     /**
561      * Check if the FTP path is a relative path, ie. does not start with a leading slash
562      *
563      * @param path String
564      * @return boolean
565      */

566     public final static boolean isRelativePath(String JavaDoc path)
567     {
568         if (path == null)
569             return false;
570         return path.startsWith(FTP_SEPERATOR) ? false : true;
571     }
572
573     /**
574      * Return the FTP path as a string
575      *
576      * @return String
577      */

578     public String JavaDoc toString()
579     {
580         StringBuilder JavaDoc str = new StringBuilder JavaDoc();
581
582         str.append("[");
583         str.append(getFTPPath());
584         str.append("=");
585         str.append(getShareName());
586         str.append(",");
587         str.append(getSharePath());
588         str.append("]");
589
590         return str.toString();
591     }
592 }
593
Popular Tags