選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Check.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //using DocumentFormat.OpenXml.Packaging;
  7. namespace System.IO.Packaging
  8. {
  9. internal static class Check
  10. {
  11. static void NotNull(object o, string name)
  12. {
  13. if (o == null)
  14. throw new ArgumentNullException(name);
  15. }
  16. public static void ContentTypeIsValid(string contentType)
  17. {
  18. if (string.IsNullOrEmpty(contentType))
  19. return;
  20. // Must be in form of: type/subtype
  21. int index = contentType.IndexOf('/');
  22. bool result = (index > 0) && contentType.Length > (index + 1) && contentType.IndexOf('/', index + 1) == -1;
  23. if (!result)
  24. throw new ArgumentException("contentType", "contentType must be in the form of 'type/subtype'");
  25. }
  26. public static void Id(object id)
  27. {
  28. NotNull(id, "id");
  29. }
  30. public static void IdIsValid(string id)
  31. {
  32. if (id == null)
  33. return;
  34. // If the ID is a zero string, need to throw a ArgNullEx
  35. if (id.Length == 0)
  36. throw new ArgumentNullException("id", "Cannot be whitespace or empty");
  37. // FIXME: I need to XSD parse this to make sure it's valid
  38. // If it's not, throw an XmlException
  39. }
  40. private static bool EmptyOrBlank(string s)
  41. {
  42. return (s != null && (s == "" || s.Trim().Length == 0));
  43. }
  44. private static void PartUriDoesntEndWithSlash(Uri uri)
  45. {
  46. var s = !uri.IsAbsoluteUri ? uri.OriginalString
  47. : uri.GetComponents(UriComponents.Path, UriFormat.UriEscaped);
  48. // We allow '/' at uri's beggining.
  49. if ((s.Length > 1) && s.EndsWith("/"))
  50. {
  51. throw new ArgumentException("Part URI cannot end with a forward slash.");
  52. }
  53. }
  54. public static void Package(object package)
  55. {
  56. if (package == null)
  57. throw new ArgumentNullException("package");
  58. }
  59. public static void PackageUri(object packageUri)
  60. {
  61. NotNull(packageUri, "packageUri");
  62. }
  63. public static void PackageUriIsValid(Uri packageUri)
  64. {
  65. if (!packageUri.IsAbsoluteUri)
  66. throw new ArgumentException("packageUri", "Uri must be absolute");
  67. }
  68. public static void PackUriIsValid(Uri packUri)
  69. {
  70. if (!packUri.IsAbsoluteUri)
  71. throw new ArgumentException("packUri", "PackUris must be absolute");
  72. if (packUri.Scheme != PackUriHelper.UriSchemePack)
  73. throw new ArgumentException("packUri", "Uri scheme is not a valid PackUri scheme");
  74. }
  75. public static void PartUri(object partUri)
  76. {
  77. if (partUri == null)
  78. throw new ArgumentNullException("partUri");
  79. }
  80. public static void PartUriIsValid(Uri partUri)
  81. {
  82. if (!partUri.OriginalString.StartsWith("/"))
  83. throw new ArgumentException("PartUris must start with '/'");
  84. if (partUri.IsAbsoluteUri)
  85. throw new ArgumentException("PartUris cannot be absolute");
  86. }
  87. public static void RelationshipTypeIsValid(string relationshipType)
  88. {
  89. if (relationshipType == null)
  90. throw new ArgumentNullException("relationshipType");
  91. if (EmptyOrBlank(relationshipType))
  92. throw new ArgumentException("relationshipType", "Cannot be whitespace or empty");
  93. }
  94. public static void PartUri(Uri partUri)
  95. {
  96. if (partUri == null)
  97. throw new ArgumentNullException("partUri");
  98. if (partUri.IsAbsoluteUri)
  99. throw new ArgumentException("partUri", "Absolute URIs are not supported");
  100. if (string.IsNullOrEmpty(partUri.OriginalString))
  101. throw new ArgumentException("partUri", "Part uri cannot be an empty string");
  102. }
  103. public static void PackUri(Uri packUri)
  104. {
  105. NotNull(packUri, "packUri");
  106. }
  107. public static void SourcePartUri(Uri sourcePartUri)
  108. {
  109. NotNull(sourcePartUri, "sourcePartUri");
  110. PartUriDoesntEndWithSlash(sourcePartUri);
  111. }
  112. public static void TargetPartUri(Uri targetPartUri)
  113. {
  114. NotNull(targetPartUri, "targetPartUri");
  115. PartUriDoesntEndWithSlash(targetPartUri);
  116. }
  117. public static void SourceUri(Uri sourceUri)
  118. {
  119. if (sourceUri == null)
  120. throw new ArgumentNullException("sourceUri");
  121. // if (sourceUri.IsAbsoluteUri)
  122. // throw new ArgumentException ("sourceUri", "Absolute URIs are not supported");
  123. if (string.IsNullOrEmpty(sourceUri.OriginalString))
  124. throw new ArgumentException("sourceUri", "Part uri cannot be an empty string");
  125. }
  126. public static void TargetUri(Uri targetUri)
  127. {
  128. if (targetUri == null)
  129. throw new ArgumentNullException("targetUri");
  130. // if (targetUri.IsAbsoluteUri)
  131. // throw new ArgumentException ("targetUri", "Absolute URIs are not supported");
  132. if (string.IsNullOrEmpty(targetUri.OriginalString))
  133. throw new ArgumentException("targetUri", "Part uri cannot be an empty string");
  134. }
  135. }
  136. }