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.vfs.protocols.DefaultURISchemeHandlerFactory;
24  import org.millscript.commons.vfs.protocols.URISchemeHandler;
25  import org.millscript.commons.vfs.protocols.URISchemeHandlerFactory;
26  
27  import java.io.File;
28  import java.net.URI;
29  import java.nio.charset.Charset;
30  
31  /**
32   * This class provides the entry point for obtaining a new virtual filesytem.
33   */
34  public class VFS implements URISchemeHandler {
35  
36      /**
37       * The default character set we will use for reading and writing the
38       * contents of a file.
39       */
40      private Charset defaultCharset = Charset.forName( "UTF8" );
41  
42      /**
43       * The URI scheme handler factory used for obtaining URI scheme handlers.
44       */
45      private URISchemeHandlerFactory factory = new DefaultURISchemeHandlerFactory( this );
46  
47      /**
48       * Returns the virtual folder for the current working folder. The current
49       * working folder is determined via the <code>user.dir</code> system
50       * property.
51       *
52       * @return	a VFolder for the current working folder
53       */
54      public VFolder getCurrentWorkingFolder() {
55          return this.resolveAsFolder( new File( System.getProperty( "user.dir" ) ).toURI() );
56      }
57  
58      /**
59       * Returns the default character set this VFS will use for reading and
60       * writing file contents.
61       *
62       * @return  the default character set this VFS will use
63       */
64      public Charset getDefaultCharset() {
65          return defaultCharset;
66      }
67  
68      /**
69       * @see org.millscript.commons.vfs.protocols.URISchemeHandler#getParentVFS()
70       */
71      public VFS getParentVFS() {
72          return this;
73      }
74  
75      /**
76       * Returns the current URI scheme hander factory.
77       *
78       * @return  the current URISchemeHandlerFactory
79       */
80      public URISchemeHandlerFactory getURISchemeHandlerFactory() {
81          return factory;
82      }
83  
84      /**
85       * @see org.millscript.commons.vfs.protocols.URISchemeHandler#resolveAsFile(java.net.URI)
86       */
87      public final VFile resolveAsFile( final URI uri ) {
88          final URISchemeHandler handler = factory.getURISchemeHandler( uri.getScheme() );
89          return handler.resolveAsFile( uri );
90      }
91  
92      /**
93       * @see org.millscript.commons.vfs.protocols.URISchemeHandler#resolveAsFolder(java.net.URI)
94       */
95      public final VFolder resolveAsFolder( final URI uri ) {
96          final URISchemeHandler handler = factory.getURISchemeHandler( uri.getScheme() );
97          return handler.resolveAsFolder( uri );
98      }
99  
100     /**
101      * @see org.millscript.commons.vfs.protocols.URISchemeHandler#resolveAsVolume(java.net.URI)
102      */
103     public final VVolume resolveAsVolume( final URI uri ) {
104         final URISchemeHandler handler = factory.getURISchemeHandler( uri.getScheme() );
105         return handler.resolveAsVolume( uri );
106     }
107 
108     /**
109      * Sets the default character set this VFS will use for reading and
110      * writing file contents.
111      *
112      * @param charset   the default character set this VFS should use
113      */
114     public void setDefaultCharset( final Charset charset ) {
115         this.defaultCharset = charset;
116     }
117 
118     /**
119      * Sets the URI scheme handler factory that should be used for resolving
120      * URI schemes to VFS protocols.
121      *
122      * @param f the new URISchemeHandlerFactor to use
123      */
124     public void setURISchemeHandlerFactory( final URISchemeHandlerFactory f ) {
125         this.factory = f;
126     }
127 
128 }