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;
22  
23  import org.millscript.commons.vfs.VFS;
24  import org.millscript.commons.vfs.alerts.UnsupportedURISchemeAlert;
25  import org.millscript.commons.vfs.protocols.file.FileSchemeHandler;
26  import org.millscript.commons.vfs.protocols.ftp.FtpSchemeHandler;
27  import org.millscript.commons.vfs.protocols.http.HttpSchemeHandler;
28  
29  /**
30   * This class provides the default factory for URI scheme handlers.
31   * <p>
32   * You can override this implementation to add support for your own custom
33   * schemes.
34   * </p>
35   */
36  public class DefaultURISchemeHandlerFactory implements URISchemeHandlerFactory {
37  
38      /**
39       * The VFS for handlers created with this factory.
40       */
41      private final VFS vfs;
42  
43      /**
44       * Constructs a new URI scheme handler with the specified VFS.
45       *
46       * @param v the VFS for handlers created with this factory
47       */
48      public DefaultURISchemeHandlerFactory( final VFS v ) {
49          this.vfs = v;
50      }
51  
52      /**
53       * @see org.millscript.commons.vfs.protocols.URISchemeHandlerFactory#createURISchemeHandler(java.lang.String)
54       */
55      public URISchemeHandler createURISchemeHandler( final String scheme ) {
56          if ( scheme.equals( "file" ) ) {
57              return new FileSchemeHandler( this.vfs );
58          } else if ( scheme.equals( "http" ) ) {
59              return new HttpSchemeHandler( this.vfs );
60          } else if ( scheme.equals( "ftp" ) ) {
61              return new FtpSchemeHandler( this.vfs );
62          }
63          return null;
64      }
65  
66      /**
67       * @see org.millscript.commons.vfs.protocols.URISchemeHandlerFactory#getURISchemeHandler(java.lang.String)
68       */
69      public URISchemeHandler getURISchemeHandler( final String scheme ) {
70          final URISchemeHandler handler = this.createURISchemeHandler( scheme );
71          if ( handler == null ) {
72              throw new UnsupportedURISchemeAlert().culprit( "scheme", scheme ).mishap();
73          }
74          return handler;
75      }
76  
77  }