Пример #1
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);
        }
Пример #2
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));
        }
Пример #3
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");
            }
        }
Пример #4
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;
            handler      = new FileRefHandler(fileRef);
        }
Пример #5
0
        ResXFileRef resXFileRef;         // same as that referenced in ResXDataNode

        public FileRefHandler(ResXFileRef fileRef)
        {
            resXFileRef = fileRef;
        }
Пример #6
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");
                }

                string filename = parts [0];

                if (Path.DirectorySeparatorChar == '/')
                {
                    filename = filename.Replace("\\", "/");
                }

                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(filename, encoding)) {
                        return(reader.ReadToEnd());
                    }
                }

                using (FileStream file = new FileStream(filename, 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 (filename) == ".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));
            }
Пример #7
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();
        }