private static string LoadFile(string basicFilePath, DslScript dslScript, int begin) { var filePaths = new List <string> { basicFilePath }; string basicFileExtension = Path.GetExtension(basicFilePath); if (basicFileExtension.Equals(".sql", StringComparison.OrdinalIgnoreCase)) { var directory = Path.GetDirectoryName(basicFilePath); var fileName = Path.GetFileNameWithoutExtension(basicFilePath); if (string.IsNullOrWhiteSpace(fileName)) { throw new DslSyntaxException("Referenced empty file name (" + basicFilePath + ") in DSL script. " + dslScript.ReportPosition(begin)); } // Look for SQL dialect-specific files before the generic SQL file: filePaths.Insert(0, Path.Combine(directory, fileName + "." + SqlUtility.DatabaseLanguage + basicFileExtension)); filePaths.Insert(1, Path.Combine(directory, fileName + " (" + SqlUtility.DatabaseLanguage + ")" + basicFileExtension)); } foreach (var filePath in filePaths) { if (File.Exists(filePath)) { return(File.ReadAllText(filePath, Encoding.Default)); } } throw new DslSyntaxException("Cannot find the extension file referenced in DSL script. " + dslScript.ReportPosition(begin) + "\r\nLooking for files:\r\n" + string.Join("\r\n", filePaths)); }
private static string ReadExternalText(DslScript dslScript, ref int end) { string script = dslScript.Script; int begin = end; end++; while (end < script.Length && script[end] != '>' && !invalidPathChars.Contains(script[end])) { end++; } if (end >= script.Length) { throw new DslSyntaxException("Unexpected end of script within external text reference. Missing closing character: '>'." + dslScript.ReportPosition(end)); } if (script[end] != '>') { throw new DslSyntaxException("Invalid filename character within external text reference. " + dslScript.ReportPosition(end)); } end++; // Skip closing character. string basicFilePath = script.Substring(begin + 1, end - begin - 2); string dslScriptFolder = Path.GetDirectoryName(dslScript.Path); return(LoadFile(Path.Combine(dslScriptFolder, basicFilePath), dslScript, begin)); }
private static string ReadQuotedString(DslScript dslScript, ref int end) { string script = dslScript.Script; char quote = script[end]; int begin = end; end++; while (true) { while (end < script.Length && script[end] != quote) { end++; } if (end >= script.Length) { throw new DslSyntaxException("Unexpected end of script within quoted string. Missing closing character: " + quote + ". " + dslScript.ReportPosition(begin)); } if (end + 1 < script.Length && script[end + 1] == quote) { // Two quote characters make escape sequence for a quote within the string: end += 2; continue; } else { // Single quote ends string: end++; break; } } return(script.Substring(begin + 1, end - begin - 2).Replace(new string(quote, 2), new string(quote, 1))); }
private static string ReadQuotedString(DslScript dslScript, ref int end) { string script = dslScript.Script; char quote = script[end]; int begin = end; end++; while (true) { while (end < script.Length && script[end] != quote) end++; if (end >= script.Length) throw new DslSyntaxException("Unexpected end of script within quoted string. Missing closing character: " + quote + ". " + dslScript.ReportPosition(begin)); if (end + 1 < script.Length && script[end + 1] == quote) { // Two quote characters make escape sequence for a quote within the string: end += 2; continue; } else { // Single quote ends string: end++; break; } } return script.Substring(begin + 1, end - begin - 2).Replace(new string(quote, 2), new string(quote, 1)); }
private static string ReadExternalText(DslScript dslScript, ref int end) { string script = dslScript.Script; int begin = end; end++; while (end < script.Length && script[end] != '>' && !invalidPathChars.Contains(script[end])) end++; if (end >= script.Length) throw new DslSyntaxException("Unexpected end of script within external text reference. Missing closing character: '>'." + dslScript.ReportPosition(end)); if (script[end] != '>') throw new DslSyntaxException("Invalid filename character within external text reference. " + dslScript.ReportPosition(end)); end++; // Skip closing character. string basicFilePath = script.Substring(begin + 1, end - begin - 2); string dslScriptFolder = Path.GetDirectoryName(dslScript.Path); return LoadFile(Path.Combine(dslScriptFolder, basicFilePath), dslScript, begin); }
private static string LoadFile(string basicFilePath, DslScript dslScript, int begin) { var filePaths = new List<string> { basicFilePath }; string basicFileExtension = Path.GetExtension(basicFilePath); if (basicFileExtension.Equals(".sql", StringComparison.OrdinalIgnoreCase)) { var directory = Path.GetDirectoryName(basicFilePath); var fileName = Path.GetFileNameWithoutExtension(basicFilePath); if (string.IsNullOrWhiteSpace(fileName)) throw new DslSyntaxException("Referenced empty file name (" + basicFilePath + ") in DSL script. " + dslScript.ReportPosition(begin)); // Look for SQL dialect-specific files before the generic SQL file: filePaths.Insert(0, Path.Combine(directory, fileName + "." + SqlUtility.DatabaseLanguage + basicFileExtension)); filePaths.Insert(1, Path.Combine(directory, fileName + " (" + SqlUtility.DatabaseLanguage + ")" + basicFileExtension)); } foreach (var filePath in filePaths) if (File.Exists(filePath)) return File.ReadAllText(filePath, Encoding.Default); throw new DslSyntaxException("Cannot find the extension file referenced in DSL script. " + dslScript.ReportPosition(begin) + "\r\nLooking for files:\r\n" + string.Join("\r\n", filePaths)); }