コード例 #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);
     }
 }
コード例 #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);
        }
コード例 #3
0
 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);
     }
 }
コード例 #4
0
        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);
        }
コード例 #5
0
ファイル: ResXFileRefTest.cs プロジェクト: Profit0004/mono
		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");
		}
コード例 #6
0
ファイル: ResXDataNode.cs プロジェクト: sengiv/WasmWinforms
        //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);
        }
コード例 #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));
            }
コード例 #8
0
ファイル: ResXDataNode.cs プロジェクト: z2516305651/winforms
        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;
        }
コード例 #9
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));
        }
コード例 #10
0
ファイル: ResXDataNode.cs プロジェクト: sengiv/WasmWinforms
        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");
            }
        }
コード例 #11
0
ファイル: ResXFileRefTest.cs プロジェクト: Profit0004/mono
		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");
		}
コード例 #12
0
ファイル: ResXDataNode.cs プロジェクト: carrie901/mono
		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;
		}
コード例 #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);
        }
コード例 #14
0
ファイル: ResXFileRefTest.cs プロジェクト: nlhepler/mono
		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
		}
コード例 #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;
 }
コード例 #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;
        }
コード例 #17
0
        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);
        }
コード例 #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);
		}
コード例 #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 ();
				}
			}
		}
コード例 #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());
            }
        }
コード例 #21
0
ファイル: ResourcesTestHelper.cs プロジェクト: nlhepler/mono
		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;
		}
コード例 #22
0
		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);
		}
コード例 #23
0
 public ResXDataNode(string name, ResXFileRef fileRef) : this(name, fileRef, null)
 {
 }
コード例 #24
0
ファイル: ResXDataNode.cs プロジェクト: z2516305651/winforms
 /// <summary>
 ///
 /// </summary>
 public ResXDataNode(string name, ResXFileRef fileRef) : this(name, fileRef, null)
 {
 }
コード例 #25
0
ファイル: ResXResourceReader.cs プロジェクト: yonder/mono
        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;
            }
        }
コード例 #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;
        }
コード例 #27
0
ファイル: WriterTest.cs プロジェクト: nlhepler/mono
		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);
			}
		}
コード例 #28
0
		ResXFileRef resXFileRef; // same as that referenced in ResXDataNode

		public FileRefHandler (ResXFileRef fileRef)
		{
			resXFileRef = fileRef;
		}
コード例 #29
0
 public ResXDataNode(string name, ResXFileRef fileRef, Func <Type, string> typeNameConverter)
 {
     _name              = name.OrThrowIfNullOrEmpty();
     _fileRef           = fileRef.OrThrowIfNull();
     _typeNameConverter = typeNameConverter;
 }
コード例 #30
0
ファイル: ResXDataNode.cs プロジェクト: nlhepler/mono
		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");
		}
コード例 #31
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().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();
        }
コード例 #32
0
		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);
		}
コード例 #33
0
        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;
        }
コード例 #34
0
ファイル: ResXDataNodeTest.cs プロジェクト: GirlD/mono
		public void FileRef ()
		{
			ResXFileRef fileRef = new ResXFileRef ("fileName", "Type.Name");
			ResXDataNode node = new ResXDataNode ("name", fileRef);
			Assert.AreEqual (fileRef, node.FileRef, "#A1");
		}
コード例 #35
0
 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;
 }
コード例 #36
0
ファイル: ResXFileRefTest.cs プロジェクト: Profit0004/mono
		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");
		}
コード例 #37
0
 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;
 }
コード例 #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");
		}
コード例 #39
0
ファイル: ResourcesTestHelper.cs プロジェクト: nlhepler/mono
		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;
		}
コード例 #40
0
 public ResXDataNode(string name, ResXFileRef fileRef)
 {
     throw null;
 }
コード例 #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 ();
				}
			}
		}
コード例 #42
0
 public ResXDataNode(string name, ResXFileRef fileRef, Func <Type, string> typeNameConverter)
 {
     throw null;
 }
コード例 #43
0
        ResXFileRef resXFileRef;         // same as that referenced in ResXDataNode

        public FileRefHandler(ResXFileRef fileRef)
        {
            resXFileRef = fileRef;
        }
コード例 #44
0
ファイル: ResXDataNode.cs プロジェクト: nlhepler/mono
		//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;
		}
コード例 #45
0
ファイル: ResXDataNodeTest.cs プロジェクト: GirlD/mono
		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");
			}
		}
コード例 #46
0
ファイル: ResXFileRefTest.cs プロジェクト: Profit0004/mono
		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");
		}
コード例 #47
0
		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);
		}
コード例 #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;
            }
        }