Ejemplo n.º 1
0
 public void Dispose()
 {
     _resourceWriter.Dispose();
 }
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
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);
        }