private bool ParseCell(Document doc, XmlElement elem, string comments) { var name = elem.GetAttribute("name"); if (String.IsNullOrEmpty(name)) { return false; } var isEvent = (elem.Name == "event"); var id = elem.GetAttribute("id"); if (isEvent && String.IsNullOrEmpty(id)) { return false; } CellDef def = (isEvent ? new EventDef() : new CellDef()); def.Name = name; if (isEvent) { ((EventDef)def).Id = id; } def.Base = elem.GetAttribute("extends"); var local = elem.GetAttribute("local"); if (!String.IsNullOrEmpty(local) && local.EndsWith("rue")) { def.IsLocal = true; } def.Comments = comments; string subComments = null; var node = elem.FirstChild; for ( ; node != null; node = node.NextSibling) { if (node.NodeType != XmlNodeType.Element) { if (node.NodeType == XmlNodeType.Comment) { subComments = node.Value.Trim(); } else { subComments = null; } continue; } var child = (XmlElement)node; switch (child.Name) { case "property": if (ParseCellProperty(def, child, subComments) == false) { return false; } break; default: break; } subComments = null; } doc.Definitions.Add(def); return true; }
public override bool Format(Document doc, string outDir) { try { var context = new CSharpFormatterContext() { Doc = doc, Target = Path.Combine(outDir, doc.BaseName + Extension) }; using (var writer = new StreamWriter(context.Target, false, Encoding.UTF8)) { context.Out = writer; FormatHead(context); FormatBody(context); writer.Flush(); } } catch (Exception e) { Console.Error.WriteLine(e.Message); return false; } return true; }
public abstract bool Format(Document doc, String outDir);
private bool ParseReference(Document doc, XmlElement elem) { var target = elem.GetAttribute("target"); if (String.IsNullOrEmpty(target)) { return false; } Reference reference = new Reference(); reference.Target = target; doc.References.Add(reference); return true; }
private bool ParseConsts(Document doc, XmlElement elem, string comments) { var name = elem.GetAttribute("name"); var type = elem.GetAttribute("type"); if (String.IsNullOrEmpty(name)) { return false; } if (String.IsNullOrEmpty(type)) { type = "int32"; // default type } var def = new ConstsDef(); def.Name = name; def.Type = type; def.Comments = comments; string subComments = null; var node = elem.FirstChild; for ( ; node != null; node = node.NextSibling) { if (node.NodeType != XmlNodeType.Element) { if (node.NodeType == XmlNodeType.Comment) { subComments = node.Value.Trim(); } else { subComments = null; } continue; } var child = (XmlElement)node; if (child.IsEmpty) { continue; } switch (child.Name) { case "const": if (ParseConstant(def, child, subComments) == false) { return false; } break; default: break; } subComments = null; } doc.Definitions.Add(def); return true; }
public bool Handle(string path, out Document doc) { doc = null; var xml = new XmlDocument(); try { xml.Load(path); } catch (Exception e) { Console.Error.WriteLine(e.Message); return false; } var rootElem = xml.DocumentElement; if (rootElem.Name != "x2") { // Not a valid x2 document. return true; } doc = new Document(); doc.Namespace = rootElem.GetAttribute("namespace"); string comments = null; var node = rootElem.FirstChild; for ( ; node != null; node = node.NextSibling) { if (node.NodeType != XmlNodeType.Element) { if (node.NodeType == XmlNodeType.Comment) { comments = node.Value.Trim(); } else { comments = null; } continue; } var elem = (XmlElement)node; switch (elem.Name) { case "ref": if (ParseReference(doc, elem) == false) { return false; } break; case "consts": if (ParseConsts(doc, elem, comments) == false) { return false; } break; case "cell": case "event": if (ParseCell(doc, elem, comments) == false) { return false; } break; default: break; } comments = null; } return true; }