Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System.XML / System.Xml / XmlReaderSettings.cs
1 //
2 // XmlReaderSettings.cs
3 //
4 // Author:
5 //   Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // (C) 2004 Novell Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.IO;
33 using System.Net;
34 using System.Xml.Schema;
35
36 #if !MOONLIGHT
37 using XsValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags;
38 #endif
39
40 namespace System.Xml
41 {
42         public sealed class XmlReaderSettings
43         {
44                 private bool checkCharacters;
45                 private bool closeInput;
46                 private ConformanceLevel conformance;
47                 private bool ignoreComments;
48                 private bool ignoreProcessingInstructions;
49                 private bool ignoreWhitespace;
50                 private int lineNumberOffset;
51                 private int linePositionOffset;
52                 private bool prohibitDtd;
53                 private XmlNameTable nameTable;
54 #if !MOONLIGHT
55                 private XmlSchemaSet schemas;
56                 private bool schemasNeedsInitialization;
57                 private XsValidationFlags validationFlags;
58                 private ValidationType validationType;
59 #endif
60                 private XmlResolver xmlResolver;
61 #if NET_4_0 || NET_2_1
62                 private DtdProcessing dtdProcessing;
63 #endif
64                 private long maxCharactersFromEntities;
65                 private long maxCharactersInDocument;
66
67 #if NET_4_5
68                 private bool isReadOnly;
69                 private bool isAsync;
70 #endif
71
72                 public XmlReaderSettings ()
73                 {
74                         Reset ();
75                 }
76
77 #if !MOONLIGHT
78                 public event ValidationEventHandler ValidationEventHandler;
79 #endif
80
81                 public XmlReaderSettings Clone ()
82                 {
83                         var clone = (XmlReaderSettings) MemberwiseClone ();
84 #if NET_4_5
85                         clone.isReadOnly = false;
86 #endif
87                         return clone;
88                 }
89
90                 public void Reset ()
91                 {
92                         checkCharacters = true;
93                         closeInput = false; // ? not documented
94                         conformance = ConformanceLevel.Document;
95                         ignoreComments = false;
96                         ignoreProcessingInstructions = false;
97                         ignoreWhitespace = false;
98                         lineNumberOffset = 0;
99                         linePositionOffset = 0;
100                         prohibitDtd = true;
101 #if MOONLIGHT
102                         xmlResolver = new XmlXapResolver ();
103 #else
104                         schemas = null;
105                         schemasNeedsInitialization = true;
106                         validationFlags =
107                                 XsValidationFlags.ProcessIdentityConstraints |
108                                 XsValidationFlags.AllowXmlAttributes;
109                         validationType = ValidationType.None;
110                         xmlResolver = new XmlUrlResolver ();
111 #endif
112 #if NET_4_5
113                         isAsync = false;
114 #endif
115                 }
116
117                 public bool CheckCharacters {
118                         get { return checkCharacters; }
119                         set { checkCharacters = value; }
120                 }
121
122                 public bool CloseInput {
123                         get { return closeInput; }
124                         set { closeInput = value; }
125                 }
126
127                 public ConformanceLevel ConformanceLevel {
128                         get { return conformance; }
129                         set { conformance = value; }
130                 }
131 #if NET_4_0 || NET_2_1
132                 public DtdProcessing DtdProcessing {
133                         get { return dtdProcessing; }
134                         set {
135                                 dtdProcessing = value;
136                                 prohibitDtd = (value == DtdProcessing.Prohibit);
137                         }
138                 }
139 #endif
140                 public long MaxCharactersFromEntities {
141                         get { return maxCharactersFromEntities; }
142                         set { maxCharactersFromEntities = value; }
143                 }
144
145                 [MonoTODO ("not used yet")]
146                 public long MaxCharactersInDocument {
147                         get { return maxCharactersInDocument; }
148                         set { maxCharactersInDocument = value; }
149                 }
150
151                 public bool IgnoreComments {
152                         get { return ignoreComments; }
153                         set { ignoreComments = value; }
154                 }
155
156                 public bool IgnoreProcessingInstructions {
157                         get { return ignoreProcessingInstructions; }
158                         set { ignoreProcessingInstructions = value; }
159                 }
160
161                 public bool IgnoreWhitespace {
162                         get { return ignoreWhitespace; }
163                         set { ignoreWhitespace = value; }
164                 }
165
166                 public int LineNumberOffset {
167                         get { return lineNumberOffset; }
168                         set { lineNumberOffset = value; }
169                 }
170
171                 public int LinePositionOffset {
172                         get { return linePositionOffset; }
173                         set { linePositionOffset = value; }
174                 }
175
176 #if NET_4_0
177                 [ObsoleteAttribute("Use DtdProcessing property instead")]
178 #endif
179                 public bool ProhibitDtd {
180                         get { return prohibitDtd; }
181                         set { prohibitDtd = value; }
182                 }
183
184                 // LAMESPEC: MSDN documentation says "An empty XmlNameTable
185                 // object" for default value, but XmlNameTable cannot be
186                 // instantiate. It actually returns null by default.
187                 public XmlNameTable NameTable {
188                         get { return nameTable; }
189                         set { nameTable = value; }
190                 }
191
192 #if !MOONLIGHT
193                 public XmlSchemaSet Schemas {
194                         get {
195                                 if (schemasNeedsInitialization) {
196                                         schemas = new XmlSchemaSet ();
197                                         schemasNeedsInitialization = false;
198                                 }
199                                 return schemas;
200                         }
201                         set {
202                                 schemas = value;
203                                 schemasNeedsInitialization = false;
204                         }
205                 }
206
207                 internal void OnValidationError (object o, ValidationEventArgs e)
208                 {
209                         if (ValidationEventHandler != null)
210                                 ValidationEventHandler (o, e);
211                         else if (e.Severity == XmlSeverityType.Error)
212                                 throw e.Exception;
213                 }
214
215                 internal void SetSchemas (XmlSchemaSet schemas)
216                 {
217                         this.schemas = schemas;
218                 }
219
220                 public XsValidationFlags ValidationFlags {
221                         get { return validationFlags; }
222                         set { validationFlags = value; }
223                 }
224
225                 public ValidationType ValidationType {
226                         get { return validationType; }
227                         set { validationType = value; }
228                 }
229 #endif
230
231                 public XmlResolver XmlResolver {
232                         internal get { return xmlResolver; }
233                         set { xmlResolver = value; }
234                 }
235
236 #if NET_4_5
237                 internal void SetReadOnly ()
238                 {
239                         isReadOnly = true;
240                 }
241
242                 /*
243                  * FIXME: The .NET 4.5 runtime throws an exception when attempting to
244                  *        modify any of the properties after the XmlReader has been constructed.
245                  */
246                 void EnsureWritability ()
247                 {
248                         if (isReadOnly)
249                                 throw new InvalidOperationException ("XmlReaderSettings in read-only");
250                 }
251
252                 public bool Async {
253                         get { return isAsync; }
254                         set {
255                                 EnsureWritability ();
256                                 isAsync = value;
257                         }
258                 }
259 #endif
260         }
261 }