ResXFileRef.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. // Gert Driesen ([email protected])
  25. //
  26. using System;
  27. using System.ComponentModel;
  28. using System.Drawing;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Text;
  32. namespace System.Resources {
  33. [Serializable]
  34. [TypeConverter(typeof(ResXFileRef.Converter))]
  35. #if INSIDE_SYSTEM_WEB
  36. internal
  37. #else
  38. public
  39. #endif
  40. class ResXFileRef {
  41. #if INSIDE_SYSTEM_WEB
  42. internal
  43. #else
  44. public
  45. #endif
  46. class Converter : TypeConverter {
  47. public Converter() {
  48. }
  49. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
  50. return sourceType == typeof(string);
  51. }
  52. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
  53. return destinationType == typeof(string);
  54. }
  55. public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
  56. byte[] buffer;
  57. if ( !(value is String)) {
  58. return null;
  59. }
  60. string [] parts = ResXFileRef.Parse ((string) value);
  61. if (parts.Length == 1)
  62. throw new ArgumentException ("value");
  63. Type type = Type.GetType (parts [1]);
  64. if (type == typeof(string)) {
  65. Encoding encoding;
  66. if (parts.Length > 2) {
  67. encoding = Encoding.GetEncoding (parts [2]);
  68. } else {
  69. encoding = Encoding.Default;
  70. }
  71. using (TextReader reader = new StreamReader(parts [0], encoding)) {
  72. return reader.ReadToEnd();
  73. }
  74. }
  75. using (FileStream file = new FileStream (parts [0], FileMode.Open, FileAccess.Read, FileShare.Read)) {
  76. buffer = new byte [file.Length];
  77. file.Read(buffer, 0, (int) file.Length);
  78. }
  79. if (type == typeof(System.Byte[]))
  80. return buffer;
  81. if (type == typeof (Bitmap) && Path.GetExtension (parts [0]) == ".ico") {
  82. MemoryStream ms = new MemoryStream (buffer);
  83. return new Icon (ms).ToBitmap ();
  84. }
  85. if (type == typeof (MemoryStream))
  86. return new MemoryStream (buffer);
  87. return Activator.CreateInstance(type, BindingFlags.CreateInstance
  88. | BindingFlags.Public | BindingFlags.Instance, null,
  89. new object[] { new MemoryStream (buffer) }, culture);
  90. }
  91. public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
  92. if (destinationType != typeof(String)) {
  93. return base.ConvertTo (context, culture, value, destinationType);
  94. }
  95. return ((ResXFileRef)value).ToString();
  96. }
  97. }
  98. private string filename;
  99. private string typename;
  100. private Encoding textFileEncoding;
  101. public ResXFileRef (string fileName, string typeName)
  102. {
  103. if (fileName == null)
  104. throw new ArgumentNullException ("fileName");
  105. if (typeName == null)
  106. throw new ArgumentNullException ("typeName");
  107. this.filename = fileName;
  108. this.typename = typeName;
  109. }
  110. public ResXFileRef (string fileName, string typeName, Encoding textFileEncoding)
  111. : this (fileName, typeName)
  112. {
  113. this.textFileEncoding = textFileEncoding;
  114. }
  115. public string FileName {
  116. get { return filename; }
  117. }
  118. public Encoding TextFileEncoding {
  119. get { return textFileEncoding; }
  120. }
  121. public string TypeName {
  122. get { return typename; }
  123. }
  124. public override string ToString() {
  125. StringBuilder sb = new StringBuilder ();
  126. if (filename != null) {
  127. sb.Append (filename);
  128. }
  129. sb.Append (';');
  130. if (typename != null) {
  131. sb.Append (typename);
  132. }
  133. if (textFileEncoding != null) {
  134. sb.Append (';');
  135. sb.Append (textFileEncoding.WebName);
  136. }
  137. return sb.ToString ();
  138. }
  139. internal static string [] Parse (string fileRef)
  140. {
  141. // we cannot return ResXFileRef, as that would mean we'd have to
  142. // instantiate the encoding, and we do not always need this
  143. if (fileRef == null)
  144. throw new ArgumentNullException ("fileRef");
  145. return fileRef.Split (';');
  146. }
  147. }
  148. }