/// <summary> /// Try to determine the format of the spreadsheet /// </summary> /// <param name="spreadsheetFile">The path to the spreadsheet</param> /// <returns>The type</returns> private DataSourceSpreadsheetFormat DetermineSpreadsheetFormat(string spreadsheetFile) { DataSourceSpreadsheetFormat spreadsheetFormat = DataSourceSpreadsheetFormat.Unknown; string extension = Path.GetExtension(spreadsheetFile); if (!string.IsNullOrEmpty(extension)) { switch (extension.ToLower()) { case ".xls": // Excel binary format spreadsheetFormat = DataSourceSpreadsheetFormat.Xls; break; case ".csv": // CSV format spreadsheetFormat = DataSourceSpreadsheetFormat.Csv; break; case ".xml": // XML Spreadsheet 2003 format spreadsheetFormat = DataSourceSpreadsheetFormat.Xml; break; } } if (spreadsheetFormat == DataSourceSpreadsheetFormat.Unknown) { if (FileUtils.IsHttpUrl(spreadsheetFile)) { // Try to determine spreadsheet type from URL // This is admittedly crude as it just looks for .xls/=xls, .csv/=csv, or .xml/=xml in the url // Other possibilities: // * use Uri class to break into more granular components // Uri uri = new Uri(spreadsheetFile); // string cleanURL = uri.Scheme + "://" + uri.GetComponents(UriComponents.HostAndPort, UriFormat.UriEscaped); // * perhaps defer to mime type (would require refactoring of download mechanism) string spreadsheetFileLowered = spreadsheetFile.ToLower(); if (spreadsheetFileLowered.Contains(".xls") || spreadsheetFileLowered.Contains("=xls")) { spreadsheetFormat = DataSourceSpreadsheetFormat.Xls; } else if (spreadsheetFileLowered.Contains(".csv") || spreadsheetFileLowered.Contains("=csv")) { spreadsheetFormat = DataSourceSpreadsheetFormat.Csv; } else if (spreadsheetFileLowered.Contains(".xml") || spreadsheetFileLowered.Contains("=xml")) { spreadsheetFormat = DataSourceSpreadsheetFormat.Xml; } } } return(spreadsheetFormat); }
/// <summary> /// Creates the preprocessing context. /// </summary> /// <returns>A new preprocessing context</returns> public PreprocessingContext CreatePreprocessingContext() { // Create the preprocessing context PreprocessingContext context = new PreprocessingContext(fixFalse); context.SettingsFiles = settings; context.PreserveMarkup = !clean; context.QuietMode = quiet; if (validate) { context.ValidateSettingsExist = true; context.ValidateXmlWellFormed = true; } else { context.ValidateSettingsExist = validateSettingsExist; context.ValidateXmlWellFormed = validateXmlWellFormed; } // create data sources collection List <DataSource> dataSources = new List <DataSource>(); foreach (string spreadsheetFile in spreadsheet) { DataSourceSpreadsheetFormat spreadsheetFormat = DetermineSpreadsheetFormat(spreadsheetFile); dataSources.Add(new DataSource(spreadsheetFile, DataSourceType.Spreadsheet, spreadsheetFormat)); } foreach (string databaseConnectionString in database) { dataSources.Add(new DataSource(databaseConnectionString, DataSourceType.Database)); } foreach (string customCommandLineString in custom) { dataSources.Add(new DataSource(customCommandLineString, DataSourceType.Custom)); } context.DataSources = dataSources; context.EnvironmentName = environment; context.FirstValueRowIndex = firstValueRow; context.EnvironmentNameRowIndex = environmentRow; context.DefaultValueColumnIndex = defaultValueCol; context.SettingNameColumnIndex = settingNameCol; context.List = list; context.PropertyToExtract = property; context.Delimiters = delimiters; context.NoDirectives = noDirectives; context.CountUsage = !string.IsNullOrEmpty(countReportFile); if (!string.IsNullOrEmpty(tokenStart)) { context.TokenStart = tokenStart; if (string.IsNullOrEmpty(tokenEnd)) { context.TokenEnd = tokenStart; } } if (!string.IsNullOrEmpty(tokenEnd)) { context.TokenEnd = tokenEnd; } return(context); }
/// <summary> /// Create a data source /// </summary> /// <param name="path">path or connection string</param> /// <param name="sourceType">type of data source</param> /// <param name="spreadsheetFormat">the format if it is a spreadsheet</param> public DataSource(string path, DataSourceType sourceType, DataSourceSpreadsheetFormat spreadsheetFormat) { Path = path; SourceType = sourceType; SpreadsheetFormat = spreadsheetFormat; }