zhao
2021-06-11 98186752629a7bd38965418af84db382d90b9c07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
 
/* ================================================================
 * About NPOI
 * Author: Tony Qu 
 * Author's email: tonyqus (at) gmail.com 
 * Author's Blog: tonyqus.wordpress.com.cn (wp.tonyqus.cn)
 * HomePage: http://www.codeplex.com/npoi
 * Contributors:
 * 
 * ==============================================================*/
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
 
 
using HH.WMS.Utils.NPOI.POIFS.Properties;
using HH.WMS.Utils.NPOI.POIFS.Dev;
using HH.WMS.Utils.NPOI.POIFS.FileSystem;
using HH.WMS.Utils.NPOI.POIFS.EventFileSystem;
using HH.WMS.Utils.NPOI.Util;
 
namespace HH.WMS.Utils.NPOI.POIFS.FileSystem
{
    /// <summary>
    /// Simple implementation of DirectoryEntry
    /// @author Marc Johnson (mjohnson at apache dot org)
    /// </summary>
    [Serializable]
    public class DirectoryNode : EntryNode, DirectoryEntry, POIFSViewable, IEnumerable<Entry>
    {
        // Map of Entry instances, keyed by their names
        private Dictionary<string, Entry> _byname;
 
        private List<Entry> _entries;
 
        // the POIFSFileSystem we belong to
        private POIFSFileSystem _oFilesSystem;
 
        private NPOIFSFileSystem _nFilesSystem;
 
        // the path described by this document
        private POIFSDocumentPath _path;
 
 
        public DirectoryNode(DirectoryProperty property,
                        POIFSFileSystem fileSystem,
                        DirectoryNode parent)
            : this(property, parent, fileSystem, (NPOIFSFileSystem)null)
        {
        }
 
        /// <summary>
        /// Create a DirectoryNode. This method Is not public by design; it
        /// Is intended strictly for the internal use of this package
        /// </summary>
        /// <param name="property">the DirectoryProperty for this DirectoryEntry</param>
        /// <param name="nFileSystem">the POIFSFileSystem we belong to</param>
        /// <param name="parent">the parent of this entry</param>
        public DirectoryNode(DirectoryProperty property,
                NPOIFSFileSystem nFileSystem,
                DirectoryNode parent)
            : this(property, parent, (POIFSFileSystem)null, nFileSystem)
        {
        }
 
        private DirectoryNode(DirectoryProperty property,
                        DirectoryNode parent,
                        POIFSFileSystem oFileSystem,
                        NPOIFSFileSystem nFileSystem)
            : base(property, parent)
        {
            this._oFilesSystem = oFileSystem;
            this._nFilesSystem = nFileSystem;
 
            if (parent == null)
                _path = new POIFSDocumentPath();
            else
            {
                _path = new POIFSDocumentPath(parent._path, new string[] { property.Name });
            }
 
            _byname = new Dictionary<string, Entry>();
            _entries = new List<Entry>();
            IEnumerator<Property> iter = property.Children;
 
            while (iter.MoveNext())
            {
                Property child = iter.Current;
                Entry childNode = null;
 
                if (child.IsDirectory)
                {
                    DirectoryProperty childDir = (DirectoryProperty)child;
                    if (_oFilesSystem != null)
                    {
                        childNode = new DirectoryNode(childDir, _oFilesSystem, this);
                    }
                    else
                    {
                        childNode = new DirectoryNode(childDir, _nFilesSystem, this);
                    }
                }
                else
                {
                    childNode = new DocumentNode((DocumentProperty)child, this);
                }
                _entries.Add(childNode);
                _byname.Add(childNode.Name, childNode);
            }
        }
        
        /// <summary>
        /// open a document in the directory's entry's list of entries
        /// </summary>
        /// <param name="documentName">the name of the document to be opened</param>
        /// <returns>a newly opened DocumentStream</returns>
        public DocumentInputStream CreatePOIFSDocumentReader(
                String documentName)
        {
            Entry document = GetEntry(documentName);
 
            if (!document.IsDocumentEntry)
            {
                throw new IOException("Entry '" + documentName
                                      + "' Is not a DocumentEntry");
            }
            return new DocumentInputStream((DocumentEntry)document);
        }
 
        /// <summary>
        /// Create a new DocumentEntry; the data will be provided later
        /// </summary>
        /// <param name="name">the name of the new DocumentEntry</param>
        /// <param name="size">the size of the new DocumentEntry</param>
        /// <returns>the new DocumentEntry</returns>
        public DocumentEntry CreateDocument(POIFSDocument document)
        {
            DocumentProperty property = document.DocumentProperty;
            DocumentNode     rval     = new DocumentNode(property, this);
 
            ((DirectoryProperty)Property).AddChild(property);
            _oFilesSystem.AddDocument(document);
 
            _entries.Add(rval);
            _byname.Add(property.Name, rval);
 
            return rval;
        }
 
        /// <summary>
        /// Change a contained Entry's name
        /// </summary>
        /// <param name="oldName">the original name</param>
        /// <param name="newName">the new name</param>
        /// <returns>true if the operation succeeded, else false</returns>
        public bool ChangeName(String oldName, String newName)
        {
            bool   rval  = false;
            EntryNode child = (EntryNode)_byname[oldName];
 
            if (child != null)
            {
                rval = ((DirectoryProperty)Property)
                    .ChangeName(child.Property, newName);
                if (rval)
                {
                    _byname.Remove(oldName);
                    _byname[child.Property.Name] = child;
                }
            }
            return rval;
        }
 
        /// <summary>
        /// Deletes the entry.
        /// </summary>
        /// <param name="entry">the EntryNode to be Deleted</param>
        /// <returns>true if the entry was Deleted, else false</returns>
        public bool DeleteEntry(EntryNode entry)
        {
            bool rval =
                ((DirectoryProperty)Property)
                    .DeleteChild(entry.Property);
 
            if (rval)
            {
                _entries.Remove(entry);
                _byname.Remove(entry.Name);
 
                if (_oFilesSystem != null)
                {
                    _oFilesSystem.Remove(entry);
                }
                else
                {
                    _nFilesSystem.Remove(entry);
                }
            }
            return rval;
        }
 
        /// <summary>
        /// Gets the path.
        /// </summary>
        /// <value>this directory's path representation</value>
        public POIFSDocumentPath Path
        {
            get { return _path; }
        }
 
        public POIFSFileSystem FileSystem
        {
            get { return _oFilesSystem; }
        }
 
        public NPOIFSFileSystem NFileSystem
        {
            get { return _nFilesSystem; }
        }
 
       /// <summary>
        /// get an iterator of the Entry instances contained directly in
        /// this instance (in other words, children only; no grandchildren
        /// etc.)
        /// </summary>
        /// <value>
        /// The entries.never null, but hasNext() may return false
        /// immediately (i.e., this DirectoryEntry is empty). All
        /// objects retrieved by next() are guaranteed to be
        /// implementations of Entry.
        /// </value>
        public IEnumerator<Entry> Entries
        {
            get { return _entries.GetEnumerator(); }
        }
        internal Entry GetEntry(int index)
        {
            return _entries[index];
        }
        /// <summary>
        /// is this DirectoryEntry empty?
        /// </summary>
        /// <value>
        ///     <c>true</c> if this instance contains no Entry instances; otherwise, <c>false</c>.
        /// </value>
        public bool IsEmpty
        {
            get { return _entries.Count == 0; }
        }
 
        /// <summary>
        /// find out how many Entry instances are contained directly within
        /// this DirectoryEntry
        /// </summary>
        /// <value>
        /// number of immediately (no grandchildren etc.) contained
        /// Entry instances
        /// </value>
        public int EntryCount
        {
            get { return _entries.Count; }
        }
 
 
        public bool HasEntry(String name)
        {
            return name != null && _byname.ContainsKey(name);
        }
 
        /// <summary>
        /// get a specified Entry by name
        /// </summary>
        /// <param name="name">the name of the Entry to obtain.</param>
        /// <returns>
        /// the specified Entry, if it is directly contained in
        /// this DirectoryEntry
        /// </returns>
        public Entry GetEntry(String name)
        {
            Entry rval = null;
 
            if (name != null)
            {
                try
                {
                    rval = (Entry)_byname[name];
                }
                catch (KeyNotFoundException)
                {
                    throw new FileNotFoundException("no such entry: \"" + name + "\"");
                }
            }
            if (rval == null)
            {
 
                // either a null name was given, or there Is no such name
                throw new FileNotFoundException("no such entry: \"" + name + "\"");
            }
            return rval;
        }
 
 
        //public DocumentReader CreateDocumentReader(string documentName)
        //{
        //    try
        //    {
        //        return CreateDocumentReader(GetEntry(documentName));
        //    }
        //    catch(IOException ex)
        //    {
        //        throw ex;
        //    }
        //}
 
        //public DocumentReader CreateDocumentReader(Entry document)
        //{
        //    if (!document.IsDirectoryEntry)
        //    {
        //        throw new IOException("Entry '" + document.Name + "' is not a DocumentEntry");
        //    }
 
        //    DocumentEntry entry = (DocumentEntry)document;
        //    return new DocumentReader(entry);
        //}
 
        public DocumentInputStream CreateDocumentInputStream(Entry document)
        {
            if (!document.IsDocumentEntry)
            {
                throw new IOException("Entry '" + document.Name
                                    + "' is not a DocumentEntry");
            }
 
            DocumentEntry entry = (DocumentEntry)document;
            return new DocumentInputStream(entry);
        }
 
        public DocumentInputStream CreateDocumentInputStream(string documentName)
        {
            return CreateDocumentInputStream(GetEntry(documentName));
        }
 
 
        public DocumentEntry CreateDocument(NPOIFSDocument document)
        {
            try
            {
                DocumentProperty property = document.DocumentProperty;
                DocumentNode rval = new DocumentNode(property, this);
 
                ((DirectoryProperty)Property).AddChild(property);
 
                _nFilesSystem.AddDocument(document);
 
                _entries.Add(rval);
                _byname[property.Name] = rval;
 
                return rval;
 
            }
            catch (IOException ex)
        {
                throw ex;
            }
        }
 
 
        /// <summary>
        /// Create a new DirectoryEntry
        /// </summary>
        /// <param name="name">the name of the new DirectoryEntry</param>
        /// <returns>the name of the new DirectoryEntry</returns>
        public DirectoryEntry CreateDirectory(String name)
        {
            DirectoryProperty property = new DirectoryProperty(name);
            DirectoryNode rval;
 
            if (_oFilesSystem != null)
            {
                rval = new DirectoryNode(property, _oFilesSystem, this);
                _oFilesSystem.AddDirectory(property);
            }
            else
            {
                rval = new DirectoryNode(property, _nFilesSystem, this);
                _nFilesSystem.AddDirectory(property);
            }
 
            ((DirectoryProperty)Property).AddChild(property);
            _entries.Add(rval);
            _byname[name] = rval;
 
            return rval;
        }
 
 
        /// <summary>
        /// Gets or Sets the storage clsid for the directory entry
        /// </summary>
        /// <value>The storage ClassID.</value>
        public ClassID StorageClsid
        {
            set{
                this.Property.StorageClsid=value;
            }
            get
            {
                return this.Property.StorageClsid;
            }
        }
 
        /// <summary>
        /// Is this a DirectoryEntry?
        /// </summary>
        /// <value>true if the Entry Is a DirectoryEntry, else false</value>
        public override bool IsDirectoryEntry
        {
            get { return true; }
        }
 
        /// <summary>
        /// extensions use this method to verify internal rules regarding
        /// deletion of the underlying store.
        /// </summary>
        /// <value> true if it's ok to Delete the underlying store, else
        /// false</value>
        protected override bool IsDeleteOK
        {
            // if this directory Is empty, we can Delete it
            get
            {
                return IsEmpty;
            }
        }
 
        public DocumentEntry CreateDocument(string name, Stream stream)
        {
            try
            {
                if (_nFilesSystem != null)
                {
                    return CreateDocument(new NPOIFSDocument(name, _nFilesSystem, stream));
                }
                else
                {
                    return CreateDocument(new POIFSDocument(name, stream));
                }
            }
            catch (IOException ex)
            {
                throw ex;
            }
        }
 
 
        public DocumentEntry CreateDocument(string name, int size, POIFSWriterListener writer)
        {
           // return CreateDocument(name, size, write);
            return CreateDocument(new POIFSDocument(name, size, _path, writer));
        }
 
 
        #region ViewableItertor interface
        /// <summary>
        /// Get an array of objects, some of which may implement POIFSViewable
        /// </summary>
        /// <value>an array of Object; may not be null, but may be empty</value>
        public Array ViewableArray
        {
            get
            {
                return new Object[0];
            }
        }
 
        /// <summary>
        /// Get an Iterator of objects, some of which may implement
        /// POIFSViewable
        /// </summary>
        /// <value>an Iterator; may not be null, but may have an empty
        /// back end store</value>
        public IEnumerator ViewableIterator
        {
            get
                {
                ArrayList components = new ArrayList();
 
                components.Add(Property);
                components.AddRange(this._entries);
                //components.Sort();
                return components.GetEnumerator();
            }
        }
 
        /// <summary>
        /// Give viewers a hint as to whether to call GetViewableArray or
        /// GetViewableIterator
        /// </summary>
        /// <value><c>true</c> if a viewer should call GetViewableArray; otherwise, <c>false</c>if
        /// a viewer should call GetViewableIterator</value>
        public bool PreferArray
        {
            get { return false; }
        }
 
        /// <summary>
        /// Provides a short description of the object, to be used when a
        /// POIFSViewable object has not provided its contents.
        /// </summary>
        /// <value>The short description.</value>
        public String ShortDescription
        {
            get { return Name; }
        }
        
        #endregion
 
 
 
        #region IEnumerable<Entry> Members
 
        public IEnumerator<Entry> GetEnumerator()
        {
            throw new NotImplementedException();
        }
 
        #endregion
 
        #region IEnumerable Members
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
 
        #endregion
 
 
 
 
        public  bool CanRead
        {
            get { throw new System.NotImplementedException(); }
        }
 
        public  bool CanSeek
        {
            get { throw new System.NotImplementedException(); }
        }
 
        public  bool CanWrite
        {
            get { throw new System.NotImplementedException(); }
        }
 
        public  void Flush()
        {
            throw new System.NotImplementedException();
        }
 
        public  long Length
        {
            get { throw new System.NotImplementedException(); }
        }
 
        public  long Position
        {
            get
            {
                throw new System.NotImplementedException();
    }
            set
            {
                throw new System.NotImplementedException();
            }
        }
 
        public  int Read(byte[] buffer, int offset, int count)
        {
            throw new System.NotImplementedException();
        }
 
        public  long Seek(long offset, SeekOrigin origin)
        {
            throw new System.NotImplementedException();
        }
 
        public  void SetLength(long value)
        {
            throw new System.NotImplementedException();
        }
 
 
 
 
    }
 
 
}