示例#1
0
        public void TestWriteEmptyPropertySet()
        {
            FileStream file = _samples.GetFile(POI_FS);
            //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);
            file.Close();
            //File.Delete(dataDir + POI_FS);
        }
示例#2
0
        public void TestDictionaryWithInvalidCodepage()
        {
            try
            {
                using (FileStream copy = File.Create(@".\Test-HPSF.ole2"))
                {
                    /* Write: */
                    POIFSFileSystem    poiFs = new POIFSFileSystem();
                    MutablePropertySet ps1   = new MutablePropertySet();
                    MutableSection     s     = (MutableSection)ps1.Sections[0];
                    Hashtable          m     = new Hashtable(3, 1.0f);
                    m[1]         = "String 1";
                    m[2]         = "String 2";
                    m[3]         = "String 3";
                    s.Dictionary = m;
                    s.SetFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID1);
                    s.SetProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, 12345);
                    poiFs.CreateDocument(ps1.GetStream(), "Test");
                    poiFs.WriteFileSystem(copy);
                    Assert.Fail("This Testcase did not detect the invalid codepage value.");
                }
            }
            catch (IllegalPropertySetDataException)
            {
                //Assert.IsTrue(true);
            }

            if (File.Exists(@".\Test-HPSF.ole2"))
            {
                File.Delete(@".\Test-HPSF.ole2");
            }
        }
示例#3
0
        public void TestDictionaryWithInvalidCodepage()
        {
            try
            {
                FileStream copy = File.Create(testContextInstance.TestDir + @"\Test-HPSF.ole2");
                //copy.deleteOnExit();

                /* Write: */
                FileStream         out1  = copy;
                POIFSFileSystem    poiFs = new POIFSFileSystem();
                MutablePropertySet ps1   = new MutablePropertySet();
                MutableSection     s     = (MutableSection)ps1.Sections[0];
                Hashtable          m     = new Hashtable(3, 1.0f);
                m[1]         = "String 1";
                m[2]         = "String 2";
                m[3]         = "String 3";
                s.Dictionary = (m);
                s.SetFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID1);
                int codepage = 12345;
                s.SetProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
                              codepage);
                poiFs.CreateDocument(ps1.GetStream(), "Test");
                poiFs.WriteFileSystem(out1);
                out1.Close();
                Assert.Fail("This Testcase did not detect the invalid codepage value.");
            }
            catch (IllegalPropertySetDataException)
            {
                //Assert.IsTrue(true);
            }
        }
示例#4
0
        public void TestWriteSimplePropertySet()
        {
            String AUTHOR = "Rainer Klute";
            String TITLE  = "Test Document";

            FileInfo   fi   = TempFile.CreateTempFile(POI_FS, ".doc");
            FileStream file = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite);

            FileStream      out1  = file;
            POIFSFileSystem poiFs = new POIFSFileSystem();

            MutablePropertySet ps = new MutablePropertySet();
            MutableSection     si = new MutableSection();

            si.SetFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
            ps.Sections[0] = si;

            MutableProperty p = new MutableProperty();

            p.ID    = PropertyIDMap.PID_AUTHOR;
            p.Type  = Variant.VT_LPWSTR;
            p.Value = AUTHOR;
            si.SetProperty(p);
            si.SetProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPSTR, TITLE);

            poiFs.CreateDocument(ps.ToInputStream(),
                                 SummaryInformation.DEFAULT_STREAM_NAME);
            poiFs.WriteFileSystem(out1);
            poiFs.Close();
            //out1.Close();
            file.Position = 0;

            POIFSReader reader1 = new POIFSReader();
            //reader1.StreamReaded += new POIFSReaderEventHandler(reader1_StreamReaded);
            POIFSReaderListener1 psl = new POIFSReaderListener1();

            reader1.RegisterListener(psl);
            reader1.Read(file);
            Assert.IsNotNull(psa[0]);
            Assert.IsTrue(psa[0].IsSummaryInformation);

            Section s  = (Section)(psa[0].Sections[0]);
            Object  p1 = s.GetProperty(PropertyIDMap.PID_AUTHOR);
            Object  p2 = s.GetProperty(PropertyIDMap.PID_TITLE);

            Assert.AreEqual(AUTHOR, p1);
            Assert.AreEqual(TITLE, p2);
            file.Close();
            try
            {
                File.Delete(fi.FullName);
            }
            catch
            {
            }
        }
示例#5
0
        public void TestWriteTwoSections()
        {
            String STREAM_NAME = "PropertySetStream";
            String SECTION1    = "Section 1";
            String SECTION2    = "Section 2";

            FileStream file = _samples.GetFile(POI_FS);
            //filename.deleteOnExit();
            FileStream out1 = file;

            POIFSFileSystem    poiFs = new POIFSFileSystem();
            MutablePropertySet ps    = new MutablePropertySet();

            ps.ClearSections();

            ClassID formatID = new ClassID();

            formatID.Bytes = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7,
                                          8, 9, 10, 11, 12, 13, 14, 15 };
            MutableSection s1 = new MutableSection();

            s1.SetFormatID(formatID);
            s1.SetProperty(2, SECTION1);
            ps.AddSection(s1);

            MutableSection s2 = new MutableSection();

            s2.SetFormatID(formatID);
            s2.SetProperty(2, SECTION2);
            ps.AddSection(s2);

            poiFs.CreateDocument(ps.GetStream(), STREAM_NAME);
            poiFs.WriteFileSystem(out1);
            //out1.Close();

            /* Read the POIFS: */
            psa = new PropertySet[1];
            POIFSReader reader2 = new POIFSReader();

            reader2.StreamReaded += new POIFSReaderEventHandler(reader2_StreamReaded);
            reader2.Read(file);
            Assert.IsNotNull(psa[0]);
            Section s = (Section)(psa[0].Sections[0]);

            Assert.AreEqual(s.FormatID, formatID);
            Object p = s.GetProperty(2);

            Assert.AreEqual(SECTION1, p);
            s = (Section)(psa[0].Sections[1]);
            p = s.GetProperty(2);
            Assert.AreEqual(SECTION2, p);

            file.Close();
            //File.Delete(dataDir + POI_FS);
        }
示例#6
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);
            }
        }
示例#7
0
        public void TestGetCustomerProperties()
        {
            long   ID_1    = 2;
            long   ID_2    = 3;
            String NAME_1  = "Schlüssel";
            String VALUE_1 = "Wert 1";
            Dictionary <object, object> dictionary = new Dictionary <object, object>();

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            CustomProperties           cps;
            MutableSection             s;

            /* A document summary information Set stream by default does have custom properties. */
            cps = dsi.CustomProperties;
            Assert.AreEqual(null, cps);

            /* Test an empty custom properties Set. */
            s = new MutableSection();
            s.SetFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID2);
            // s.SetCodepage(CodePageUtil.CP_UNICODE);
            dsi.AddSection(s);
            cps = dsi.CustomProperties;
            Assert.AreEqual(0, cps.Count);

            /* Add a custom property. */
            MutableProperty p = new MutableProperty();

            p.ID    = ID_1;
            p.Type  = Variant.VT_LPWSTR;
            p.Value = VALUE_1;
            s.SetProperty(p);
            dictionary[ID_1] = NAME_1;
            s.Dictionary     = dictionary;
            cps = dsi.CustomProperties;
            Assert.AreEqual(1, cps.Count);
            Assert.IsTrue(cps.IsPure);

            /* Add another custom property. */
            s.SetProperty((int)ID_2, Variant.VT_LPWSTR, VALUE_1);
            dictionary[ID_2] = NAME_1;
            s.Dictionary     = dictionary;
            cps = dsi.CustomProperties;
            Assert.AreEqual(1, cps.Count);
            Assert.IsFalse(cps.IsPure);
        }
示例#8
0
        public void TestDictionary()
        {
            FileStream copy = File.Create(testContextInstance.TestDir + @"\Test-HPSF.ole2");
            //copy.deleteOnExit();

            /* Write: */
            FileStream         out1  = copy;
            POIFSFileSystem    poiFs = new POIFSFileSystem();
            MutablePropertySet ps1   = new MutablePropertySet();
            MutableSection     s     = (MutableSection)ps1.Sections[0];
            Hashtable          m     = new Hashtable(3, 1.0f);

            m[1]         = "String 1";
            m[2]         = "String 2";
            m[3]         = "String 3";
            s.Dictionary = (m);
            s.SetFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID1);
            int codepage = (int)Constants.CP_UNICODE;

            s.SetProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
                          codepage);
            poiFs.CreateDocument(ps1.GetStream(), "Test");
            poiFs.WriteFileSystem(out1);

            /* Read back: */
            POIFile[] psf = Util.ReadPropertySets(copy);
            Assert.AreEqual(1, psf.Length);
            byte[]      bytes = psf[0].GetBytes();
            Stream      in1   = new MemoryStream(bytes);
            PropertySet ps2   = PropertySetFactory.Create(in1);

            /* Check if the result is a DocumentSummaryInformation stream, as
             * specified. */
            Assert.IsTrue(ps2.IsDocumentSummaryInformation);

            /* Compare the property Set stream with the corresponding one
             * from the origin file and check whether they are equal. */
            Assert.IsTrue(ps1.Equals(ps2));

            out1.Close();
            copy.Close();
            File.Delete(testContextInstance.TestDir + @"\Test-HPSF.ole2");
        }
示例#9
0
        public void TestDictionary()
        {
            using (FileStream copy = File.Create(@".\Test-HPSF.ole2"))
            {
                /* Write: */
                POIFSFileSystem             poiFs = new POIFSFileSystem();
                MutablePropertySet          ps1   = new MutablePropertySet();
                MutableSection              s     = (MutableSection)ps1.Sections[0];
                Dictionary <object, object> m     = new Dictionary <object, object>(3);
                m[1]         = "String 1";
                m[2]         = "String 2";
                m[3]         = "String 3";
                s.Dictionary = (m);
                s.SetFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID1);
                int codepage = CodePageUtil.CP_UNICODE;
                s.SetProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, codepage);
                poiFs.CreateDocument(ps1.ToInputStream(), "Test");
                poiFs.WriteFileSystem(copy);

                /* Read back: */
                POIFile[] psf = Util.ReadPropertySets(copy);
                Assert.AreEqual(1, psf.Length);
                byte[]      bytes = psf[0].GetBytes();
                Stream      in1   = new ByteArrayInputStream(bytes);
                PropertySet ps2   = PropertySetFactory.Create(in1);

                /* Check if the result is a DocumentSummaryInformation stream, as
                 * specified. */
                Assert.IsTrue(ps2.IsDocumentSummaryInformation);

                /* Compare the property Set stream with the corresponding one
                 * from the origin file and check whether they are equal. */
                Assert.IsTrue(ps1.Equals(ps2));
            }

            if (File.Exists(@".\Test-HPSF.ole2"))
            {
                File.Delete(@".\Test-HPSF.ole2");
            }
        }
示例#10
0
        public void TestDictionaryWithInvalidCodepage()
        {
            using (FileStream copy = File.Create(@".\Test-HPSF.ole2"))
            {
                /* Write: */
                POIFSFileSystem    poiFs = new POIFSFileSystem();
                MutablePropertySet ps1   = new MutablePropertySet();
                MutableSection     s     = (MutableSection)ps1.Sections[0];
                Hashtable          m     = new Hashtable(3, 1.0f);
                m[1] = "String 1";
                m[2] = "String 2";
                m[3] = "String 3";

                try
                {
                    Assert.Throws <IllegalPropertySetDataException>(() => {
                        s.Dictionary = m;
                        s.SetFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID1);
                        s.SetProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, 12345);
                        poiFs.CreateDocument(ps1.ToInputStream(), "Test");
                        poiFs.WriteFileSystem(copy);
                    });
                }
                finally
                {
                    poiFs.Close();
                }
            }



            if (File.Exists(@".\Test-HPSF.ole2"))
            {
                File.Delete(@".\Test-HPSF.ole2");
            }
        }