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.protocols.file;
22  
23  import org.millscript.commons.alert.Alert;
24  import org.millscript.commons.vfs.AbstractVFile;
25  import org.millscript.commons.vfs.VFolder;
26  import org.millscript.commons.vfs.alerts.VCannotOpenEntryAlert;
27  
28  import java.io.File;
29  import java.io.FileInputStream;
30  import java.io.FileNotFoundException;
31  import java.io.FileOutputStream;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  
35  /**
36   * This class implements a virtual filesystem file for a local filesystem file.
37   */
38  public class LocalFile extends AbstractVFile< LocalVolume > {
39  
40      /**
41       * Constructs a new virtual filesystem file with the specified name,
42       * parent folder and volume.
43       *
44       * @param vol   the volume this entry is held on
45       * @param parent    the parent folder for this file
46       * @param name  the name of the file
47       */
48      LocalFile( final LocalVolume vol, final VFolder parent, final String name ) {
49          super( vol, parent, name );
50      }
51  
52      /**
53       * Constructs a new virtual filesystem file with the specified name,
54       * parent folder. This file will belong to the same volume as the specified
55       * parent folder.
56       *
57       * @param parent    the parent folder for this file
58       * @param name  the name of the file
59       */
60      LocalFile( final LocalFolder parent, final String name ) {
61          super( parent.getVolume(), parent, name );
62      }
63  
64      /**
65       * @see org.millscript.commons.vfs.VEntry#exists()
66       */
67      public boolean exists() {
68          return this.getFile().isFile();
69      }
70  
71      /**
72       * @see org.millscript.commons.vfs.VFile#getAppendOutputStream()
73       */
74      public OutputStream getAppendOutputStream() {
75          try {
76              return new FileOutputStream( this.getFile(), true );
77          } catch ( FileNotFoundException ex ) {
78              // OK, into error handling mode
79              throw(
80                  this.handleException( ex ).remishap()
81              );
82          }
83      }
84  
85      /**
86       * Returns the File for this local virtual file.
87       *
88       * @return  a File for this local virtual file
89       */
90      public File getFile() {
91          return new File( this.getURI().getPath() );
92      }
93  
94      /**
95       * @see org.millscript.commons.vfs.VFile#getInputStream()
96       */
97      public InputStream getInputStream() {
98          try {
99              return new FileInputStream( this.getFile() );
100         } catch ( FileNotFoundException ex ) {
101             // OK, into error handling mode
102             throw(
103                 this.handleException( ex ).remishap()
104             );
105         }
106     }
107 
108     /**
109      * @see org.millscript.commons.vfs.VFile#getOutputStream()
110      */
111     public OutputStream getOutputStream() {
112         try {
113             return new FileOutputStream( this.getFile() );
114         } catch ( FileNotFoundException ex ) {
115             // OK, into error handling mode
116             throw(
117                 this.handleException( ex ).remishap()
118             );
119         }
120     }
121 
122     private Alert handleException( final FileNotFoundException ex ) {
123         File f = this.getFile();
124         if ( f.isDirectory() ) {
125             // The file already exists and is a directory!
126             return VCannotOpenEntryAlert.entryIsNotAFile( this ).setParentThrowable( ex ).mishap();
127         }
128         // The entry doesn't exist at all.
129         return VCannotOpenEntryAlert.fileNotFound( this ).setParentThrowable( ex ).mishap();
130     }
131 
132     /**
133      * @see org.millscript.commons.vfs.AbstractVFile#toLocal()
134      */
135     @Override
136     public LocalFile toLocal() {
137         return this;
138     }
139 
140 }