001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: DirectoryPath.java,v $
023   Revision 1.5  2004/05/05 21:22:45  markl
024   Comment header updates.
025
026   Revision 1.4  2003/01/19 09:42:38  markl
027   Javadoc & comment header updates.
028
029   Revision 1.3  2001/03/12 02:57:39  markl
030   Source code cleanup.
031
032   Revision 1.2  1999/01/10 03:47:05  markl
033   added GPL header & RCS tag
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.util;
038
039import java.util.*;
040
041/** A convenience class for maintaining a directory path (that is, an ordered
042  * list of directories).
043  *
044  * @author Mark Lindner
045  */
046
047public class DirectoryPath
048  {
049  private String psep;
050  private Vector _dirs;
051
052  /** Construct a new, empty <code>DirectoryPath</code>. */
053      
054  public DirectoryPath()
055    {
056    this(null);
057    }
058
059  /** Construct a new <code>DirectoryPath</code> for the given directories.
060    *
061    * @param dirs An array of directory names.
062    */
063  
064  public DirectoryPath(String dirs[])
065    {
066    psep = System.getProperty("path.separator");
067    _dirs = new Vector();
068    
069    if(dirs != null)
070      for(int i = 0; i < dirs.length; i++)
071        _dirs.addElement(dirs[i]);
072    }
073
074  /** Prepend a directory to the beginning of the path.
075    *
076    * @param dir The directory to add.
077    */
078
079  public synchronized void prepend(String dir)
080    {
081    _dirs.insertElementAt(dir, 0);
082    }
083
084  /** Prepend a list directories to the beginning of the path. The order of
085    * the directories is preserved.
086    *
087    * @param dirs The directories to add.
088    */
089  
090  public synchronized void prepend(String dirs[])
091    {
092    for(int i = 0; i < dirs.length; i++)
093      _dirs.insertElementAt(dirs[i], i);
094    }
095  
096  /** Append a directory to the end of the path.
097    *
098    * @param dir The directory to add.
099    */
100  
101  public synchronized void append(String dir)
102    {
103    _dirs.addElement(dir);
104    }
105
106  /** Append a list directories to the end of the path. The order of the
107    * directories is preserved.
108    *
109    * @param dirs The directories to add.
110    */
111
112  public synchronized void append(String dirs[])
113    {
114    for(int i = 0; i < dirs.length; i++)
115      _dirs.addElement(dirs[i]);
116    }
117  
118  /** Get the list of directories for this path.
119    *
120    * @return An array of directory names.
121    */
122  
123  public synchronized String[] getDirectories()
124    {
125    String s[] = new String[_dirs.size()];
126
127    _dirs.copyInto(s);
128    
129    return(s);
130    }
131
132  /** Convert this path to a string, using the appropriate path separator for
133    * this platform.
134    */
135  
136  public String toString()
137    {
138    StringBuffer sb = new StringBuffer();
139
140    Enumeration e = _dirs.elements();
141    boolean first = true;
142    while(e.hasMoreElements())
143      {
144      String s = (String)e.nextElement();
145      if(!first) sb.append(psep);
146      first = false;
147      sb.append(s);
148      }
149
150    return(sb.toString());
151    }
152  
153  }
154
155/* end of source file */