View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2005 Kevin Rogers
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
20  ////////////////////////////////////////////////////////////////////////////////
21  package org.millscript.commons.vfs.util;
22  
23  import org.millscript.commons.alert.alerts.Fault;
24  import org.millscript.commons.vfs.VEntry;
25  import org.millscript.commons.vfs.VFile;
26  import org.millscript.commons.vfs.VFolder;
27  import org.millscript.commons.vfs.VVolume;
28  import org.millscript.commons.vfs.mime.MIMETypeHandler;
29  
30  import java.util.List;
31  
32  /**
33   * This class implements a virtual folder for a Unix style chroot volume.
34   */
35  public class ChrootFolder extends AbstractChrootEntry implements VFolder {
36  
37      /**
38       * The root folder for this volume.
39       */
40      private final ChrootVolume chrootVolume;
41  
42      /**
43       * The hidden virtual folder.
44       */
45      private final VFolder chrootFolder;
46  
47      /**
48       * The parent folder for this folder.
49       */
50      private final ChrootFolder parentFolder;
51  
52      /**
53       * Constructs a new chroot folder for the specified folder, in the
54       * specified folder, on the specified volume.
55       *
56       * @param vol   the chroot volume this folder belongs on
57       * @param parent    the chroot parent folder for this folder
58       * @param folder    the underlying virtual folder
59       */
60      ChrootFolder( final ChrootVolume vol, final ChrootFolder parent, final VFolder folder ) {
61          this.chrootVolume = vol;
62          this.chrootFolder = folder;
63          this.parentFolder = parent;
64      }
65  
66      /**
67       * @see org.millscript.commons.vfs.VEntry#appendRelativePath(java.lang.StringBuffer)
68       */
69      public StringBuffer appendRelativePath( final StringBuffer buffer ) {
70          // If we have a parent, append it's relative path to the buffer.
71          if ( this.getParent() == null ) {
72              // This is the root, so we append any base path for this volume.
73              return buffer;
74          } else {
75              // This will recurse all the way up the chain to give us the
76              // relative path from the root of the filesystem.
77              // Append the parent entry to the buffer
78              this.getParent().appendRelativePath( buffer );
79              // Append this entry to the buffer followed by a slash
80              return buffer.append( this.getName() ).append( '/' );
81          }
82      }
83  
84      /**
85       * @see org.millscript.commons.vfs.VEntry#appendURI(java.lang.StringBuffer)
86       */
87      public StringBuffer appendURI( final StringBuffer buffer ) {
88          // If we have a parent, append it's URI to the buffer.
89          // This will recurse all the way up the chain to give us the complete
90          // URI for this entry
91          if ( this.getParent() == null ) {
92              // No parent - this is the root entry
93              this.getVolume().appendBaseURI( buffer );
94          } else {
95              // We have a parent, so append it's URI
96              this.getParent().appendURI( buffer );
97              buffer.append( this.getName() );
98          }
99          return buffer.append( '/' );
100     }
101 
102     /**
103      * @see org.millscript.commons.vfs.VFolder#checkVEntry(java.lang.String)
104      */
105     public VEntry checkVEntry( final String segment ) {
106         VEntry entry = this.chrootFolder.checkVEntry( segment );
107         if ( entry instanceof VFile ) {
108             return new ChrootFile( this.chrootVolume, this, (VFile) entry );
109         } else if ( entry instanceof VFolder ) {
110             return new ChrootFolder( this.chrootVolume, this, (VFolder) entry );
111         } else {
112             throw new Fault(
113                 "Unexpected type of virtual filesystem entry"
114             ).culprit( "entry", entry ).mishap();
115         }
116     }
117 
118     /**
119      * @see org.millscript.commons.vfs.VFolder#checkVFile(java.lang.String)
120      */
121     public ChrootFile checkVFile( final String segment ) {
122         return new ChrootFile( this.chrootVolume, this, this.chrootFolder.checkVFile( segment ) );
123     }
124 
125     /**
126      * @see org.millscript.commons.vfs.VFolder#checkVFolder(java.lang.String)
127      */
128     public ChrootFolder checkVFolder( final String segment ) {
129         return new ChrootFolder( this.chrootVolume, this, this.chrootFolder.checkVFolder( segment ) );
130     }
131 
132     /**
133      * @see org.millscript.commons.vfs.VFolder#chroot()
134      */
135     public ChrootVolume chroot() {
136         return new ChrootVolume( this.chrootFolder );
137     }
138 
139     /**
140      * @see org.millscript.commons.vfs.VEntry#exists()
141      */
142     public boolean exists() {
143         return this.chrootFolder.exists();
144     }
145 
146     /**
147      * @see org.millscript.commons.vfs.VEntry#getMIMEType(java.lang.String)
148      */
149     public String getMIMEType( final String fileName ) {
150         return this.chrootFolder.getMIMEType( fileName );
151     }
152 
153     /**
154      * @see org.millscript.commons.vfs.VEntry#getMIMETypeHandler(java.lang.String)
155      */
156     public MIMETypeHandler getMIMETypeHandler( final String type ) {
157         return this.chrootFolder.getMIMETypeHandler( type );
158     }
159 
160     /**
161      * @see org.millscript.commons.vfs.VEntry#getName()
162      */
163     public String getName() {
164         return this.chrootFolder.getName();
165     }
166 
167     /**
168      * @see org.millscript.commons.vfs.VEntry#getParent()
169      */
170     public VFolder getParent() {
171         return this.parentFolder;
172     }
173 
174     /**
175      * @see org.millscript.commons.vfs.VFolder#getVFile(java.lang.String)
176      */
177     public ChrootFile getVFile( final String segment ) {
178         return new ChrootFile( this.chrootVolume, this, this.chrootFolder.getVFile( segment ) );
179     }
180 
181     /**
182      * @see org.millscript.commons.vfs.VFolder#getVFolder(java.lang.String)
183      */
184     public ChrootFolder getVFolder( final String segment ) {
185         return new ChrootFolder( this.chrootVolume, this, this.chrootFolder.getVFolder( segment ) );
186     }
187 
188     /**
189      * @see org.millscript.commons.vfs.VEntry#getVolume()
190      */
191     public ChrootVolume getVolume() {
192         return this.chrootVolume;
193     }
194 
195     /**
196      * @see org.millscript.commons.vfs.VFolder#listEntries()
197      */
198     public List< VEntry > listEntries() {
199         final List< VEntry > list = this.chrootFolder.listEntries();
200         for ( int n = 0; n < list.size(); n++ ) {
201             VEntry entry = list.get( n );
202             if ( entry instanceof VFile ) {
203                 list.set( n, new ChrootFile( this.chrootVolume, this, (VFile) entry ) );
204             } else if ( entry instanceof VFolder ) {
205                 list.set( n, new ChrootFolder( this.chrootVolume, this, (VFolder) entry ) );
206             } else {
207                 throw new Fault(
208                     "Unexpected type of virtual filesystem entry"
209                 ).culprit( "entry", entry ).mishap();
210             }
211         }
212         return list;
213     }
214 
215     /**
216      * @see org.millscript.commons.vfs.VFolder#listFiles()
217      */
218     public List< VFile > listFiles() {
219         final List< VFile > list = this.chrootFolder.listFiles();
220         for ( int n = 0; n < list.size(); n++ ) {
221             list.set( n, new ChrootFile( this.chrootVolume, this, list.get( n ) ) );
222         }
223         return list;
224     }
225 
226     /**
227      * @see org.millscript.commons.vfs.VFolder#listFolders()
228      */
229     public List< VFolder > listFolders() {
230         final List< VFolder > list = this.chrootFolder.listFolders();
231         for ( int n = 0; n < list.size(); n++ ) {
232             list.set( n, new ChrootFolder( this.chrootVolume, this, list.get( n ) ) );
233         }
234         return list;
235     }
236 
237     /**
238      * @see org.millscript.commons.vfs.VFolder#make()
239      */
240     public VFolder make() {
241         return this.chrootFolder.make();
242     }
243 
244     /**
245      * @see org.millscript.commons.vfs.VFolder#make(java.lang.String)
246      */
247     public VFolder make( final String segment ) {
248         return this.chrootFolder.make( segment );
249     }
250 
251     /**
252      * @see org.millscript.commons.vfs.VFolder#resolveAsFile(java.lang.String)
253      */
254     public VFile resolveAsFile( final String path ) {
255         return new ChrootFile( this.chrootVolume, this, Resolver.resolveAsFile( this.chrootFolder, path ) );
256     }
257 
258     /**
259      * @see org.millscript.commons.vfs.VFolder#resolveAsFolder(java.lang.String)
260      */
261     public VFolder resolveAsFolder( final String path ) {
262         return new ChrootFolder( this.chrootVolume, this, Resolver.resolveAsFolder( this.chrootFolder, path ) );
263     }
264 
265     /**
266      * @see org.millscript.commons.vfs.VFolder#resolveAsVolume(java.lang.String)
267      */
268     public VVolume resolveAsVolume( final String path ) {
269         return this.resolveAsFolder( path ).toVolume();
270     }
271 
272     /**
273      * @see org.millscript.commons.vfs.VEntry#toLocal()
274      */
275     public VFolder toLocal() {
276         return new ChrootFolder( this.chrootVolume, this.parentFolder, this.chrootFolder.toLocal() );
277     }
278 
279     /**
280      * @see java.lang.Object#toString()
281      */
282     @Override
283     public String toString() {
284         final StringBuffer buff = new StringBuffer( "CHROOT_FOLDER( " );
285         return this.appendAbsolutePath( buff ).append( " )" ).toString();
286     }
287 
288     /**
289      * @see org.millscript.commons.vfs.VFolder#toVolume()
290      */
291     public VVolume toVolume() {
292         return new SimpleVolume( this );
293     }
294 
295 }