コード例 #1
0
        string CompileResource(AppResourceFileInfo arfi, bool local)
        {
            string path  = arfi.Info.FullName;
            string rname = Path.GetFileNameWithoutExtension(path) + ".resources";

            if (!local)
            {
                rname = "Resources." + rname;
            }

            string          resource = Path.Combine(TempDirectory, rname);
            FileStream      source = null, destination = null;
            IResourceReader reader = null;
            ResourceWriter  writer = null;

            try
            {
                source      = new FileStream(path, FileMode.Open, FileAccess.Read);
                destination = new FileStream(resource, FileMode.Create, FileAccess.Write);
                reader      = GetReaderForKind(arfi.Kind, source, path);
                writer      = new ResourceWriter(destination);
                foreach (DictionaryEntry de in reader)
                {
                    object val = de.Value;
                    if (val is string)
                    {
                        writer.AddResource((string)de.Key, (string)val);
                    }
                    else
                    {
                        writer.AddResource((string)de.Key, val);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new HttpException("Failed to compile resource file", ex);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
                if (source != null)
                {
                    source.Dispose();
                }
                if (writer != null)
                {
                    writer.Dispose();
                }
                if (destination != null)
                {
                    destination.Dispose();
                }
            }

            return(resource);
        }
コード例 #2
0
 public override bool Walk(FunctionDefinition node)
 {
     if (IsInitializeComponentMethod(node))
     {
         Type type = GetComponentType();
         component = componentCreator.CreateComponent(type, componentName);
         IResourceReader reader = componentCreator.GetResourceReader(CultureInfo.InvariantCulture);
         if (reader != null)
         {
             reader.Dispose();
         }
         node.Body.Walk(this);
     }
     return(false);
 }
コード例 #3
0
        protected override void Walk(MethodDefinition node)
        {
            if (IsInitializeComponentMethod(node))
            {
                Type type = GetComponentType();
                component = componentCreator.CreateComponent(type, componentName);

                IResourceReader reader = componentCreator.GetResourceReader(CultureInfo.InvariantCulture);
                if (reader != null)
                {
                    reader.Dispose();
                }

                WalkMethodStatements(node.Body.Statements);
            }
        }
コード例 #4
0
    static int CompileResourceFile(string sname, string dname, bool useSourcePath)
    {
        FileStream      source = null;
        FileStream      dest   = null;
        IResourceReader reader = null;
        IResourceWriter writer = null;

        try {
            source = new FileStream(sname, FileMode.Open, FileAccess.Read);
            reader = GetReader(source, sname, useSourcePath);

            dest   = new FileStream(dname, FileMode.Create, FileAccess.Write);
            writer = GetWriter(dest, dname);

            int rescount = 0;
            foreach (DictionaryEntry e in reader)
            {
                rescount++;
                object val = e.Value;
                if (val is string)
                {
                    writer.AddResource((string)e.Key, (string)e.Value);
                }
                else
                {
                    writer.AddResource((string)e.Key, e.Value);
                }
            }
            Console.WriteLine("Read in {0} resources from '{1}'", rescount, sname);

            reader.Close();
            writer.Close();
            Console.WriteLine("Writing resource file...  Done.");
        } catch (Exception e) {
            Console.WriteLine("Error: {0}", e.Message);
            Exception inner = e.InnerException;

            // under 2.0 ResXResourceReader can wrap an exception into an XmlException
            // and this hides some helpful message from the original exception
            XmlException xex = (inner as XmlException);
            if (xex != null)
            {
                // message is identical to the inner exception (from MWF ResXResourceReader)
                Console.WriteLine("Position: Line {0}, Column {1}.", xex.LineNumber, xex.LinePosition);
                inner = inner.InnerException;
            }

            if (inner is System.Reflection.TargetInvocationException && inner.InnerException != null)
            {
                inner = inner.InnerException;
            }
            if (inner != null)
            {
                Console.WriteLine("Inner exception: {0}", inner.Message);
            }

            if (reader != null)
            {
                reader.Dispose();
            }
            if (source != null)
            {
                source.Close();
            }
            if (writer != null)
            {
                writer.Dispose();
            }
            if (dest != null)
            {
                dest.Close();
            }

            // since we're not first reading all entries in source, we may get a
            // read failure after we're started writing to the destination file
            // and leave behind a broken resources file, so remove it here
            try {
                File.Delete(dname);
            } catch {
            }
            return(1);
        }
        return(0);
    }
コード例 #5
0
        static int CompileResourceFile(string sname, string dname, bool useSourcePath, Options options)
        {
            FileStream      source = null;
            FileStream      dest   = null;
            IResourceReader reader = null;
            IResourceWriter writer = null;

            try {
                source = new FileStream(sname, FileMode.Open, FileAccess.Read);
                reader = GetReader(source, sname, useSourcePath, options);

                dest   = new FileStream(dname, FileMode.Create, FileAccess.Write);
                writer = GetWriter(dest, dname, options, sname);

                int rescount = 0;
                foreach (DictionaryEntry e in reader)
                {
                    rescount++;
                    object val = e.Value;
                    if (val is string)
                    {
                        writer.AddResource((string)e.Key, (string)e.Value);
                    }
                    else
                    {
                        // refactoring to do: We should probably wrap the ResXResourceWriter, and replace our use of IResourceWriter with a ResourceItem based interface
                        ResourceItem item = val as ResourceItem;
                        try {
                            if (writer is ResXResourceWriter && item != null)
                            {
                                // only write if the ResourceItem can be cast to ResXDataNode
                                ResXDataNode dataNode = ((ResourceItem)val).ToResXDataNode();
                                if (dataNode != null)
                                {
                                    writer.AddResource((string)e.Key, dataNode);
                                }
                            }
                            else
                            {
                                writer.AddResource((string)e.Key, e.Value);
                            }
                        } catch {
                            if (item != null && item.Metadata_OriginalSourceLine > 0)
                            {
                                Console.WriteLine("Line: {0}", item.Metadata_OriginalSourceLine);
                            }
                            throw;
                        }
                    }
                }
                Console.WriteLine("Read in {0} resources from '{1}'", rescount, sname);

                reader.Close();
                writer.Close();
                Console.WriteLine("Writing resource file...  Done.");
            } catch (Exception e) {
                Console.WriteLine("Error: {0}", e.Message);
                Exception inner = e.InnerException;

                // under 2.0 ResXResourceReader can wrap an exception into an XmlException
                // and this hides some helpful message from the original exception
                XmlException xex = (inner as XmlException);
                if (xex != null)
                {
                    // message is identical to the inner exception (from MWF ResXResourceReader)
                    Console.WriteLine("Position: Line {0}, Column {1}.", xex.LineNumber, xex.LinePosition);
                    inner = inner.InnerException;
                }

                if (inner is TargetInvocationException && inner.InnerException != null)
                {
                    inner = inner.InnerException;
                }
                if (inner != null)
                {
                    Console.WriteLine("Inner exception: {0}", inner.Message);
                }

                if (reader != null)
                {
                    reader.Dispose();
                }
                if (source != null)
                {
                    source.Close();
                }
                if (writer != null)
                {
                    writer.Dispose();
                }
                if (dest != null)
                {
                    dest.Close();
                }

                // since we're not first reading all entries in source, we may get a
                // read failure after we're started writing to the destination file
                // and leave behind a broken resources file, so remove it here
                try {
                    File.Delete(dname);
                } catch {
                }
                return(1);
            }
            return(0);
        }