Exemplo n.º 1
0
 /// <devdoc>
 ///     Adds a resource to the resources. If the resource is a string,
 ///     it will be saved that way, otherwise it will be serialized
 ///     and stored as in binary.
 /// </devdoc>
 private void AddDataRow(string elementName, string name, object value)
 {
     Debug.WriteLineIf(ResValueProviderSwitch.TraceVerbose, "  resx: adding resource " + name);
     if (value is string)
     {
         AddDataRow(elementName, name, (string)value);
     }
     else if (value is byte[])
     {
         AddDataRow(elementName, name, (byte[])value);
     }
     else if (value is ResXFileRef)
     {
         ResXFileRef  fileRef = (ResXFileRef)value;
         ResXDataNode node    = new ResXDataNode(name, fileRef, this.typeNameConverter);
         if (fileRef != null)
         {
             fileRef.MakeFilePathRelative(BasePath);
         }
         DataNodeInfo info = node.GetDataNodeInfo();
         AddDataRow(elementName, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment);
     }
     else
     {
         ResXDataNode node = new ResXDataNode(name, value, this.typeNameConverter);
         DataNodeInfo info = node.GetDataNodeInfo();
         AddDataRow(elementName, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment);
     }
 }
Exemplo n.º 2
0
        /// <include file='doc\ResXResourceWriter.uex' path='docs/doc[@for="ResXResourceWriter.AddResource3"]/*' />
        /// <devdoc>
        ///     Adds a string resource to the resources.
        /// </devdoc>
        public void AddResource(ResXDataNode node)
        {
            // we're modifying the node as we're adding it to the resxwriter
            // this is BAD, so we clone it. adding it to a writer doesnt change it
            // we're messing with a copy
            ResXDataNode nodeClone = node.DeepClone();

            ResXFileRef fileRef          = nodeClone.FileRef;
            string      modifiedBasePath = BasePath;

            if (!string.IsNullOrEmpty(modifiedBasePath))
            {
                if (!(modifiedBasePath.EndsWith("\\")))
                {
                    modifiedBasePath += "\\";
                }
                if (fileRef != null)
                {
                    fileRef.MakeFilePathRelative(modifiedBasePath);
                }
            }
            DataNodeInfo info = nodeClone.GetDataNodeInfo();

            AddDataRow(DataStr, info.Name, info.ValueData, info.TypeName, info.MimeType, info.Comment);
        }
 private void AddDataRow(string elementName, string name, object value)
 {
     if (value is string)
     {
         this.AddDataRow(elementName, name, (string)value);
     }
     else if (value is byte[])
     {
         this.AddDataRow(elementName, name, (byte[])value);
     }
     else if (value is ResXFileRef)
     {
         ResXFileRef  fileRef = (ResXFileRef)value;
         ResXDataNode node    = new ResXDataNode(name, fileRef, this.typeNameConverter);
         if (fileRef != null)
         {
             fileRef.MakeFilePathRelative(this.BasePath);
         }
         DataNodeInfo dataNodeInfo = node.GetDataNodeInfo();
         this.AddDataRow(elementName, dataNodeInfo.Name, dataNodeInfo.ValueData, dataNodeInfo.TypeName, dataNodeInfo.MimeType, dataNodeInfo.Comment);
     }
     else
     {
         DataNodeInfo info2 = new ResXDataNode(name, value, this.typeNameConverter).GetDataNodeInfo();
         this.AddDataRow(elementName, info2.Name, info2.ValueData, info2.TypeName, info2.MimeType, info2.Comment);
     }
 }
        public object GetValue(ITypeResolutionService typeResolver)
        {
            if (this.value != null)
            {
                return(this.value);
            }
            object obj2 = null;

            if (this.FileRefFullPath != null)
            {
                if (this.ResolveType(this.FileRefType, typeResolver) != null)
                {
                    if (this.FileRefTextEncoding != null)
                    {
                        this.fileRef = new ResXFileRef(this.FileRefFullPath, this.FileRefType, Encoding.GetEncoding(this.FileRefTextEncoding));
                    }
                    else
                    {
                        this.fileRef = new ResXFileRef(this.FileRefFullPath, this.FileRefType);
                    }
                    return(TypeDescriptor.GetConverter(typeof(ResXFileRef)).ConvertFrom(this.fileRef.ToString()));
                }
                TypeLoadException exception = new TypeLoadException(System.Windows.Forms.SR.GetString("TypeLoadExceptionShort", new object[] { this.FileRefType }));
                throw exception;
            }
            if ((obj2 == null) && (this.nodeInfo.ValueData != null))
            {
                return(this.GenerateObjectFromDataNodeInfo(this.nodeInfo, typeResolver));
            }
            return(null);
        }
Exemplo n.º 5
0
		public void Constructor1 ()
		{
			ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
			MonoTests.System.Windows.Forms.TestHelper.RemoveWarning (r);
			Assert.AreEqual ("mono.bmp", r.FileName, "#1");
			Assert.AreEqual ("Bitmap", r.TypeName, "#2");
		}
Exemplo n.º 6
0
        //FIXME: .net doesnt instantiate encoding at this stage
        ResXFileRef BuildFileRef(string dataString, string basePath)
        {
            ResXFileRef fr;

            string[] parts = ResXFileRef.Parse(dataString);

            if (parts.Length < 2)
            {
                throw new ArgumentException("ResXFileRef cannot be generated");
            }

            string fileName = parts[0];

            if (basePath != null)
            {
                fileName = Path.Combine(basePath, parts[0]);
            }

            string typeName = parts[1];

            if (parts.Length == 3)
            {
                Encoding encoding = Encoding.GetEncoding(parts[2]);
                fr = new ResXFileRef(fileName, typeName, encoding);
            }
            else
            {
                fr = new ResXFileRef(fileName, typeName);
            }
            return(fr);
        }
Exemplo n.º 7
0
            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                byte[] buffer;

                if (!(value is String))
                {
                    return(null);
                }

                string [] parts = ResXFileRef.Parse((string)value);
                if (parts.Length == 1)
                {
                    throw new ArgumentException("value");
                }

                Type type = Type.GetType(parts [1]);

                if (type == typeof(string))
                {
                    Encoding encoding;
                    if (parts.Length > 2)
                    {
                        encoding = Encoding.GetEncoding(parts [2]);
                    }
                    else
                    {
                        encoding = Encoding.Default;
                    }

                    using (TextReader reader = new StreamReader(parts [0], encoding)) {
                        return(reader.ReadToEnd());
                    }
                }

                using (FileStream file = new FileStream(parts [0], FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    buffer = new byte [file.Length];
                    file.Read(buffer, 0, (int)file.Length);
                }

                if (type == typeof(System.Byte[]))
                {
                    return(buffer);
                }

                if (type == typeof(Bitmap) && Path.GetExtension(parts [0]) == ".ico")
                {
                    MemoryStream ms = new MemoryStream(buffer);
                    return(new Icon(ms).ToBitmap());
                }

                if (type == typeof(MemoryStream))
                {
                    return(new MemoryStream(buffer));
                }

                return(Activator.CreateInstance(type, BindingFlags.CreateInstance
                                                | BindingFlags.Public | BindingFlags.Instance, null,
                                                new object[] { new MemoryStream(buffer) }, culture));
            }
Exemplo n.º 8
0
        public ResXDataNode(string name, ResXFileRef fileRef, Func <Type, string> typeNameConverter)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            this.name              = name;
            this.fileRef           = fileRef ?? throw new ArgumentNullException(nameof(fileRef));
            this.typeNameConverter = typeNameConverter;
        }
        ResXFileRef ProcessFileRefBasePath(ResXFileRef fileRef)
        {
            if (String.IsNullOrEmpty(BasePath))
            {
                return(fileRef);
            }

            string newPath = AbsoluteToRelativePath(BasePath, fileRef.FileName);

            return(new ResXFileRef(newPath, fileRef.TypeName, fileRef.TextFileEncoding));
        }
Exemplo n.º 10
0
        internal ResXDataNode(string nameAtt, string mimeTypeAtt, string typeAtt,
                              string dataString, string commentString, Point_ position,
                              string basePath)
        {
            name     = nameAtt;
            comment  = commentString;
            pos      = position;
            MimeType = mimeTypeAtt;
            Type     = typeAtt;

            if (!String.IsNullOrEmpty(mimeTypeAtt))
            {
                if (!String.IsNullOrEmpty(typeAtt))
                {
                    handler = new TypeConverterFromResXHandler(dataString, mimeTypeAtt, typeAtt);
                }
                else
                {
                    handler = new SerializedFromResXHandler(dataString, mimeTypeAtt);
                }
            }
            else if (!String.IsNullOrEmpty(typeAtt))                  //using hard coded types to avoid version mismatches
            {
                if (typeAtt.StartsWith("System.Resources.ResXNullRef, System.Windows.Forms"))
                {
                    handler = new NullRefHandler(typeAtt);
                }
                else if (typeAtt.StartsWith("System.Byte[], mscorlib"))
                {
                    handler = new ByteArrayFromResXHandler(dataString);
                }
                else if (typeAtt.StartsWith("System.Resources.ResXFileRef, System.Windows.Forms"))
                {
                    ResXFileRef newFileRef = BuildFileRef(dataString, basePath);
                    handler      = new FileRefHandler(newFileRef);
                    this.fileRef = newFileRef;
                }
                else
                {
                    handler = new TypeConverterFromResXHandler(dataString, mimeTypeAtt, typeAtt);
                }
            }
            else
            {
                handler = new InMemoryHandler(dataString);
            }

            if (handler == null)
            {
                throw new Exception("handler is null");
            }
        }
Exemplo n.º 11
0
		public void Constructor2 ()
		{
			Encoding utf8 = Encoding.UTF8;

			ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap", utf8);
			Assert.AreEqual ("mono.bmp", r.FileName, "#A1");
			Assert.AreSame (utf8, r.TextFileEncoding, "#A2");
			Assert.AreEqual ("Bitmap", r.TypeName, "#A3");

			r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
			Assert.AreEqual ("mono.bmp", r.FileName, "#B1");
			Assert.IsNull (r.TextFileEncoding, "#B2");
			Assert.AreEqual ("Bitmap", r.TypeName, "#B3");
		}
Exemplo n.º 12
0
		public ResXDataNode (string name, ResXFileRef fileRef)
		{
			if (name == null)
				throw new ArgumentNullException ("name");

			if (fileRef == null)
				throw new ArgumentNullException ("fileRef");

			if (name.Length == 0)
				throw new ArgumentException ("name");

			this.name = name;
			this.fileRef = fileRef;
			pos = Point.Empty;
		}
Exemplo n.º 13
0
        /// <include file='doc\ResXDataNode.uex' path='docs/doc[@for="ResXDataNode.GetValue"]/*' />
        /// <devdoc>
        ///    Get the value contained in this datanode
        /// </devdoc>
        public object GetValue(ITypeResolutionService typeResolver)
        {
            if (value != null)
            {
                return(value);
            }

            object result = null;

            if (FileRefFullPath != null)
            {
                Type objectType = ResolveType(FileRefType, typeResolver);
                if (objectType != null)
                {
                    // we have the FQN for this type
                    if (FileRefTextEncoding != null)
                    {
                        fileRef = new ResXFileRef(FileRefFullPath, FileRefType, Encoding.GetEncoding(FileRefTextEncoding));
                    }
                    else
                    {
                        fileRef = new ResXFileRef(FileRefFullPath, FileRefType);
                    }
                    TypeConverter tc = TypeDescriptor.GetConverter(typeof(ResXFileRef));
                    result = tc.ConvertFrom(fileRef.ToString());
                }
                else
                {
                    string            newMessage = string.Format(SR.TypeLoadExceptionShort, FileRefType);
                    TypeLoadException newTle     = new TypeLoadException(newMessage);
                    throw (newTle);
                }
            }
            else if (result == null && nodeInfo.ValueData != null)
            {
                // it's embedded, we deserialize it
                result = GenerateObjectFromDataNodeInfo(nodeInfo, typeResolver);
            }
            else
            {
                // schema is wrong and say minOccur for Value is 0,
                // but it's too late to change it...
                // we need to return null here
                return(null);
            }
            return(result);
        }
Exemplo n.º 14
0
		public void Constructor1_TypeName_Null ()
		{
#if NET_2_0
			try {
				new ResXFileRef ("mono.bmp", (string) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNotNull (ex.Message, "#3");
				Assert.IsNotNull (ex.ParamName, "#4");
				Assert.AreEqual ("typeName", ex.ParamName, "#5");
				Assert.IsNull (ex.InnerException, "#6");
			}
#else
			ResXFileRef r = new ResXFileRef ("mono.bmp", (string) null);
			Assert.AreEqual ("mono.bmp;", r.ToString ());
#endif
		}
Exemplo n.º 15
0
 public ResXDataNode(string name, ResXFileRef fileRef, Func <Type, string> typeNameConverter)
 {
     if (name == null)
     {
         throw (new ArgumentNullException(nameof(name)));
     }
     if (fileRef == null)
     {
         throw (new ArgumentNullException(nameof(fileRef)));
     }
     if (name.Length == 0)
     {
         throw (new ArgumentException(nameof(name)));
     }
     this.name              = name;
     this.fileRef           = fileRef;
     this.typeNameConverter = typeNameConverter;
 }
Exemplo n.º 16
0
        public ResXDataNode(string name, ResXFileRef fileRef)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (fileRef == null)
            {
                throw new ArgumentNullException("fileRef");
            }

            if (name.Length == 0)
            {
                throw new ArgumentException("name");
            }

            this.name    = name;
            this.fileRef = fileRef;
            pos          = Point.Empty;
        }
        public void AddResource(ResXDataNode node)
        {
            ResXDataNode node2    = node.DeepClone();
            ResXFileRef  fileRef  = node2.FileRef;
            string       basePath = this.BasePath;

            if (!string.IsNullOrEmpty(basePath))
            {
                if (!basePath.EndsWith(@"\"))
                {
                    basePath = basePath + @"\";
                }
                if (fileRef != null)
                {
                    fileRef.MakeFilePathRelative(basePath);
                }
            }
            DataNodeInfo dataNodeInfo = node2.GetDataNodeInfo();

            this.AddDataRow("data", dataNodeInfo.Name, dataNodeInfo.ValueData, dataNodeInfo.TypeName, dataNodeInfo.MimeType, dataNodeInfo.Comment);
        }
Exemplo n.º 18
0
		ResXFileRef ProcessFileRefBasePath (ResXFileRef fileRef)
		{
			if (String.IsNullOrEmpty (BasePath))
				return fileRef;

			string newPath = AbsoluteToRelativePath (BasePath, fileRef.FileName);
			return new ResXFileRef (newPath, fileRef.TypeName, fileRef.TextFileEncoding);
		}
Exemplo n.º 19
0
		public void FileRef_TypeCantBeResolved ()
		{
			string aFile = Path.GetTempFileName ();
			ResXFileRef fileRef = new ResXFileRef (aFile, "a.type.doesnt.exist");

			StringBuilder sb = new StringBuilder ();
			using (StringWriter sw = new StringWriter (sb)) {
				using (ResXResourceWriter writer = new ResXResourceWriter (sw)) {
					writer.AddResource ("test", fileRef);
				}
			}

			using (StringReader sr = new StringReader (sb.ToString ())) {
				using (ResXResourceReader reader = new ResXResourceReader (sr)) {
					reader.GetEnumerator ();
				}
			}
		}
Exemplo n.º 20
0
        private byte[] GenerateResource()
        {
            using (var writer = new StringWriter())
            using (var resx = new ResXResourceWriter(writer))
            {
                resx.AddAlias("System.Windows.Forms", new AssemblyName("System.Windows.Forms"));
                resx.AddResource(new ResXDataNode("110", _manifest.Name));
                resx.AddResource(new ResXDataNode("112", _manifest.Description));

                if (!string.IsNullOrEmpty(_manifest.Icon))
                {
                    string iconFileName = Path.GetFileNameWithoutExtension(InputFilePath) + ".ico";

                    var fileRef = new ResXFileRef(iconFileName, "System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
                    resx.AddResource(new ResXDataNode("400", fileRef));
                }

                resx.Generate();
                writer.Flush();

                var sb = writer.GetStringBuilder();
                return Encoding.UTF8.GetBytes(sb.ToString());
            }
        }
Exemplo n.º 21
0
		public ResXDataNode GetNodeFileRefToIcon ()
		{
			tempFileWithIcon = Path.GetTempFileName (); // remember to delete file in teardown
			Path.ChangeExtension (tempFileWithIcon, "ico");

			WriteEmbeddedResource ("32x32.ico", tempFileWithIcon);
			ResXFileRef fileRef = new ResXFileRef (tempFileWithIcon, typeof (Icon).AssemblyQualifiedName);
			ResXDataNode node = new ResXDataNode ("test", fileRef);

			return node;
		}
		public void LoadingFileFails ()
		{
			string corruptFile = Path.GetTempFileName ();
			ResXFileRef fileRef = new ResXFileRef (corruptFile, typeof (serializable).AssemblyQualifiedName);

			File.AppendAllText (corruptFile, "corrupt");
			ResXDataNode node = new ResXDataNode ("aname", fileRef);
			node.GetValue ((AssemblyName []) null);
		}
 public ResXDataNode(string name, ResXFileRef fileRef) : this(name, fileRef, null)
 {
 }
Exemplo n.º 24
0
 /// <summary>
 ///
 /// </summary>
 public ResXDataNode(string name, ResXFileRef fileRef) : this(name, fileRef, null)
 {
 }
Exemplo n.º 25
0
        private void ParseDataNode(bool meta)
        {
            Hashtable hashtable = ((meta && !useResXDataNodes) ? hashtm : hasht);
            Point     pos       = new Point(xmlReader.LineNumber, xmlReader.LinePosition);
            string    name      = GetAttribute("name");
            string    type_name = GetAttribute("type");
            string    mime_type = GetAttribute("mimetype");

            Type type = type_name == null ? null : ResolveType(type_name);

            if (type_name != null && type == null)
            {
                throw new ArgumentException(String.Format(
                                                "The type '{0}' of the element '{1}' could not be resolved.", type_name, name));
            }

            if (type == typeof(ResXNullRef))
            {
                if (useResXDataNodes)
                {
                    hashtable [name] = new ResXDataNode(name, null, pos);
                }
                else
                {
                    hashtable [name] = null;
                }
                return;
            }

            string comment = null;
            string value   = GetDataValue(meta, out comment);
            object obj     = null;

            if (mime_type != null && mime_type.Length > 0)
            {
                if (mime_type == ResXResourceWriter.BinSerializedObjectMimeType)
                {
                    byte []         data = Convert.FromBase64String(value);
                    BinaryFormatter f    = new BinaryFormatter();
                    using (MemoryStream s = new MemoryStream(data)) {
                        obj = f.Deserialize(s);
                    }
                }
                else if (mime_type == ResXResourceWriter.ByteArraySerializedObjectMimeType)
                {
                    if (type != null)
                    {
                        TypeConverter c = TypeDescriptor.GetConverter(type);
                        if (c.CanConvertFrom(typeof(byte [])))
                        {
                            obj = c.ConvertFrom(Convert.FromBase64String(value));
                        }
                    }
                }
            }
            else if (type != null)
            {
                if (type == typeof(byte []))
                {
                    obj = Convert.FromBase64String(value);
                }
                else
                {
                    TypeConverter c = TypeDescriptor.GetConverter(type);
                    if (c.CanConvertFrom(typeof(string)))
                    {
                        if (BasePath != null && type == typeof(ResXFileRef))
                        {
                            string [] parts = ResXFileRef.Parse(value);
                            parts [0] = Path.Combine(BasePath, parts [0]);
                            obj       = c.ConvertFromInvariantString(string.Join(";", parts));
                        }
                        else
                        {
                            obj = c.ConvertFromInvariantString(value);
                        }
                    }
                }
            }
            else
            {
                obj = value;
            }

            if (name == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                          "Could not find a name for a resource. The resource value "
                                                          + "was '{0}'.", obj));
            }
            if (useResXDataNodes)
            {
                ResXDataNode dataNode = new ResXDataNode(name, obj, pos);
                dataNode.Comment = comment;
                hashtable [name] = dataNode;
            }
            else
            {
                hashtable [name] = obj;
            }
        }
Exemplo n.º 26
0
        /// <include file='doc\ResXDataNode.uex' path='docs/doc[@for="ResXDataNode.GetValue"]/*' />
        /// <devdoc>
        ///    Get the value contained in this datanode
        /// </devdoc>
        public object GetValue(ITypeResolutionService typeResolver) {

            if(value != null) {
                return value;
            }

            object result = null;
            if(FileRefFullPath != null) {
                Type objectType = ResolveType(FileRefType , typeResolver);
                if(objectType != null) {
                    // we have the FQN for this type
                    if(FileRefTextEncoding != null) {
                        fileRef = new ResXFileRef(FileRefFullPath, FileRefType, Encoding.GetEncoding(FileRefTextEncoding));
                    } else {
                        fileRef = new ResXFileRef(FileRefFullPath, FileRefType);
                    }
                    TypeConverter tc = TypeDescriptor.GetConverter(typeof(ResXFileRef));
                    result = tc.ConvertFrom(fileRef.ToString());
                } else {
                    string newMessage = SR.GetString(SR.TypeLoadExceptionShort, FileRefType);
                    TypeLoadException newTle = new TypeLoadException(newMessage);
                    throw (newTle);
                }
            } else if(result == null && nodeInfo.ValueData!= null) {
                // it's embedded, we deserialize it
                result = GenerateObjectFromDataNodeInfo(nodeInfo, typeResolver);
            } else {
                // schema is wrong and say minOccur for Value is 0,
                // but it's too late to change it...
                // we need to return null here vswhidbey 180605
                return null;
            }    
            return result;
        }
Exemplo n.º 27
0
		public void BasePath_ChangesAbsoluteFileRef_Object ()
		{
			var fileref = new ResXFileRef (@"/dir/dir/filename.ext", "System.String");
			
			string basePath = @"/dir";
			
			StringWriter sw = new StringWriter ();
			using (ResXResourceWriter writer = new ResXResourceWriter (sw)) {
				writer.BasePath = basePath;
				writer.AddResource ("name", fileref);
			}
			
			StringReader sr = new StringReader (sw.ToString ());
			
			using (ResXResourceReader reader = new ResXResourceReader (sr)) {
				reader.UseResXDataNodes = true;
				IDictionaryEnumerator enumerator = reader.GetEnumerator ();
				enumerator.MoveNext ();
				var returnedNode = ((DictionaryEntry) enumerator.Current).Value as ResXDataNode;
				Assert.AreEqual (@"dir/filename.ext", returnedNode.FileRef.FileName);
			}
		}
Exemplo n.º 28
0
		ResXFileRef resXFileRef; // same as that referenced in ResXDataNode

		public FileRefHandler (ResXFileRef fileRef)
		{
			resXFileRef = fileRef;
		}
Exemplo n.º 29
0
 public ResXDataNode(string name, ResXFileRef fileRef, Func <Type, string> typeNameConverter)
 {
     _name              = name.OrThrowIfNullOrEmpty();
     _fileRef           = fileRef.OrThrowIfNull();
     _typeNameConverter = typeNameConverter;
 }
Exemplo n.º 30
0
		internal ResXDataNode (string nameAtt, string mimeTypeAtt, string typeAtt, 
		                       string dataString, string commentString, Point position, 
		                       string basePath)
		{

			name = nameAtt;
			comment = commentString;
			pos = position;
			MimeType = mimeTypeAtt;
			Type = typeAtt;

			if (!String.IsNullOrEmpty (mimeTypeAtt)) {
				if (!String.IsNullOrEmpty(typeAtt)) {
					handler = new TypeConverterFromResXHandler (dataString, mimeTypeAtt, typeAtt);
				} else {
					handler = new SerializedFromResXHandler (dataString, mimeTypeAtt);
				}
			} else if (!String.IsNullOrEmpty (typeAtt)) { //using hard coded types to avoid version mismatches
				if (typeAtt.StartsWith ("System.Resources.ResXNullRef, System.Windows.Forms")) {
					handler = new NullRefHandler (typeAtt);
				} else if (typeAtt.StartsWith ("System.Byte[], mscorlib")) { 
					handler = new ByteArrayFromResXHandler (dataString);
				} else if (typeAtt.StartsWith ("System.Resources.ResXFileRef, System.Windows.Forms")) {
					ResXFileRef newFileRef = BuildFileRef (dataString, basePath);
					handler = new FileRefHandler (newFileRef);
					this.fileRef = newFileRef;
				} else {
					handler = new TypeConverterFromResXHandler (dataString, mimeTypeAtt, typeAtt);
				}
			} else {
				handler = new InMemoryHandler (dataString);
			}

			if (handler == null)
				throw new Exception ("handler is null");
		}
        private void AddResource(string name, object value, string comment)
        {
            if (value is string)
            {
                AddResource(name, (string)value, comment);
                return;
            }

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (value != null && !value.GetType().IsSerializable)
            {
                throw new InvalidOperationException(String.Format("The element '{0}' of type '{1}' is not serializable.", name, value.GetType().Name));
            }

            if (written)
            {
                throw new InvalidOperationException("The resource is already generated.");
            }

            if (writer == null)
            {
                InitWriter();
            }

            if (value is byte[])
            {
                WriteBytes(name, value.GetType(), (byte [])value, comment);
                return;
            }

            if (value == null)
            {
                // nulls written as ResXNullRef
                WriteString(name, "", typeof(ResXNullRef), comment);
                return;
            }

            TypeConverter converter = TypeDescriptor.GetConverter(value);

            if (value is ResXFileRef)
            {
                ResXFileRef fileRef = ProcessFileRefBasePath((ResXFileRef)value);
                string      str     = (string)converter.ConvertToInvariantString(fileRef);
                WriteString(name, str, value.GetType(), comment);
                return;
            }

            if (converter != null && converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string)))
            {
                string str = (string)converter.ConvertToInvariantString(value);
                WriteString(name, str, value.GetType(), comment);
                return;
            }

            if (converter != null && converter.CanConvertTo(typeof(byte[])) && converter.CanConvertFrom(typeof(byte[])))
            {
                byte[] b = (byte[])converter.ConvertTo(value, typeof(byte[]));
                WriteBytes(name, value.GetType(), b, comment);
                return;
            }

            MemoryStream    ms  = new MemoryStream();
            BinaryFormatter fmt = new BinaryFormatter();

            try {
                fmt.Serialize(ms, value);
            } catch (Exception e) {
                throw new InvalidOperationException("Cannot add a " + value.GetType() +
                                                    "because it cannot be serialized: " +
                                                    e.Message);
            }

            WriteBytes(name, null, ms.GetBuffer(), 0, (int)ms.Length, comment);
            ms.Close();
        }
		public void IfTypeResolutionFailsReturnsOrigString()
		{
			ResXFileRef fileRef = new ResXFileRef ("afile.name", "a.type.name");
			ResXDataNode node = new ResXDataNode ("aname", fileRef);

			string returnedType = node.GetValueTypeName ((AssemblyName []) null);
			Assert.AreEqual ("a.type.name", returnedType);
		}
        public void ExportToFile(string filePath)
        {
            if (Persistence == Persistence.Linked)
                throw new InvalidOperationException ("Resource is already linked");

            SaveFile (filePath);

            var newFileRef = new ResXFileRef (filePath, TypeName);
            var newNode = new ResXDataNode (node.Name, newFileRef);
            newNode.Comment = node.Comment;
            node = newNode;
        }
Exemplo n.º 34
0
		public void FileRef ()
		{
			ResXFileRef fileRef = new ResXFileRef ("fileName", "Type.Name");
			ResXDataNode node = new ResXDataNode ("name", fileRef);
			Assert.AreEqual (fileRef, node.FileRef, "#A1");
		}
 public object GetValue(ITypeResolutionService typeResolver)
 {
     if (this.value != null)
     {
         return this.value;
     }
     object obj2 = null;
     if (this.FileRefFullPath != null)
     {
         if (this.ResolveType(this.FileRefType, typeResolver) != null)
         {
             if (this.FileRefTextEncoding != null)
             {
                 this.fileRef = new ResXFileRef(this.FileRefFullPath, this.FileRefType, Encoding.GetEncoding(this.FileRefTextEncoding));
             }
             else
             {
                 this.fileRef = new ResXFileRef(this.FileRefFullPath, this.FileRefType);
             }
             return TypeDescriptor.GetConverter(typeof(ResXFileRef)).ConvertFrom(this.fileRef.ToString());
         }
         TypeLoadException exception = new TypeLoadException(System.Windows.Forms.SR.GetString("TypeLoadExceptionShort", new object[] { this.FileRefType }));
         throw exception;
     }
     if ((obj2 == null) && (this.nodeInfo.ValueData != null))
     {
         return this.GenerateObjectFromDataNodeInfo(this.nodeInfo, typeResolver);
     }
     return null;
 }
Exemplo n.º 36
0
		public void ConvertTo ()
		{
			ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
			Assert.AreEqual ("mono.bmp;Bitmap", (string) _converter.ConvertTo (
				r, typeof (string)), "#1");

			r = new ResXFileRef ("mono.bmp", "Bitmap", Encoding.UTF8);
			Assert.AreEqual ("mono.bmp;Bitmap;utf-8", (string) _converter.ConvertTo (
				r, typeof (string)), "#2");

			r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
			Assert.AreEqual ("mono.bmp;Bitmap", (string) _converter.ConvertTo (
				r, typeof (string)), "#3");
		}
 public ResXDataNode(string name, ResXFileRef fileRef, Func<System.Type, string> typeNameConverter)
 {
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     if (fileRef == null)
     {
         throw new ArgumentNullException("fileRef");
     }
     if (name.Length == 0)
     {
         throw new ArgumentException("name");
     }
     this.name = name;
     this.fileRef = fileRef;
     this.typeNameConverter = typeNameConverter;
 }
Exemplo n.º 38
0
		public void FileRefWithEncoding_WriteBack ()
		{
			ResXFileRef fileRef = new ResXFileRef ("afilename", "A.Type.Name", Encoding.UTF7);
			ResXDataNode node = new ResXDataNode ("aname", fileRef);
			node.Comment = "acomment";
			ResXDataNode returnedNode = GetNodeFromResXReader (node);
			Assert.IsNotNull (returnedNode, "#A1");
			Assert.AreEqual ("afilename", returnedNode.FileRef.FileName, "#A2");
			Assert.AreEqual ("A.Type.Name", returnedNode.FileRef.TypeName, "#A3");
			Assert.AreEqual ("acomment", returnedNode.Comment, "#A4");
			Assert.IsInstanceOfType (Encoding.UTF7.GetType (), returnedNode.FileRef.TextFileEncoding, "#A5");
			ResXDataNode finalNode = GetNodeFromResXReader (returnedNode);
			Assert.IsNotNull (finalNode, "#A6");
			Assert.AreEqual ("afilename", finalNode.FileRef.FileName, "#A7");
			Assert.AreEqual ("A.Type.Name", finalNode.FileRef.TypeName, "#A8");
			Assert.AreEqual ("acomment", finalNode.Comment, "#A9");
			Assert.IsInstanceOfType (Encoding.UTF7.GetType (), finalNode.FileRef.TextFileEncoding, "#A10");
		}
Exemplo n.º 39
0
		public ResXDataNode GetNodeFileRefToSerializable (string filename, bool assemblyQualifiedName)
		{
			tempFileWithSerializable = Path.GetTempFileName ();  // remember to delete file in teardown
			serializable ser = new serializable ("name", "value");
			
			SerializeToFile (tempFileWithSerializable, ser);

			string typeName;

			if (assemblyQualifiedName)
				typeName = typeof (serializable).AssemblyQualifiedName;
			else
				typeName = typeof (serializable).FullName;

			ResXFileRef fileRef = new ResXFileRef (tempFileWithSerializable, typeName);
			ResXDataNode node = new ResXDataNode ("test", fileRef);

			return node;
		}
Exemplo n.º 40
0
 public ResXDataNode(string name, ResXFileRef fileRef)
 {
     throw null;
 }
Exemplo n.º 41
0
		public void FileRef_DeserializationFails ()
		{
			string corruptFile = Path.GetTempFileName ();
			ResXFileRef fileRef = new ResXFileRef (corruptFile, typeof (serializable).AssemblyQualifiedName);

			File.AppendAllText (corruptFile,"corrupt");

			StringBuilder sb = new StringBuilder();
			using (StringWriter sw = new StringWriter (sb)) {
				using (ResXResourceWriter writer = new ResXResourceWriter (sw)) {
					writer.AddResource ("test", fileRef);
				}
			}

			using (StringReader sr = new StringReader (sb.ToString ())) {
				using (ResXResourceReader reader = new ResXResourceReader (sr)) {
					reader.GetEnumerator ();
				}
			}
		}
Exemplo n.º 42
0
 public ResXDataNode(string name, ResXFileRef fileRef, Func <Type, string> typeNameConverter)
 {
     throw null;
 }
Exemplo n.º 43
0
        ResXFileRef resXFileRef;         // same as that referenced in ResXDataNode

        public FileRefHandler(ResXFileRef fileRef)
        {
            resXFileRef = fileRef;
        }
Exemplo n.º 44
0
		//FIXME: .net doesnt instantiate encoding at this stage
		ResXFileRef BuildFileRef (string dataString, string basePath)
		{
			ResXFileRef fr;

			string[] parts = ResXFileRef.Parse (dataString);

			if (parts.Length < 2)
				throw new ArgumentException ("ResXFileRef cannot be generated");

			string fileName = parts[0];
			if (basePath != null) 
				fileName = Path.Combine (basePath, parts[0]);

			string typeName = parts[1];

			if (parts.Length == 3) {
				Encoding encoding = Encoding.GetEncoding(parts[2]);
				fr = new ResXFileRef (fileName, typeName, encoding);
			} else
				fr = new ResXFileRef (fileName, typeName);
			return fr;
		}
Exemplo n.º 45
0
		public void BasePathSetOnResXResourceReaderDoesAffectResXDataNode ()
		{
			ResXFileRef fileRef = new ResXFileRef ("file.name", "type.name");
			ResXDataNode node = new ResXDataNode("anode", fileRef);
			string resXFile = GetResXFileWithNode (node, "afilename.xxx");

			using (ResXResourceReader rr = new ResXResourceReader (resXFile)) {
				rr.BasePath = "basePath";
				rr.UseResXDataNodes = true;
				IDictionaryEnumerator en = rr.GetEnumerator ();
				en.MoveNext ();

				ResXDataNode returnedNode = ((DictionaryEntry) en.Current).Value as ResXDataNode;

				Assert.IsNotNull (node, "#A1");
				Assert.AreEqual (Path.Combine ("basePath", "file.name"), returnedNode.FileRef.FileName, "#A2");
			}
		}
Exemplo n.º 46
0
		public void ToStringTest ()
		{
			ResXFileRef r = new ResXFileRef ("mono.bmp", "Bitmap");
			Assert.AreEqual ("mono.bmp;Bitmap", r.ToString (), "#1");

			r = new ResXFileRef ("mono.bmp", "Bitmap", Encoding.UTF8);
			Assert.AreEqual ("mono.bmp;Bitmap;utf-8", r.ToString (), "#2");

			r = new ResXFileRef ("mono.bmp", "Bitmap", (Encoding) null);
			Assert.AreEqual ("mono.bmp;Bitmap", r.ToString (), "#3");
		}
		public void AttemptsTypeResolution ()
		{
			ResXFileRef fileRef = new ResXFileRef ("afile.name", "System.String");
			ResXDataNode node = new ResXDataNode ("aname", fileRef);

			string returnedType = node.GetValueTypeName ((AssemblyName []) null);
			Assert.AreEqual (typeof (string).AssemblyQualifiedName, returnedType);
		}
Exemplo n.º 48
0
        private void AddResource(string name, object value, string comment)
        {
            if (value is string)
            {
                AddResource(name, (string)value, comment);
                return;
            }

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (value != null && !value.GetType().GetTypeInfo().IsSerializable)
            {
                throw new InvalidOperationException(String.Format("The element '{0}' of type '{1}' is not serializable.", name, value.GetType().Name));
            }

            if (written)
            {
                throw new InvalidOperationException("The resource is already generated.");
            }

            if (writer == null)
            {
                InitWriter();
            }

            if (value is byte[])
            {
                WriteBytes(name, value.GetType(), (byte [])value, comment);
                return;
            }

            if (value == null)
            {
                // nulls written as ResXNullRef
                WriteString(name, "", typeof(ResXNullRef), comment);
                return;
            }

            var converter = System.Windows.Forms.TypeDescriptor.GetConverter(value);

            if (value is ResXFileRef)
            {
                ResXFileRef fileRef = ProcessFileRefBasePath((ResXFileRef)value);
                string      str     = (string)converter.ConvertToInvariantString(fileRef);
                WriteString(name, str, value.GetType(), comment);
                return;
            }

            if (converter != null && converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string)))
            {
                string str = (string)converter.ConvertToInvariantString(value);
                WriteString(name, str, value.GetType(), comment);
                return;
            }

            if (converter != null && converter.CanConvertTo(typeof(byte[])) && converter.CanConvertFrom(typeof(byte[])))
            {
                byte[] b = (byte[])converter.ConvertTo(value, typeof(byte[]));
                WriteBytes(name, value.GetType(), b, comment);
                return;
            }
        }