public static FHXObject FromXMLObject(XmlNode node) { FHXObject o = new FHXObject(); foreach (XmlAttribute a in node.Attributes) { if (a.Name == "TYPE") { o.Type = a.Value; } else { o.AddParameter(new FHXParameter(a.Name, a.Value, true)); } } foreach (XmlNode c in node.ChildNodes) { if (c.Name == "parameters") { foreach (XmlAttribute a in c.Attributes) { o.AddParameter(new FHXParameter(a.Name, a.Value, false)); } } else if (c.Name == "children") { foreach (XmlNode cc in c.ChildNodes) { o.AddChild(FromXMLObject(cc)); } } else { Console.WriteLine("Unknown tag : {0}", c.Name); } } return(o); }
public FHXObject ParseAll() { List <FHXObject> objects = new List <FHXObject>(); List <FHXParameter> parameters = new List <FHXParameter>(); FHXObject root = new FHXObject("ROOT", "Project"); objects.Add(root); bool waitingForBody = false; //Flag to see if we started an item and are looking for a bracketed body FHXObject currentObject = root; while (!EOF()) { Token t = Peek(); if (t.Type == TokenType.COMMENT_START) { while (t.Type != TokenType.COMMENT_END) { t = Next(); } t = Next(); // Skip last comment token } else if (t.Type == TokenType.WHITESPACE) { //Skips whitespaces Next(); } else if (t.Type == TokenType.OPEN_BRACKET) { if (waitingForBody) { waitingForBody = false; } else { Console.WriteLine("Brackets without an item identifier"); } Next(); } else if (t.Type == TokenType.CLOSE_BRACKET) { currentObject = currentObject.Parent; Next(); } else if (t.Type == TokenType.TEXT) { string s = Next().Value; if (Peek().Type == TokenType.WHITESPACE) { if (IsHex(s) && s.Length == 2) { //Cas où on a { 00 01 02 ... XX } if (currentObject.HasParameter("VALUE")) { FHXParameter p = currentObject.GetParameter("VALUE"); p.Value = p.Value + " " + s; } else { FHXParameter p = new FHXParameter("VALUE", s); currentObject.AddParameter(p); parameters.Add(p); } } else { //New object FHXObject o = NewObject(s); currentObject.AddChild(o); currentObject = o; objects.Add(o); waitingForBody = true; } } else if (Peek().Type == TokenType.EQUAL) { Next(); //Skips the equal sign if (Peek().Type == TokenType.WHITESPACE) { Next(); FHXObject o = new FHXObject(); o.mName = s; currentObject.AddChild(o); currentObject = o; objects.Add(o); waitingForBody = true; } else { string value = Next().Value; //Retieves the parameter's value FHXParameter p = new FHXParameter(s, value, waitingForBody); currentObject.AddParameter(p); parameters.Add(p); } } else { //Unexpected token Console.WriteLine("Unexpected token while parsing TEXT : {0}", Peek().GetTypeString()); } } else { Console.WriteLine("Unhandled token : {0}", Peek().GetTypeString()); Next(); } } return(root); }