Example #1
0
        /**
         * Performs the check described in {@link #TestReCreate()} for a single
         * POI filesystem.
         *
         * @param f the POI filesystem to check
         */
        private void TestRecreate(FileInfo f)
        {
            Console.WriteLine("Recreating file \"" + f.Name + "\"");

            /* Read the POI filesystem's property Set streams: */
            POIFile[] psf1 = Util.ReadPropertySets(_samples.GetFile(f.Name));

            /* Create a new POI filesystem containing the origin file's
             * property Set streams: */
            FileInfo   fi   = new FileInfo(f.Name);
            FileStream copy = File.Create(fi.Name);
            //copy.deleteOnExit();
            FileStream      out1  = copy;
            POIFSFileSystem poiFs = new POIFSFileSystem();

            for (int i = 0; i < psf1.Length; i++)
            {
                Stream in1 =
                    new ByteArrayInputStream(psf1[i].GetBytes());
                PropertySet        psIn     = PropertySetFactory.Create(in1);
                MutablePropertySet psOut    = new MutablePropertySet(psIn);
                MemoryStream       psStream =
                    new MemoryStream();
                psOut.Write(psStream);
                psStream.Close();
                byte[] streamData = psStream.ToArray();
                poiFs.CreateDocument(new ByteArrayInputStream(streamData),
                                     psf1[i].GetName());
                poiFs.WriteFileSystem(out1);
            }

            /* Read the property Set streams from the POI filesystem just
             * Created. */
            POIFile[] psf2 = Util.ReadPropertySets(copy);
            for (int i = 0; i < psf2.Length; i++)
            {
                byte[]      bytes1 = psf1[i].GetBytes();
                byte[]      bytes2 = psf2[i].GetBytes();
                Stream      in1    = new ByteArrayInputStream(bytes1);
                Stream      in2    = new ByteArrayInputStream(bytes2);
                PropertySet ps1    = PropertySetFactory.Create(in1);
                PropertySet ps2    = PropertySetFactory.Create(in2);

                /* Compare the property Set stream with the corresponding one
                 * from the origin file and check whether they are equal. */

                Assert.AreEqual(ps1, ps2, "Equality for file " + f.Name);
            }
            out1.Close();
        }
Example #2
0
        public void TestWithoutAFormatID()
        {
            FileInfo fi = TempFile.CreateTempFile(POI_FS, ".doc");

            using (FileStream file = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite))
            {
                //FileStream filename = File.OpenRead(dataDir + POI_FS);
                //filename.deleteOnExit();

                /* Create a mutable property Set with a section that does not have the
                 * formatID Set: */
                FileStream         out1  = file;
                POIFSFileSystem    poiFs = new POIFSFileSystem();
                MutablePropertySet ps    = new MutablePropertySet();
                ps.ClearSections();
                ps.AddSection(new MutableSection());

                /* Write it to a POIFS and the latter to disk: */
                try
                {
                    MemoryStream psStream = new MemoryStream();
                    ps.Write(psStream);
                    psStream.Close();
                    byte[] streamData = psStream.ToArray();
                    poiFs.CreateDocument(new MemoryStream(streamData),
                                         SummaryInformation.DEFAULT_STREAM_NAME);
                    poiFs.WriteFileSystem(out1);
                    out1.Close();
                    Assert.Fail("Should have thrown a NoFormatIDException.");
                }
                catch (Exception ex)
                {
                    Assert.IsTrue(ex is NoFormatIDException);
                }
                finally
                {
                    poiFs.Close();
                    out1.Close();
                }
            }
            try
            {
                File.Delete(fi.FullName);
            }
            catch
            {
            }
        }
Example #3
0
        public void TestUnicodeWrite8Bit()
        {
            String             TITLE = "This is a sample title";
            MutablePropertySet mps   = new MutablePropertySet();
            MutableSection     ms    = (MutableSection)mps.Sections[0];

            ms.SetFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
            MutableProperty p = new MutableProperty();

            p.ID    = PropertyIDMap.PID_TITLE;
            p.Type  = Variant.VT_LPSTR;
            p.Value = TITLE;
            ms.SetProperty(p);

            Exception t = null;

            try
            {
                MemoryStream out1 = new MemoryStream();
                mps.Write(out1);
                out1.Close();
                byte[] bytes = out1.ToArray();

                PropertySet psr = new PropertySet(bytes);
                Assert.IsTrue(psr.IsSummaryInformation);
                Section sr    = (Section)psr.Sections[0];
                String  title = (String)sr.GetProperty(PropertyIDMap.PID_TITLE);
                Assert.AreEqual(TITLE, title);
            }
            catch (WritingNotSupportedException e)
            {
                t = e;
            }
            catch (IOException e)
            {
                t = e;
            }
            catch (NoPropertySetStreamException e)
            {
                t = e;
            }
            if (t != null)
            {
                Assert.Fail(t.Message);
            }
        }
Example #4
0
        /// <summary>
        /// Writes out a given ProperySet
        /// </summary>
        /// <param name="name">the (POIFS Level) name of the property to Write.</param>
        /// <param name="Set">the PropertySet to Write out.</param>
        /// <param name="outFS">the POIFSFileSystem to Write the property into.</param>
        protected void WritePropertySet(String name, PropertySet Set, POIFSFileSystem outFS)
        {
            try
            {
                MutablePropertySet mSet = new MutablePropertySet(Set);
                MemoryStream       bOut = new MemoryStream();

                mSet.Write(bOut);
                byte[]       data = bOut.ToArray();
                MemoryStream bIn  = new MemoryStream(data);
                outFS.CreateDocument(bIn, name);

                //logger.Log(POILogger.INFO, "Wrote property Set " + name + " of size " + data.Length);
            }
            catch (WritingNotSupportedException)
            {
                Console.Error.WriteLine("Couldn't Write property Set with name " + name + " as not supported by HPSF yet");
            }
        }
Example #5
0
        public void TestWriteEmptyPropertySet()
        {
            FileInfo fi = TempFile.CreateTempFile(POI_FS, ".doc");

            using (FileStream file = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite))
            {
                //filename.deleteOnExit();

                /* Create a mutable property Set and Write it to a POIFS: */
                FileStream         out1  = file;
                POIFSFileSystem    poiFs = new POIFSFileSystem();
                MutablePropertySet ps    = new MutablePropertySet();
                MutableSection     s     = (MutableSection)ps.Sections[0];
                s.SetFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);

                MemoryStream psStream = new MemoryStream();
                ps.Write(psStream);
                psStream.Close();
                byte[] streamData = psStream.ToArray();
                poiFs.CreateDocument(new MemoryStream(streamData),
                                     SummaryInformation.DEFAULT_STREAM_NAME);
                poiFs.WriteFileSystem(out1);
                //out1.Close();
                file.Position = 0;
                /* Read the POIFS: */
                POIFSReader reader3 = new POIFSReader();
                reader3.StreamReaded += new POIFSReaderEventHandler(reader3_StreamReaded);
                reader3.Read(file);

                poiFs.Close();
                file.Close();
                //File.Delete(dataDir + POI_FS);
            }
            try
            {
                File.Delete(fi.FullName);
            }
            catch
            {
            }
        }
Example #6
0
 /// <summary>
 /// Writes out a given ProperySet
 /// </summary>
 /// <param name="name">the (POIFS Level) name of the property to Write.</param>
 /// <param name="Set">the PropertySet to Write out.</param>
 /// <param name="outFS">the POIFSFileSystem to Write the property into.</param>
 protected void WritePropertySet(String name, PropertySet Set, NPOIFSFileSystem outFS)
 {
     try
     {
         MutablePropertySet mSet = new MutablePropertySet(Set);
         using (MemoryStream bOut = new MemoryStream())
         {
             mSet.Write(bOut);
             byte[] data = bOut.ToArray();
             using (MemoryStream bIn = new MemoryStream(data))
             {
                 // Create or Update the Property Set stream in the POIFS
                 outFS.CreateOrUpdateDocument(bIn, name);
             }
             //logger.Log(POILogger.INFO, "Wrote property Set " + name + " of size " + data.Length);
         }
     }
     catch (WritingNotSupportedException)
     {
         //logger.log(POILogger.ERROR, "Couldn't Write property Set with name " + name + " as not supported by HPSF yet");
     }
 }
Example #7
0
 /// <summary>
 /// Writes out a given ProperySet
 /// </summary>
 /// <param name="name">the (POIFS Level) name of the property to Write.</param>
 /// <param name="Set">the PropertySet to Write out.</param>
 /// <param name="outFS">the POIFSFileSystem to Write the property into.</param>
 protected void WritePropertySet(String name, PropertySet Set, POIFSFileSystem outFS)
 {
     try
     {
         MutablePropertySet mSet = new MutablePropertySet(Set);
         using (MemoryStream bOut = new MemoryStream())
         {
             mSet.Write(bOut);
             byte[] data = bOut.ToArray();
             using (MemoryStream bIn = new MemoryStream(data))
             {
                 outFS.CreateDocument(bIn, name);
             }
             //logger.Log(POILogger.INFO, "Wrote property Set " + name + " of size " + data.Length);
         }
     }
     catch (WritingNotSupportedException)
     {
         Console.Error.WriteLine("Couldn't Write property Set with name " + name + " as not supported by HPSF yet");
     }
 }