2010-03-05 Rodrigo Kumpera <rkumpera@novell.com>
[mcs.git] / class / WindowsBase / System.IO.Packaging / PackagePropertiesPart.cs
bloba0f2aa67669d8d96794a391ade8c2581f0f70aa8
1 // PackagePropertiesPart.cs created with MonoDevelop
2 // User: alan at 11:07 04/11/2008
3 //
4 // To change standard headers go to Edit->Preferences->Coding->Standard Headers
5 //
7 using System;
8 using System.Xml;
10 namespace System.IO.Packaging
12 class PackagePropertiesPart : PackageProperties
14 const string NSDc = "http://purl.org/dc/elements/1.1/";
15 const string NSDcTerms = "http://purl.org/dc/terms/";
16 const string NSXsi = "http://www.w3.org/2001/XMLSchema-instance";
18 string category;
19 string contentStatus;
20 string contentType;
21 DateTime? created;
22 string creator;
23 string description;
24 string identifier;
25 string keywords;
26 string language;
27 string lastModifiedBy;
28 DateTime? lastPrinted;
29 DateTime? modified;
30 string revision;
31 string subject;
32 string title;
33 string version;
35 public PackagePropertiesPart ()
40 public override string Category {
41 get {
42 return category;
44 set {
45 Package.CheckIsReadOnly ();
46 category = value;
49 public override string ContentStatus {
50 get {
51 return contentStatus;
53 set {
54 Package.CheckIsReadOnly ();
55 contentStatus = value;
58 public override string ContentType {
59 get {
60 return contentType;
62 set {
63 Package.CheckIsReadOnly ();
64 contentType = value;
67 public override DateTime? Created {
68 get {
69 return created;
71 set {
72 Package.CheckIsReadOnly ();
73 created = value;
76 public override string Creator {
77 get {
78 return creator;
80 set {
81 Package.CheckIsReadOnly ();
82 creator = value;
85 public override string Description {
86 get {
87 return description;
89 set {
90 Package.CheckIsReadOnly ();
91 description = value;
94 public override string Identifier {
95 get {
96 return identifier;
98 set {
99 Package.CheckIsReadOnly ();
100 identifier = value;
103 public override string Keywords {
104 get {
105 return keywords;
107 set {
108 Package.CheckIsReadOnly ();
109 keywords = value;
112 public override string Language {
113 get {
114 return language;
116 set {
117 Package.CheckIsReadOnly ();
118 language = value;
121 public override string LastModifiedBy {
122 get {
123 return lastModifiedBy;
125 set {
126 Package.CheckIsReadOnly ();
127 lastModifiedBy = value;
130 public override DateTime? LastPrinted {
131 get {
132 return lastPrinted;
134 set {
135 Package.CheckIsReadOnly ();
136 lastPrinted = value;
139 public override DateTime? Modified {
140 get {
141 return modified;
143 set {
144 Package.CheckIsReadOnly ();
145 modified = value;
148 public override string Revision {
149 get {
150 return revision;
152 set {
153 Package.CheckIsReadOnly ();
154 revision = value;
157 public override string Subject {
158 get {
159 return subject;
161 set {
162 Package.CheckIsReadOnly ();
163 subject = value;
166 public override string Title {
167 get {
168 return title;
170 set {
171 Package.CheckIsReadOnly ();
172 title = value;
175 public override string Version {
176 get {
177 return version;
179 set {
180 Package.CheckIsReadOnly ();
181 version = value;
185 internal override void LoadFrom (Stream stream)
187 if (stream.Length == 0)
188 return;
190 XmlDocument doc = new XmlDocument ();
191 doc.Load (stream);
193 XmlNamespaceManager manager = new XmlNamespaceManager (doc.NameTable);
194 manager.AddNamespace ("prop", NSPackageProperties);
195 manager.AddNamespace ("dc", NSDc);
196 manager.AddNamespace ("dcterms", NSDcTerms);
197 manager.AddNamespace ("xsi", NSXsi);
199 XmlNode node;
200 if ((node = doc.SelectSingleNode ("prop:coreProperties/prop:category", manager)) != null)
201 category = node.InnerXml;
202 if ((node = doc.SelectSingleNode ("prop:coreProperties/prop:contentStatus", manager)) != null)
203 contentStatus = node.InnerXml;
204 if ((node = doc.SelectSingleNode ("prop:coreProperties/prop:contentType", manager)) != null)
205 contentType = node.InnerXml;
206 if ((node = doc.SelectSingleNode ("prop:coreProperties/dcterms:created", manager)) != null)
207 created = DateTime.Parse (node.InnerXml);
208 if ((node = doc.SelectSingleNode ("prop:coreProperties/dc:creator", manager)) != null)
209 creator = node.InnerXml;
210 if ((node = doc.SelectSingleNode ("prop:coreProperties/dc:description", manager)) != null)
211 description = node.InnerXml;
212 if ((node = doc.SelectSingleNode ("prop:coreProperties/dc:identifier", manager)) != null)
213 identifier = node.InnerXml;
214 if ((node = doc.SelectSingleNode ("prop:coreProperties/prop:keywords", manager)) != null)
215 keywords = node.InnerXml;
216 if ((node = doc.SelectSingleNode ("prop:coreProperties/dc:language", manager)) != null)
217 language = node.InnerXml;
218 if ((node = doc.SelectSingleNode ("prop:coreProperties/prop:lastModifiedBy", manager)) != null)
219 lastModifiedBy = node.InnerXml;
220 if ((node = doc.SelectSingleNode ("prop:coreProperties/prop:lastPrinted", manager)) != null)
221 lastPrinted = DateTime.Parse (node.InnerXml);
222 if ((node = doc.SelectSingleNode ("prop:coreProperties/dcterms:modified", manager)) != null)
223 modified = DateTime.Parse (node.InnerXml);
224 if ((node = doc.SelectSingleNode ("prop:coreProperties/prop:revision", manager)) != null)
225 revision = node.InnerXml;
226 if ((node = doc.SelectSingleNode ("prop:coreProperties/dc:subject", manager)) != null)
227 subject = node.InnerXml;
228 if ((node = doc.SelectSingleNode ("prop:coreProperties/dc:title", manager)) != null)
229 title = node.InnerXml;
230 if ((node = doc.SelectSingleNode ("prop:coreProperties/prop:version", manager)) != null)
231 version = node.InnerXml;
234 internal override void WriteTo(XmlTextWriter writer)
236 XmlDocument doc = new XmlDocument ();
237 XmlNamespaceManager manager = new XmlNamespaceManager (doc.NameTable);
238 manager.AddNamespace ("prop", NSPackageProperties);
239 manager.AddNamespace ("dc", NSDc);
240 manager.AddNamespace ("dcterms", NSDcTerms);
241 manager.AddNamespace ("xsi", NSXsi);
243 // Create XML declaration
244 doc.AppendChild (doc.CreateXmlDeclaration ("1.0", "UTF-8", null));
246 // Create root node with required namespace declarations
247 XmlNode coreProperties = doc.AppendChild (doc.CreateNode (XmlNodeType.Element, "coreProperties", NSPackageProperties));
248 coreProperties.Attributes.Append (doc.CreateAttribute ("xmlns:dc")).Value = NSDc;
249 coreProperties.Attributes.Append (doc.CreateAttribute ("xmlns:dcterms")).Value = NSDcTerms;
250 coreProperties.Attributes.Append (doc.CreateAttribute ("xmlns:xsi")).Value = NSXsi;
252 // Create the children
253 if (Category != null)
254 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "category", NSPackageProperties)).InnerXml = Category;
255 if (ContentStatus != null)
256 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "contentStatus", NSPackageProperties)).InnerXml = ContentStatus;
257 if (ContentType != null)
258 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "contentType", NSPackageProperties)).InnerXml = ContentType;
259 if (Created.HasValue)
260 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "dcterms", "created", NSDcTerms)).InnerXml = Created.Value.ToString ();
261 if (Creator != null)
262 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "dc", "creator", NSDc)).InnerXml = Creator;
263 if (Description != null)
264 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "dc", "description", NSDc)).InnerXml = Description;
265 if (Identifier != null)
266 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "dc", "identifier", NSDc)).InnerXml = Identifier;
267 if (Keywords != null)
268 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "keywords", NSPackageProperties)).InnerXml = Keywords;
269 if (Language != null)
270 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "dc", "language", NSDc)).InnerXml = Language;
271 if (LastModifiedBy != null)
272 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "lastModifiedBy", NSPackageProperties)).InnerXml = LastModifiedBy;
273 if (LastPrinted.HasValue)
274 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "lastPrinted", NSPackageProperties)).InnerXml = LastPrinted.Value.ToString ();
275 if (Revision != null)
276 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "revision", NSPackageProperties)).InnerXml = Revision;
277 if (Subject != null)
278 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "dc", "subject", NSDc)).InnerXml = Subject;
279 if (Title != null)
280 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "dc", "title", NSDc)).InnerXml = Title;
281 if (Version != null)
282 coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "version", NSPackageProperties)).InnerXml = Version;
284 if (Modified.HasValue)
286 XmlAttribute att = doc.CreateAttribute("xsi", "type", NSXsi);
287 att.Value = "dcterms:W3CDTF";
289 XmlNode modified = coreProperties.AppendChild (doc.CreateNode (XmlNodeType.Element, "dcterms", "modified", NSDcTerms));
290 modified.Attributes.Append (att);
291 modified.InnerXml = Modified.Value.ToString ();
294 doc.WriteContentTo (writer);