private static bool PropertyRead(ConfigResource resource, PropertyType propertyType, Queue <string> keyQueue, string key, string val) { bool success = false; if (config != null) { switch (propertyType) { case PropertyType.Property: success = true; ApplyProperty(keyQueue, key, val); break; case PropertyType.If: if (config.Properties.ContainsKey(val)) { success = !Convert.ToBoolean(config.Properties[val]); } break; case PropertyType.Import: ConfigResource res = resource.GetLocalConfigResource(string.Format(@"{0}.properties", val)); success = res.Exists; break; } } return(success); }
private static bool PropertyRead(ConfigResource resource, PropertyType propertyType, Queue<string> keyQueue, string key, string val) { bool success = false; if (config != null) { switch (propertyType) { case PropertyType.Property: success = true; ApplyProperty(keyQueue, key, val); break; case PropertyType.If: if (config.Properties.ContainsKey(val)) { success = !Convert.ToBoolean(config.Properties[val]); } break; case PropertyType.Import: ConfigResource res = resource.GetLocalConfigResource(string.Format(@"{0}.properties", val)); success = res.Exists; break; } } return success; }
/// <summary> /// /// </summary> /// <param name="file"></param> /// <param name="propertyType"></param> /// <param name="keyQueue"></param> /// <param name="key"></param> /// <param name="data"></param> /// <returns></returns> protected static bool PropertyRead(ConfigResource resource, PropertyType propertyType, Queue<string> keyQueue, string key, string var) { bool success = false; string filePatternPrefix = "file.patterns."; string languageNameListPrefix = "language.names"; string lang, extList; if (s_props != null) { switch (propertyType) { case PropertyType.Property: success = true; s_props[key] = var; if (key.StartsWith(languageNameListPrefix)) { extList = s_props.Evaluate(var); s_props.AddLanguageNames(var.Split(' ')); } else if (key.StartsWith(filePatternPrefix)) { lang = key.Substring(filePatternPrefix.Length); if (lang.LastIndexOf('.') == -1) { extList = s_props.Evaluate(var); s_props.AddFileExtentionMapping(extList, lang); } } break; case PropertyType.If: if (s_props.ContainsKey(var)) { success = !Convert.ToBoolean(s_props[var]); } break; case PropertyType.Import: if (!s_supressImports) { ConfigResource res = resource.GetLocalConfigResource(string.Format(@"{0}.properties", var)); success = res.Exists; //FileInfo fileToImport = new FileInfo(string.Format(@"{0}\{1}.properties", file.Directory.FullName, var)); //success = fileToImport.Exists; } break; } } return success; }
/// <summary> /// /// </summary> /// <param name="propsFileInfo"></param> /// <param name="propertyRead"></param> public static void Read(ConfigResource resource, PropertyReadDelegate propertyRead) { if (!resource.Exists) return; StreamReader reader = new StreamReader(resource.OpenRead()); char c = ' ', prev = '\\'; int lastStart = 0, ignoreCount = 0; bool ignoreProperties = false; string key = null, var = null; StringBuilder currentVar = new StringBuilder(); StringBuilder currentToken = new StringBuilder(); Queue<string> queue = new Queue<string>(); StringBuilder currentTokenPiece = new StringBuilder(); ReadMode mode = ReadMode.Key; ReadMode nextModeAfterSpaces = ReadMode.Key; string line = reader.ReadLine(); while (line != null) { int start = 0; bool skipLine = false; while ((start < line.Length) && char.IsWhiteSpace(line[start])) ++start; if (start >= line.Length) { propertyRead(resource, PropertyType.EmptyLine, queue, string.Empty, string.Empty); } else if (line[start] == '#') { propertyRead(resource, PropertyType.Comment, queue, "#", line); } else { if (ignoreProperties) { if ((ignoreCount == 0) || (start == lastStart)) { ignoreCount++; lastStart = start; skipLine = true; } else { ignoreCount = 0; ignoreProperties = false; } } if (skipLine) { propertyRead(resource, PropertyType.EmptyLine, queue, string.Empty, string.Empty); } else { for (int i = start; i < line.Length; i++) { c = line[i]; if (mode == ReadMode.Key) { if (c == '=') { if (currentTokenPiece.Length > 0) { queue.Enqueue(currentTokenPiece.ToString()); } currentTokenPiece.Remove(0, currentTokenPiece.Length); key = currentToken.ToString(); currentToken.Remove(0, currentToken.Length); mode = ReadMode.Value; continue; } else if (char.IsWhiteSpace(c)) { key = currentToken.ToString(); currentToken.Remove(0, currentToken.Length); currentTokenPiece.Remove(0, currentTokenPiece.Length); if (key == "if") { nextModeAfterSpaces = ReadMode.If; } else if (key == "import") { nextModeAfterSpaces = ReadMode.Import; } else { break; } mode = ReadMode.FlushWhiteSpace; continue; } else if (c == '.') { currentToken.Append(c); queue.Enqueue(currentTokenPiece.ToString()); currentTokenPiece.Remove(0, currentTokenPiece.Length); } else { currentTokenPiece.Append(c); currentToken.Append(c); } } else if (mode == ReadMode.FlushWhiteSpace) { if (!char.IsWhiteSpace(c)) { currentToken.Append(c); mode = nextModeAfterSpaces; } } else if (mode == ReadMode.Import) { currentToken.Append(c); } else if (mode == ReadMode.If) { currentToken.Append(c); } else if (mode == ReadMode.Value) { currentToken.Append(c); } prev = c; } if (prev != '\\') { var = currentToken.ToString(); if (mode == ReadMode.If) { ignoreProperties = propertyRead(resource, PropertyType.If, queue, key, var); } else if (mode == ReadMode.Import) { // Open another file inline with this one. if (propertyRead(resource, PropertyType.Import, queue, key, var)) { ConfigResource res = resource.GetLocalConfigResource(string.Format(@"{0}.properties", var)); //success = res.Exists; //FileInfo fileToImport = new FileInfo(string.Format(@"{0}\{1}.properties", propsFileInfo.Directory.FullName, var)); if (res.Exists) { Read(res, propertyRead); } } } else if (mode == ReadMode.Value) { propertyRead(resource, PropertyType.Property, queue, key, var); } currentToken.Remove(0, currentToken.Length); queue.Clear(); key = null; mode = ReadMode.Key; } else { currentToken.Remove(currentToken.Length - 1, 1); } } } line = reader.ReadLine(); } reader.Close(); if (key != null) { var = currentToken.ToString(); propertyRead(resource, PropertyType.Property, queue, key, var); } }
/// <summary> /// /// </summary> /// <param name="propsFileInfo"></param> /// <param name="propertyRead"></param> public static void Read(ConfigResource resource, PropertyReadDelegate propertyRead) { if (!resource.Exists) { return; } StreamReader reader = new StreamReader(resource.OpenRead()); char c = ' ', prev = '\\'; int lastStart = 0, ignoreCount = 0; bool ignoreProperties = false; string key = null, var = null; StringBuilder currentVar = new StringBuilder(); StringBuilder currentToken = new StringBuilder(); Queue <string> queue = new Queue <string>(); StringBuilder currentTokenPiece = new StringBuilder(); ReadMode mode = ReadMode.Key; ReadMode nextModeAfterSpaces = ReadMode.Key; string line = reader.ReadLine(); while (line != null) { int start = 0; bool skipLine = false; while ((start < line.Length) && char.IsWhiteSpace(line[start])) { ++start; } if (start >= line.Length) { propertyRead(resource, PropertyType.EmptyLine, queue, string.Empty, string.Empty); } else if (line[start] == '#') { propertyRead(resource, PropertyType.Comment, queue, "#", line); } else { if (ignoreProperties) { if ((ignoreCount == 0) || (start == lastStart)) { ignoreCount++; lastStart = start; skipLine = true; } else { ignoreCount = 0; ignoreProperties = false; } } if (skipLine) { propertyRead(resource, PropertyType.EmptyLine, queue, string.Empty, string.Empty); } else { for (int i = start; i < line.Length; i++) { c = line[i]; if (mode == ReadMode.Key) { if (c == '=') { if (currentTokenPiece.Length > 0) { queue.Enqueue(currentTokenPiece.ToString()); } currentTokenPiece.Remove(0, currentTokenPiece.Length); key = currentToken.ToString(); currentToken.Remove(0, currentToken.Length); mode = ReadMode.Value; continue; } else if (char.IsWhiteSpace(c)) { key = currentToken.ToString(); currentToken.Remove(0, currentToken.Length); currentTokenPiece.Remove(0, currentTokenPiece.Length); if (key == "if") { nextModeAfterSpaces = ReadMode.If; } else if (key == "import") { nextModeAfterSpaces = ReadMode.Import; } else { break; } mode = ReadMode.FlushWhiteSpace; continue; } else if (c == '.') { currentToken.Append(c); queue.Enqueue(currentTokenPiece.ToString()); currentTokenPiece.Remove(0, currentTokenPiece.Length); } else { currentTokenPiece.Append(c); currentToken.Append(c); } } else if (mode == ReadMode.FlushWhiteSpace) { if (!char.IsWhiteSpace(c)) { currentToken.Append(c); mode = nextModeAfterSpaces; } } else if (mode == ReadMode.Import) { currentToken.Append(c); } else if (mode == ReadMode.If) { currentToken.Append(c); } else if (mode == ReadMode.Value) { currentToken.Append(c); } prev = c; } if (prev != '\\') { var = currentToken.ToString(); if (mode == ReadMode.If) { ignoreProperties = propertyRead(resource, PropertyType.If, queue, key, var); } else if (mode == ReadMode.Import) { // Open another file inline with this one. if (propertyRead(resource, PropertyType.Import, queue, key, var)) { ConfigResource res = resource.GetLocalConfigResource(string.Format(@"{0}.properties", var)); //success = res.Exists; //FileInfo fileToImport = new FileInfo(string.Format(@"{0}\{1}.properties", propsFileInfo.Directory.FullName, var)); if (res.Exists) { Read(res, propertyRead); } } } else if (mode == ReadMode.Value) { propertyRead(resource, PropertyType.Property, queue, key, var); } currentToken.Remove(0, currentToken.Length); queue.Clear(); key = null; mode = ReadMode.Key; } else { currentToken.Remove(currentToken.Length - 1, 1); } } } line = reader.ReadLine(); } reader.Close(); if (key != null) { var = currentToken.ToString(); propertyRead(resource, PropertyType.Property, queue, key, var); } }