Пример #1
0
        public void TestTrailingSpacesInURI_53282()
        {
            OPCPackage pkg = null;

            using (Stream stream = OpenXml4NetTestDataSamples.OpenSampleStream("53282.xlsx"))
            {
                pkg = OPCPackage.Open(stream);
            }

            PackageRelationshipCollection sheetRels = pkg.GetPartsByName(new Regex("/xl/worksheets/sheet1.xml"))[0].Relationships;

            Assert.AreEqual(3, sheetRels.Size);
            PackageRelationship rId1 = sheetRels.GetRelationshipByID("rId1");

            Assert.AreEqual(TargetMode.External, rId1.TargetMode);
            Uri targetUri = rId1.TargetUri;

            Assert.AreEqual("mailto:[email protected]%C2%A0", targetUri.OriginalString);
            Assert.AreEqual("[email protected]\u00A0", targetUri.Scheme);

            MemoryStream out1 = new MemoryStream();

            pkg.Save(out1);


            pkg = OPCPackage.Open(new ByteArrayInputStream(out1.ToArray()));
            out1.Close();
            sheetRels = pkg.GetPartsByName(new Regex("/xl/worksheets/sheet1.xml"))[(0)].Relationships;
            Assert.AreEqual(3, sheetRels.Size);
            rId1 = sheetRels.GetRelationshipByID("rId1");
            Assert.AreEqual(TargetMode.External, rId1.TargetMode);
            targetUri = rId1.TargetUri;
            Assert.AreEqual("mailto:[email protected]%C2%A0", targetUri.OriginalString);
            Assert.AreEqual("[email protected]\u00A0", targetUri.Scheme);
        }
Пример #2
0
        public void TestLoadExcelHyperlinkRelations()
        {
            Stream      is1   = OpenXml4NetTestDataSamples.OpenSampleStream("ExcelWithHyperlinks.xlsx");
            OPCPackage  pkg   = OPCPackage.Open(is1);
            PackagePart sheet = pkg.GetPart(
                PackagingUriHelper.CreatePartName(SHEET_WITH_COMMENTS));

            Assert.IsNotNull(sheet);

            // rId1 is url
            PackageRelationship url = sheet.GetRelationship("rId1");

            Assert.IsNotNull(url);
            Assert.AreEqual("rId1", url.Id);
            Assert.AreEqual("/xl/worksheets/sheet1.xml", url.SourceUri.ToString());
            Assert.AreEqual("http://poi.apache.org/", url.TargetUri.ToString());

            // rId2 is file
            PackageRelationship file = sheet.GetRelationship("rId2");

            Assert.IsNotNull(file);
            Assert.AreEqual("rId2", file.Id);
            Assert.AreEqual("/xl/worksheets/sheet1.xml", file.SourceUri.ToString());
            Assert.AreEqual("WithVariousData.xlsx", file.TargetUri.ToString());

            // rId3 is mailto
            PackageRelationship mailto = sheet.GetRelationship("rId3");

            Assert.IsNotNull(mailto);
            Assert.AreEqual("rId3", mailto.Id);
            Assert.AreEqual("/xl/worksheets/sheet1.xml", mailto.SourceUri.ToString());
            Assert.AreEqual("mailto:[email protected]?subject=XSSF%20Hyperlinks", mailto.TargetUri.AbsoluteUri);
        }
Пример #3
0
        public void TestFileWithContentTypeEntities()
        {
            Stream     is1 = OpenXml4NetTestDataSamples.OpenSampleStream("ContentTypeHasEntities.ooxml");
            OPCPackage p   = OPCPackage.Open(is1);

            // Check we found the contents of it
            bool foundCoreProps = false, foundDocument = false, foundTheme1 = false;

            foreach (PackagePart part in p.GetParts())
            {
                if (part.PartName.ToString().Equals("/docProps/core.xml"))
                {
                    Assert.AreEqual(ContentTypes.CORE_PROPERTIES_PART, part.ContentType);
                    foundCoreProps = true;
                }
                if (part.PartName.ToString().Equals("/word/document.xml"))
                {
                    Assert.AreEqual(XWPFRelation.DOCUMENT.ContentType, part.ContentType);
                    foundDocument = true;
                }
                if (part.PartName.ToString().Equals("/word/theme/theme1.xml"))
                {
                    Assert.AreEqual(XWPFRelation.THEME.ContentType, part.ContentType);
                    foundTheme1 = true;
                }
            }
            Assert.IsTrue(foundCoreProps, "Core not found in " + p.GetParts());
            Assert.IsTrue(foundDocument, "Document not found in " + p.GetParts());
            Assert.IsTrue(foundTheme1, "Theme1 not found in " + p.GetParts());
        }
Пример #4
0
        public void TestEntitiesInCoreProps_56164()
        {
            Stream     is1 = OpenXml4NetTestDataSamples.OpenSampleStream("CorePropertiesHasEntities.ooxml");
            OPCPackage p   = OPCPackage.Open(is1);

            is1.Close();

            // Should have 3 root relationships
            bool foundDocRel = false, foundCorePropRel = false, foundExtPropRel = false;

            foreach (PackageRelationship pr in p.Relationships)
            {
                if (pr.RelationshipType.Equals(PackageRelationshipTypes.CORE_DOCUMENT))
                {
                    foundDocRel = true;
                }
                if (pr.RelationshipType.Equals(PackageRelationshipTypes.CORE_PROPERTIES))
                {
                    foundCorePropRel = true;
                }
                if (pr.RelationshipType.Equals(PackageRelationshipTypes.EXTENDED_PROPERTIES))
                {
                    foundExtPropRel = true;
                }
            }
            Assert.IsTrue(foundDocRel, "Core/Doc Relationship not found in " + p.Relationships);
            Assert.IsTrue(foundCorePropRel, "Core Props Relationship not found in " + p.Relationships);
            Assert.IsTrue(foundExtPropRel, "Ext Props Relationship not found in " + p.Relationships);

            // Get the Core Properties
            PackagePropertiesPart props = (PackagePropertiesPart)p.GetPackageProperties();

            // Check
            Assert.AreEqual("Stefan Kopf", props.GetCreatorProperty());
        }
Пример #5
0
        public void TestBug56479()
        {
            Stream     is1 = OpenXml4NetTestDataSamples.OpenSampleStream("dcterms_bug_56479.zip");
            OPCPackage p   = OPCPackage.Open(is1);

            // Check we found the contents of it
            bool foundCoreProps = false, foundDocument = false, foundTheme1 = false;

            foreach (PackagePart part in p.GetParts())
            {
                if (part.PartName.ToString().Equals("/docProps/core.xml"))
                {
                    Assert.AreEqual(ContentTypes.CORE_PROPERTIES_PART, part.ContentType);
                    foundCoreProps = true;
                }
                if (part.PartName.ToString().Equals("/word/document.xml"))
                {
                    Assert.AreEqual(XWPFRelation.DOCUMENT.ContentType, part.ContentType);
                    foundDocument = true;
                }
                if (part.PartName.ToString().Equals("/word/theme/theme1.xml"))
                {
                    Assert.AreEqual(XWPFRelation.THEME.ContentType, part.ContentType);
                    foundTheme1 = true;
                }
            }
            Assert.IsTrue(foundCoreProps, "Core not found in " + p.GetParts());
            Assert.IsFalse(foundDocument, "Document should not be found in " + p.GetParts());
            Assert.IsFalse(foundTheme1, "Theme1 should not found in " + p.GetParts());
            p.Close();
            is1.Close();
        }
Пример #6
0
        public void TestClosingStreamOnException()
        {
            Stream   is1 = OpenXml4NetTestDataSamples.OpenSampleStream("dcterms_bug_56479.zip");
            FileInfo tmp = TempFile.CreateTempFile("poi-test-truncated-zip", "");
            // create a corrupted zip file by truncating a valid zip file to the first 100 bytes
            Stream os = new FileStream(tmp.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            for (int i = 0; i < 100; i++)
            {
                os.WriteByte((byte)is1.ReadByte());
            }
            os.Flush();
            os.Close();
            is1.Close();
            // feed the corrupted zip file to OPCPackage
            try
            {
                OPCPackage.Open(tmp, PackageAccess.READ);
            }
            catch (Exception)
            {
                // expected: the zip file is invalid
                // this test does not care if open() throws an exception or not.
            }
            tmp.Delete();
            // If the stream is not closed on exception, it will keep a file descriptor to tmp,
            // and requests to the OS to delete the file will fail.
            Assert.IsFalse(tmp.Exists, "Can't delete tmp file");
        }
Пример #7
0
        public void TestGetProperties()
        {
            // Open the namespace
            OPCPackage p = OPCPackage.Open(OpenXml4NetTestDataSamples.OpenSampleStream("TestPackageCoreProperiesGetters.docx"));

            CompareProperties(p);
            p.Revert();
        }
Пример #8
0
        public void TestEntitiesInRels_56164()
        {
            Stream     is1 = OpenXml4NetTestDataSamples.OpenSampleStream("PackageRelsHasEntities.ooxml");
            OPCPackage p   = OPCPackage.Open(is1);

            is1.Close();

            // Should have 3 root relationships
            bool foundDocRel = false, foundCorePropRel = false, foundExtPropRel = false;

            foreach (PackageRelationship pr in p.Relationships)
            {
                if (pr.RelationshipType.Equals(PackageRelationshipTypes.CORE_DOCUMENT))
                {
                    foundDocRel = true;
                }
                if (pr.RelationshipType.Equals(PackageRelationshipTypes.CORE_PROPERTIES))
                {
                    foundCorePropRel = true;
                }
                if (pr.RelationshipType.Equals(PackageRelationshipTypes.EXTENDED_PROPERTIES))
                {
                    foundExtPropRel = true;
                }
            }
            Assert.IsTrue(foundDocRel, "Core/Doc Relationship not found in " + p.Relationships);
            Assert.IsTrue(foundCorePropRel, "Core Props Relationship not found in " + p.Relationships);
            Assert.IsTrue(foundExtPropRel, "Ext Props Relationship not found in " + p.Relationships);

            // Should have normal work parts
            bool foundCoreProps = false, foundDocument = false, foundTheme1 = false;

            foreach (PackagePart part in p.GetParts())
            {
                if (part.PartName.ToString().Equals("/docProps/core.xml"))
                {
                    Assert.AreEqual(ContentTypes.CORE_PROPERTIES_PART, part.ContentType);
                    foundCoreProps = true;
                }
                if (part.PartName.ToString().Equals("/word/document.xml"))
                {
                    Assert.AreEqual(XWPFRelation.DOCUMENT.ContentType, part.ContentType);
                    foundDocument = true;
                }
                if (part.PartName.ToString().Equals("/word/theme/theme1.xml"))
                {
                    Assert.AreEqual(XWPFRelation.THEME.ContentType, part.ContentType);
                    foundTheme1 = true;
                }
            }
            Assert.IsTrue(foundCoreProps, "Core not found in " + Arrays.ToString(p.GetParts().ToArray()));
            Assert.IsTrue(foundDocument, "Document not found in " + Arrays.ToString(p.GetParts().ToArray()));
            Assert.IsTrue(foundTheme1, "Theme1 not found in " + Arrays.ToString(p.GetParts().ToArray()));
        }
Пример #9
0
        public void TestAlternateCorePropertyTimezones()
        {
            Stream                is1   = OpenXml4NetTestDataSamples.OpenSampleStream("OPCCompliance_CoreProperties_AlternateTimezones.docx");
            OPCPackage            pkg   = OPCPackage.Open(is1);
            PackagePropertiesPart props = (PackagePropertiesPart)pkg.GetPackageProperties();

            is1.Close();

            // We need predictable dates for testing!
            //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"); //use fff for millisecond.

            df.TimeZone = TimeZoneInfo.Utc;
            // Check text properties first
            Assert.AreEqual("Lorem Ipsum", props.GetTitleProperty());
            Assert.AreEqual("Apache POI", props.GetCreatorProperty());

            // Created at has a +3 timezone and milliseconds
            //   2006-10-13T18:06:00.123+03:00
            // = 2006-10-13T15:06:00.123+00:00
            Assert.AreEqual("2006-10-13T15:06:00Z", props.GetCreatedPropertyString());
            Assert.AreEqual("2006-10-13T15:06:00.123Z", df.Format(props.GetCreatedProperty()));

            // Modified at has a -13 timezone but no milliseconds
            //   2007-06-20T07:59:00-13:00
            // = 2007-06-20T20:59:00-13:00
            Assert.AreEqual("2007-06-20T20:59:00Z", props.GetModifiedPropertyString());
            Assert.AreEqual("2007-06-20T20:59:00.000Z", df.Format(props.GetModifiedProperty()));


            // Ensure we can change them with other timezones and still read back OK
            props.SetCreatedProperty("2007-06-20T20:57:00+13:00");
            props.SetModifiedProperty("2007-06-20T20:59:00.123-13:00");

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            pkg.Save(baos);
            pkg = OPCPackage.Open(new ByteArrayInputStream(baos.ToByteArray()));

            // Check text properties first - should be unchanged
            Assert.AreEqual("Lorem Ipsum", props.GetTitleProperty());
            Assert.AreEqual("Apache POI", props.GetCreatorProperty());

            // Check the updated times
            //   2007-06-20T20:57:00+13:00
            // = 2007-06-20T07:57:00Z
            Assert.AreEqual("2007-06-20T07:57:00.000Z", df.Format(props.GetCreatedProperty().Value));

            //   2007-06-20T20:59:00.123-13:00
            // = 2007-06-21T09:59:00.123Z
            Assert.AreEqual("2007-06-21T09:59:00.123Z", df.Format(props.GetModifiedProperty().Value));
        }
Пример #10
0
        public void TestGetPropertiesLO()
        {
            // Open the namespace
            OPCPackage        pkg1   = OPCPackage.Open(OpenXml4NetTestDataSamples.OpenSampleStream("51444.xlsx"));
            PackageProperties props1 = pkg1.GetPackageProperties();

            Assert.AreEqual(null, props1.GetTitleProperty());
            props1.SetTitleProperty("Bug 51444 fixed");
            MemoryStream out1 = new MemoryStream();

            pkg1.Save(out1);
            out1.Close();

            OPCPackage        pkg2   = OPCPackage.Open(new MemoryStream(out1.ToArray()));
            PackageProperties props2 = pkg2.GetPackageProperties();

            props2.SetTitleProperty("Bug 51444 fixed");
        }
Пример #11
0
        public void TestReplaceContentType()
        {
            Stream     is1 = OpenXml4NetTestDataSamples.OpenSampleStream("sample.xlsx");
            OPCPackage p   = OPCPackage.Open(is1);

            ContentTypeManager mgr = GetContentTypeManager(p);

            Assert.True(mgr.IsContentTypeRegister("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"));
            Assert.False(mgr.IsContentTypeRegister("application/vnd.ms-excel.sheet.macroEnabled.main+xml"));

            Assert.True(
                p.ReplaceContentType(
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
                    "application/vnd.ms-excel.sheet.macroEnabled.main+xml")
                );

            Assert.False(mgr.IsContentTypeRegister("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"));
            Assert.True(mgr.IsContentTypeRegister("application/vnd.ms-excel.sheet.macroEnabled.main+xml"));
        }
Пример #12
0
        public void TestLoadRelationships()
        {
            Stream     is1 = OpenXml4NetTestDataSamples.OpenSampleStream("sample.xlsx");
            OPCPackage pkg = OPCPackage.Open(is1);

            logger.Log(POILogger.DEBUG, "1: " + pkg);
            PackageRelationshipCollection rels = pkg.GetRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);
            PackageRelationship           coreDocRelationship = rels.GetRelationship(0);
            PackagePart corePart = pkg.GetPart(coreDocRelationship);

            String[] relIds = { "rId1", "rId2", "rId3" };
            foreach (String relId in relIds)
            {
                PackageRelationship rel = corePart.GetRelationship(relId);
                Assert.IsNotNull(rel);
                PackagePartName relName   = PackagingUriHelper.CreatePartName(rel.TargetUri);
                PackagePart     sheetPart = pkg.GetPart(relName);
                Assert.AreEqual(1, sheetPart.Relationships.Size, "Number of relationships1 for " + sheetPart.PartName);
            }
        }
Пример #13
0
        public void TestListParts1()
        {
            Stream is1 = OpenXml4NetTestDataSamples.OpenSampleStream("sample.docx");

            OPCPackage p;

            p = OPCPackage.Open(is1);

            foreach (PackagePart part in p.GetParts())
            {
                values.Add(part.PartName, part.ContentType);
                logger.Log(POILogger.DEBUG, part.PartName);
            }

            // Compare expected values with values return by the namespace
            foreach (PackagePartName partName in expectedValues.Keys)
            {
                Assert.IsNotNull(values[partName]);
                Assert.AreEqual(expectedValues[partName], values[partName]);
            }
        }
Пример #14
0
        public void TestFetchFromCollection()
        {
            Stream      is1   = OpenXml4NetTestDataSamples.OpenSampleStream("ExcelWithHyperlinks.xlsx");
            OPCPackage  pkg   = OPCPackage.Open(is1);
            PackagePart sheet = pkg.GetPart(
                PackagingUriHelper.CreatePartName(SHEET_WITH_COMMENTS));

            Assert.IsNotNull(sheet);

            Assert.IsTrue(sheet.HasRelationships);
            Assert.AreEqual(6, sheet.Relationships.Size);

            // Should have three hyperlinks, and one comment
            PackageRelationshipCollection hyperlinks =
                sheet.GetRelationshipsByType(HYPERLINK_REL_TYPE);
            PackageRelationshipCollection comments =
                sheet.GetRelationshipsByType(COMMENTS_REL_TYPE);

            Assert.AreEqual(3, hyperlinks.Size);
            Assert.AreEqual(1, comments.Size);

            // Check we can Get bits out by id
            // Hyperlinks are rId1, rId2 and rId3
            // Comment is rId6
            Assert.IsNotNull(hyperlinks.GetRelationshipByID("rId1"));
            Assert.IsNotNull(hyperlinks.GetRelationshipByID("rId2"));
            Assert.IsNotNull(hyperlinks.GetRelationshipByID("rId3"));
            Assert.IsNull(hyperlinks.GetRelationshipByID("rId6"));

            Assert.IsNull(comments.GetRelationshipByID("rId1"));
            Assert.IsNull(comments.GetRelationshipByID("rId2"));
            Assert.IsNull(comments.GetRelationshipByID("rId3"));
            Assert.IsNotNull(comments.GetRelationshipByID("rId6"));

            Assert.IsNotNull(sheet.GetRelationship("rId1"));
            Assert.IsNotNull(sheet.GetRelationship("rId2"));
            Assert.IsNotNull(sheet.GetRelationship("rId3"));
            Assert.IsNotNull(sheet.GetRelationship("rId6"));
        }
Пример #15
0
        public void TestOnlyOneCorePropertiesPart()
        {
            // We have relaxed this check, so we can read the file anyway
            try {
                ExtractInvalidFormatMessage("OnlyOneCorePropertiesPartFAIL.docx");
                Assert.Fail("M4.1 should be being relaxed");
            }
            catch (AssertionException e) { }

            // We will use the first core properties, and ignore the others
            Stream     is1 = OpenXml4NetTestDataSamples.OpenSampleStream("MultipleCoreProperties.docx");
            OPCPackage pkg = OPCPackage.Open(is1);

            // We can see 2 by type
            Assert.AreEqual(2, pkg.GetPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).Count);
            // But only the first one by relationship
            Assert.AreEqual(1, pkg.GetPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).Count);
            // It should be core.xml not the older core1.xml
            Assert.AreEqual(
                "/docProps/core.xml",
                pkg.GetPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES)[0].PartName.ToString()
                );
        }
Пример #16
0
        public void TestFileWithContentTypeParams()
        {
            Stream is1 = OpenXml4NetTestDataSamples.OpenSampleStream("ContentTypeHasParameters.ooxml");

            OPCPackage p = OPCPackage.Open(is1);

            String typeResqml = "application/x-resqml+xml";

            // Check the types on everything
            foreach (PackagePart part in p.GetParts())
            {
                // _rels type doesn't have any params
                if (part.IsRelationshipPart)
                {
                    Assert.AreEqual(ContentTypes.RELATIONSHIPS_PART, part.ContentType);
                    Assert.AreEqual(ContentTypes.RELATIONSHIPS_PART, part.ContentTypeDetails.ToString());
                    Assert.AreEqual(false, part.ContentTypeDetails.HasParameters());
                    Assert.AreEqual(0, part.ContentTypeDetails.GetParameterKeys().Length);
                }
                // Core type doesn't have any params
                else if (part.PartName.ToString().Equals("/docProps/core.xml"))
                {
                    Assert.AreEqual(ContentTypes.CORE_PROPERTIES_PART, part.ContentType);
                    Assert.AreEqual(ContentTypes.CORE_PROPERTIES_PART, part.ContentTypeDetails.ToString());
                    Assert.AreEqual(false, part.ContentTypeDetails.HasParameters());
                    Assert.AreEqual(0, part.ContentTypeDetails.GetParameterKeys().Length);
                }
                // Global Crs types do have params
                else if (part.PartName.ToString().Equals("/global1dCrs.xml"))
                {
                    Assert.AreEqual(typeResqml, part.ContentType.Substring(0, typeResqml.Length));
                    Assert.AreEqual(typeResqml, part.ContentTypeDetails.ToString(false));
                    Assert.AreEqual(true, part.ContentTypeDetails.HasParameters());
                    Assert.AreEqual(typeResqml + ";version=2.0;type=obj_global1dCrs", part.ContentTypeDetails.ToString());
                    Assert.AreEqual(2, part.ContentTypeDetails.GetParameterKeys().Length);
                    Assert.AreEqual("2.0", part.ContentTypeDetails.GetParameter("version"));
                    Assert.AreEqual("obj_global1dCrs", part.ContentTypeDetails.GetParameter("type"));
                }
                else if (part.PartName.ToString().Equals("/global2dCrs.xml"))
                {
                    Assert.AreEqual(typeResqml, part.ContentType.Substring(0, typeResqml.Length));
                    Assert.AreEqual(typeResqml, part.ContentTypeDetails.ToString(false));
                    Assert.AreEqual(true, part.ContentTypeDetails.HasParameters());
                    Assert.AreEqual(typeResqml + ";version=2.0;type=obj_global2dCrs", part.ContentTypeDetails.ToString());
                    Assert.AreEqual(2, part.ContentTypeDetails.GetParameterKeys().Length);
                    Assert.AreEqual("2.0", part.ContentTypeDetails.GetParameter("version"));
                    Assert.AreEqual("obj_global2dCrs", part.ContentTypeDetails.GetParameter("type"));
                }
                // Other thingy
                else if (part.PartName.ToString().Equals("/myTestingGuid.xml"))
                {
                    Assert.AreEqual(typeResqml, part.ContentType.Substring(0, typeResqml.Length));
                    Assert.AreEqual(typeResqml, part.ContentTypeDetails.ToString(false));
                    Assert.AreEqual(true, part.ContentTypeDetails.HasParameters());
                    Assert.AreEqual(typeResqml + ";version=2.0;type=obj_tectonicBoundaryFeature", part.ContentTypeDetails.ToString());
                    Assert.AreEqual(2, part.ContentTypeDetails.GetParameterKeys().Length);
                    Assert.AreEqual("2.0", part.ContentTypeDetails.GetParameter("version"));
                    Assert.AreEqual("obj_tectonicBoundaryFeature", part.ContentTypeDetails.GetParameter("type"));
                }
                // That should be it!
                else
                {
                    Assert.Fail("Unexpected part " + part);
                }
            }
        }