/// <summary> /// Sets the value and the variant type of the property with the /// specified ID. If a property with this ID is not yet present in /// the section, it will be Added. An alReady present property with /// the specified ID will be overwritten. A default mapping will be /// used To choose the property's type. /// </summary> /// <param name="id">The property's ID.</param> /// <param name="variantType">The property's variant type.</param> /// <param name="value">The property's value.</param> public void SetProperty(int id, long variantType, Object value) { MutableProperty p = new MutableProperty(); p.ID = id; p.Type = variantType; p.Value = value; SetProperty(p); dirty = true; }
/// <summary> /// Adds a named date property. /// </summary> /// <param name="name">The property's name.</param> /// <param name="value">The property's value.</param> /// <returns>the property that was stored under the specified name before, or /// <c>null</c> /// if there was no such property before.</returns> public Object Put(String name, DateTime value) { MutableProperty p = new MutableProperty(); p.ID = -1; p.Type = Variant.VT_FILETIME; p.Value = value; CustomProperty cp = new CustomProperty(p, name); return(Put(cp)); }
/// <summary> /// Adds a named bool property. /// </summary> /// <param name="name">The property's name.</param> /// <param name="value">The property's value.</param> /// <returns>the property that was stored under the specified name before, or /// <c>null</c> /// if there was no such property before.</returns> public Object Put(String name, bool value) { MutableProperty p = new MutableProperty(); p.ID = -1; p.Type = Variant.VT_BOOL; p.Value = value; CustomProperty cp = new CustomProperty(p, name); return(Put(cp)); }
/// <summary> /// Constructs a <c>MutableSection</c> by doing a deep copy of an /// existing <c>Section</c>. All nested <c>Property</c> /// instances, will be their mutable counterparts in the new /// <c>MutableSection</c>. /// </summary> /// <param name="s">The section Set To copy</param> public MutableSection(Section s) { SetFormatID(s.FormatID); Property[] pa = s.Properties; MutableProperty[] mpa = new MutableProperty[pa.Length]; for (int i = 0; i < pa.Length; i++) { mpa[i] = new MutableProperty(pa[i]); } SetProperties(mpa); this.Dictionary = (s.Dictionary); }
/// <summary> /// Writes this section into an output stream. /// Internally this is done by writing into three byte array output /// streams: one for the properties, one for the property list and one for /// the section as such. The two former are Appended To the latter when they /// have received all their data. /// </summary> /// <param name="out1">The stream To Write into.</param> /// <returns>The number of bytes written, i.e. the section's size.</returns> public int Write(Stream out1) { /* Check whether we have alReady generated the bytes making out the * section. */ if (!dirty && sectionBytes != null) { out1.Write(sectionBytes, 0, sectionBytes.Length); return(sectionBytes.Length); } /* The properties are written To this stream. */ using (MemoryStream propertyStream = new MemoryStream()) { /* The property list is established here. After each property that has * been written To "propertyStream", a property list entry is written To * "propertyListStream". */ using (MemoryStream propertyListStream = new MemoryStream()) { /* Maintain the current position in the list. */ int position = 0; /* Increase the position variable by the size of the property list so * that it points behind the property list and To the beginning of the * properties themselves. */ position += 2 * LittleEndianConsts.INT_SIZE + PropertyCount * 2 * LittleEndianConsts.INT_SIZE; /* Writing the section's dictionary it tricky. If there is a dictionary * (property 0) the codepage property (property 1) must be Set, Too. */ int codepage = -1; if (GetProperty(PropertyIDMap.PID_DICTIONARY) != null) { Object p1 = GetProperty(PropertyIDMap.PID_CODEPAGE); if (p1 != null) { if (!(p1 is int)) { throw new IllegalPropertySetDataException ("The codepage property (ID = 1) must be an " + "Integer object."); } } else { /* Warning: The codepage property is not Set although a * dictionary is present. In order To cope with this problem we * Add the codepage property and Set it To Unicode. */ SetProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, (int)Constants.CP_UNICODE); } codepage = Codepage; } /* Sort the property list by their property IDs: */ preprops.Sort(new PropertyComparer()); /* Write the properties and the property list into their respective * streams: */ for (int i = 0; i < preprops.Count; i++) { MutableProperty p = (MutableProperty)preprops[i]; long id = p.ID; /* Write the property list entry. */ TypeWriter.WriteUIntToStream(propertyListStream, (uint)p.ID); TypeWriter.WriteUIntToStream(propertyListStream, (uint)position); /* If the property ID is not equal 0 we Write the property and all * is fine. However, if it Equals 0 we have To Write the section's * dictionary which has an implicit type only and an explicit * value. */ if (id != 0) { /* Write the property and update the position To the next * property. */ position += p.Write(propertyStream, Codepage); } else { if (codepage == -1) { throw new IllegalPropertySetDataException ("Codepage (property 1) is undefined."); } position += WriteDictionary(propertyStream, dictionary, codepage); } } propertyStream.Flush(); propertyListStream.Flush(); /* Write the section: */ byte[] pb1 = propertyListStream.ToArray(); byte[] pb2 = propertyStream.ToArray(); /* Write the section's Length: */ TypeWriter.WriteToStream(out1, LittleEndianConsts.INT_SIZE * 2 + pb1.Length + pb2.Length); /* Write the section's number of properties: */ TypeWriter.WriteToStream(out1, PropertyCount); /* Write the property list: */ out1.Write(pb1, 0, pb1.Length); /* Write the properties: */ out1.Write(pb2, 0, pb2.Length); int streamLength = LittleEndianConsts.INT_SIZE * 2 + pb1.Length + pb2.Length; return(streamLength); } } }
/// <summary> /// Adds a named date property. /// </summary> /// <param name="name">The property's name.</param> /// <param name="value">The property's value.</param> /// <returns>the property that was stored under the specified name before, or /// <c>null</c> /// if there was no such property before.</returns> public Object Put(String name,DateTime value) { MutableProperty p = new MutableProperty(); p.ID=-1; p.Type=Variant.VT_FILETIME; p.Value=value; CustomProperty cp = new CustomProperty(p, name); return Put(cp); }
/// <summary> /// Adds a named bool property. /// </summary> /// <param name="name">The property's name.</param> /// <param name="value">The property's value.</param> /// <returns>the property that was stored under the specified name before, or /// <c>null</c> /// if there was no such property before.</returns> public Object Put(String name, bool value) { MutableProperty p = new MutableProperty(); p.ID=-1; p.Type=Variant.VT_BOOL; p.Value=value; CustomProperty cp = new CustomProperty(p, name); return Put(cp); }
public void TestGetCustomerProperties() { long ID_1 = 2; long ID_2 = 3; String NAME_1 = "Schlüssel"; String VALUE_1 = "Wert 1"; Hashtable dictionary = new Hashtable(); 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(Constants.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); }
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); }
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.GetStream(), SummaryInformation.DEFAULT_STREAM_NAME); poiFs.WriteFileSystem(out1); //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 { } }
/// <summary> /// Constructs a <c>MutableSection</c> by doing a deep copy of an /// existing <c>Section</c>. All nested <c>Property</c> /// instances, will be their mutable counterparts in the new /// <c>MutableSection</c>. /// </summary> /// <param name="s">The section Set To copy</param> public MutableSection(Section s) { SetFormatID(s.FormatID); Property[] pa = s.Properties; MutableProperty[] mpa = new MutableProperty[pa.Length]; for (int i = 0; i < pa.Length; i++) mpa[i] = new MutableProperty(pa[i]); SetProperties(mpa); this.Dictionary=(s.Dictionary); }
/// <summary> /// Sets the value and the variant type of the property with the /// specified ID. If a property with this ID is not yet present in /// the section, it will be Added. An alReady present property with /// the specified ID will be overwritten. A default mapping will be /// used To choose the property's type. /// </summary> /// <param name="id">The property's ID.</param> /// <param name="variantType">The property's variant type.</param> /// <param name="value">The property's value.</param> public void SetProperty(int id, long variantType, Object value) { MutableProperty p = new MutableProperty(); p.ID=id; p.Type=variantType; p.Value=value; SetProperty(p); dirty = true; }