Ejemplo n.º 1
0
        /**
         * Set the contents of a document, creating if needed,
         *  otherwise updating. Returns the created / updated DocumentEntry
         *
         * @param name the name of the new or existing DocumentEntry
         * @param stream the InputStream from which to populate the DocumentEntry
         *
         * @return the new or updated DocumentEntry
         *
         * @exception IOException
         */
        public DocumentEntry CreateOrUpdateDocument(String name,
                                                    Stream stream)

        {
            if (!HasEntry(name))
            {
                return(CreateDocument(name, stream));
            }
            else
            {
                DocumentNode existing = (DocumentNode)GetEntry(name);
                if (_nFilesSystem != null)
                {
                    NPOIFSDocument nDoc = new NPOIFSDocument(existing);
                    nDoc.ReplaceContents(stream);
                    return(existing);
                }
                else
                {
                    // Do it the hard way for Old POIFS...
                    DeleteEntry(existing);
                    return(CreateDocument(name, stream));
                }
            }
        }
Ejemplo n.º 2
0
        /**
         * remove an entry
         *
         * @param entry to be Removed
         */

        public void Remove(EntryNode entry)
        {
            // If it's a document, free the blocks
            if (entry is DocumentEntry)
            {
                NPOIFSDocument doc = new NPOIFSDocument((DocumentProperty)entry.Property, this);
                doc.Free();
            }
            _property_table.RemoveProperty(entry.Property);
        }
Ejemplo n.º 3
0
 /**
  * Create an InputStream from the specified Document
  *
  * @param document the Document to be read
  */
 public NDocumentInputStream(NPOIFSDocument document)
 {
     _current_offset      = 0;
     _current_block_count = 0;
     _marked_offset       = 0;
     _marked_offset_count = 0;
     _document_size       = document.Size;
     _closed   = false;
     _document = document;
     _data     = _document.GetBlockIterator();
 }
Ejemplo n.º 4
0
        /**
         * Create an OutputStream from the specified DocumentEntry.
         * The specified entry will be emptied.
         *
         * @param document the DocumentEntry to be written
         */
        public NDocumentOutputStream(DocumentEntry document)
        {
            if (!(document is DocumentNode))
            {
                throw new IOException("Cannot open internal document storage, " + document + " not a Document Node");
            }
            _document_size = 0;
            _closed        = false;

            _property = (DocumentProperty)((DocumentNode)document).Property;

            _document = new NPOIFSDocument((DocumentNode)document);
            _document.Free();
        }
Ejemplo n.º 5
0
        /**
         * Create an OutputStream from the specified DocumentEntry.
         * The specified entry will be emptied.
         * 
         * @param document the DocumentEntry to be written
         */
        public NDocumentOutputStream(DocumentEntry document)
        {
            if (!(document is DocumentNode))
            {
                throw new IOException("Cannot open internal document storage, " + document + " not a Document Node");
            }
            _document_size = 0;
            _closed = false;

            _property = (DocumentProperty)((DocumentNode)document).Property;

            _document = new NPOIFSDocument((DocumentNode)document);
            _document.Free();
        }
Ejemplo n.º 6
0
        /**
         * Create an OutputStream to create the specified new Entry
         *
         * @param parent Where to create the Entry
         * @param name Name of the new entry
         */
        public NDocumentOutputStream(DirectoryEntry parent, String name)
        {
            if (!(parent is DirectoryNode))
            {
                throw new IOException("Cannot open internal directory storage, " + parent + " not a Directory Node");
            }
            _document_size = 0;
            _closed        = false;

            // Have an empty one Created for now
            DocumentEntry doc = parent.CreateDocument(name, new MemoryStream(new byte[0]));

            _property = (DocumentProperty)((DocumentNode)doc).Property;
            _document = new NPOIFSDocument((DocumentNode)doc);
        }
Ejemplo n.º 7
0
        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;
            }
        }
Ejemplo n.º 8
0
        /**
         * Create an InputStream from the specified DocumentEntry
         *
         * @param document the DocumentEntry to be read
         *
         * @exception IOException if the DocumentEntry cannot be opened (like, maybe it has
         *                been deleted?)
         */
        public NDocumentInputStream(DocumentEntry document)
        {
            if (!(document is DocumentNode))
            {
                throw new IOException("Cannot open internal document storage, " + document + " not a Document Node");
            }
            _current_offset      = 0;
            _current_block_count = 0;
            _marked_offset       = 0;
            _marked_offset_count = 0;
            _document_size       = document.Size;
            _closed = false;

            DocumentNode     doc      = (DocumentNode)document;
            DocumentProperty property = (DocumentProperty)doc.Property;

            _document = new NPOIFSDocument(
                property,
                ((DirectoryNode)doc.Parent).NFileSystem
                );
            _data = _document.GetBlockIterator();
        }
Ejemplo n.º 9
0
        /**
         * Create an OutputStream to create the specified new Entry
         * 
         * @param parent Where to create the Entry
         * @param name Name of the new entry
         */
        public NDocumentOutputStream(DirectoryEntry parent, String name)
        {
            if (!(parent is DirectoryNode))
            {
                throw new IOException("Cannot open internal directory storage, " + parent + " not a Directory Node");
            }
            _document_size = 0;
            _closed = false;

            // Have an empty one Created for now
            DocumentEntry doc = parent.CreateDocument(name, new MemoryStream(new byte[0]));
            _property = (DocumentProperty)((DocumentNode)doc).Property;
            _document = new NPOIFSDocument((DocumentNode)doc);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DocumentInputStream"/> class.
 /// Create an <see cref="InputStream"/> from the specified DocumentEntry
 /// </summary>
 /// <param name="document">the DocumentEntry to be read</param>
 public DocumentInputStream(NPOIFSDocument document)
 {
     delegate1 = new NDocumentInputStream(document);
 }
Ejemplo n.º 11
0
 /**
  * add a new POIFSDocument to the FileSytem
  *
  * @param document the POIFSDocument being Added
  */
 public void AddDocument(NPOIFSDocument document)
 {
     _property_table.AddProperty(document.DocumentProperty);
 }
Ejemplo n.º 12
0
        /**
         * Create an InputStream from the specified DocumentEntry
         * 
         * @param document the DocumentEntry to be read
         * 
         * @exception IOException if the DocumentEntry cannot be opened (like, maybe it has
         *                been deleted?)
         */
        public NDocumentInputStream(DocumentEntry document)
        {
            if (!(document is DocumentNode))
            {
                throw new IOException("Cannot open internal document storage, " + document + " not a Document Node");
            }
            _current_offset = 0;
            _current_block_count = 0;
            _marked_offset = 0;
            _marked_offset_count = 0;
            _document_size = document.Size;
            _closed = false;

            DocumentNode doc = (DocumentNode)document;
            DocumentProperty property = (DocumentProperty)doc.Property;
            _document = new NPOIFSDocument(
                  property,
                      ((DirectoryNode)doc.Parent).NFileSystem
            );
            _data = _document.GetBlockIterator();
        }
Ejemplo n.º 13
0
 /**
  * Create an InputStream from the specified Document
  * 
  * @param document the Document to be read
  */
 public NDocumentInputStream(NPOIFSDocument document)
 {
     _current_offset = 0;
     _current_block_count = 0;
     _marked_offset = 0;
     _marked_offset_count = 0;
     _document_size = document.Size;
     _closed = false;
     _document = document;
     _data = _document.GetBlockIterator();
 }
Ejemplo n.º 14
0
        public void ReadWriteRead()
        {
            SummaryInformation sinf = null;
            DocumentSummaryInformation dinf = null;
            DirectoryEntry root = null, testDir = null;
            NPOIFSFileSystem[] testFS = get512and4kFileAndInput();
            for (int i=0;i<testFS.Length;i++)
            {
                NPOIFSFileSystem fs = testFS[i];
                // Check we can find the entries we expect
                root = fs.Root;
                Assert.AreEqual(5, root.EntryCount);
                
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Tags"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));


                // Write out1, re-load
                fs = WriteOutAndReadBack(fs);

                // Check they're still there
                root = fs.Root;
                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Tags"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));


                // Check the contents of them - parse the summary block and check
                sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, sinf.OSVersion);

                dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, dinf.OSVersion);


                // Add a test mini stream
                testDir = root.CreateDirectory("Testing 123");
                testDir.CreateDirectory("Testing 456");
                testDir.CreateDirectory("Testing 789");
                byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
                testDir.CreateDocument("Mini", new MemoryStream(mini));


                // Write out1, re-load
                fs = WriteOutAndReadBack(fs);
                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");
                Assert.AreEqual(6, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Tags"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));


                // Check old and new are there
                sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, sinf.OSVersion);

                dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, dinf.OSVersion);

                assertContentsMatches(mini, (DocumentEntry)testDir.GetEntry("Mini"));


                // Write out and read once more, just to be sure
                fs = WriteOutAndReadBack(fs);

                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");
                Assert.AreEqual(6, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Tags"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));

                sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, sinf.OSVersion);

                dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, dinf.OSVersion);
                assertContentsMatches(mini, (DocumentEntry)testDir.GetEntry("Mini"));

                byte[] main4096 = new byte[4096];
                main4096[0] = unchecked((byte)-10);
                main4096[4095] = unchecked((byte)-11);
                testDir.CreateDocument("Normal4096", new MemoryStream(main4096));

                root.GetEntry("Tags").Delete();


                // Write out1, re-load
                fs = WriteOutAndReadBack(fs);

                // Check it's all there
                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");
                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));


                // Check old and new are there
                sinf = (SummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(SummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, sinf.OSVersion);

                dinf = (DocumentSummaryInformation)PropertySetFactory.Create(new NDocumentInputStream(
                        (DocumentEntry)root.GetEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME)));
                Assert.AreEqual(131333, dinf.OSVersion);

                assertContentsMatches(mini, (DocumentEntry)testDir.GetEntry("Mini"));
                assertContentsMatches(main4096, (DocumentEntry)testDir.GetEntry("Normal4096"));


                // Delete a directory, and add one more
                testDir.GetEntry("Testing 456").Delete();
                testDir.CreateDirectory("Testing ABC");


                // Save
                fs = WriteOutAndReadBack(fs);

                // Check
                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");

                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));

                Assert.AreEqual(4, testDir.EntryCount);
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Normal4096"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing 789"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing ABC"));


                // Add another mini stream
                byte[] mini2 = new byte[] { unchecked((byte)-42), 0, unchecked((byte)-1), 
                    unchecked((byte)-2), unchecked((byte)-3), unchecked((byte)-4), unchecked((byte)-42) };
                testDir.CreateDocument("Mini2", new MemoryStream(mini2));

                // Save, load, check
                fs = WriteOutAndReadBack(fs);

                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");

                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));

                Assert.AreEqual(5, testDir.EntryCount);
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini2"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Normal4096"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing 789"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing ABC"));

                assertContentsMatches(mini, (DocumentEntry)testDir.GetEntry("Mini"));
                assertContentsMatches(mini2, (DocumentEntry)testDir.GetEntry("Mini2"));
                assertContentsMatches(main4096, (DocumentEntry)testDir.GetEntry("Normal4096"));


                // Delete a mini stream, add one more
                testDir.GetEntry("Mini").Delete();

                byte[] mini3 = new byte[] { 42, 0, 42, 0, 42, 0, 42 };
                testDir.CreateDocument("Mini3", new MemoryStream(mini3));


                // Save, load, check
                fs = WriteOutAndReadBack(fs);

                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");

                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));

                Assert.AreEqual(5, testDir.EntryCount);
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini2"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini3"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Normal4096"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing 789"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing ABC"));

                assertContentsMatches(mini2, (DocumentEntry)testDir.GetEntry("Mini2"));
                assertContentsMatches(mini3, (DocumentEntry)testDir.GetEntry("Mini3"));
                assertContentsMatches(main4096, (DocumentEntry)testDir.GetEntry("Normal4096"));

                // Change some existing streams
                NPOIFSDocument mini2Doc = new NPOIFSDocument((DocumentNode)testDir.GetEntry("Mini2"));
                mini2Doc.ReplaceContents(new MemoryStream(mini));

                byte[] main4106 = new byte[4106];
                main4106[0] = 41;
                main4106[4105] = 42;
                NPOIFSDocument mainDoc = new NPOIFSDocument((DocumentNode)testDir.GetEntry("Normal4096"));
                mainDoc.ReplaceContents(new MemoryStream(main4106));


                // Re-check
                fs = WriteOutAndReadBack(fs);

                root = fs.Root;
                testDir = (DirectoryEntry)root.GetEntry("Testing 123");

                Assert.AreEqual(5, root.EntryCount);
                Assert.That(root.EntryNames, new ContainsConstraint("Thumbnail"));
                Assert.That(root.EntryNames, new ContainsConstraint("Image"));
                Assert.That(root.EntryNames, new ContainsConstraint("Testing 123"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005DocumentSummaryInformation"));
                Assert.That(root.EntryNames, new ContainsConstraint("\u0005SummaryInformation"));

                Assert.AreEqual(5, testDir.EntryCount);
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini2"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Mini3"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Normal4096"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing 789"));
                Assert.That(testDir.EntryNames, new ContainsConstraint("Testing ABC"));

                assertContentsMatches(mini, (DocumentEntry)testDir.GetEntry("Mini2"));
                assertContentsMatches(mini3, (DocumentEntry)testDir.GetEntry("Mini3"));
                assertContentsMatches(main4106, (DocumentEntry)testDir.GetEntry("Normal4096"));
           


                fs.Close();
            }
        }
Ejemplo n.º 15
0
 /**
  * Create an InputStream from the specified Document
  * 
  * @param document the Document to be read
  */
 public DocumentInputStream(NPOIFSDocument document)
 {
     delegate1 = new NDocumentInputStream(document);
 }
Ejemplo n.º 16
0
 /**
  * add a new POIFSDocument to the FileSytem 
  *
  * @param document the POIFSDocument being Added
  */
 public void AddDocument(NPOIFSDocument document)
 {
     _property_table.AddProperty(document.DocumentProperty);
 }
Ejemplo n.º 17
0
        /**
         * remove an entry
         *
         * @param entry to be Removed
         */

        public void Remove(EntryNode entry)
        {
            // If it's a document, free the blocks
            if (entry is DocumentEntry) {
                NPOIFSDocument doc = new NPOIFSDocument((DocumentProperty)entry.Property, this);
                doc.Free();
            }
            _property_table.RemoveProperty(entry.Property);
        }