public void TestCoreProperties_bug51374() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); String strDate = "2007-05-12T08:00:00Z"; DateTime date = DateTime.Parse(strDate).ToUniversalTime(); OPCPackage pkg = new ZipPackage(); PackagePropertiesPart props = (PackagePropertiesPart)pkg.GetPackageProperties(); // Created Assert.AreEqual("", props.GetCreatedPropertyString()); Assert.IsNull(props.GetCreatedProperty()); props.SetCreatedProperty((String)null); Assert.AreEqual("", props.GetCreatedPropertyString()); Assert.IsNull(props.GetCreatedProperty()); props.SetCreatedProperty(new Nullable <DateTime>()); Assert.AreEqual("", props.GetCreatedPropertyString()); Assert.IsNull(props.GetCreatedProperty()); props.SetCreatedProperty(new Nullable <DateTime>(date)); Assert.AreEqual(strDate, props.GetCreatedPropertyString()); Assert.AreEqual(date, props.GetCreatedProperty()); props.SetCreatedProperty(strDate); Assert.AreEqual(strDate, props.GetCreatedPropertyString()); Assert.AreEqual(date, props.GetCreatedProperty()); // lastPrinted Assert.AreEqual("", props.GetLastPrintedPropertyString()); Assert.IsNull(props.GetLastPrintedProperty()); props.SetLastPrintedProperty((String)null); Assert.AreEqual("", props.GetLastPrintedPropertyString()); Assert.IsNull(props.GetLastPrintedProperty()); props.SetLastPrintedProperty(new Nullable <DateTime>()); Assert.AreEqual("", props.GetLastPrintedPropertyString()); Assert.IsNull(props.GetLastPrintedProperty()); props.SetLastPrintedProperty(new Nullable <DateTime>(date)); Assert.AreEqual(strDate, props.GetLastPrintedPropertyString()); Assert.AreEqual(date, props.GetLastPrintedProperty()); props.SetLastPrintedProperty(strDate); Assert.AreEqual(strDate, props.GetLastPrintedPropertyString()); Assert.AreEqual(date, props.GetLastPrintedProperty()); // modified Assert.IsNull(props.GetModifiedProperty()); props.SetModifiedProperty((String)null); Assert.IsNull(props.GetModifiedProperty()); props.SetModifiedProperty(new Nullable <DateTime>()); Assert.IsNull(props.GetModifiedProperty()); props.SetModifiedProperty(new Nullable <DateTime>(date)); Assert.AreEqual(strDate, props.GetModifiedPropertyString()); Assert.AreEqual(date, props.GetModifiedProperty()); props.SetModifiedProperty(strDate); Assert.AreEqual(strDate, props.GetModifiedPropertyString()); Assert.AreEqual(date, props.GetModifiedProperty()); pkg.Close(); }
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)); }
public void SetCreated(string date) { part.SetCreatedProperty(date); }
// TODO Load element with XMLBeans or dynamic table // TODO Check every element/namespace for compliance public PackagePart Unmarshall(UnmarshallContext context, Stream in1) { PackagePropertiesPart coreProps = new PackagePropertiesPart(context .Package, context.PartName); // If the input stream is null then we try to get it from the // package. if (in1 == null) { if (context.ZipEntry != null) { in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(context.ZipEntry); } else if (context.Package != null) { // Try to retrieve the part inputstream from the URI ZipEntry zipEntry; try { zipEntry = ZipHelper .GetCorePropertiesZipEntry((ZipPackage)context .Package); } catch (OpenXml4NetException) { throw new IOException( "Error while trying to get the part input stream."); } in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(zipEntry); } else throw new IOException( "Error while trying to get the part input stream."); } XmlDocument xmlDoc = null; try { xmlDoc = DocumentHelper.LoadDocument(in1); nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("cp", namespaceCP); nsmgr.AddNamespace("dc", namespaceDC); nsmgr.AddNamespace("dcterms", namespaceDcTerms); nsmgr.AddNamespace("xsi", namespaceXSI); nsmgr.AddNamespace("cml", PackageNamespaces.MARKUP_COMPATIBILITY); nsmgr.AddNamespace("dcmitype", PackageNamespaces.DCMITYPE); //xmlDoc.ReadNode(reader); //try { //xmlDoc = xmlReader.read(in1); /* Check OPC compliance */ // Rule M4.2, M4.3, M4.4 and M4.5/ CheckElementForOPCCompliance(xmlDoc.DocumentElement); /* End OPC compliance */ //} catch (DocumentException e) { // throw new IOException(e.getMessage()); //} } catch (XmlException ex) { if (ex.Message.Contains("MaxCharactersFromEntities")) { // } else throw ex; } if (xmlDoc!=null && xmlDoc.DocumentElement != null) { coreProps.SetCategoryProperty(LoadCategory(xmlDoc)); coreProps.SetContentStatusProperty(LoadContentStatus(xmlDoc)); coreProps.SetContentTypeProperty(LoadContentType(xmlDoc)); coreProps.SetCreatedProperty(LoadCreated(xmlDoc)); coreProps.SetCreatorProperty(LoadCreator(xmlDoc)); coreProps.SetDescriptionProperty(LoadDescription(xmlDoc)); coreProps.SetIdentifierProperty(LoadIdentifier(xmlDoc)); coreProps.SetKeywordsProperty(LoadKeywords(xmlDoc)); coreProps.SetLanguageProperty(LoadLanguage(xmlDoc)); coreProps.SetLastModifiedByProperty(LoadLastModifiedBy(xmlDoc)); coreProps.SetLastPrintedProperty(LoadLastPrinted(xmlDoc)); coreProps.SetModifiedProperty(LoadModified(xmlDoc)); coreProps.SetRevisionProperty(LoadRevision(xmlDoc)); coreProps.SetSubjectProperty(LoadSubject(xmlDoc)); coreProps.SetTitleProperty(LoadTitle(xmlDoc)); coreProps.SetVersionProperty(LoadVersion(xmlDoc)); } return coreProps; }
public void SetCreated(Nullable <DateTime> date) { part.SetCreatedProperty(date); }
// TODO Load element with XMLBeans or dynamic table // TODO Check every element/namespace for compliance public PackagePart Unmarshall(UnmarshallContext context, Stream in1) { PackagePropertiesPart coreProps = new PackagePropertiesPart(context .Package, context.PartName); // If the input stream is null then we try to get it from the // package. if (in1 == null) { if (context.ZipEntry != null) { in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(context.ZipEntry); } else if (context.Package != null) { // Try to retrieve the part inputstream from the URI ZipEntry zipEntry; try { zipEntry = ZipHelper .GetCorePropertiesZipEntry((ZipPackage)context .Package); } catch (OpenXml4NetException) { throw new IOException( "Error while trying to get the part input stream."); } in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(zipEntry); } else { throw new IOException( "Error while trying to get the part input stream."); } } XmlDocument xmlDoc = null; try { xmlDoc = DocumentHelper.LoadDocument(in1); nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("cp", namespaceCP); nsmgr.AddNamespace("dc", namespaceDC); nsmgr.AddNamespace("dcterms", namespaceDcTerms); nsmgr.AddNamespace("xsi", namespaceXSI); nsmgr.AddNamespace("cml", PackageNamespaces.MARKUP_COMPATIBILITY); nsmgr.AddNamespace("dcmitype", PackageNamespaces.DCMITYPE); //xmlDoc.ReadNode(reader); //try { //xmlDoc = xmlReader.read(in1); /* Check OPC compliance */ // Rule M4.2, M4.3, M4.4 and M4.5/ CheckElementForOPCCompliance(xmlDoc.DocumentElement); /* End OPC compliance */ //} catch (DocumentException e) { // throw new IOException(e.getMessage()); //} } catch (XmlException ex) { throw new IOException(ex.Message, ex); } if (xmlDoc != null && xmlDoc.DocumentElement != null) { coreProps.SetCategoryProperty(LoadCategory(xmlDoc)); coreProps.SetContentStatusProperty(LoadContentStatus(xmlDoc)); coreProps.SetContentTypeProperty(LoadContentType(xmlDoc)); coreProps.SetCreatedProperty(LoadCreated(xmlDoc)); coreProps.SetCreatorProperty(LoadCreator(xmlDoc)); coreProps.SetDescriptionProperty(LoadDescription(xmlDoc)); coreProps.SetIdentifierProperty(LoadIdentifier(xmlDoc)); coreProps.SetKeywordsProperty(LoadKeywords(xmlDoc)); coreProps.SetLanguageProperty(LoadLanguage(xmlDoc)); coreProps.SetLastModifiedByProperty(LoadLastModifiedBy(xmlDoc)); coreProps.SetLastPrintedProperty(LoadLastPrinted(xmlDoc)); coreProps.SetModifiedProperty(LoadModified(xmlDoc)); coreProps.SetRevisionProperty(LoadRevision(xmlDoc)); coreProps.SetSubjectProperty(LoadSubject(xmlDoc)); coreProps.SetTitleProperty(LoadTitle(xmlDoc)); coreProps.SetVersionProperty(LoadVersion(xmlDoc)); } return(coreProps); }