protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            CodeCompileUnit codeCompileUnit;

            if (string.IsNullOrEmpty(inputFileContent) || IsLocalizedFile(inputFileName))
            {
                if (base.CodeGeneratorProgress != null)
                {
                    NativeMethods.ThrowOnFailure(base.CodeGeneratorProgress.Progress(100, 100));
                }

                return(null);
            }

            using (MemoryStream codeStream = new MemoryStream())
            {
                using (StreamWriter streamWriter = new StreamWriter(codeStream, Encoding.UTF8))
                {
                    IDictionary nodeDictionary         = new Hashtable(StringComparer.OrdinalIgnoreCase);
                    IDictionary nodePositionDictionary = new Hashtable(StringComparer.OrdinalIgnoreCase);
                    using (IResourceReader resourceReader = ResXResourceReader.FromFileContents(inputFileContent))
                    {
                        ResXResourceReader resXResourceReader = resourceReader as ResXResourceReader;
                        if (resXResourceReader != null)
                        {
                            resXResourceReader.UseResXDataNodes = true;
                            string inputDirectoryName = Path.GetDirectoryName(inputFileName);
                            resXResourceReader.BasePath = Path.GetFullPath(inputDirectoryName);
                        }
                        foreach (DictionaryEntry resourceEntry in resourceReader)
                        {
                            ResXDataNode resXNode = (ResXDataNode)resourceEntry.Value;
                            nodeDictionary.Add(resourceEntry.Key, resXNode);
                            nodePositionDictionary.Add(resourceEntry.Key, resXNode.GetNodePosition());
                        }
                    }

                    string resourceNamespace             = this.GetResourcesNamespace();
                    string inputFileNameWithoutExtension = Path.GetFileNameWithoutExtension(inputFileName);

                    List <Tools.ResourceErrorData> unmatchableResources = new List <Tools.ResourceErrorData>();
                    if (resourceNamespace != null)
                    {
                        codeCompileUnit = Tools.StronglyTypedResourceBuilderEx.Create(this.GetType(),
                                                                                      nodeDictionary, inputFileNameWithoutExtension, base.FileNameSpace, resourceNamespace,
                                                                                      this.CodeProvider, GenerateInternalClass, unmatchableResources);
                    }
                    else
                    {
                        codeCompileUnit = Tools.StronglyTypedResourceBuilderEx.Create(this.GetType(),
                                                                                      nodeDictionary, inputFileNameWithoutExtension, base.FileNameSpace, this.CodeProvider,
                                                                                      GenerateInternalClass, unmatchableResources);
                    }

                    if (base.CodeGeneratorProgress != null)
                    {
                        foreach (Tools.ResourceErrorData resourceErrorData in unmatchableResources)
                        {
                            Point nodePosition = (Point)nodePositionDictionary[resourceErrorData.ResourceKey];
                            base.CodeGeneratorProgress.GeneratorError(1, 1, resourceErrorData.ErrorString,
                                                                      (uint)nodePosition.Y, (uint)nodePosition.X);
                        }

                        base.CodeGeneratorProgress.Progress(70, 100);
                    }

                    this.HandleCodeCompileUnit(codeCompileUnit);

                    if (base.CodeGeneratorProgress != null)
                    {
                        base.CodeGeneratorProgress.Progress(0x4b, 100);
                    }

                    ICodeGenerator codeGenerator = this.CodeProvider.CreateGenerator();
                    codeGenerator.GenerateCodeFromCompileUnit(codeCompileUnit, streamWriter, null);
                    if (base.CodeGeneratorProgress != null)
                    {
                        NativeMethods.ThrowOnFailure(base.CodeGeneratorProgress.Progress(100, 100));
                    }

                    streamWriter.Flush();
                    codeStream.Flush();

                    return(EnsureSilverlighCompatibility(codeStream.ToArray(), codeCompileUnit));
                }
            }
        }
Example #2
0
        /// <summary>Performs code generation</summary>
        /// <param name="inputFileName">Name of file to convert. Note: May be invalid or inactual. <paramref name="inputFileContent"/> is more important.</param>
        /// <param name="inputFileContent">Content of file to convert</param>
        /// <returns>Converted content</returns>
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {
            CodeCompileUnit codeCompileUnit;

            if (string.IsNullOrEmpty(inputFileContent) || IsLocalizedFile(inputFileName))
            {
                if (CodeGeneratorProgress != null)
                {
                    NativeMethods.ThrowOnFailure(CodeGeneratorProgress.Progress(100, 100));
                }

                return(null);
            }

            StreamWriter streamWriter           = new StreamWriter(new MemoryStream(), Encoding.UTF8);
            IDictionary  nodeDictionary         = new Hashtable(StringComparer.OrdinalIgnoreCase);
            IDictionary  nodePositionDictionary = new Hashtable(StringComparer.OrdinalIgnoreCase);

            using (IResourceReader resourceReader = ResXResourceReader.FromFileContents(inputFileContent)) {
                ResXResourceReader resXResourceReader = resourceReader as ResXResourceReader;
                if (resXResourceReader != null)
                {
                    resXResourceReader.UseResXDataNodes = true;
                    string inputDirectoryName = Path.GetDirectoryName(inputFileName);
                    resXResourceReader.BasePath = Path.GetFullPath(inputDirectoryName);
                }
                foreach (DictionaryEntry resourceEntry in resourceReader)
                {
                    ResXDataNode resXNode = (ResXDataNode)resourceEntry.Value;
                    nodeDictionary.Add(resourceEntry.Key, resXNode);
                    nodePositionDictionary.Add(resourceEntry.Key, resXNode.GetNodePosition());
                }
            }

            string resourceNamespace             = this.GetResourcesNamespace(false);
            string logicalName                   = this.GetResourcesNamespace(true);//Logical name added by Ðonny
            string inputFileNameWithoutExtension = Path.GetFileNameWithoutExtension(inputFileName);
            string className = inputFileNameWithoutExtension;

            //className from logical name added by Ðonny:
            GetClassNameFromLogicalName(ref className);


            List <ResourceErrorData> unmatchableResources = new List <ResourceErrorData>();

            if (resourceNamespace != null)
            {
                codeCompileUnit = StronglyTypedResourceBuilderEx.Create(this.GetType(),
                                                                        nodeDictionary, className, FileNamespace, resourceNamespace,                  //className added by Ðonny
                                                                        this.CodeProvider, GenerateInternalClass, unmatchableResources, logicalName); //Logical name added by Ðonny
            }
            else
            {
                codeCompileUnit = StronglyTypedResourceBuilderEx.Create(this.GetType(),
                                                                        nodeDictionary, className, base.FileNamespace, this.CodeProvider, //className added by Ðonny
                                                                        GenerateInternalClass, unmatchableResources, logicalName);        //Logical name added by Ðonny
            }
            if (base.CodeGeneratorProgress != null)
            {
                foreach (var resourceErrorData in unmatchableResources)
                {
                    Point nodePosition = (Point)nodePositionDictionary[resourceErrorData.ResourceKey];
                    base.CodeGeneratorProgress.GeneratorError(1, 1, resourceErrorData.ErrorString,
                                                              (uint)nodePosition.Y, (uint)nodePosition.X);
                }

                base.CodeGeneratorProgress.Progress(70, 100);
            }

            this.HandleCodeCompileUnit(codeCompileUnit);

            if (base.CodeGeneratorProgress != null)
            {
                base.CodeGeneratorProgress.Progress(0x4b, 100);
            }

            ICodeGenerator codeGenerator = this.CodeProvider.CreateGenerator();

            if (BeforeGenerateText != null)
            {
                BeforeGenerateText(codeGenerator);
            }
            codeGenerator.GenerateCodeFromCompileUnit(codeCompileUnit, streamWriter, null);
            if (base.CodeGeneratorProgress != null)
            {
                NativeMethods.ThrowOnFailure(base.CodeGeneratorProgress.Progress(100, 100));
            }

            streamWriter.Flush();

            return(StreamToBytes(streamWriter.BaseStream));
        }