// Copyright LGPL : (2005) Philip Van Hoof using System; using System.Xml; using System.Collections; namespace DiaParser { public class Argument { public Interface Type; public string Name; public Argument () { } public Argument (Interface type, string name) { this.Name = name; this.Type = type; } } public class Interface { public string Name; public ArrayList Properties = new ArrayList(); public ArrayList Interfaces = new ArrayList(); public ArrayList Methods = new ArrayList(); public Interface (string name, ArrayList properties, ArrayList methods, ArrayList interfaces) { this.Name = name; this.Properties = properties; this.Interfaces = interfaces; this.Methods = methods; } public Interface () { } } public class Property { public Interface Type; public ArrayList Indexes = new ArrayList(); public bool IsReadOnly; public string Name; public Property (Interface type, ArrayList indexes, bool isreadonly) { this.IsReadOnly = isreadonly; this.Indexes = indexes; this.Type = type; } public Property () { } } public class Method { public string Name; public ArrayList Arguments = new ArrayList(); public Interface Type; public Method (Interface type, string name, ArrayList arguments) { this.Name = name; this.Arguments = arguments; this.Type = type; } public Method () { } } public class Class : Interface { public Interface Inherits = null; public Class (string name, ArrayList properties, ArrayList methods, ArrayList interfaces, Class inherits) : base (name, properties, methods, interfaces) { this.Inherits = inherits; } public Class () { } } public class DiaParser { private string filename; public Hashtable Interfaces = new Hashtable(); public ArrayList UnknownInterfaces = new ArrayList(); public DiaParser (string filename) { this.filename = filename; } private void AddInterface (string hint, Interface intf) { if (Interfaces.ContainsKey(hint)) { Interface mine = (Interface)Interfaces[hint]; mine.Name = intf.Name; mine.Interfaces = intf.Interfaces; mine.Properties = intf.Properties; } else { Interfaces[hint] = intf; } Interface torem = null; foreach (Interface f in UnknownInterfaces) { if (f.Name == intf.Name) { torem = f; break; } } if (torem != null) { UnknownInterfaces.Remove (torem); } } private Interface GetInterface (string name, bool isclass, string hint) { Interface retval = null; if (hint != "-") { if (Interfaces.ContainsKey (hint)) { retval = (Interface)Interfaces[hint]; } else { Interface mine = null; foreach (Interface f in UnknownInterfaces) { if (f.Name == name) { Interfaces.Add (hint, f); retval = (Interface)Interfaces[hint]; break; } } if (retval == null) { if (isclass) mine = new Class(); else mine = new Interface(); mine.Name = name; Interfaces.Add (hint, mine); retval = (Interface)Interfaces [hint]; } else { UnknownInterfaces.Remove (retval); } } } else { IDictionaryEnumerator myenum = Interfaces.GetEnumerator (); while (myenum.MoveNext()) { Interface cl = (Interface)myenum.Value; if (cl.Name == name) { retval = cl; break; } } foreach (Interface cl in UnknownInterfaces) { if (cl.Name == name) { retval = cl; break; } } if (retval == null) { if (isclass) { retval = new Class(); } else { retval = new Interface(); } retval.Name = name; UnknownInterfaces.Add (retval); } } return retval; } public void Run () { XmlDocument d = new XmlDocument(); d.Load (this.filename); XmlNodeList list = d.GetElementsByTagName ("dia:object"); foreach (XmlNode node in list) { string hint = node.Attributes["id"].Value; switch (node.Attributes["type"].Value) { case "UML - Class": Interface myinterface = null; foreach (XmlNode kid in node.ChildNodes) { if (kid.Name == "dia:attribute") { if (kid.Attributes["name"].Value == "name") { string name = kid.FirstChild.InnerText.Replace("#", ""); if (name[0] != 'I') { myinterface = this.GetInterface (name, true, hint); } else { myinterface = this.GetInterface (name, false, hint); } } } } if (myinterface == null) { myinterface = this.GetInterface ("FromUnkownInterfaces", false, "-"); } foreach (XmlNode kid in node.ChildNodes) { if (kid.Name == "dia:attribute") { if (kid.Attributes["name"].Value == "operations") { foreach (XmlNode kidkid in kid.ChildNodes) { if (kidkid.Name == "dia:composite") { if (kidkid.Attributes["type"].Value == "umloperation") { Method method = new Method(); foreach (XmlNode kidkidkid in kidkid.ChildNodes) { if (kidkidkid.Attributes["name"].Value == "parameters") { foreach (XmlNode kidkidkidkid in kidkidkid.ChildNodes) { if (kidkidkidkid.Attributes["type"].Value == "umlparameter") { Argument a = new Argument(); foreach (XmlNode kidkidkidkidkid in kidkidkidkid.ChildNodes) { if (kidkidkidkidkid.Attributes["name"].Value == "name") { a.Name = kidkidkidkidkid.FirstChild.InnerText.Replace("#", ""); } if (kidkidkidkidkid.Attributes["name"].Value == "type") { string type = kidkidkidkidkid.FirstChild.InnerText.Replace("#", ""); if (type != null && type.Length > 0 && type[0] != 'I') { a.Type = this.GetInterface (type, true, "-"); } else { a.Type = this.GetInterface (type, false, "-"); } } } method.Arguments.Add (a); } } } if (kidkidkid.Attributes["name"].Value == "name") { method.Name = kidkidkid.FirstChild.InnerText.Replace("#", ""); } if (kidkidkid.Attributes["name"].Value == "type") { string type = kidkidkid.FirstChild.InnerText.Replace("#", ""); if (type != null && type.Length > 0 && type[0] != 'I') { method.Type = this.GetInterface (type, true, "-"); } else { method.Type = this.GetInterface (type, false, "-"); } } } myinterface.Methods.Add (method); } } } } if (kid.Attributes["name"].Value == "attributes") { foreach (XmlNode kidkid in kid.ChildNodes) { if (kidkid.Name == "dia:composite") { if (kidkid.Attributes["type"].Value == "umlattribute") { Property property = new Property(); foreach (XmlNode kidkidkid in kidkid.ChildNodes) { if (kidkidkid.Name == "dia:attribute") { if (kidkidkid.Attributes["name"].Value == "name") { property.Name = kidkidkid.FirstChild.InnerText.Replace("#", ""); } if (kidkidkid.Attributes["name"].Value == "type") { string type = kidkidkid.FirstChild.InnerText.Replace("#", ""); if (type != null && type.Length > 0 && type[0] != 'I') { property.Type = this.GetInterface (type, true, "-"); } else { property.Type = this.GetInterface (type, false, "-"); } } } } myinterface.Properties.Add (property); } } } } } } this.AddInterface (hint, myinterface); break; } } XmlNodeList mlist = d.GetElementsByTagName ("dia:object"); foreach (XmlNode mnode in mlist) { string mhint = mnode.Attributes["id"].Value; switch (mnode.Attributes["type"].Value) { case "UML - Association": // TODO: Support aggregation // TODO: Support composition break; case "UML - Dependency": break; case "UML - Generalization": foreach (XmlNode mkid in mnode.ChildNodes) { if (mkid.Name == "dia:connections") { bool first = true; Interface to = null, from = null; Class clfrom = null, clto = null; foreach (XmlNode mkidkid in mkid.ChildNodes) { if (mkidkid.Name == "dia:connection") { string id = mkidkid.Attributes["to"].Value; if (first) to = (Interface)Interfaces[id]; else from = (Interface)Interfaces[id]; first = false; } } if (to != null && from != null) { if (from.GetType() == typeof(Class) && to.GetType() == typeof(Class)) { clfrom = (Class)from; clto = (Class)to; clfrom.Inherits = clto; } } } } break; case "UML - Realizes": foreach (XmlNode mkid in mnode.ChildNodes) { if (mkid.Name == "dia:connections") { bool first = true; Interface to = null, from = null; foreach (XmlNode mkidkid in mkid.ChildNodes) { if (mkidkid.Name == "dia:connection") { string id = mkidkid.Attributes["to"].Value; if (first) to = (Interface)Interfaces[id]; else from = (Interface)Interfaces[id]; first = false; } } if (to != null && from != null) from.Interfaces.Add (to); } } break; } } IDictionaryEnumerator mynenum = Interfaces.GetEnumerator (); while (mynenum.MoveNext()) { Interface intf = (Interface)mynenum.Value; bool haveinherit = false; int interfcnt = 0; bool isinterf = false; if (intf.GetType() == typeof (Interface)) { Console.Write ("public interface " + intf.Name); isinterf = true; } else { Class cl = (Class) intf; isinterf = false; Console.Write ("public class " + cl.Name); if (cl.Inherits != null || cl.Interfaces.Count > 0) { haveinherit = true; Console.Write (": "); } if (cl.Inherits != null) { Console.Write (cl.Inherits.Name); if (cl.Interfaces.Count > 0) { Console.Write (", "); } } } foreach (Interface kidintf in intf.Interfaces) { if (interfcnt == 0 && !haveinherit) { Console.Write (": "); } if (interfcnt != 0) { Console.Write (", "); } Console.Write (kidintf.Name); interfcnt++; } Console.WriteLine (); Console.WriteLine ("{"); foreach (Interface ii in intf.Interfaces) { foreach (Property pe in ii.Properties) { bool already=false; foreach (Property inpe in intf.Properties) { if (inpe.Name == pe.Name) { already=true; break; } } if (!already) { intf.Properties.Add (pe); } } } foreach (Property pr in intf.Properties) { string typename = pr.Type.Name; if (typename == null || typename == "") typename = "void"; if (!isinterf) { Console.WriteLine ("\tpublic " + typename + " " + pr.Name); Console.WriteLine ("\t{"); Console.WriteLine ("\t\tget"); Console.WriteLine ("\t\t{"); Console.WriteLine ("\t\t}"); Console.WriteLine ("\t\tset"); Console.WriteLine ("\t\t{"); Console.WriteLine ("\t\t}"); } else { Console.WriteLine ("\t" + typename + " " + pr.Name); Console.WriteLine ("\t{"); Console.WriteLine ("\t\tget;"); Console.WriteLine ("\t\tset;"); } Console.WriteLine ("\t}"); } foreach (Interface ii in intf.Interfaces) { foreach (Method me in ii.Methods) { bool already=false; foreach (Method inme in intf.Methods) { if (inme.Name == me.Name) { already=true; break; } } if (!already) { intf.Methods.Add (me); } } } foreach (Method me in intf.Methods) { string typename = me.Type.Name; if (typename == null || typename == "") typename = "void"; Console.Write ("\tpublic " + typename + " " + me.Name + "("); bool first = true; foreach (Argument ai in me.Arguments) { string mtypename = ai.Type.Name; string instance = ai.Name; if (mtypename == null || mtypename == "") { mtypename = "void"; if (instance == null || instance == "") instance = "ptr"; } else { if (instance == null || instance == "") instance = mtypename.ToLower(); } if (!first) { Console.Write (", "); } Console.Write (mtypename + " " + instance); first = false; } Console.Write (")"); if (!isinterf) { Console.WriteLine (""); Console.WriteLine ("\t{"); Console.WriteLine ("\t}"); } else { Console.WriteLine (";"); } } Console.WriteLine ("}"); } Console.ReadLine(); } [STAThread] static void Main(string[] args) { DiaParser p = null; if (args.Length == 0) { p = new DiaParser ("C:\\dia.xml"); } else { p = new DiaParser (args[0]); } p.Run(); } } }