Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. //using DocumentFormat.OpenXml.Packaging;
  9. namespace System.IO.Packaging
  10. {
  11. public abstract class PackagePart
  12. {
  13. string contentType;
  14. internal bool IsRelationship { get; set; }
  15. int relationshipId;
  16. Dictionary<string, PackageRelationship> relationships;
  17. PackageRelationshipCollection relationshipsCollection = new PackageRelationshipCollection();
  18. Dictionary<string, PackageRelationship> Relationships
  19. {
  20. get
  21. {
  22. if (relationships == null)
  23. {
  24. relationships = new Dictionary<string, PackageRelationship>(StringComparer.OrdinalIgnoreCase);
  25. if (Package.PartExists(RelationshipsPartUri))
  26. using (Stream s = Package.GetPart(RelationshipsPartUri).GetStream())
  27. LoadRelationships(relationships, s);
  28. }
  29. return relationships;
  30. }
  31. }
  32. Stream PartStream { get; set; }
  33. internal Uri RelationshipsPartUri
  34. {
  35. get;
  36. set;
  37. }
  38. protected PackagePart(Package package, Uri partUri)
  39. : this(package, partUri, null)
  40. {
  41. }
  42. protected internal PackagePart(Package package, Uri partUri, string contentType)
  43. : this(package, partUri, contentType, CompressionOption.Normal)
  44. {
  45. }
  46. protected internal PackagePart(Package package, Uri partUri, string contentType, CompressionOption compressionOption)
  47. {
  48. Check.Package(package);
  49. Check.PartUri(partUri);
  50. Check.ContentTypeIsValid(contentType);
  51. Package = package;
  52. Uri = partUri;
  53. ContentType = contentType;
  54. CompressionOption = compressionOption;
  55. RelationshipsPartUri = PackUriHelper.GetRelationshipPartUri(Uri);
  56. }
  57. public CompressionOption CompressionOption
  58. {
  59. get;
  60. private set;
  61. }
  62. public string ContentType
  63. {
  64. get
  65. {
  66. if (contentType == null && (contentType = GetContentTypeCore()) == null)
  67. throw new NotSupportedException("If contentType is not supplied in the constructor, GetContentTypeCore must be overridden");
  68. return contentType;
  69. }
  70. private set
  71. {
  72. contentType = value;
  73. }
  74. }
  75. public Package Package
  76. {
  77. get;
  78. internal set;
  79. }
  80. public Uri Uri
  81. {
  82. get;
  83. private set;
  84. }
  85. private void CheckIsRelationship()
  86. {
  87. if (IsRelationship)
  88. throw new InvalidOperationException("A relationship cannot have relationships to other parts");
  89. }
  90. public PackageRelationship CreateRelationship(Uri targetUri, TargetMode targetMode, string relationshipType)
  91. {
  92. return CreateRelationship(targetUri, targetMode, relationshipType, null);
  93. }
  94. public PackageRelationship CreateRelationship(Uri targetUri, TargetMode targetMode, string relationshipType, string id)
  95. {
  96. return CreateRelationship(targetUri, targetMode, relationshipType, id, false);
  97. }
  98. private PackageRelationship CreateRelationship(Uri targetUri, TargetMode targetMode, string relationshipType, string id, bool loading)
  99. {
  100. //Package.CheckIsReadOnly();
  101. Check.TargetUri(targetUri);
  102. Check.RelationshipTypeIsValid(relationshipType);
  103. Check.IdIsValid(id);
  104. if (id == null)
  105. id = NextId();
  106. if (Relationships.ContainsKey(id))
  107. throw new XmlException("A relationship with this ID already exists");
  108. PackageRelationship r = new PackageRelationship(id, Package, relationshipType, Uri, targetMode, targetUri);
  109. Relationships.Add(r.Id, r);
  110. if (!loading)
  111. WriteRelationships();
  112. return r;
  113. }
  114. public void DeleteRelationship(string id)
  115. {
  116. Package.CheckIsReadOnly();
  117. CheckIsRelationship();
  118. Relationships.Remove(id);
  119. WriteRelationships();
  120. }
  121. void LoadRelationships(Dictionary<string, PackageRelationship> relationships, Stream stream)
  122. {
  123. XmlDocument doc = new XmlDocument();
  124. doc.Load(stream);
  125. XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
  126. manager.AddNamespace("rel", Package.RelationshipNamespace);
  127. foreach (XmlNode node in doc.SelectNodes("/rel:Relationships/*", manager))
  128. {
  129. TargetMode mode = TargetMode.Internal;
  130. if (node.Attributes["TargetMode"] != null)
  131. mode = (TargetMode)Enum.Parse(typeof(TargetMode), node.Attributes["TargetMode"].Value);
  132. CreateRelationship(new Uri(node.Attributes["Target"].Value.ToString(), UriKind.RelativeOrAbsolute),
  133. mode,
  134. node.Attributes["Type"].Value.ToString(),
  135. node.Attributes["Id"].Value.ToString(),
  136. true);
  137. }
  138. }
  139. public bool RelationshipExists(string id)
  140. {
  141. CheckIsRelationship();
  142. return Relationships.ContainsKey(id);
  143. }
  144. public PackageRelationship GetRelationship(string id)
  145. {
  146. CheckIsRelationship();
  147. return Relationships[id];
  148. }
  149. public PackageRelationshipCollection GetRelationships()
  150. {
  151. CheckIsRelationship();
  152. relationshipsCollection.Relationships.Clear();
  153. relationshipsCollection.Relationships.AddRange(Relationships.Values);
  154. return relationshipsCollection;
  155. }
  156. public PackageRelationshipCollection GetRelationshipsByType(string relationshipType)
  157. {
  158. CheckIsRelationship();
  159. PackageRelationshipCollection collection = new PackageRelationshipCollection();
  160. foreach (PackageRelationship r in Relationships.Values)
  161. if (r.RelationshipType == relationshipType)
  162. collection.Relationships.Add(r);
  163. return collection;
  164. }
  165. public Stream GetStream()
  166. {
  167. return GetStream(Package.FileOpenAccess == FileAccess.Read && !IsRelationship ? FileMode.Open : FileMode.OpenOrCreate);
  168. }
  169. public Stream GetStream(FileMode mode)
  170. {
  171. return GetStream(mode, IsRelationship ? FileAccess.ReadWrite : Package.FileOpenAccess);
  172. }
  173. public Stream GetStream(FileMode mode, FileAccess access)
  174. {
  175. bool notAllowed = mode == FileMode.Append || mode == FileMode.CreateNew || mode == FileMode.Truncate;
  176. if (access != FileAccess.Read && notAllowed)
  177. throw new ArgumentException(string.Format(string.Format("FileMode '{0}' not supported", mode)));
  178. if (access == FileAccess.Read && (notAllowed || mode == FileMode.Create))
  179. throw new IOException(string.Format("FileMode '{0}' not allowed on a readonly stream", mode));
  180. return GetStreamCore(mode, access);
  181. }
  182. protected abstract Stream GetStreamCore(FileMode mode, FileAccess access);
  183. protected virtual string GetContentTypeCore()
  184. {
  185. return null;
  186. }
  187. private string NextId()
  188. {
  189. while (true)
  190. {
  191. string s = "Re" + relationshipId.ToString();
  192. if (!RelationshipExists(s))
  193. return s;
  194. relationshipId++;
  195. }
  196. }
  197. void WriteRelationships()
  198. {
  199. bool exists = Package.PartExists(RelationshipsPartUri);
  200. if (exists && Relationships.Count == 0)
  201. {
  202. Package.DeletePart(RelationshipsPartUri);
  203. return;
  204. }
  205. if (!exists)
  206. {
  207. PackagePart part = Package.CreatePart(RelationshipsPartUri, Package.RelationshipContentType);
  208. part.IsRelationship = true;
  209. }
  210. using (Stream s = Package.GetPart(RelationshipsPartUri).GetStream())
  211. Package.WriteRelationships(Relationships, s);
  212. }
  213. }
  214. }