ResXDataNode.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.
  19. //
  20. // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Andreia Gaita ([email protected])
  24. // Olivier Dufour [email protected]
  25. // Gary Barnett [email protected]
  26. using System;
  27. using System.Runtime.Serialization;
  28. using System.Drawing;
  29. using System.ComponentModel;
  30. using System.Reflection;
  31. using System.ComponentModel.Design;
  32. using System.Runtime.Serialization.Formatters.Binary;
  33. using System.IO;
  34. using System.Text;
  35. namespace System.Resources
  36. {
  37. [SerializableAttribute]
  38. #if INSIDE_SYSTEM_WEB
  39. internal
  40. #else
  41. public
  42. #endif
  43. sealed class ResXDataNode : ISerializable
  44. {
  45. private string name;
  46. private ResXFileRef fileRef;
  47. private string comment;
  48. private Point pos;
  49. internal ResXDataNodeHandler handler;
  50. public string Comment {
  51. get { return comment ?? String.Empty; }
  52. set { this.comment = value; }
  53. }
  54. public ResXFileRef FileRef {
  55. get { return this.fileRef; }
  56. }
  57. public string Name {
  58. get { return name ?? String.Empty; }
  59. set {
  60. if (value == null)
  61. throw new ArgumentNullException ("name");
  62. if (value == String.Empty)
  63. throw new ArgumentException ("name");
  64. this.name = value;
  65. }
  66. }
  67. internal bool IsWritable {
  68. get {
  69. return (handler is IWritableHandler);
  70. }
  71. }
  72. internal string MimeType { get; set; }
  73. internal string Type { get;set;}
  74. internal string DataString {
  75. get {
  76. if (IsWritable)
  77. return ((IWritableHandler) handler).DataString;
  78. else
  79. throw new NotSupportedException ("Node Not Writable");
  80. }
  81. }
  82. public ResXDataNode (string name, object value) : this (name, value, Point.Empty)
  83. {
  84. }
  85. public ResXDataNode (string name, ResXFileRef fileRef)
  86. {
  87. if (name == null)
  88. throw new ArgumentNullException ("name");
  89. if (fileRef == null)
  90. throw new ArgumentNullException ("fileRef");
  91. if (name.Length == 0)
  92. throw new ArgumentException ("name");
  93. this.name = name;
  94. this.fileRef = fileRef;
  95. pos = Point.Empty;
  96. handler = new FileRefHandler (fileRef);
  97. }
  98. internal ResXDataNode (string name, object value, Point position)
  99. {
  100. if (name == null)
  101. throw new ArgumentNullException ("name");
  102. if (name.Length == 0)
  103. throw new ArgumentException ("name");
  104. Type type = (value == null) ? typeof (object) : value.GetType ();
  105. if ((value != null) && !type.IsSerializable) {
  106. throw new InvalidOperationException (String.Format ("'{0}' of type '{1}' cannot be added"
  107. + " because it is not serializable",
  108. name, type));
  109. }
  110. this.name = name;
  111. this.pos = position;
  112. handler = new InMemoryHandler (value);
  113. }
  114. internal ResXDataNode (string nameAtt, string mimeTypeAtt, string typeAtt,
  115. string dataString, string commentString, Point position,
  116. string basePath)
  117. {
  118. name = nameAtt;
  119. comment = commentString;
  120. pos = position;
  121. MimeType = mimeTypeAtt;
  122. Type = typeAtt;
  123. if (!String.IsNullOrEmpty (mimeTypeAtt)) {
  124. if (!String.IsNullOrEmpty(typeAtt)) {
  125. handler = new TypeConverterFromResXHandler (dataString, mimeTypeAtt, typeAtt);
  126. } else {
  127. handler = new SerializedFromResXHandler (dataString, mimeTypeAtt);
  128. }
  129. } else if (!String.IsNullOrEmpty (typeAtt)) { //using hard coded types to avoid version mismatches
  130. if (typeAtt.StartsWith ("System.Resources.ResXNullRef, System.Windows.Forms")) {
  131. handler = new NullRefHandler (typeAtt);
  132. } else if (typeAtt.StartsWith ("System.Byte[], mscorlib")) {
  133. handler = new ByteArrayFromResXHandler (dataString);
  134. } else if (typeAtt.StartsWith ("System.Resources.ResXFileRef, System.Windows.Forms")) {
  135. ResXFileRef newFileRef = BuildFileRef (dataString, basePath);
  136. handler = new FileRefHandler (newFileRef);
  137. this.fileRef = newFileRef;
  138. } else {
  139. handler = new TypeConverterFromResXHandler (dataString, mimeTypeAtt, typeAtt);
  140. }
  141. } else {
  142. handler = new InMemoryHandler (dataString);
  143. }
  144. if (handler == null)
  145. throw new Exception ("handler is null");
  146. }
  147. public Point GetNodePosition ()
  148. {
  149. return pos;
  150. }
  151. public string GetValueTypeName (AssemblyName[] names)
  152. {
  153. return handler.GetValueTypeName (names);
  154. }
  155. public string GetValueTypeName (ITypeResolutionService typeResolver)
  156. {
  157. return handler.GetValueTypeName (typeResolver);
  158. }
  159. public Object GetValue (AssemblyName[] names)
  160. {
  161. return handler.GetValue (names);
  162. }
  163. public Object GetValue (ITypeResolutionService typeResolver)
  164. {
  165. return handler.GetValue (typeResolver);
  166. }
  167. //FIXME: .net doesnt instantiate encoding at this stage
  168. ResXFileRef BuildFileRef (string dataString, string basePath)
  169. {
  170. ResXFileRef fr;
  171. string[] parts = ResXFileRef.Parse (dataString);
  172. if (parts.Length < 2)
  173. throw new ArgumentException ("ResXFileRef cannot be generated");
  174. string fileName = parts[0];
  175. if (basePath != null)
  176. fileName = Path.Combine (basePath, parts[0]);
  177. string typeName = parts[1];
  178. if (parts.Length == 3) {
  179. Encoding encoding = Encoding.GetEncoding(parts[2]);
  180. fr = new ResXFileRef (fileName, typeName, encoding);
  181. } else
  182. fr = new ResXFileRef (fileName, typeName);
  183. return fr;
  184. }
  185. #region ISerializable Members
  186. void ISerializable.GetObjectData (SerializationInfo si, StreamingContext context)
  187. {
  188. si.AddValue ("Name", this.Name);
  189. si.AddValue ("Comment", this.Comment);
  190. }
  191. #endregion
  192. }
  193. }