2008-03-30 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[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.nameTable = nt;
160
161                         this.namespaceManager = nsMgr;
162                         if (dtd != null) {
163                                 this.DocTypeName = dtd.Name;
164                                 this.PublicId = dtd.PublicId;
165                                 this.SystemId = dtd.SystemId;
166                                 this.InternalSubset = dtd.InternalSubset;
167                                 this.dtd = dtd;
168                         }
169                         this.encoding = enc;
170
171                         this.BaseURI = baseURI;
172                         this.XmlLang = xmlLang;
173                         this.xmlSpace = xmlSpace;
174
175                         contextItems = new ArrayList ();
176                 }
177                 #endregion
178
179                 #region Fields
180
181                 private string baseURI = String.Empty;
182                 private string docTypeName = String.Empty;
183                 private Encoding encoding;
184                 private string internalSubset = String.Empty;
185                 private XmlNamespaceManager namespaceManager;
186                 private XmlNameTable nameTable;
187                 private string publicID = String.Empty;
188                 private string systemID = String.Empty;
189                 private string xmlLang = String.Empty;
190                 private XmlSpace xmlSpace;
191                 private ArrayList contextItems;
192                 private int contextItemCount;
193                 private DTDObjectModel dtd;
194
195                 #endregion
196
197                 #region Properties
198
199                 public string BaseURI {
200                         get { return baseURI; }
201                         set { baseURI = value != null ? value : String.Empty; }
202                 }
203
204                 public string DocTypeName {
205                         get { return docTypeName != null ? docTypeName : dtd != null ? dtd.Name : null; }
206                         set { docTypeName = value != null ? value : String.Empty; }
207                 }
208
209                 internal DTDObjectModel Dtd {
210                         get { return dtd; }
211                         set { dtd = value; }
212                 }
213
214                 public Encoding Encoding {
215                         get { return encoding; }
216                         set { encoding = value; }
217                 }
218
219                 public string InternalSubset {
220                         get { return internalSubset != null ? internalSubset : dtd != null ? dtd.InternalSubset : null; }
221                         set { internalSubset = value != null ? value : String.Empty; }
222                 }
223
224                 public XmlNamespaceManager NamespaceManager {
225                         get { return namespaceManager; }
226                         set { namespaceManager = value; }
227                 }
228
229                 public XmlNameTable NameTable {
230                         get { return nameTable; }
231                         set { nameTable = value; }
232                 }
233
234                 public string PublicId {
235                         get { return publicID != null ? publicID : dtd != null ? dtd.PublicId : null; }
236                         set { publicID = value != null ? value : String.Empty; }
237                 }
238
239                 public string SystemId {
240                         get { return systemID != null ? systemID : dtd != null ? dtd.SystemId : null; }
241                         set { systemID = value != null ? value : String.Empty; }
242                 }
243
244                 public string XmlLang {
245                         get { return xmlLang; }
246                         set { xmlLang = value != null ? value : String.Empty; }
247                 }
248
249                 public XmlSpace XmlSpace {
250                         get { return xmlSpace; }
251                         set { xmlSpace = value; }
252                 }
253
254                 #endregion
255
256                 #region Methods
257                 internal void PushScope ()
258                 {
259                         ContextItem item = null;
260                         if (contextItems.Count == contextItemCount) {
261                                 item = new ContextItem ();
262                                 contextItems.Add (item);
263                         }
264                         else
265                                 item = (ContextItem) contextItems [contextItemCount];
266                         item.BaseURI = BaseURI;
267                         item.XmlLang = XmlLang;
268                         item.XmlSpace = XmlSpace;
269                         contextItemCount++;
270                 }
271
272                 internal void PopScope ()
273                 {
274                         if (contextItemCount == 0)
275                                 throw new XmlException ("Unexpected end of element scope.");
276                         contextItemCount--;
277                         ContextItem prev = (ContextItem) contextItems [contextItemCount];
278                         baseURI = prev.BaseURI;
279                         xmlLang = prev.XmlLang;
280                         xmlSpace = prev.XmlSpace;
281                 }
282                 #endregion
283         }
284 }