Merge pull request #93 from konrad-kruczynski/dispatcher_timer_fix
[mono.git] / mcs / class / System.XML / System.Xml / XmlParserContext.cs
1 //
2 // System.Xml.XmlParserContext
3 //
4 // Author:
5 //   Jason Diamond (jason@injektilo.org)
6 //   Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 //
8 // (C) 2001, 2002 Jason Diamond  http://injektilo.org/
9 // (C) 2003 Atsushi Enomoto
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 using System.Collections;
33 using System.IO;
34 using System.Text;
35 using Mono.Xml;
36
37 #if NET_2_0
38 using XmlTextReaderImpl = Mono.Xml2.XmlTextReader;
39 #else
40 using XmlTextReaderImpl = System.Xml.XmlTextReader;
41 #endif
42
43 namespace System.Xml
44 {
45         public class XmlParserContext
46         {
47                 #region Class
48                 class ContextItem
49                 {
50                         public string BaseURI;
51                         public string XmlLang;
52                         public XmlSpace XmlSpace;
53                 }
54                 #endregion
55
56                 #region Constructors
57
58                 public XmlParserContext (
59                         XmlNameTable nt,
60                         XmlNamespaceManager nsMgr,
61                         string xmlLang,
62                         XmlSpace xmlSpace) :
63
64                         this (
65                                 nt,
66                                 nsMgr,
67                                 null,
68                                 null,
69                                 null,
70                                 null,
71                                 null,
72                                 xmlLang,
73                                 xmlSpace,
74                                 null
75                         )
76                 {
77                 }
78
79                 public XmlParserContext (
80                         XmlNameTable nt,
81                         XmlNamespaceManager nsMgr,
82                         string xmlLang,
83                         XmlSpace xmlSpace,
84                         Encoding enc) :
85
86                         this (
87                                 nt,
88                                 nsMgr,
89                                 null,
90                                 null,
91                                 null,
92                                 null,
93                                 null,
94                                 xmlLang,
95                                 xmlSpace,
96                                 enc
97                         )
98                 {
99                 }
100
101                 public XmlParserContext (
102                         XmlNameTable nt,
103                         XmlNamespaceManager nsMgr,
104                         string docTypeName,
105                         string pubId,
106                         string sysId,
107                         string internalSubset,
108                         string baseURI,
109                         string xmlLang,
110                         XmlSpace xmlSpace) :
111
112                         this (
113                                 nt,
114                                 nsMgr,
115                                 docTypeName,
116                                 pubId,
117                                 sysId,
118                                 internalSubset,
119                                 baseURI,
120                                 xmlLang,
121                                 xmlSpace,
122                                 null
123                         )
124                 {
125                 }
126
127                 public XmlParserContext (
128                         XmlNameTable nt,
129                         XmlNamespaceManager nsMgr,
130                         string docTypeName,
131                         string pubId,
132                         string sysId,
133                         string internalSubset,
134                         string baseURI,
135                         string xmlLang,
136                         XmlSpace xmlSpace,
137                         Encoding enc)
138                         : this (
139                                 nt,
140                                 nsMgr,
141                                 (docTypeName != null && docTypeName != String.Empty) ?
142                                         new XmlTextReaderImpl (TextReader.Null, nt).GenerateDTDObjectModel (
143                                                 docTypeName, pubId, sysId, internalSubset) : null,
144                                 baseURI,
145                                 xmlLang,
146                                 xmlSpace,
147                                 enc)
148                 {
149                 }
150
151                 internal XmlParserContext (XmlNameTable nt,
152                         XmlNamespaceManager nsMgr,
153                         DTDObjectModel dtd,
154                         string baseURI,
155                         string xmlLang,
156                         XmlSpace xmlSpace,
157                         Encoding enc)
158                 {
159                         this.namespaceManager = nsMgr;
160                         this.nameTable = nt != null ? nt : nsMgr != null ? nsMgr.NameTable : null;
161                         if (dtd != null) {
162                                 this.DocTypeName = dtd.Name;
163                                 this.PublicId = dtd.PublicId;
164                                 this.SystemId = dtd.SystemId;
165                                 this.InternalSubset = dtd.InternalSubset;
166                                 this.dtd = dtd;
167                         }
168                         this.encoding = enc;
169
170                         this.BaseURI = baseURI;
171                         this.XmlLang = xmlLang;
172                         this.xmlSpace = xmlSpace;
173
174                         contextItems = new ArrayList ();
175                 }
176                 #endregion
177
178                 #region Fields
179
180                 private string baseURI = String.Empty;
181                 private string docTypeName = String.Empty;
182                 private Encoding encoding;
183                 private string internalSubset = String.Empty;
184                 private XmlNamespaceManager namespaceManager;
185                 private XmlNameTable nameTable;
186                 private string publicID = String.Empty;
187                 private string systemID = String.Empty;
188                 private string xmlLang = String.Empty;
189                 private XmlSpace xmlSpace;
190                 private ArrayList contextItems;
191                 private int contextItemCount;
192                 private DTDObjectModel dtd;
193
194                 #endregion
195
196                 #region Properties
197
198                 public string BaseURI {
199                         get { return baseURI; }
200                         set { baseURI = value != null ? value : String.Empty; }
201                 }
202
203                 public string DocTypeName {
204                         get { return docTypeName != null ? docTypeName : dtd != null ? dtd.Name : null; }
205                         set { docTypeName = value != null ? value : String.Empty; }
206                 }
207
208                 internal DTDObjectModel Dtd {
209                         get { return dtd; }
210                         set { dtd = value; }
211                 }
212
213                 public Encoding Encoding {
214                         get { return encoding; }
215                         set { encoding = value; }
216                 }
217
218                 public string InternalSubset {
219                         get { return internalSubset != null ? internalSubset : dtd != null ? dtd.InternalSubset : null; }
220                         set { internalSubset = value != null ? value : String.Empty; }
221                 }
222
223                 public XmlNamespaceManager NamespaceManager {
224                         get { return namespaceManager; }
225                         set { namespaceManager = value; }
226                 }
227
228                 public XmlNameTable NameTable {
229                         get { return nameTable; }
230                         set { nameTable = value; }
231                 }
232
233                 public string PublicId {
234                         get { return publicID != null ? publicID : dtd != null ? dtd.PublicId : null; }
235                         set { publicID = value != null ? value : String.Empty; }
236                 }
237
238                 public string SystemId {
239                         get { return systemID != null ? systemID : dtd != null ? dtd.SystemId : null; }
240                         set { systemID = value != null ? value : String.Empty; }
241                 }
242
243                 public string XmlLang {
244                         get { return xmlLang; }
245                         set { xmlLang = value != null ? value : String.Empty; }
246                 }
247
248                 public XmlSpace XmlSpace {
249                         get { return xmlSpace; }
250                         set { xmlSpace = value; }
251                 }
252
253                 #endregion
254
255                 #region Methods
256                 internal void PushScope ()
257                 {
258                         ContextItem item = null;
259                         if (contextItems.Count == contextItemCount) {
260                                 item = new ContextItem ();
261                                 contextItems.Add (item);
262                         }
263                         else
264                                 item = (ContextItem) contextItems [contextItemCount];
265                         item.BaseURI = BaseURI;
266                         item.XmlLang = XmlLang;
267                         item.XmlSpace = XmlSpace;
268                         contextItemCount++;
269                 }
270
271                 internal void PopScope ()
272                 {
273                         if (contextItemCount == 0)
274                                 throw new XmlException ("Unexpected end of element scope.");
275                         contextItemCount--;
276                         ContextItem prev = (ContextItem) contextItems [contextItemCount];
277                         baseURI = prev.BaseURI;
278                         xmlLang = prev.XmlLang;
279                         xmlSpace = prev.XmlSpace;
280                 }
281                 #endregion
282         }
283 }