View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-Excel: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2006 Open World Ltd, Kevin Rogers
4   //
5   // This file is part of MillScript-Excel.
6   //
7   // MillScript-Excel 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-Excel 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-Excel; 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.office.compound.vfs;
22  
23  import org.millscript.commons.alert.reporters.StandardWarningAlertReporter;
24  import org.millscript.commons.util.MapIterator;
25  import org.millscript.commons.vfs.AbstractVFolder;
26  import org.millscript.commons.vfs.VEntry;
27  import org.millscript.commons.vfs.VFile;
28  import org.millscript.commons.vfs.VFolder;
29  import org.millscript.commons.vfs.alerts.VEntryNotFoundAlert;
30  import org.millscript.commons.vfs.alerts.VFSAlert;
31  import org.millscript.office.compound.DirectoryEntry;
32  import org.millscript.office.compound.DirectoryEntryType;
33  import org.millscript.office.compound.DirectoryMap;
34  
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  /**
39   * 
40   */
41  public class CompoundDocumentFolder extends AbstractVFolder< CompoundDocumentVolume > {
42  
43      private final DirectoryEntry entry;
44  
45      private final DirectoryMap directory;
46  
47      public CompoundDocumentFolder( final CompoundDocumentVolume vol, final VFolder parent, final String name ) {
48          super( vol, parent, name );
49          this.entry = null;
50          this.directory = null;
51      }
52  
53      public CompoundDocumentFolder( final CompoundDocumentFolder parent, final String name ) {
54          this( parent.getVolume(), parent, name );
55      }
56  
57      /**
58       * @param vol
59       * @param parent
60       * @param name
61       */
62      public CompoundDocumentFolder( final CompoundDocumentVolume vol, final VFolder parent, final int did ) {
63          super( vol, parent, vol.getDirectoryEntry( did ).getName() );
64          this.entry = vol.getDirectoryEntry( did );
65          this.directory = new DirectoryMap( vol.getDirectory(), this.entry.getCenter() );
66      }
67  
68      public CompoundDocumentFolder( final CompoundDocumentFolder parent, final int did ) {
69          this( parent.getVolume(), parent, did );
70      }
71  
72      /**
73       * @see org.millscript.commons.vfs.VRootFolder#checkVEntry(java.lang.String)
74       */
75      public VEntry checkVEntry( final String segment ) {
76          final DirectoryEntry queryEntry = this.directory.get( segment );
77          if ( queryEntry == null ) {
78              // It doesn't exist!
79              throw VEntryNotFoundAlert.entryDoesNotExist(
80                  segment
81              ).decorate( this ).mishap();
82          } else {
83              switch ( queryEntry.getType() ) {
84                  case USER_STORAGE:
85                      return new CompoundDocumentFolder( this, queryEntry.getDirectoryID() );
86                  case USER_STREAM:
87                      return new CompoundDocumentFile( this, queryEntry.getDirectoryID() );
88                  default:
89                      // It's not a file or a directory!
90                      throw VEntryNotFoundAlert.entryIsNotAFileOrFolder(
91                          queryEntry.getName()
92                      ).culprit(
93                          "underlying directory entry",
94                          queryEntry
95                      ).mishap();
96              }
97          }
98      }
99  
100     /**
101      * @see org.millscript.commons.vfs.VRootFolder#checkVFile(java.lang.String)
102      */
103     public VFile checkVFile( final String segment ) {
104         final DirectoryEntry queryEntry = this.directory.get( segment );
105         if ( queryEntry == null ) {
106             // It doesn't exist!
107             throw VEntryNotFoundAlert.entryDoesNotExist(
108                 segment
109             ).decorate( this ).mishap();
110         } else if ( queryEntry.getType() == DirectoryEntryType.USER_STREAM ) {
111             return new CompoundDocumentFile( this, queryEntry.getDirectoryID() );
112         } else {
113             // It's not a file or a directory!
114             throw VEntryNotFoundAlert.entryIsNotAFile(
115                 queryEntry.getName()
116             ).culprit(
117                 "underlying directory entry",
118                 queryEntry
119             ).mishap();
120         }
121     }
122 
123     /**
124      * @see org.millscript.commons.vfs.VRootFolder#checkVFolder(java.lang.String)
125      */
126     public VFolder checkVFolder( final String segment ) {
127         final DirectoryEntry queryEntry = this.directory.get( segment );
128         if ( queryEntry == null ) {
129             // It doesn't exist!
130             throw VEntryNotFoundAlert.entryDoesNotExist(
131                 segment
132             ).decorate( this ).mishap();
133         } else if ( queryEntry.getType() == DirectoryEntryType.USER_STORAGE ) {
134             return new CompoundDocumentFolder( this, queryEntry.getDirectoryID() );
135         } else {
136             // It's not a file or a directory!
137             throw VEntryNotFoundAlert.entryIsNotAFolder(
138                 queryEntry.getName()
139             ).culprit(
140                 "underlying directory entry",
141                 queryEntry
142             ).mishap();
143         }
144     }
145 
146     /**
147      * @see org.millscript.commons.vfs.VEntry#exists()
148      */
149     public boolean exists() {
150         // This is sufficient, at least for the moment.
151         return this.entry != null;
152     }
153 
154     /**
155      * @see org.millscript.commons.vfs.VRootFolder#getVFile(java.lang.String)
156      */
157     public VFile getVFile( final String segment ) {
158         final DirectoryEntry queryEntry = this.directory.get( segment );
159         if ( queryEntry != null && queryEntry.getType() == DirectoryEntryType.USER_STREAM ) {
160             return new CompoundDocumentFile( this, queryEntry.getDirectoryID() );
161         } else {
162             return new CompoundDocumentFile( this, segment );
163         }
164     }
165 
166     /**
167      * @see org.millscript.commons.vfs.VRootFolder#getVFolder(java.lang.String)
168      */
169     public VFolder getVFolder( final String segment ) {
170         final DirectoryEntry queryEntry = this.directory.get( segment );
171         if ( queryEntry != null && queryEntry.getType() == DirectoryEntryType.USER_STORAGE ) {
172             return new CompoundDocumentFolder( this, queryEntry.getDirectoryID() );
173         } else {
174             return new CompoundDocumentFolder( this, segment );
175         }
176     }
177 
178     /**
179      * @see org.millscript.commons.vfs.VRootFolder#listEntries()
180      */
181     public List< VEntry > listEntries() {
182         final ArrayList< VEntry > list = new ArrayList< VEntry >();
183         final MapIterator< String, DirectoryEntry > it = this.directory.iterator( true );
184         while ( it.hasNext() ) {
185             final DirectoryEntry itEntry = it.nextValue();
186             switch ( itEntry.getType() ) {
187                 case USER_STORAGE:
188                     list.add( new CompoundDocumentFolder( this, itEntry.getDirectoryID() ) );
189                     break;
190                 case USER_STREAM:
191                     list.add( new CompoundDocumentFile( this, itEntry.getDirectoryID() ) );
192                     break;
193                 default:
194                     // It's not a file or a directory!
195                     // TODO - This should support an application level reporting
196                     // mechanism as soon as it is available via the Alert library
197                     StandardWarningAlertReporter.WARNING_REPORTER.report(
198                         VEntryNotFoundAlert.entryIsNotAFileOrFolder(
199                             itEntry.getName()
200                         ).culprit(
201                             "underlying directory entry",
202                             itEntry
203                         )
204                     );
205             }
206         }
207         return list;
208     }
209 
210     /**
211      * @see org.millscript.commons.vfs.VRootFolder#listFiles()
212      */
213     public List< VFile > listFiles() {
214         final ArrayList< VFile > list = new ArrayList< VFile >();
215         final MapIterator< String, DirectoryEntry > it = this.directory.iterator( true );
216         while ( it.hasNext() ) {
217             final DirectoryEntry itEntry = it.nextValue();
218             switch ( itEntry.getType() ) {
219                 case USER_STREAM:
220                     list.add( new CompoundDocumentFile( this, itEntry.getDirectoryID() ) );
221                     break;
222                 default:
223                     // It's not a file!
224                     // TODO - This should support an application level reporting
225                     // mechanism as soon as it is available via the Alert library
226                     StandardWarningAlertReporter.WARNING_REPORTER.report(
227                         VEntryNotFoundAlert.entryIsNotAFile(
228                             itEntry.getName()
229                         ).culprit(
230                             "underlying directory entry",
231                             itEntry
232                         )
233                     );
234             }
235         }
236         return list;
237     }
238 
239     /**
240      * @see org.millscript.commons.vfs.VRootFolder#listFolders()
241      */
242     public List< VFolder > listFolders() {
243         final ArrayList< VFolder > list = new ArrayList< VFolder >();
244         final MapIterator< String, DirectoryEntry > it = this.directory.iterator( true );
245         while ( it.hasNext() ) {
246             final DirectoryEntry itEntry = it.nextValue();
247             switch ( itEntry.getType() ) {
248                 case USER_STORAGE:
249                     list.add( new CompoundDocumentFolder( this, itEntry.getDirectoryID() ) );
250                     break;
251                 default:
252                     // It's not a file or a directory!
253                     // TODO - This should support an application level reporting
254                     // mechanism as soon as it is available via the Alert library
255                     StandardWarningAlertReporter.WARNING_REPORTER.report(
256                         VEntryNotFoundAlert.entryIsNotAFolder(
257                             itEntry.getName()
258                         ).culprit(
259                             "underlying directory entry",
260                             itEntry
261                         )
262                     );
263             }
264         }
265         return list;
266     }
267 
268     /**
269      * @see org.millscript.commons.vfs.VFolder#make()
270      */
271     public VFolder make() {
272         throw(
273             new VFSAlert(
274                 "Can't make a folder in a read-only compound document"
275             ).decorate( this ).mishap()
276         );
277     }
278 
279 }