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.vfs.VFile;
24  import org.millscript.commons.vfs.VFolder;
25  import org.millscript.commons.vfs.VVolume;
26  import org.millscript.commons.vfs.mime.MIMETypeHandler;
27  
28  import java.io.InputStream;
29  import java.io.OutputStream;
30  import java.io.Reader;
31  import java.io.Writer;
32  
33  /**
34   * This class implements a virtual file for a Unix style chroot volume.
35   */
36  public class ChrootFile extends AbstractChrootEntry implements VFile {
37  
38      /**
39       * The root folder for this volume.
40       */
41      private final ChrootVolume chrootVolume;
42  
43      /**
44       * The hidden virtual file.
45       */
46      private final VFile chrootFile;
47  
48      /**
49       * The parent folder for this file.
50       */
51      private final ChrootFolder parentFolder;
52  
53      /**
54       * Constructs a new chroot file for the specified file, in the specified
55       * folder, on the specified volume.
56       *
57       * @param vol   the chroot volume this file belongs on
58       * @param parent    the chroot parent folder for this file
59       * @param file  the underlying virtual file
60       */
61      ChrootFile( final ChrootVolume vol, final ChrootFolder parent, final VFile file ) {
62          this.chrootVolume = vol;
63          this.chrootFile = file;
64          this.parentFolder = parent;
65      }
66  
67      /**
68       * @see org.millscript.commons.vfs.VEntry#appendRelativePath(java.lang.StringBuffer)
69       */
70      public StringBuffer appendRelativePath( final StringBuffer buffer ) {
71          // If we have a parent, append it's relative path to the buffer.
72          if ( this.getParent() != null ) {
73              // This will recurse all the way up the chain to give us the
74              // relative path from the root of the filesystem.
75              // Append the parent entry to the buffer
76              this.getParent().appendRelativePath( buffer );
77          }
78          // Append this entry to the buffer followed by a slash
79          return buffer.append( this.getName() );
80      }
81  
82      /**
83       * @see org.millscript.commons.vfs.VEntry#appendURI(java.lang.StringBuffer)
84       */
85      public StringBuffer appendURI( final StringBuffer buffer ) {
86          // If we have a parent, append it's URI to the buffer.
87          // This will recurse all the way up the chain to give us the complete
88          // URI for this entry
89          if ( this.getParent() == null ) {
90              // No parent - this is the root entry
91              this.getVolume().appendBaseURI( buffer );
92          } else {
93              // We have a parent, so append it's URI
94              this.getParent().appendURI( buffer );
95          }
96          return buffer.append( this.getName() );
97      }
98  
99      /**
100      * @see org.millscript.commons.vfs.VEntry#exists()
101      */
102     public boolean exists() {
103         return this.chrootFile.exists();
104     }
105 
106     /**
107      * @see org.millscript.commons.vfs.VFile#getAppendOutputStream()
108      */
109     public OutputStream getAppendOutputStream() {
110         return this.chrootFile.getAppendOutputStream();
111     }
112 
113     /**
114      * @see org.millscript.commons.vfs.VFile#getAppendWriter()
115      */
116     public Writer getAppendWriter() {
117         return this.chrootFile.getAppendWriter();
118     }
119 
120     /**
121      * @see org.millscript.commons.vfs.VFile#getContentMIMEType()
122      */
123     public String getContentMIMEType() {
124         return this.chrootFile.getContentMIMEType();
125     }
126 
127     /**
128      * @see org.millscript.commons.vfs.VFile#getInputStream()
129      */
130     public InputStream getInputStream() {
131         return this.chrootFile.getInputStream();
132     }
133 
134     /**
135      * @see org.millscript.commons.vfs.VEntry#getMIMEType(java.lang.String)
136      */
137     public String getMIMEType( final String fileName ) {
138         return this.chrootFile.getMIMEType( fileName );
139     }
140 
141     /**
142      * @see org.millscript.commons.vfs.VEntry#getMIMETypeHandler(java.lang.String)
143      */
144     public MIMETypeHandler getMIMETypeHandler( final String type ) {
145         return this.chrootFile.getMIMETypeHandler( type );
146     }
147 
148     /**
149      * @see org.millscript.commons.vfs.VEntry#getName()
150      */
151     public String getName() {
152         return this.chrootFile.getName();
153     }
154 
155     /**
156      * @see org.millscript.commons.vfs.VFile#getOutputStream()
157      */
158     public OutputStream getOutputStream() {
159         return this.chrootFile.getOutputStream();
160     }
161 
162     /**
163      * @see org.millscript.commons.vfs.VEntry#getParent()
164      */
165     public VFolder getParent() {
166         return this.parentFolder;
167     }
168 
169     /**
170      * @see org.millscript.commons.vfs.VFile#getReader()
171      */
172     public Reader getReader() {
173         return this.chrootFile.getReader();
174     }
175 
176     /**
177      * @see org.millscript.commons.vfs.VEntry#getVolume()
178      */
179     public VVolume getVolume() {
180         return this.chrootVolume;
181     }
182 
183     /**
184      * @see org.millscript.commons.vfs.VFile#getWriter()
185      */
186     public Writer getWriter() {
187         return this.chrootFile.getWriter();
188     }
189 
190     /**
191      * @see org.millscript.commons.vfs.VEntry#toLocal()
192      */
193     public VFile toLocal() {
194         return new ChrootFile( this.chrootVolume, this.parentFolder, this.chrootFile.toLocal() );
195     }
196 
197     /**
198      * @see java.lang.Object#toString()
199      */
200     @Override
201     public String toString() {
202         final StringBuffer buff = new StringBuffer( "CHROOT_FILE( " );
203         return this.appendAbsolutePath( buff ).append( " )" ).toString();
204     }
205 
206 }