Пример #1
0
        public static void Generate(String[] args,IFileCreationObserver fileCreationObserver)
        {
            //this has to change... dirty things during porting
            ClassMapping.ResetComponents();
            nsmgr = new XmlNamespaceManager(new NameTable());
            nsmgr.AddNamespace("urn", "urn:nhibernate-mapping-2.2");
            children = new ArrayList();
            allMaps = new MultiMap();
            ArrayList mappingFiles = new ArrayList();
            string outputDir = null;
            SupportClass.ListCollectionSupport generators = new SupportClass.ListCollectionSupport();
            MultiMap globalMetas = new MultiHashMap();
            // parse command line parameters
            cmdLine = new Hbm2NetParameters(args);
            try
            {
                cmdLine.Parse();
                if (0 == cmdLine.FileNames.Count())
                {
                    Console.Error.WriteLine("No input file(s) specified");
                    throw new NotEnougthParametersException();
                }
            }
            catch (NotEnougthParametersException)
            {
                Console.Error.WriteLine(string.Format("Use:hbm2net {0} files.hbm.xml ( wildcards allowed )",cmdLine.GetShortHelp()));
                Console.Error.WriteLine(cmdLine.GetHelp());
                Environment.Exit(-1);
            }
            if (!string.IsNullOrEmpty(cmdLine.ConfigFile))
            {
                if (File.Exists(cmdLine.ConfigFile))
                {
                    try
                    {
                        FileInfo configFile = new FileInfo(cmdLine.ConfigFile);
                        // parse config xml file
                        Document document = new XmlDocument();
                        document.Load(configFile.FullName);
                        var cfgValidator = new ConfigurationValidator();
                        cfgValidator.Validate(document);
                        if (!string.IsNullOrEmpty(cfgValidator.WarningMessage))
                        {
                            log.Warn("Configuration:" + cfgValidator.WarningMessage);
                        }
                        globalMetas = MetaAttributeHelper.LoadAndMergeMetaMap((document["codegen"]), null);
                        IEnumerator generateElements = document["codegen"].SelectNodes("generate").GetEnumerator();
                        while (generateElements.MoveNext())
                        {
                            generators.Add(new Generator(configFile.Directory, (Element)generateElements.Current));
                        }
                    }
                    catch (ConfigurationValidationException validationException)
                    {
                        Console.Error.WriteLine("Error validating configuration file:" + validationException.Message);
                        Environment.Exit(-1);
                    }
                    catch (Exception genericException)
                    {
                        Console.Error.WriteLine("Error reading configuration file:" + genericException.Message);
                        Environment.Exit(-1);
                    }
                }
                else
                {
                    log.Error("Configuration file:" + cmdLine.ConfigFile + " does not exist");
                    Environment.Exit(-1);
                }
            }
            if (generators.Count == 0)
            {
                log.Info("No configuration file specified: using T4 generator with default template.");
                T4Render t4 = new T4Render();
                t4.Configure(new DirectoryInfo(Directory.GetCurrentDirectory()), new NameValueCollection());
                generators.Add(new Generator(t4));
            }
            if (!string.IsNullOrEmpty(cmdLine.OutputDir))
            {
                outputDir = cmdLine.OutputDir;
            }
            foreach (string inFile in cmdLine.FileNames)
            {
                if (inFile.IndexOf("*") > -1)
                {
                    mappingFiles.AddRange(GetFiles(inFile));
                }
                else
                {
                    mappingFiles.Add(inFile);
                }
            }

            Hashtable classMappings = new Hashtable();
            for (IEnumerator iter = mappingFiles.GetEnumerator(); iter.MoveNext(); )
            {
                log.Info(iter.Current.ToString());

                string mappingFile = (string)iter.Current;
                if (!Path.IsPathRooted(mappingFile))
                {
                    mappingFile = Path.Combine(Environment.CurrentDirectory, mappingFile);
                }
                if (!File.Exists(mappingFile))
                    throw new FileNotFoundException("Mapping file does not exist.", mappingFile);

                // parse the mapping file
                NameTable nt = new NameTable();
                nt.Add("urn:nhibernate-mapping-2.2");
                Document document = new XmlDocument(nt);
                document.Load(mappingFile);
                FileInfo mappingFileInfo = new FileInfo(mappingFile);
                SourceFileInfoMap.Instance.Add(document, mappingFileInfo);
                Element rootElement = document["hibernate-mapping"];

                if (rootElement == null)
                    continue;

                XmlAttribute a = rootElement.Attributes["namespace"];
                string pkg = null;
                if (a != null)
                {
                    pkg = a.Value;
                }
                MappingElement me = new MappingElement(rootElement, null);
                IEnumerator classElements = rootElement.SelectNodes("urn:class", nsmgr).GetEnumerator();
                MultiMap mm = MetaAttributeHelper.LoadAndMergeMetaMap(rootElement, globalMetas);
                HandleClass(pkg, me, classMappings, classElements, mm, false);

                classElements = rootElement.SelectNodes("urn:subclass", nsmgr).GetEnumerator();
                HandleClass(pkg, me, classMappings, classElements, mm, true);

                classElements = rootElement.SelectNodes("urn:joined-subclass", nsmgr).GetEnumerator();
                HandleClass(pkg, me, classMappings, classElements, mm, true);

                // Ok, pickup subclasses that we found before their superclasses
                ProcessChildren(classMappings);

            }
            // generate source files
            for (IEnumerator iterator = generators.GetEnumerator(); iterator.MoveNext(); )
            {
                Generator g = (Generator)iterator.Current;
                g.BaseDirName = outputDir ?? ".\\";
                g.Generate(classMappings,fileCreationObserver,cmdLine.CheckTime!=null);
            }
        }
Пример #2
0
        public static void Generate(String[] args, IFileCreationObserver fileCreationObserver)
        {
            //this has to change... dirty things during porting
            ClassMapping.ResetComponents();
            nsmgr = new XmlNamespaceManager(new NameTable());
            nsmgr.AddNamespace("urn", "urn:nhibernate-mapping-2.2");
            children = new ArrayList();
            allMaps  = new MultiMap();
            ArrayList mappingFiles = new ArrayList();
            string    outputDir    = null;

            SupportClass.ListCollectionSupport generators = new SupportClass.ListCollectionSupport();
            MultiMap globalMetas = new MultiHashMap();

            // parse command line parameters
            cmdLine = new Hbm2NetParameters(args);
            try
            {
                cmdLine.Parse();
                if (0 == cmdLine.FileNames.Count())
                {
                    Console.Error.WriteLine("No input file(s) specified");
                    throw new NotEnougthParametersException();
                }
            }
            catch (NotEnougthParametersException)
            {
                Console.Error.WriteLine(string.Format("Use:hbm2net {0} files.hbm.xml ( wildcards allowed )", cmdLine.GetShortHelp()));
                Console.Error.WriteLine(cmdLine.GetHelp());
                Environment.Exit(-1);
            }
            if (!string.IsNullOrEmpty(cmdLine.ConfigFile))
            {
                if (File.Exists(cmdLine.ConfigFile))
                {
                    try
                    {
                        FileInfo configFile = new FileInfo(cmdLine.ConfigFile);
                        // parse config xml file
                        Document document = new XmlDocument();
                        document.Load(configFile.FullName);
                        var cfgValidator = new ConfigurationValidator();
                        cfgValidator.Validate(document);
                        if (!string.IsNullOrEmpty(cfgValidator.WarningMessage))
                        {
                            log.Warn("Configuration:" + cfgValidator.WarningMessage);
                        }
                        globalMetas = MetaAttributeHelper.LoadAndMergeMetaMap((document["codegen"]), null);
                        IEnumerator generateElements = document["codegen"].SelectNodes("generate").GetEnumerator();
                        while (generateElements.MoveNext())
                        {
                            generators.Add(new Generator(configFile.Directory, (Element)generateElements.Current));
                        }
                    }
                    catch (ConfigurationValidationException validationException)
                    {
                        Console.Error.WriteLine("Error validating configuration file:" + validationException.Message);
                        Environment.Exit(-1);
                    }
                    catch (Exception genericException)
                    {
                        Console.Error.WriteLine("Error reading configuration file:" + genericException.Message);
                        Environment.Exit(-1);
                    }
                }
                else
                {
                    log.Error("Configuration file:" + cmdLine.ConfigFile + " does not exist");
                    Environment.Exit(-1);
                }
            }
            if (generators.Count == 0)
            {
                log.Info("No configuration file specified: using T4 generator with default template.");
                T4Render t4 = new T4Render();
                t4.Configure(new DirectoryInfo(Directory.GetCurrentDirectory()), new NameValueCollection());
                generators.Add(new Generator(t4));
            }
            if (!string.IsNullOrEmpty(cmdLine.OutputDir))
            {
                outputDir = cmdLine.OutputDir;
            }
            foreach (string inFile in cmdLine.FileNames)
            {
                if (inFile.IndexOf("*") > -1)
                {
                    mappingFiles.AddRange(GetFiles(inFile));
                }
                else
                {
                    mappingFiles.Add(inFile);
                }
            }

            Hashtable classMappings = new Hashtable();

            for (IEnumerator iter = mappingFiles.GetEnumerator(); iter.MoveNext();)
            {
                log.Info(string.Concat("Handling:", iter.Current.ToString()));


                string mappingFile = (string)iter.Current;
                if (!Path.IsPathRooted(mappingFile))
                {
                    mappingFile = Path.Combine(Environment.CurrentDirectory, mappingFile);
                }
                if (!File.Exists(mappingFile))
                {
                    throw new FileNotFoundException("Mapping file does not exist.", mappingFile);
                }

                // parse the mapping file
                NameTable nt = new NameTable();
                nt.Add("urn:nhibernate-mapping-2.2");
                Document document = new XmlDocument(nt);
                document.Load(mappingFile);
                FileInfo mappingFileInfo = new FileInfo(mappingFile);
                SourceFileInfoMap.Instance.Add(document, mappingFileInfo);
                Element rootElement = document["hibernate-mapping"];

                if (rootElement == null)
                {
                    continue;
                }

                XmlAttribute a   = rootElement.Attributes["namespace"];
                string       pkg = null;
                if (a != null)
                {
                    pkg = a.Value;
                }
                MappingElement me            = new MappingElement(rootElement, null);
                IEnumerator    classElements = rootElement.SelectNodes("urn:class", nsmgr).GetEnumerator();
                MultiMap       mm            = MetaAttributeHelper.LoadAndMergeMetaMap(rootElement, globalMetas);
                HandleClass(pkg, me, classMappings, classElements, mm, false);

                classElements = rootElement.SelectNodes("urn:subclass", nsmgr).GetEnumerator();
                HandleClass(pkg, me, classMappings, classElements, mm, true);

                classElements = rootElement.SelectNodes("urn:joined-subclass", nsmgr).GetEnumerator();
                HandleClass(pkg, me, classMappings, classElements, mm, true);

                // Ok, pickup subclasses that we found before their superclasses
                ProcessChildren(classMappings);
            }
            // generate source files
            for (IEnumerator iterator = generators.GetEnumerator(); iterator.MoveNext();)
            {
                Generator g = (Generator)iterator.Current;
                g.BaseDirName = outputDir ?? ".\\";
                g.Generate(classMappings, fileCreationObserver, cmdLine.CheckTime != null);
            }
        }