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.alerts.ClassCastAlert;
24  import org.millscript.commons.alert.alerts.ClassNotFoundAlert;
25  import org.millscript.commons.alert.alerts.Fault;
26  import org.millscript.commons.alert.alerts.IllegalAccessAlert;
27  import org.millscript.commons.alert.alerts.InstantiationAlert;
28  import org.millscript.commons.vfs.mime.MIMETypeHandler;
29  
30  import java.net.URI;
31  import java.net.URISyntaxException;
32  
33  import javax.activation.CommandInfo;
34  import javax.activation.CommandMap;
35  import javax.activation.FileTypeMap;
36  
37  /**
38   * This class provides the skeletal implementation of a virtual filesystem
39   * entry,
40   */
41  public abstract class AbstractVEntry< V extends VVolume > implements VEntry {
42  
43      /**
44       * The command map for determining the handler for a given MIME type.
45       */
46      private static final CommandMap COMMANDS = CommandMap.getDefaultCommandMap();
47  
48      /**
49       * The file type map for determining the MIME type of a file, based on file
50       * extension.
51       */
52      private static final FileTypeMap MIME_TYPES = FileTypeMap.getDefaultFileTypeMap();
53  
54      /**
55       * The entries name.
56       */
57      private final String entryName;
58  
59      /**
60       * The entries parent folder.
61       */
62      private final VFolder parentFolder;
63  
64      /**
65       * The volume this entry is held on.
66       */
67      private final V volume;
68  
69      /**
70       * Constructs a new virtual filesystem entry with the specified name,
71       * parent folder and volume.
72       *
73       * @param vol   the volume this entry is held on
74       * @param parent    the parent folder for this entry
75       * @param name  the name of the entry
76       */
77      protected AbstractVEntry( final V vol, final VFolder parent, final String name ) {
78          this.volume = vol;
79          this.parentFolder = parent;
80          this.entryName = name;
81      }
82  
83      /**
84       * @see org.millscript.commons.vfs.VEntry#appendAbsolutePath(java.lang.StringBuffer)
85       */
86      public final StringBuffer appendAbsolutePath( final StringBuffer buffer ) {
87          return this.appendRelativePath( buffer.append( '/' ) );
88      }
89  
90      /**
91       * @see org.millscript.commons.vfs.VEntry#appendAbsolutePathOnVolume(java.lang.StringBuffer)
92       */
93      public final StringBuffer appendAbsolutePathOnVolume( final StringBuffer buffer ) {
94          return this.appendRelativePathOnVolume( buffer.append( '/' ) );
95      }
96  
97      /**
98       * @see org.millscript.commons.vfs.VEntry#getAbsolutePath()
99       */
100     public final String getAbsolutePath() {
101         return this.appendAbsolutePath( new StringBuffer() ).toString();
102     }
103 
104     /**
105      * @see org.millscript.commons.vfs.VEntry#getAbsolutePathOnVolume()
106      */
107     public final String getAbsolutePathOnVolume() {
108         return this.appendAbsolutePathOnVolume( new StringBuffer() ).toString();
109     }
110 
111     /**
112      * @see org.millscript.commons.vfs.VEntry#getMIMEType(java.lang.String)
113      */
114     public final String getMIMEType( final String fileName ) {
115         return MIME_TYPES.getContentType( fileName );
116     }
117 
118     /**
119      * @see org.millscript.commons.vfs.VEntry#getMIMETypeHandler(java.lang.String)
120      */
121     public final MIMETypeHandler getMIMETypeHandler( final String type ) {
122         // Do we have a command to handle this type
123         final CommandInfo command = COMMANDS.getCommand( type, "vfs-mime-handler" );
124         if ( command == null ) {
125             // We don't have a command for this type
126             return null;
127         }
128         // Yes we do have a command for this type
129         // Try to make a new instance of the command
130         try {
131             final Class c = Class.forName( command.getCommandClass() );
132             return (MIMETypeHandler) c.newInstance();
133         } catch ( ClassCastException ex ) {
134             throw new ClassCastAlert().setParentThrowable( ex ).mishap();
135         } catch ( ClassNotFoundException ex ) {
136             throw new ClassNotFoundAlert().culpritClass( command.getCommandClass() ).setParentThrowable( ex ).mishap();
137         } catch ( InstantiationException ex ) {
138             throw new InstantiationAlert().culpritClass( command.getCommandClass() ).setParentThrowable( ex ).mishap();
139         } catch ( IllegalAccessException ex ) {
140             throw new IllegalAccessAlert().culpritClass( command.getCommandClass() ).setParentThrowable( ex ).mishap();
141         }
142     }
143 
144     /**
145      * @see org.millscript.commons.vfs.VEntry#getName()
146      */
147     public final String getName() {
148         return this.entryName;
149     }
150 
151     /**
152      * @see org.millscript.commons.vfs.VEntry#getRelativePath()
153      */
154     public final String getRelativePath() {
155         return this.appendRelativePath( new StringBuffer() ).toString();
156     }
157 
158     /**
159      * @see org.millscript.commons.vfs.VEntry#getRelativePathOnVolume()
160      */
161     public final String getRelativePathOnVolume() {
162         return this.appendRelativePathOnVolume( new StringBuffer() ).toString();
163     }
164 
165     /**
166      * @see org.millscript.commons.vfs.VEntry#getParent()
167      */
168     public final VFolder getParent() {
169         return this.parentFolder;
170     }
171 
172     /**
173      * @see org.millscript.commons.vfs.VEntry#getURI()
174      */
175     public final URI getURI() {
176         try {
177             return new URI( this.appendURI( new StringBuffer() ).toString() );
178         } catch ( URISyntaxException ex ) {
179             throw new Fault(
180                 "Could not make a valid URI from this virtual filesystem entry"
181             ).culprit( "ventry", this ).mishap();
182         }
183     }
184 
185     /**
186      * @see org.millscript.commons.vfs.VEntry#getVolume()
187      */
188     public final V getVolume() {
189         return this.volume;
190     }
191 
192 }