private void Dispose(bool disposing)
 {
     if (_document != null)
     {
         _document.Dispose();
         _document = null;
     }
 }
        public DatabaseIndexedBuilder(DataSources dataSources)
        {
            if (dataSources == null)
            {
                throw new ArgumentNullException("dataSources");
            }
            if (!dataSources.IsBuilding || !dataSources.IsValid)
            {
                throw new ArgumentException("dataSources");
            }

            _dataSources = dataSources;
            _workingDir  = dataSources.OutputDir;
            _document    = new DatabaseIndexedDocument(true, true,
                                                       _workingDir);
        }
        public DatabaseIndexedBuilder(string workingDir, bool isComments)
        {
            if (workingDir == null)
            {
                throw new ArgumentNullException("workingDir",
                                                "The working directory is required and cannot be null (or Nothing).");
            }
            if (workingDir.Length == 0 || !Directory.Exists(workingDir))
            {
                throw new ArgumentException(
                          "The working directory is required and cannot be null (or Nothing).",
                          "workingDir");
            }

            _workingDir = workingDir;
            _document   = new DatabaseIndexedDocument(true, isComments,
                                                      _workingDir);
        }
        public bool Build()
        {
            if (String.IsNullOrEmpty(_workingDir) ||
                !Directory.Exists(_workingDir))
            {
                return(false);
            }

            if (_dataSources != null)
            {
                if (!_dataSources.IsValid || _dataSources.SourceCount == 0)
                {
                    return(false);
                }
                foreach (string dataDir in _dataSources.Sources)
                {
                    if (Directory.Exists(dataDir))
                    {
                        this.AddDocuments(dataDir, "*.xml", false, false);
                    }
                }
            }
            else
            {
                string dataDir = null;
                if (_dataSource != null && Directory.Exists(_dataSource.InputDir))
                {
                    dataDir = _dataSource.InputDir;
                }
                else
                {
                    dataDir = Environment.ExpandEnvironmentVariables(
                        @"%DXROOT%\Data\Reflection");
                    dataDir = Path.GetFullPath(dataDir);
                }
                if (!Directory.Exists(dataDir))
                {
                    return(false);
                }

                this.AddDocuments(dataDir, "*.xml", false, false);
            }

            if (_document != null)
            {
                _document.Dispose();
                _document = null;
            }

            // Perform a defragmentation of the PersistentDictionary.edb database
            Process process = new Process();

            ProcessStartInfo startInfo = process.StartInfo;

            startInfo.FileName               = "esentutl.exe";
            startInfo.Arguments              = "-d " + DataSource.DatabaseFileName + " -o";
            startInfo.UseShellExecute        = false;
            startInfo.CreateNoWindow         = true;
            startInfo.WorkingDirectory       = _workingDir;
            startInfo.RedirectStandardOutput = false;

            // Now, start the process - there will still not be output till...
            process.Start();
            // We must wait for the process to complete...
            process.WaitForExit();
            int exitCode = process.ExitCode;

            process.Close();
            if (exitCode != 0)
            {
                return(false);
            }

            string[] logFiles = Directory.GetFiles(_workingDir, "*.log",
                                                   SearchOption.TopDirectoryOnly);
            if (logFiles != null)
            {
                for (int i = 0; i < logFiles.Length; i++)
                {
                    File.Delete(logFiles[i]);
                }
            }

            if (_dataSources != null && _dataSources.Exists)
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent             = true;
                settings.IndentChars        = new string(' ', 4);
                settings.Encoding           = Encoding.UTF8;
                settings.OmitXmlDeclaration = false;

                XmlWriter writer = null;
                try
                {
                    writer = XmlWriter.Create(Path.Combine(_workingDir,
                                                           DataSources.XmlFileName), settings);

                    writer.WriteStartDocument();

                    _dataSources.WriteXml(writer);

                    writer.WriteEndDocument();
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        writer = null;
                    }
                }
            }

            return(true);
        }