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;
22  
23  import org.millscript.commons.alert.Alert;
24  import org.millscript.commons.alert.AlertDecorator;
25  import org.millscript.commons.vfs.alerts.VFSAlert;
26  import org.millscript.commons.vfs.protocols.file.LocalFolder;
27  import org.millscript.commons.vfs.util.ChrootVolume;
28  import org.millscript.commons.vfs.util.Resolver;
29  import org.millscript.commons.vfs.util.SimpleVolume;
30  
31  /**
32   * This class provides the skeletal implementation of a virtual filesystem
33   * folder,
34   */
35  public abstract class AbstractVFolder< V extends VVolume > extends AbstractVEntry< V > implements AlertDecorator, VFolder {
36  
37      /**
38       * Constructs a new virtual filesystem folder with the specified name,
39       * parent folder and volume.
40       *
41       * @param vol   the volume this entry is held on
42       * @param parent    the parent folder for this folder
43       * @param name  the name of the folder
44       */
45      public AbstractVFolder( final V vol, final VFolder parent, final String name ) {
46          super( vol, parent, name );
47      }
48  
49      /**
50       * @see org.millscript.commons.vfs.VEntry#appendRelativePath(java.lang.StringBuffer)
51       */
52      public final StringBuffer appendRelativePath( final StringBuffer buff ) {
53          // If we have a parent, append it's relative path to the buffer.
54          if ( this.getParent() == null ) {
55              // This is the root, so we append any base path for this volume.
56              return buff;
57          } else {
58              // This will recurse all the way up the chain to give us the
59              // relative path from the root of the filesystem.
60              // Append the parent entry to the buffer
61              this.getParent().appendRelativePath( buff );
62              // Append this entry to the buffer followed by a slash
63              return buff.append( this.getName() ).append( '/' );
64          }
65      }
66  
67      /**
68       * @see org.millscript.commons.vfs.VEntry#appendRelativePathOnVolume(java.lang.StringBuffer)
69       */
70      public final StringBuffer appendRelativePathOnVolume( final StringBuffer buff ) {
71          // If we have a parent and it's volume is the same as ours, append it's
72          // relative path to the buffer.
73          // This will recurse all the way up the chain to the root of the volume
74          // this file belongs on, to give us the relative path from the root of
75          // the volume.
76          // TODO - This should probably use equality not identity but we need
77          // implementations of hashCode() and equals()
78          if ( this.getParent() == null ) {
79              // Ah. No parent, so we must be at the top of the hierarchy
80              return buff;
81          } else if ( this.getParent().getVolume() == this.getVolume() ) {
82              // The parent volume is the same as this one, so we should append
83              // the parents path as it's on the same volume
84              // Append the parent entry to the buffer
85              this.getParent().appendRelativePathOnVolume( buff );
86              // Append this entry to the buffer followed by a slash
87              return buff.append( this.getName() ).append( '/' );
88          } else {
89              // This is the root, so we append any base path for this volume.
90              return buff;
91          }
92      }
93  
94      /**
95       * @see org.millscript.commons.vfs.VEntry#appendURI(java.lang.StringBuffer)
96       */
97      public final StringBuffer appendURI( final StringBuffer buffer ) {
98          // If we have a parent, append it's URI to the buffer.
99          // This will recurse all the way up the chain to give us the complete
100         // URI for this entry
101         if ( this.getParent() == null ) {
102             // No parent - this is the root entry
103             this.getVolume().appendBaseURI( buffer );
104         } else {
105             // We have a parent, so append it's URI
106             this.getParent().appendURI( buffer );
107             buffer.append( this.getName() );
108         }
109         return buffer.append( '/' );
110     }
111 
112     /**
113      * @see org.millscript.commons.vfs.VFolder#chroot()
114      */
115     public final VVolume chroot() {
116         return new ChrootVolume( this );
117     }
118 
119     /**
120      * @see org.millscript.commons.alert.AlertDecorator#decorate(org.millscript.commons.alert.Alert)
121      */
122     public Alert decorate( final Alert alert ) {
123         return alert.culprit( "vfolder", this );
124     }
125 
126     /**
127      * @see org.millscript.commons.vfs.VFolder#make(java.lang.String)
128      */
129     public final VFolder make( final String segment ) {
130         // First get the virtual folder for the new segment
131         final VFolder child = this.getVFolder( segment );
132         // Make it...
133         return child.make();
134     }
135 
136     /**
137      * @see org.millscript.commons.vfs.VFolder#resolveAsFile(java.lang.String)
138      */
139     public final VFile resolveAsFile( final String path ) {
140         return Resolver.resolveAsFile( this, path );
141     }
142 
143     /**
144      * @see org.millscript.commons.vfs.VFolder#resolveAsFolder(java.lang.String)
145      */
146     public final VFolder resolveAsFolder( final String path ) {
147         return Resolver.resolveAsFolder( this, path );
148     }
149 
150     /**
151      * @see org.millscript.commons.vfs.VFolder#resolveAsVolume(java.lang.String)
152      */
153     public final VVolume resolveAsVolume( final String path ) {
154         return Resolver.resolveAsVolume( this, path );
155     }
156 
157     /**
158      * @see org.millscript.commons.vfs.VFolder#toLocal()
159      */
160     public LocalFolder toLocal() {
161         // TODO - Must implement and add hook to remove temporary files
162         throw new VFSAlert(
163             "Can't make a local copy of a virtual folder yet"
164         ).decorate( this ).mishap();
165     }
166 
167     /**
168      * @see java.lang.Object#toString()
169      */
170     @Override
171     public String toString() {
172         final StringBuffer buff = new StringBuffer( "VFOLDER( " );
173         this.appendAbsolutePath( buff );
174         buff.append( " )" );
175         return buff.toString();
176     }
177 
178     /**
179      * @see org.millscript.commons.vfs.VFolder#toVolume()
180      */
181     public final VVolume toVolume() {
182         return new SimpleVolume( this );
183     }
184 
185 }