Merge pull request #273 from joncham/bug-getpid
[mono.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Relaxng / RelaxngDefaultDatatypes.cs
1 //
2 // RelaxngDefaultDatatypes.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (c) 2004 Novell Inc.
8 // All rights reserved
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 using System;
32 using System.Xml;
33
34 namespace Commons.Xml.Relaxng
35 {
36         public class RelaxngString : RelaxngDatatype
37         {
38                 static RelaxngString instance;
39                 static RelaxngString ()
40                 {
41                         instance = new RelaxngString ();
42                 }
43
44                 internal static RelaxngString Instance {
45                         get { return instance; }
46                 }
47
48                 public override string Name { get { return "string"; } }
49                 public override string NamespaceURI { get { return String.Empty; } }
50
51                 internal override bool IsContextDependent {
52                         get { return false; }
53                 }
54
55                 public override bool IsValid (string text, XmlReader reader)
56                 {
57                         return true;
58                 }
59
60                 public override object Parse (string text, XmlReader reader)
61                 {
62                         return text;
63                 }
64
65                 public override bool Compare (object o1, object o2)
66                 {
67                         return (string) o1 == (string) o2;
68                 }
69         }
70
71         public class RelaxngToken : RelaxngDatatype
72         {
73                 static RelaxngToken instance;
74                 static RelaxngToken ()
75                 {
76                         instance = new RelaxngToken ();
77                 }
78
79                 internal static RelaxngToken Instance {
80                         get { return instance; }
81                 }
82
83                 public override string Name { get { return "token"; } }
84                 public override string NamespaceURI { get { return String.Empty; } }
85
86                 internal override bool IsContextDependent {
87                         get { return false; }
88                 }
89
90                 public override bool IsValid (string text, XmlReader reader)
91                 {
92                         return true;
93                 }
94
95                 public override object Parse (string text, XmlReader reader)
96                 {
97                         return Util.NormalizeWhitespace (text);
98                 }
99
100                 int SkipWhitespaces (string s, int i)
101                 {
102                         while (i < s.Length) {
103                                 switch (s [i]) {
104                                 case '\n': case '\r': case ' ': case '\t':
105                                         i++;
106                                         continue;
107                                 }
108                                 break;
109                         }
110                         return i;
111                 }
112
113                 public override bool Compare (object o1, object o2)
114                 {
115                         string s1 = o1 as string;
116                         string s2 = o2 as string;
117
118                         int i1 = 0;
119                         int i2 = 0;
120
121                         while (i1 < s1.Length && i2 < s2.Length) {
122                                 i1 = SkipWhitespaces (s1, i1);
123                                 i2 = SkipWhitespaces (s2, i2);
124                                 while (i1 < s1.Length && i2 < s2.Length) {
125                                         if (s1 [i1] != s2 [i2])
126                                                 return false;
127                                         i1++;
128                                         i2++;
129                                         if (i1 == s1.Length || i2 == s2.Length)
130                                                 break;
131                                         if (XmlChar.IsWhitespace (s1 [i1])) {
132                                                 if (!XmlChar.IsWhitespace (s2 [i2]))
133                                                         return false;
134                                                 else
135                                                         break;
136                                         }
137                                         else if (XmlChar.IsWhitespace (s2 [i2]))
138                                                 return false;
139                                 }
140                         }
141                         i1 = SkipWhitespaces (s1, i1);
142                         i2 = SkipWhitespaces (s2, i2);
143                         return i1 == s1.Length && i2 == s2.Length;
144                 }
145
146                 public override bool CompareString (string s1, string s2, XmlReader reader)
147                 {
148                         return Compare (s1, s2);
149                 }
150         }
151 }