1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
23 // Alan McGovern (amcgovern@novell.com)
28 namespace System.IO.Packaging
30 internal static class Check
32 static void NotNull (object o, string name)
35 throw new ArgumentNullException (name);
38 public static void ContentTypeIsValid (string contentType)
40 if (string.IsNullOrEmpty (contentType))
43 // Must be in form of: type/subtype
44 int index = contentType.IndexOf ('/');
45 bool result = (index > 0) && contentType.Length > (index + 1) && contentType.IndexOf ('/', index + 1) == -1;
48 throw new ArgumentException ("contentType", "contentType must be in the form of 'type/subtype'");
51 public static void Id (object id)
56 public static void IdIsValid (string id)
61 // If the ID is a zero string, need to throw a ArgNullEx
63 throw new ArgumentNullException ("id", "Cannot be whitespace or empty");
65 // FIXME: I need to XSD parse this to make sure it's valid
66 // If it's not, throw an XmlException
69 private static bool EmptyOrBlank (string s)
71 return (s != null && (s == "" || s.Trim ().Length == 0));
74 public static void Package(object package)
77 throw new ArgumentNullException ("package");
81 public static void PackageUri (object packageUri)
83 NotNull (packageUri, "packageUri");
86 public static void PackageUriIsValid (Uri packageUri)
88 if (!packageUri.IsAbsoluteUri)
89 throw new ArgumentException ("packageUri", "Uri must be absolute");
92 public static void PackUriIsValid (Uri packUri)
94 if (packUri.Scheme != PackUriHelper.UriSchemePack)
95 throw new ArgumentException ("packUri", "Uri scheme is not a valid PackUri scheme");
97 if (!packUri.IsAbsoluteUri)
98 throw new ArgumentException ("packUri", "PackUris must be absolute");
101 public static void PartUri (object partUri)
104 throw new ArgumentNullException ("partUri");
107 public static void PartUriIsValid (Uri partUri)
109 if (!partUri.OriginalString.StartsWith ("/"))
110 throw new UriFormatException ("PartUris must start with '/'");
112 if (partUri.IsAbsoluteUri)
113 throw new UriFormatException ("PartUris cannot be absolute");
116 public static void RelationshipTypeIsValid (string relationshipType)
118 if (relationshipType == null)
119 throw new ArgumentNullException ("relationshipType");
120 if (EmptyOrBlank (relationshipType))
121 throw new ArgumentException ("relationshipType", "Cannot be whitespace or empty");
124 public static void PartUri (Uri partUri)
127 throw new ArgumentNullException ("partUri");
128 if (partUri.IsAbsoluteUri)
129 throw new ArgumentException ("partUri", "Absolute URIs are not supported");
130 if (string.IsNullOrEmpty (partUri.OriginalString))
131 throw new ArgumentException ("partUri", "Part uri cannot be an empty string");
134 public static void SourcePartUri (Uri sourcePartUri)
136 NotNull(sourcePartUri, "sourcePartUri");
138 public static void SourceUri (Uri sourceUri)
140 if (sourceUri == null)
141 throw new ArgumentNullException ("sourceUri");
142 // if (sourceUri.IsAbsoluteUri)
143 // throw new ArgumentException ("sourceUri", "Absolute URIs are not supported");
144 if (string.IsNullOrEmpty (sourceUri.OriginalString))
145 throw new ArgumentException ("sourceUri", "Part uri cannot be an empty string");
148 public static void TargetUri (Uri targetUri)
150 if (targetUri == null)
151 throw new ArgumentNullException ("targetUri");
152 // if (targetUri.IsAbsoluteUri)
153 // throw new ArgumentException ("targetUri", "Absolute URIs are not supported");
154 if (string.IsNullOrEmpty (targetUri.OriginalString))
155 throw new ArgumentException ("targetUri", "Part uri cannot be an empty string");