2005-12-05 Lluis Sanchez Gual <lluis@novell.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.Text;
34 using Mono.Xml;
35
36 #if NET_2_0
37 using XmlTextReaderImpl = Mono.Xml2.XmlTextReader;
38 #else
39 using XmlTextReaderImpl = System.Xml.XmlTextReader;
40 #endif
41
42 namespace System.Xml
43 {
44         public class XmlParserContext
45         {
46                 #region Class
47                 class ContextItem
48                 {
49                         public string BaseURI;
50                         public string XmlLang;
51                         public XmlSpace XmlSpace;
52                 }
53                 #endregion
54
55                 #region Constructors
56
57                 public XmlParserContext (
58                         XmlNameTable nt,
59                         XmlNamespaceManager nsMgr,
60                         string xmlLang,
61                         XmlSpace xmlSpace) :
62
63                         this (
64                                 nt,
65                                 nsMgr,
66                                 null,
67                                 null,
68                                 null,
69                                 null,
70                                 null,
71                                 xmlLang,
72                                 xmlSpace,
73                                 null
74                         )
75                 {
76                 }
77
78                 public XmlParserContext (
79                         XmlNameTable nt,
80                         XmlNamespaceManager nsMgr,
81                         string xmlLang,
82                         XmlSpace xmlSpace,
83                         Encoding enc) :
84
85                         this (
86                                 nt,
87                                 nsMgr,
88                                 null,
89                                 null,
90                                 null,
91                                 null,
92                                 null,
93                                 xmlLang,
94                                 xmlSpace,
95                                 enc
96                         )
97                 {
98                 }
99
100                 public XmlParserContext (
101                         XmlNameTable nt,
102                         XmlNamespaceManager nsMgr,
103                         string docTypeName,
104                         string pubId,
105                         string sysId,
106                         string internalSubset,
107                         string baseURI,
108                         string xmlLang,
109                         XmlSpace xmlSpace) :
110
111                         this (
112                                 nt,
113                                 nsMgr,
114                                 docTypeName,
115                                 pubId,
116                                 sysId,
117                                 internalSubset,
118                                 baseURI,
119                                 xmlLang,
120                                 xmlSpace,
121                                 null
122                         )
123                 {
124                 }
125
126                 public XmlParserContext (
127                         XmlNameTable nt,
128                         XmlNamespaceManager nsMgr,
129                         string docTypeName,
130                         string pubId,
131                         string sysId,
132                         string internalSubset,
133                         string baseURI,
134                         string xmlLang,
135                         XmlSpace xmlSpace,
136                         Encoding enc)
137                         : this (
138                                 nt,
139                                 nsMgr,
140                                 (docTypeName != null && docTypeName != String.Empty) ?
141                                         new XmlTextReaderImpl ("", nt).GenerateDTDObjectModel (
142                                                 docTypeName, pubId, sysId, internalSubset) : null,
143                                 baseURI,
144                                 xmlLang,
145                                 xmlSpace,
146                                 enc)
147                 {
148                 }
149
150                 internal XmlParserContext (XmlNameTable nt,
151                         XmlNamespaceManager nsMgr,
152                         DTDObjectModel dtd,
153                         string baseURI,
154                         string xmlLang,
155                         XmlSpace xmlSpace,
156                         Encoding enc)
157                 {
158                         if (nt == null)
159                                 this.nameTable = nsMgr == null ? new NameTable () : nsMgr.NameTable;
160                         else
161                                 this.nameTable = nt;
162
163                         this.namespaceManager = nsMgr != null ? nsMgr : new XmlNamespaceManager (nameTable);
164                         if (dtd != null) {
165                                 this.DocTypeName = dtd.Name;
166                                 this.PublicId = dtd.PublicId;
167                                 this.SystemId = dtd.SystemId;
168                                 this.InternalSubset = dtd.InternalSubset;
169                                 this.dtd = dtd;
170                         }
171                         this.encoding = enc;
172
173                         this.BaseURI = baseURI;
174                         this.XmlLang = xmlLang;
175                         this.xmlSpace = xmlSpace;
176
177                         contextItems = new ArrayList ();
178                 }
179                 #endregion
180
181                 #region Fields
182
183                 private string baseURI = String.Empty;
184                 private string docTypeName = String.Empty;
185                 private Encoding encoding;
186                 private string internalSubset = String.Empty;
187                 private XmlNamespaceManager namespaceManager;
188                 private XmlNameTable nameTable;
189                 private string publicID = String.Empty;
190                 private string systemID = String.Empty;
191                 private string xmlLang = String.Empty;
192                 private XmlSpace xmlSpace;
193                 private ArrayList contextItems;
194                 private int contextItemCount;
195                 private DTDObjectModel dtd;
196
197                 #endregion
198
199                 #region Properties
200
201                 public string BaseURI {
202                         get { return baseURI; }
203                         set { baseURI = value != null ? value : String.Empty; }
204                 }
205
206                 public string DocTypeName {
207                         get { return docTypeName != null ? docTypeName : dtd != null ? dtd.Name : null; }
208                         set { docTypeName = value != null ? value : String.Empty; }
209                 }
210
211                 internal DTDObjectModel Dtd {
212                         get { return dtd; }
213                         set { dtd = value; }
214                 }
215
216                 public Encoding Encoding {
217                         get { return encoding; }
218                         set { encoding = value; }
219                 }
220
221                 public string InternalSubset {
222                         get { return internalSubset != null ? internalSubset : dtd != null ? dtd.InternalSubset : null; }
223                         set { internalSubset = value != null ? value : String.Empty; }
224                 }
225
226                 public XmlNamespaceManager NamespaceManager {
227                         get { return namespaceManager; }
228                         set { namespaceManager = value; }
229                 }
230
231                 public XmlNameTable NameTable {
232                         get { return nameTable; }
233                         set { nameTable = value; }
234                 }
235
236                 public string PublicId {
237                         get { return publicID != null ? publicID : dtd != null ? dtd.PublicId : null; }
238                         set { publicID = value != null ? value : String.Empty; }
239                 }
240
241                 public string SystemId {
242                         get { return systemID != null ? systemID : dtd != null ? dtd.SystemId : null; }
243                         set { systemID = value != null ? value : String.Empty; }
244                 }
245
246                 public string XmlLang {
247                         get { return xmlLang; }
248                         set { xmlLang = value != null ? value : String.Empty; }
249                 }
250
251                 public XmlSpace XmlSpace {
252                         get { return xmlSpace; }
253                         set { xmlSpace = value; }
254                 }
255
256                 #endregion
257
258                 #region Methods
259                 internal void PushScope ()
260                 {
261                         ContextItem item = null;
262                         if (contextItems.Count == contextItemCount) {
263                                 item = new ContextItem ();
264                                 contextItems.Add (item);
265                         }
266                         else
267                                 item = (ContextItem) contextItems [contextItemCount];
268                         item.BaseURI = BaseURI;
269                         item.XmlLang = XmlLang;
270                         item.XmlSpace = XmlSpace;
271                         contextItemCount++;
272                 }
273
274                 internal void PopScope ()
275                 {
276                         if (contextItemCount == 0)
277                                 throw new XmlException ("Unexpected end of element scope.");
278                         contextItemCount--;
279                         ContextItem prev = (ContextItem) contextItems [contextItemCount];
280                         baseURI = prev.BaseURI;
281                         xmlLang = prev.XmlLang;
282                         xmlSpace = prev.XmlSpace;
283                 }
284                 #endregion
285         }
286 }