/**<summary>Writes the Xap group to the stream.</summary>*/ private void WriteGroup(XapGroup currentGroup, StreamWriter writer, int depth = 0) { foreach (XapVariable variable in currentGroup.Variables) { writer.WriteLine(GetTab(depth) + variable.Name + " = " + variable.Value + ";"); } if (currentGroup.Variables.Count > 0 && currentGroup.Groups.Count > 0) { writer.WriteLine(); } bool first = true; foreach (XapGroup group in currentGroup.Groups) { if (!first) { writer.WriteLine(); } else { first = false; } writer.WriteLine(GetTab(depth) + group.Name); writer.WriteLine(GetTab(depth) + "{"); WriteGroup(group, writer, depth + 1); writer.WriteLine(GetTab(depth) + "}"); } }
/**<summary>Adds a group.</summary>*/ public void AddGroup(XapGroup group) { Groups.Add(group); }
//========= CONSTRUCTORS ========= #region Constructors /**<summary>Constructs the xap file.</summary>*/ public XapFile() { Root = new XapGroup(""); UseSpaces = false; }
/**<summary>Parses a group.</summary>*/ private XapGroup ParseGroup(string[] tokens, ref int index, string name) { XapGroup group = new XapGroup(name); string currentText = null; string variableName = null; for (; index < tokens.Length; index++) { if (IsFormatCharacter(tokens[index].First())) { switch (tokens[index].First()) { case '=': if (variableName != null) { throw new ArgumentException("Invalid token '='. Variable name already declared."); } if (currentText != null) { variableName = currentText; } else { throw new ArgumentException("Invalid token '='. No variable name."); } break; case ';': if (variableName != null) { if (currentText != null) { group.AddVariable(variableName, currentText); variableName = null; } else { throw new ArgumentException("Invalid token ';'. No variable value."); } } else { throw new ArgumentException("Invalid token ';'. No variable name or value."); } break; case '{': if (variableName != null) { throw new ArgumentException( "Invalid token '{'. " + (currentText == null ? "Expected variable value." : "Expected ';'.") ); } else { if (currentText != null) { index++; group.AddGroup(ParseGroup(tokens, ref index, currentText)); } else { throw new ArgumentException("Invalid token '{'. No group name."); } } break; case '}': if (variableName != null) { throw new ArgumentException( "Unexpected end of group. " + (currentText == null ? "Expected variable value." : "Expected ';'.") ); } else if (currentText != null) { throw new ArgumentException("Unexpected end of group. Leftover text."); } else if (name == "") { throw new ArgumentException("Invalid token '}'. Not in group."); } else { return(group); } } currentText = null; } else { currentText = tokens[index]; } } if (variableName != null) { throw new ArgumentException( "Unexpected end of file. " + (currentText == null ? "Expected variable value." : "Expected ';'.") ); } else if (currentText != null) { throw new ArgumentException("Unexpected end of file. Leftover text."); } else if (name != "") { throw new ArgumentException("Unexpected end of file. Expected '}'."); } return(group); }