Exemple #1
0
 public void Reset()
 {
     _code = null;
     _name = null;
     _type = CodeType.CSharpFastCode;
     _fileName = null;
     _mappings = new List<MapConfig>();
     _connection = new ConnectionConfig();
     _dataContext = new DataContextConfig();
     _usings = new List<string>();
     _references = new List<string>();
 }
Exemple #2
0
        public bool Load()
        {
            bool ret = false;
            XDocument doc = XDocument.Load(_fileName);

            if (doc.Element("spoolpad") != doc.Root) {
                _log.Error("The document root is corrupted!");
                return false;
            }

            var dataContext = doc.Root.Elements("datacontext").SingleOrDefault();

            if (dataContext != null) {
                _dataContext = new DataContextConfig();
                _dataContext.AutoGen = bool.Parse(dataContext.Attribute("autogen").Value);
                _dataContext.Enabled = bool.Parse(dataContext.Attribute("enabled").Value);

                if (dataContext.HasElements) {
                    var usingsDataContext = dataContext.Elements("using");
                    if (usingsDataContext != null) {
                        foreach (XElement u in usingsDataContext) {
                            _dataContext.UsingNamespaces.Add(u.Attribute("namespace").Value);
                        }
                    }
                    var referencesDataContext = dataContext.Elements("reference");
                    if (referencesDataContext != null) {
                        foreach (XElement r in referencesDataContext) {
                            string refAsm = r.Attribute("assembly").Value;
                            string pattern = Path.GetFileName(refAsm);
                            string fileSystemFolderAssembly = Path.GetDirectoryName(refAsm);
                            if (string.IsNullOrEmpty(fileSystemFolderAssembly)) {
                                _dataContext.References.Add(refAsm);
                            } else {
                                foreach (string reffile in Directory.GetFiles(Path.GetDirectoryName(refAsm), pattern, SearchOption.TopDirectoryOnly))
                                    _dataContext.References.Add(reffile);
                            }
                        }
                    }
                }
            }

            var name = doc.Root.Elements("name").SingleOrDefault();
            if (name != null)
                _name = name.Value;

            var code = doc.Root.Elements("code").SingleOrDefault();
            if (code != null)
                _code = code.Value;

            var type = doc.Root.Elements("codetype").SingleOrDefault();
            if (type != null)
                _type = (CodeType)int.Parse(type.Value);

            var connInfo = doc.Root.Elements("connection").SingleOrDefault();
            if (connInfo != null) {
                if (connInfo.HasElements) {
                    _connection = new ConnectionConfig();
                    var val = connInfo.Elements("connectionstring").SingleOrDefault();
                    if (val != null)
                        _connection.ConnectionString = val.Value;
                    val = connInfo.Elements("user").SingleOrDefault();
                    if (val != null)
                        _connection.User = val.Value;
                    val = connInfo.Elements("password").SingleOrDefault();
                    if (val != null)
                        _connection.Password = val.Value;
                    val = connInfo.Elements("driver").SingleOrDefault();
                    if (val != null)
                        _connection.Driver = val.Value;
                    val = connInfo.Elements("dialect").SingleOrDefault();
                    if (val != null)
                        _connection.Dialect = val.Value;
                    val = connInfo.Elements("showsql").SingleOrDefault();
                    if (val != null)
                        _connection.ShowSql = val.Value;
                }
            }

            var mappings = doc.Root.Elements("mappings").SingleOrDefault();
            if (mappings != null) {
                _mappings.Clear();

                foreach (XElement ele in mappings.Descendants("assembly")) {
                    if (File.Exists(ele.Value)) {
                        Assembly asm = Assembly.ReflectionOnlyLoadFrom(ele.Value);
                        foreach (string resource in asm.GetManifestResourceNames()) {
                            if (!resource.EndsWith("hbm.xml"))
                                continue;
                            _mappings.Add(MapConfig.FromAssemblyResource(ele.Value, resource));
                        }
                    }
                }

                foreach (XElement ele in mappings.Descendants("file")) {
                    string pattern = Path.GetFileName(ele.Value);
                    if (!pattern.EndsWith("hbm.xml")) {
                        _log.ErrorFormat("Mapping file {0} must match *hbm.xml pattern. Skipping file.", pattern);
                        continue;
                    }
                    foreach (string hbm in Directory.GetFiles(Path.GetDirectoryName(ele.Value), pattern, SearchOption.TopDirectoryOnly))
                        _mappings.Add(MapConfig.FromFile(hbm));
                }
            }

            var usings = doc.Root.Elements("usings").SingleOrDefault();
            if (usings != null) {
                _usings.Clear();
                foreach (XElement usn in usings.Descendants("namespace")) {
                    _usings.Add(usn.Value);
                }
            }

            var references = doc.Root.Elements("references").SingleOrDefault();
            if (references != null) {
                _references.Clear();
                foreach (XElement asm in references.Descendants("assembly")) {
                    string pattern = Path.GetFileName(asm.Value);
                    string fileSystemFolderAssembly = Path.GetDirectoryName(asm.Value);
                    if (string.IsNullOrEmpty(fileSystemFolderAssembly)) {
                        _references.Add(asm.Value);
                    } else {
                        foreach (string reffile in Directory.GetFiles(Path.GetDirectoryName(asm.Value), pattern, SearchOption.TopDirectoryOnly))
                            _references.Add(reffile);
                    }
                }
            }

            ret = true;
            return ret;
        }