public virtual string GetImplements(ClassMapping cmap) { SupportClass.ListCollectionSupport interfaces = new SupportClass.ListCollectionSupport(); // implement proxy, but NOT if the proxy is the class it self! if ((Object)cmap.Proxy != null && (!cmap.Proxy.Equals(cmap.FullyQualifiedName))) { interfaces.Add(cmap.Proxy); } if (!cmap.Interface) { if (cmap.SuperClassMapping != null && cmap.SuperClassMapping.Interface) { interfaces.Add(cmap.SuperClassMapping.ClassName.FullyQualifiedName); } if (cmap.GetMeta("implements") != null) { interfaces.AddAll(cmap.GetMeta("implements")); } //interfaces.Add(typeof(System.Runtime.Serialization.ISerializable).FullName); } else { // interfaces can't implement suff } if (interfaces.Count > 0) { StringBuilder sbuf = new StringBuilder(); bool first = true; for (IEnumerator iter = interfaces.GetEnumerator(); iter.MoveNext();) { if (first) { first = false; } else { sbuf.Append(","); } sbuf.Append(ShortenType(iter.Current.ToString(), cmap.Imports)); } return(sbuf.ToString()); } else { return(string.Empty); } }
public virtual void AddCritera(ClassMapping criteriaClass, FieldProperty field, string condition) { string newCritera = criteriaClass.Name.ToLower() + "." + field.FieldName + condition + "?"; params_Renamed.Add(FinderRenderer.GetFieldAsObject(false, field)); criteria.Add(newCritera); criteriaParamTypes.Add(FinderRenderer.GetFieldAsHibernateType(false, field)); }
public virtual string GetImplements(ClassMapping cmap) { SupportClass.ListCollectionSupport interfaces = new SupportClass.ListCollectionSupport(); // implement proxy, but NOT if the proxy is the class it self! if ((Object) cmap.Proxy != null && (!cmap.Proxy.Equals(cmap.FullyQualifiedName))) { interfaces.Add(cmap.Proxy); } if (!cmap.Interface) { if (cmap.SuperClassMapping != null && cmap.SuperClassMapping.Interface) { interfaces.Add(cmap.SuperClassMapping.ClassName.FullyQualifiedName); } if (cmap.GetMeta("implements") != null) { interfaces.AddAll(cmap.GetMeta("implements")); } //interfaces.Add(typeof(System.Runtime.Serialization.ISerializable).FullName); } else { // interfaces can't implement suff } if (interfaces.Count > 0) { StringBuilder sbuf = new StringBuilder(); bool first = true; for (IEnumerator iter = interfaces.GetEnumerator(); iter.MoveNext();) { if (first) first = false; else sbuf.Append(","); sbuf.Append(ShortenType(iter.Current.ToString(), cmap.Imports)); } return sbuf.ToString(); } else { return string.Empty; } }
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); } }
public virtual void DoEqualsAndHashCode(string savedToClass, ClassMapping classMapping, StringWriter writer) { if (classMapping.MustImplementEquals()) { writer.WriteLine(" public override bool Equals(object obj) {"); writer.WriteLine(" if(this == obj) return true;"); writer.WriteLine(" if((obj == null) || (obj.GetType() != this.GetType())) return false;"); writer.WriteLine(" " + savedToClass + " castObj = (" + savedToClass + ") obj;"); writer.Write(" return (castObj != null) "); int usedFields = 0; SupportClass.ListCollectionSupport idFields = new SupportClass.ListCollectionSupport(); for (IEnumerator fields = classMapping.Fields.GetEnumerator(); fields.MoveNext();) { FieldProperty field = (FieldProperty) fields.Current; if (field.GetMetaAsBool("use-in-equals")) { writer.Write(" && (this." + field.fieldcase + " == castObj." + field.fieldcase + ")"); usedFields++; } if (field.Identifier) { idFields.Add(field); } } if (usedFields == 0) { log.Warn("No properties has been marked as being used in equals/hashcode for " + classMapping.Name + ". Using object identifier which is RARELY safe to use! See http://hibernate.org/109.html"); for (IEnumerator fields = idFields.GetEnumerator(); fields.MoveNext();) { FieldProperty field = (FieldProperty) fields.Current; writer.Write(" && (this." + field.fieldcase + " == castObj." + field.fieldcase + ")"); } } writer.WriteLine(";"); writer.WriteLine(" }"); writer.WriteLine(); writer.WriteLine(" public override int GetHashCode() {"); writer.WriteLine(" int hash = 69;"); //writer.Write(" return"); for (IEnumerator fields = classMapping.Fields.GetEnumerator(); fields.MoveNext();) { FieldProperty field = (FieldProperty) fields.Current; if (field.GetMetaAsBool("use-in-equals")) { //writer.Write("\n " + field.FieldName + ".GetHashCode() ^"); writer.WriteLine(" hash = 31 * hash + " + field.fieldcase + ".GetHashCode();"); } } if (usedFields == 0) { for (IEnumerator fields = idFields.GetEnumerator(); fields.MoveNext();) { FieldProperty field = (FieldProperty) fields.Current; //writer.Write("\n " + field.FieldName + ".GetHashCode() ^"); writer.WriteLine(" hash = 31 * hash + " + field.fieldcase + ".GetHashCode();"); } } //writer.WriteLine(" 0;"); writer.WriteLine(" return hash;"); writer.WriteLine(" }"); writer.WriteLine(); } }
public static void Main(String[] args) { nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("urn", "urn:nhibernate-mapping-2.2"); children = new ArrayList(); allMaps = new MultiMap(); File.Delete("error-log.txt"); // DOMConfigurator is deprecated in the latest log4net, but we are using an earlier // version that comes with NVelocity. XmlConfigurator.Configure(new FileInfo("NHibernate.Tool.hbm2net.exe.config")); if (args.Length == 0) { Console.Error.WriteLine("No arguments provided. Nothing to do. Exit."); Environment.Exit(- 1); } ArrayList mappingFiles = new ArrayList(); string outputDir = null; SupportClass.ListCollectionSupport generators = new SupportClass.ListCollectionSupport(); MultiMap globalMetas = new MultiHashMap(); // parse command line parameters for (int i = 0; i < args.Length; i++) { if (args[i].StartsWith("--")) { if (args[i].StartsWith("--config=")) { FileInfo configFile = new FileInfo(args[i].Substring(9)); // parse config xml file Document document = new XmlDocument(); document.Load(configFile.FullName); 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)); } } else if (args[i].StartsWith("--output=")) { outputDir = args[i].Substring(9); } } else if (args[i].IndexOf("*") > -1) { // Handle wildcards mappingFiles.AddRange(GetFiles(args[i])); } else { mappingFiles.Add(args[i]); } } // if no config xml file, add a default generator if (generators.Count == 0) { generators.Add(new Generator(new DirectoryInfo(Environment.CurrentDirectory))); } 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); 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); } } }
public virtual void DoEqualsAndHashCode(string savedToClass, ClassMapping classMapping, StringWriter writer) { if (classMapping.MustImplementEquals()) { writer.WriteLine(" public override bool Equals(object obj) {"); writer.WriteLine(" if(this == obj) return true;"); writer.WriteLine(" if((obj == null) || (obj.GetType() != this.GetType())) return false;"); writer.WriteLine(" " + savedToClass + " castObj = (" + savedToClass + ") obj;"); writer.Write(" return (castObj != null) "); int usedFields = 0; SupportClass.ListCollectionSupport idFields = new SupportClass.ListCollectionSupport(); for (IEnumerator fields = classMapping.Fields.GetEnumerator(); fields.MoveNext();) { FieldProperty field = (FieldProperty)fields.Current; if (field.GetMetaAsBool("use-in-equals")) { writer.Write(" && (this." + field.fieldcase + " == castObj." + field.fieldcase + ")"); usedFields++; } if (field.Identifier) { idFields.Add(field); } } if (usedFields == 0) { log.Warn("No properties has been marked as being used in equals/hashcode for " + classMapping.Name + ". Using object identifier which is RARELY safe to use! See http://hibernate.org/109.html"); for (IEnumerator fields = idFields.GetEnumerator(); fields.MoveNext();) { FieldProperty field = (FieldProperty)fields.Current; writer.Write(" && (this." + field.fieldcase + " == castObj." + field.fieldcase + ")"); } } writer.WriteLine(";"); writer.WriteLine(" }"); writer.WriteLine(); writer.WriteLine(" public override int GetHashCode() {"); writer.WriteLine(" int hash = 69;"); //writer.Write(" return"); for (IEnumerator fields = classMapping.Fields.GetEnumerator(); fields.MoveNext();) { FieldProperty field = (FieldProperty)fields.Current; if (field.GetMetaAsBool("use-in-equals")) { //writer.Write("\n " + field.FieldName + ".GetHashCode() ^"); writer.WriteLine(" hash = 31 * hash + " + field.fieldcase + ".GetHashCode();"); } } if (usedFields == 0) { for (IEnumerator fields = idFields.GetEnumerator(); fields.MoveNext();) { FieldProperty field = (FieldProperty)fields.Current; //writer.Write("\n " + field.FieldName + ".GetHashCode() ^"); writer.WriteLine(" hash = 31 * hash + " + field.fieldcase + ".GetHashCode();"); } } //writer.WriteLine(" 0;"); writer.WriteLine(" return hash;"); writer.WriteLine(" }"); writer.WriteLine(); } }
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); } }