Ejemplo n.º 1
0
        private static void ProcessFile(String inFile, String outFile, ResourceClassOptions resourceClassOptions, bool useSourcePath)
        {
            //Console.WriteLine("Processing {0} --> {1}", inFile, outFile);
            // Reset state
            resources.Clear();
            resourcesHashTable.Clear();

            try {
                // Explicitly handle missing input files here - don't catch a
                // FileNotFoundException since we can get them from the loader
                // if we try loading an assembly version we can't find.
                if (!File.Exists(inFile))
                {
                    Error(SR.GetString(SR.FileNotFound, inFile));
                    return;
                }

                ReadResources(inFile, useSourcePath);
            }
            catch (ArgumentException ae) {
                if (ae.InnerException is XmlException)
                {
                    XmlException xe = (XmlException)ae.InnerException;
                    Error(xe.Message, inFile, xe.LineNumber, xe.LinePosition);
                }
                else
                {
                    Error(ae.Message, inFile);
                }
                return;
            }
            catch (TextFileException tfe) {
                // Used to pass back error context from ReadTextResources to here.
                Error(tfe.Message, tfe.FileName, tfe.LineNumber, tfe.LinePosition);
                return;
            }
            catch (Exception e) {
                Error(e.Message, inFile);
                // We need to give meaningful error messages to the user.
                // Note that ResXResourceReader wraps any exception it gets
                // in an ArgumentException with the message "Invalid ResX input."
                // If you don't look at the InnerException, you have to attach
                // a debugger to find the problem.
                if (e.InnerException != null)
                {
                    Exception     inner = e.InnerException;
                    StringBuilder sb    = new StringBuilder(200);
                    sb.Append(e.Message);
                    while (inner != null)
                    {
                        sb.Append(" ---> ");
                        sb.Append(inner.GetType().Name);
                        sb.Append(": ");
                        sb.Append(inner.Message);
                        inner = inner.InnerException;
                    }
                    Error(SR.GetString(SR.SpecificError, e.InnerException.GetType().Name, sb.ToString()), inFile);
                }
                return;
            }

            try {
                WriteResources(outFile);
            }
            catch (IOException io) {
                Error(SR.GetString(SR.WriteError, outFile), outFile);
                if (io.Message != null)
                {
                    Error(SR.GetString(SR.SpecificError, io.GetType().Name, io.Message), outFile);
                }
                if (File.Exists(outFile))
                {
                    Error(SR.GetString(SR.CorruptOutput, outFile));
                    try {
                        File.Delete(outFile);
                    }
                    catch (Exception) {
                        Error(SR.GetString(SR.DeleteOutputFileFailed, outFile));
                    }
                }
                return;
            }
            catch (Exception e) {
                Error(SR.GetString(SR.GenericWriteError, outFile));
                if (e.Message != null)
                {
                    Error(SR.GetString(SR.SpecificError, e.GetType().Name, e.Message));
                }
            }
        }
Ejemplo n.º 2
0
        private static void ProcessFile(String inFile, String outFile, ResourceClassOptions resourceClassOptions, bool useSourcePath) {
            //Console.WriteLine("Processing {0} --> {1}", inFile, outFile);
            // Reset state
            resources.Clear();
            resourcesHashTable.Clear();
    
            try {
                // Explicitly handle missing input files here - don't catch a 
                // FileNotFoundException since we can get them from the loader
                // if we try loading an assembly version we can't find.
                if (!File.Exists(inFile)) {
                    Error(SR.GetString(SR.FileNotFound, inFile));
                    return;
                }

                ReadResources(inFile, useSourcePath);
            }
            catch (ArgumentException ae) {
                if (ae.InnerException is XmlException) {
                    XmlException xe = (XmlException) ae.InnerException;
                    Error(xe.Message, inFile, xe.LineNumber, xe.LinePosition);
                }
                else {
                    Error(ae.Message, inFile);
                }
                return;
            }
            catch (TextFileException tfe) {
                // Used to pass back error context from ReadTextResources to here.
                Error(tfe.Message, tfe.FileName, tfe.LineNumber, tfe.LinePosition);
                return;
            }
            catch (Exception e) {
                Error(e.Message, inFile);
                // We need to give meaningful error messages to the user. 
                // Note that ResXResourceReader wraps any exception it gets
                // in an ArgumentException with the message "Invalid ResX input."
                // If you don't look at the InnerException, you have to attach
                // a debugger to find the problem.
                if (e.InnerException != null) {
                    Exception inner = e.InnerException;
                    StringBuilder sb = new StringBuilder(200);
                    sb.Append(e.Message);
                    while (inner != null) {
                        sb.Append(" ---> ");
                        sb.Append(inner.GetType().Name);
                        sb.Append(": ");
                        sb.Append(inner.Message);
                        inner = inner.InnerException;
                    }
                    Error(SR.GetString(SR.SpecificError, e.InnerException.GetType().Name, sb.ToString()), inFile);
                }
                return;
            }
    
            try {
                WriteResources(outFile);
            }
            catch (IOException io) {
                Error(SR.GetString(SR.WriteError, outFile), outFile);
                if (io.Message != null)
                    Error(SR.GetString(SR.SpecificError, io.GetType().Name, io.Message), outFile);
                if (File.Exists(outFile)) {
                    Error(SR.GetString(SR.CorruptOutput, outFile));
                    try {
                        File.Delete(outFile);
                    }
                    catch (Exception) {
                        Error(SR.GetString(SR.DeleteOutputFileFailed, outFile));
                    }
                }
                return;
            }
            catch (Exception e) {
                Error(SR.GetString(SR.GenericWriteError, outFile));
                if (e.Message != null)
                    Error(SR.GetString(SR.SpecificError, e.GetType().Name, e.Message));
            }
        }
Ejemplo n.º 3
0
        /// <include file='doc\ResGen.uex' path='docs/doc[@for="ResGen.Main"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public static void Main(String[] args)
        {
            // Tell build we had an error, then set this to 0 if we complete successfully.
            Environment.ExitCode = errorCode;
            if (args.Length < 1 || args[0].Equals("-h") || args[0].Equals("-?") ||
                args[0].Equals("/h") || args[0].Equals("/?"))
            {
                Usage();
                return;
            }

            String[] inFiles  = null;
            String[] outFiles = null;
            // Default resource class options for all classes
            ResourceClassOptions resourceClassOptions = null;
            int  argIndex           = 0;
            bool setSimpleInputFile = false; // For resgen a.resources a.resx
            bool gotOutputFileName  = false; // For resgen a.txt a.resources b.txt
            bool useSourcePath      = false;
            bool isClassInternal    = true;

            while (argIndex < args.Length && errors == 0)
            {
                if (args[argIndex].Equals("/compile"))
                {
                    inFiles  = new String[args.Length - argIndex - 1];
                    outFiles = new String[args.Length - argIndex - 1];
                    for (int i = 0; i < inFiles.Length; i++)
                    {
                        inFiles[i] = args[argIndex + 1];
                        int index = inFiles[i].IndexOf(",");
                        if (index != -1)
                        {
                            String tmp = inFiles[i];
                            inFiles[i] = tmp.Substring(0, index);
                            if (!ValidResourceFileName(inFiles[i]))
                            {
                                Error(SR.GetString(SR.BadFileExtension, inFiles[i]));
                                break;
                            }
                            if (index == tmp.Length - 1)
                            {
                                Error(SR.GetString(SR.MalformedCompileString, tmp));
                                inFiles = new String[0];
                                break;
                            }
                            outFiles[i] = tmp.Substring(index + 1);
                            if (!ValidResourceFileName(outFiles[i]))
                            {
                                Error(SR.GetString(SR.BadFileExtension, outFiles[i]));
                                break;
                            }
                        }
                        else
                        {
                            if (!ValidResourceFileName(inFiles[i]))
                            {
                                if (inFiles[i][0] == '/' || inFiles[i][0] == '-')
                                {
                                    Error(SR.GetString(SR.InvalidCommandLineSyntax, "/compile", inFiles[i]));
                                }
                                else
                                {
                                    Error(SR.GetString(SR.BadFileExtension, inFiles[i]));
                                }
                                break;
                            }

                            string resourceFileName = GetResourceFileName(inFiles[i]);
                            Debug.Assert(resourceFileName != null, "Unexpected null file name!");
                            outFiles[i] = resourceFileName;
                        }
                        argIndex++;
                    }
                }
                else if (args[argIndex].StartsWith("/r:") || args[argIndex].StartsWith("-r:"))
                {
                    // assembly names syntax /r:c:\system\System.Drawing.dll
                    String s = args[argIndex];
                    s = s.Substring(3);  // Skip over "/r:"
                    if (assemblyList == null)
                    {
                        assemblyList = new List <AssemblyName>();
                    }
                    try {
                        assemblyList.Add(AssemblyName.GetAssemblyName(s));
                    }
                    catch (Exception e) {
                        Error(SR.GetString(SR.CantLoadAssembly, s, e.GetType().Name, e.Message));
                    }
                }
                else if (args[argIndex].ToLower(CultureInfo.InvariantCulture).Equals("/usesourcepath") ||
                         args[argIndex].ToLower(CultureInfo.InvariantCulture).Equals("-usesourcepath"))
                {
                    useSourcePath = true;
                }
                else if (args[argIndex].ToLower(CultureInfo.InvariantCulture).Equals("/publicclass") ||
                         args[argIndex].ToLower(CultureInfo.InvariantCulture).Equals("-publicclass"))
                {
                    isClassInternal = false;
                }
                else
                {
                    if (ValidResourceFileName(args[argIndex]))
                    {
                        if (!setSimpleInputFile)
                        {
                            inFiles            = new String[1];
                            inFiles[0]         = args[argIndex];
                            outFiles           = new String[1];
                            outFiles[0]        = GetResourceFileName(inFiles[0]);
                            setSimpleInputFile = true;
                        }
                        else
                        {
                            if (!gotOutputFileName)
                            {
                                outFiles[0]       = args[argIndex];
                                gotOutputFileName = true;
                            }
                            else
                            {
                                Error(SR.GetString(SR.InvalidCommandLineSyntax, "<none>", args[argIndex]));
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (args[argIndex][0] == '/' || args[argIndex][0] == '-')
                        {
                            Error(SR.GetString(SR.BadCommandLineOption, args[argIndex]));
                        }
                        else
                        {
                            Error(SR.GetString(SR.BadFileExtension, args[argIndex]));
                        }
                        return;
                    }
                }
                argIndex++;
            }

            if ((inFiles == null || inFiles.Length == 0) && errors == 0)
            {
                Usage();
                return;
            }

            if (resourceClassOptions != null)
            {
                resourceClassOptions.InternalClass = isClassInternal;

                // Verify we don't produce two identically named resource classes,
                // or write different classes to the same file when using the
                // /compile option.
                if (inFiles.Length > 1)
                {
                    if (resourceClassOptions.ClassName != null || resourceClassOptions.OutputFileName != null)
                    {
                        Error(SR.GetString(SR.CompileAndSTRDontMix));
                    }
                }
            }

            // Do all the work.
            if (errors == 0)
            {
                for (int i = 0; i < inFiles.Length; i++)
                {
                    ProcessFile(inFiles[i], outFiles[i], resourceClassOptions, useSourcePath);
                }
            }

            // Quit & report errors, if necessary.
            if (warnings != 0)
            {
                Console.Error.WriteLine(SR.GetString(SR.WarningCount, warnings));
            }

            if (errors != 0)
            {
                Console.Error.WriteLine(SR.GetString(SR.ErrorCount, errors));
                Debug.Assert(Environment.ExitCode != 0);
                // Now delete all the output files, ensuring the build won't
                // continue using half-generated output files.  This is a
                // backstop for other errors up above.
                if (outFiles != null)
                {
                    foreach (String outFile in outFiles)
                    {
                        if (File.Exists(outFile))
                        {
                            try {
                                File.Delete(outFile);
                            }
                            catch {}
                        }
                    }
                }
            }
            else
            {
                // Tell build we succeeded.
                Environment.ExitCode = 0;
            }
        }