1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004 Novell, Inc.
23 // Duncan Mak duncan@ximian.com
24 // Nick Drochak ndrochak@gol.com
25 // Paolo Molaro lupus@ximian.com
30 using System
.Collections
;
31 using System
.Resources
;
35 namespace System
.Resources
37 public sealed class ResXResourceReader
: IResourceReader
, IDisposable
44 public ResXResourceReader (Stream stream
)
47 throw new ArgumentNullException ("Value cannot be null.");
50 throw new ArgumentException ("Stream was not readable.");
56 public ResXResourceReader (string fileName
)
58 stream
= File
.OpenRead (fileName
);
63 reader
= new XmlTextReader (stream
);
64 hasht
= new Hashtable ();
66 if (!IsStreamValid()){
67 throw new ArgumentException("Stream is not a valid .resx file! It was possibly truncated.");
72 static string get_attr (XmlTextReader reader
, string name
) {
73 if (!reader
.HasAttributes
)
75 for (int i
= 0; i
< reader
.AttributeCount
; i
++) {
76 reader
.MoveToAttribute (i
);
77 if (String
.Compare (reader
.Name
, name
, true) == 0) {
78 string v
= reader
.Value
;
79 reader
.MoveToElement ();
83 reader
.MoveToElement ();
87 static string get_value (XmlTextReader reader
, string name
) {
88 bool gotelement
= false;
89 while (reader
.Read ()) {
90 if (reader
.NodeType
== XmlNodeType
.Element
&& String
.Compare (reader
.Name
, name
, true) == 0) {
97 while (reader
.Read ()) {
98 if (reader
.NodeType
== XmlNodeType
.Text
|| reader
.NodeType
== XmlNodeType
.CDATA
) {
99 string v
= reader
.Value
;
102 else if (reader
.NodeType
== XmlNodeType
.EndElement
&& reader
.Value
== string.Empty
)
104 string v
= reader
.Value
;
111 private bool IsStreamValid() {
112 bool gotroot
= false;
113 bool gotmime
= false;
115 while (reader
.Read ()) {
116 if (reader
.NodeType
== XmlNodeType
.Element
&& String
.Compare (reader
.Name
, "root", true) == 0) {
123 while (reader
.Read ()) {
124 if (reader
.NodeType
== XmlNodeType
.Element
&& String
.Compare (reader
.Name
, "resheader", true) == 0) {
125 string v
= get_attr (reader
, "name");
126 if (v
!= null && String
.Compare (v
, "resmimetype", true) == 0) {
127 v
= get_value (reader
, "value");
128 if (String
.Compare (v
, "text/microsoft-resx", true) == 0) {
133 } else if (reader
.NodeType
== XmlNodeType
.Element
&& String
.Compare (reader
.Name
, "data", true) == 0) {
134 /* resheader apparently can appear anywhere, so we collect
135 * the data even if we haven't validated yet.
137 string n
= get_attr (reader
, "name");
139 string v
= get_value (reader
, "value");
147 private void load_data ()
149 while (reader
.Read ()) {
150 if (reader
.NodeType
== XmlNodeType
.Element
&& String
.Compare (reader
.Name
, "data", true) == 0) {
151 string n
= get_attr (reader
, "name");
153 string v
= get_value (reader
, "value");
166 public IDictionaryEnumerator
GetEnumerator () {
168 throw new InvalidOperationException("ResourceReader is closed.");
171 return hasht
.GetEnumerator ();
175 IEnumerator IEnumerable
.GetEnumerator ()
177 return ((IResourceReader
) this).GetEnumerator();
181 void IDisposable
.Dispose ()
183 // FIXME: is this all we need to do?
187 } // public sealed class ResXResourceReader
188 } // namespace System.Resources