(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Resources / ResXResourceReader.cs
blob1d64baa31271cc3ec0b30fd3bc5f6b7f881bc537
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
22 // Authors:
23 // Duncan Mak duncan@ximian.com
24 // Nick Drochak ndrochak@gol.com
25 // Paolo Molaro lupus@ximian.com
28 // NOT COMPLETE
30 using System.Collections;
31 using System.Resources;
32 using System.IO;
33 using System.Xml;
35 namespace System.Resources
37 public sealed class ResXResourceReader : IResourceReader, IDisposable
39 Stream stream;
40 XmlTextReader reader;
41 Hashtable hasht;
43 // Constructors
44 public ResXResourceReader (Stream stream)
46 if (stream == null)
47 throw new ArgumentNullException ("Value cannot be null.");
49 if (!stream.CanRead)
50 throw new ArgumentException ("Stream was not readable.");
52 this.stream = stream;
53 basic_setup ();
56 public ResXResourceReader (string fileName)
58 stream = File.OpenRead (fileName);
59 basic_setup ();
62 void basic_setup () {
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.");
69 load_data ();
72 static string get_attr (XmlTextReader reader, string name) {
73 if (!reader.HasAttributes)
74 return null;
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 ();
80 return v;
83 reader.MoveToElement ();
84 return null;
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) {
91 gotelement = true;
92 break;
95 if (!gotelement)
96 return null;
97 while (reader.Read ()) {
98 if (reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.CDATA) {
99 string v = reader.Value;
100 return v;
102 else if (reader.NodeType == XmlNodeType.EndElement && reader.Value == string.Empty)
104 string v = reader.Value;
105 return v;
108 return null;
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) {
117 gotroot = true;
118 break;
121 if (!gotroot)
122 return false;
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) {
129 gotmime = true;
130 break;
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");
138 if (n != null) {
139 string v = get_value (reader, "value");
140 hasht [n] = v;
144 return gotmime;
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");
152 if (n != null) {
153 string v = get_value (reader, "value");
154 hasht [n] = v;
160 public void Close ()
162 stream.Close ();
163 stream = null;
166 public IDictionaryEnumerator GetEnumerator () {
167 if (null == stream){
168 throw new InvalidOperationException("ResourceReader is closed.");
170 else {
171 return hasht.GetEnumerator ();
175 IEnumerator IEnumerable.GetEnumerator ()
177 return ((IResourceReader) this).GetEnumerator();
180 [MonoTODO]
181 void IDisposable.Dispose ()
183 // FIXME: is this all we need to do?
184 Close();
187 } // public sealed class ResXResourceReader
188 } // namespace System.Resources