Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / WindowsBase / System.IO.Packaging / Check.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Alan McGovern (amcgovern@novell.com)
24 //
25
26 using System;
27
28 namespace System.IO.Packaging
29 {
30         internal static class Check
31         {
32                 static void NotNull (object o, string name)
33                 {
34                         if (o == null)
35                                 throw new ArgumentNullException (name);
36                 }
37                 
38                 public static void ContentTypeIsValid (string contentType)
39                 {
40                         if (string.IsNullOrEmpty (contentType))
41                                 return;
42
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;
46
47                         if(!result)
48                                 throw new ArgumentException ("contentType", "contentType must be in the form of 'type/subtype'");
49                 }
50
51                 public static void Id (object id)
52                 {
53                         NotNull (id, "id");
54                 }
55                 
56                 public static void IdIsValid (string id)
57                 {
58                         if (id == null)
59                                 return;
60
61                         // If the ID is a zero string, need to throw a ArgNullEx
62                         if (id.Length == 0)
63                                 throw new ArgumentNullException ("id", "Cannot be whitespace or empty");
64
65                         // FIXME: I need to XSD parse this to make sure it's valid
66                         // If it's not, throw an XmlException
67                 }
68
69                 private static bool EmptyOrBlank (string s)
70                 {
71                         return (s != null && (s == "" || s.Trim ().Length == 0));
72                 }
73                 
74                 public static void Package(object package)
75                 {
76                         if (package == null)
77                                 throw new ArgumentNullException ("package");
78                 }
79
80
81                 public static void PackageUri (object packageUri)
82                 {
83                         NotNull (packageUri, "packageUri");
84                 }
85
86                 public static void PackageUriIsValid (Uri packageUri)
87                 {
88                         if (!packageUri.IsAbsoluteUri)
89                                 throw new ArgumentException ("packageUri", "Uri must be absolute");
90                 }
91                 
92                 public static void PackUriIsValid (Uri packUri)
93                 {
94                         if (packUri.Scheme != PackUriHelper.UriSchemePack)
95                                 throw new ArgumentException ("packUri", "Uri scheme is not a valid PackUri scheme");
96                             
97                         if (!packUri.IsAbsoluteUri)
98                                 throw new ArgumentException ("packUri", "PackUris must be absolute");
99                 }
100
101                 public static void PartUri (object partUri)
102                 {
103                         if (partUri == null)
104                                 throw new ArgumentNullException ("partUri");
105                 }
106
107                 public static void PartUriIsValid (Uri partUri)
108                 {
109                         if (!partUri.OriginalString.StartsWith ("/"))
110                                 throw new UriFormatException ("PartUris must start with '/'");
111
112                         if (partUri.IsAbsoluteUri)
113                                 throw new UriFormatException ("PartUris cannot be absolute");
114                 }
115
116                 public static void RelationshipTypeIsValid (string relationshipType)
117                 {
118                         if (relationshipType == null)
119                                 throw new ArgumentNullException ("relationshipType");
120                         if (EmptyOrBlank (relationshipType))
121                                 throw new ArgumentException ("relationshipType", "Cannot be whitespace or empty");
122                 }
123
124                 public static void PartUri (Uri partUri)
125                 {
126                         if (partUri == null)
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");
132                 }
133
134                 public static void SourcePartUri (Uri sourcePartUri)
135                 {
136                         NotNull(sourcePartUri, "sourcePartUri");
137                 }
138                 public static void SourceUri (Uri sourceUri)
139                 {
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");
146                 }
147
148                 public static void TargetUri (Uri targetUri)
149                 {
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");
156                 }
157         }
158 }