2009-04-14 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Runtime.Serialization / System.Xml / XmlDictionaryReaderQuotas.cs
1 //
2 // XmlDictionaryReaderQuotas.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 #if NET_2_0
29 namespace System.Xml
30 {
31         public sealed class XmlDictionaryReaderQuotas
32         {
33                 static XmlDictionaryReaderQuotas max;
34
35                 static XmlDictionaryReaderQuotas ()
36                 {
37                         max = new XmlDictionaryReaderQuotas (true);
38                 }
39
40                 readonly bool is_readonly;
41                 int array_len, bytes, depth, nt_chars, text_len;
42
43                 public XmlDictionaryReaderQuotas ()
44                         : this (false)
45                 {
46                 }
47
48                 private XmlDictionaryReaderQuotas (bool max)
49                 {
50                         is_readonly = max;
51                         array_len = max ? int.MaxValue : 0x4000;
52                         bytes = max ? int.MaxValue : 0x1000;
53                         depth = max ? int.MaxValue : 0x20;
54                         nt_chars = max ? int.MaxValue : 0x4000;
55                         text_len = max ? int.MaxValue : 0x2000;
56                 }
57
58                 public static XmlDictionaryReaderQuotas Max {
59                         get { return max; }
60                 }
61
62                 public int MaxArrayLength {
63                         get { return array_len; }
64                         set { array_len = Check (value); }
65                 }
66
67                 public int MaxBytesPerRead {
68                         get { return bytes; }
69                         set { bytes = Check (value); }
70                 }
71
72                 public int MaxDepth {
73                         get { return depth; }
74                         set { depth = Check (value); }
75                 }
76
77                 public int MaxNameTableCharCount {
78                         get { return nt_chars; }
79                         set { nt_chars = Check (value); }
80                 }
81
82                 public int MaxStringContentLength {
83                         get { return text_len; }
84                         set { text_len = Check (value); }
85                 }
86
87                 private int Check (int value)
88                 {
89                         if (is_readonly)
90                                 throw new InvalidOperationException ("This quota is read-only.");
91                         if (value <= 0)
92                                 throw new ArgumentException ("Value must be positive integer.");
93                         return value;
94                 }
95
96                 public void CopyTo (XmlDictionaryReaderQuotas quota)
97                 {
98                         quota.array_len = array_len;
99                         quota.bytes = bytes;
100                         quota.depth = depth;
101                         quota.nt_chars = nt_chars;
102                         quota.text_len = text_len;
103                 }
104         }
105 }
106 #endif