Exemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                ProcessCommandLineInfo processCommandLineInfo = new ProcessCommandLine().Process(args);

                LanguageWriterBase.CrossPlatformSerializationSupport = processCommandLineInfo.CrossPlatformSerializationSupport;

                foreach (string sourceFile in processCommandLineInfo.SourceFiles)
                {
                    try
                    {
                        CodeGenDom codeGenerator = new CodeGenDom(processCommandLineInfo.OutputDirectory);
                        codeGenerator.GenerateCode(sourceFile, "", processCommandLineInfo.TargetLanguage);
                    }
                    catch (Exception x)
                    {
                        Console.WriteLine(String.Format("Code gen exception for file : {0}", sourceFile));
                        Console.WriteLine(x.ToString());
                    }
                }
            }
            catch (Exception x)
            {
                ProcessCommandLine.DisplayUsage();
                Console.WriteLine(x.ToString());
            }
        }
Exemplo n.º 2
0
        void DetectAndExtractNestedNamespaces(XmlSchema xsd, string xsdSchemaFileName, String orgDir, TargetLanguage targetLanguage)
        {
            try
            {
                foreach (XmlSchemaInclude inc in xsd.Includes)
                {
                    string    ns    = "";
                    XmlSchema xsdin = null;
                    FileInfo  f     = new FileInfo(xsdSchemaFileName);

                    string includeSchema = inc.SchemaLocation;
                    if (false == File.Exists(includeSchema))
                    {
                        includeSchema = f.Directory + "\\" + inc.SchemaLocation;
                    }

                    if (false == File.Exists(includeSchema))
                    {
                        includeSchema = orgDir + "\\" + new FileInfo(inc.SchemaLocation).Name;
                    }

                    using (StreamReader fs = new StreamReader(includeSchema))
                    {
                        FileInfo schemaLocation = new FileInfo(includeSchema);

                        CodeGenDom gen = new CodeGenDom(_destinationFolder);
                        FileInfo   src = new FileInfo(xsdSchemaFileName);


                        string headerFileName = string.Empty;

                        if (_targetLanguage == TargetLanguage.CSharp)
                        {
                            headerFileName = gen.GenerateCode(includeSchema, _destinationFolder + "\\" + schemaLocation.Name.Replace(".xsd", ".cs"), targetLanguage);
                        }
                        else if (_targetLanguage == TargetLanguage.JAVA)
                        {
                            headerFileName = gen.GenerateCode(includeSchema, _destinationFolder + "\\" + schemaLocation.Name.Replace(".xsd", ".java"), targetLanguage);
                        }
                        else
                        {
                            headerFileName = gen.GenerateCode(includeSchema, _destinationFolder + "\\" + schemaLocation.Name.Replace(".xsd", ".h"), targetLanguage);
                        }

                        xsdin = XmlSchema.Read(fs, null);
                        DetectAndExtractNestedNamespaces(xsdin, xsdSchemaFileName, schemaLocation.Directory.FullName, targetLanguage);

                        if (xsdin.Id == null)
                        {
                            xsdin.Id = schemaLocation.Name.Split('.')[0];
                        }

                        _externalSchemas.Add(xsdin);

                        string currDir = Environment.CurrentDirectory;

                        try
                        {
                            Environment.CurrentDirectory = _destinationFolder;
                            string tempCurDir = new FileInfo(includeSchema).DirectoryName;
                            if (!Directory.Exists(tempCurDir))
                            {
                                tempCurDir = orgDir;
                            }

                            Environment.CurrentDirectory = tempCurDir;
                            xsdin.Compile(null);
                        }
                        catch (Exception x)
                        {
                            string s = x.Message;
                        }
                        finally
                        {
                            Environment.CurrentDirectory = currDir;
                        }

                        if (headerFileName != string.Empty)
                        {
                            _includeFiles.Add(headerFileName);
                        }

                        // if ((MixedMode == true) && inc.Id == "SuppressManagedCodeGeneration")
                        //    _includeFilesToSkip.Add(headerFileName);

                        if ((MixedMode == true) && (inc.Annotation != null))
                        {
                            if (inc.Annotation.Items.Count == 1)
                            {
                                XmlSchemaDocumentation ann = inc.Annotation.Items[0] as XmlSchemaDocumentation;
                                if (ann != null)
                                {
                                    if (ann.Markup[0].InnerText == "SuppressManagedCodeGeneration")
                                    {
                                        _includeFilesToSkip.Add(headerFileName);
                                    }
                                }
                            }
                        }

                        string nSpace = GetNamespaceFromXsd(xsdin);
                        if (nSpace != string.Empty)
                        {
                            if (_externalNamespaces.Contains(nSpace) == false)
                            {
                                _externalNamespaces.Add(nSpace);
                            }
                        }
                    }
                }
            }
            catch (Exception x)
            {
                Console.WriteLine("File Name = '{0}' , Message = '{1}'", _fileUnderProcessing, x.ToString());
            }
        }