/// <summary> /// Parses all XML files found in the source directory including sub-directories /// </summary> /// <returns>List with jQueryUI entries</returns> public IList<Entry> Parse() { DirectoryInfo source = new DirectoryInfo(SourcePath); FileInfo[] files = source.GetFiles("*.xml", SearchOption.AllDirectories); IList<Entry> entities = new List<Entry>(); foreach (FileInfo file in files.OrderBy(f => f.Name.ToLowerInvariant() == "widget.xml" ? 1 : 2)) { var entry = ParseFile(file.FullName); if (entry != null) { if (file.Name.ToLowerInvariant() == "widget.xml") Widget = entry; entities.Add(entry); } } return entities; }
private Entry ParseEntry(XmlNode xmlEntry) { if (xmlEntry == null) { return null; } Entry entry = new Entry(); entry.Name = GetAttributeStringValue(xmlEntry, "name"); entry.Namespace = GetAttributeStringValue(xmlEntry, "namespace"); entry.Type = GetAttributeStringValue(xmlEntry, "type"); entry.WidgetNamespace = GetAttributeStringValue(xmlEntry, "widgetnamespace"); entry.Description = GetNodeInnerXml(xmlEntry, "desc"); entry.LongDescription = GetNodeInnerXml(xmlEntry, "longdesc"); entry.Created = GetNodeInnerXml(xmlEntry, "created"); entry.Signatures = ParseSignatures(GetNodeList(xmlEntry, "signature")); entry.Options = ParseOptions(GetNodeList(xmlEntry, "//options//option")); entry.Events = ParseEvents(GetNodeList(xmlEntry, "//events//event")); entry.Methods = ParseMethods(GetNodeList(xmlEntry, "//methods//method")); XmlNode xmlExample = xmlEntry.SelectSingleNode("//example"); if (xmlExample != null) { entry.Example = ParseExample(xmlExample); } XmlNode xmlCategory = xmlEntry.SelectSingleNode("//category"); if (xmlCategory != null) { entry.Category = ParseCategory(xmlCategory); } return entry; }
/// <summary> /// Gets namespace for an entry /// </summary> /// <param name="entry">Entry to be generated</param> /// <returns>The namespace</returns> public static string GetNamespace(Entry entry) { string @namespace = "jQueryApi.UI"; if (entry == null) { return @namespace; } if (entry.Name.ToLower() == "widget" || entry.Category.ToLower() == "utilities") { return @namespace; } if (string.IsNullOrEmpty(entry.Category)) { return @namespace; } return @namespace + "." + PascalCase(entry.Category); }
private void RenderPositionExtensionMethods(Entry entry) { string content = @"using System; using System.Html; using System.Runtime.CompilerServices; namespace jQueryApi.UI {{ [Imported] [IgnoreNamespace] public static class PositionExtensions {{{0} }} }}"; var methods = new StringBuilder(); methods.AppendLine(); methods.AppendLine(); methods.AppendLine(" /// <summary>"); methods.AppendLine(" /// " + Utils.FormatXmlComment(entry.Description.Replace("<entryname />", entry.Name))); methods.AppendLine(" /// </summary>"); methods.AppendLine(" [InstanceMethodOnFirstArgument]"); methods.AppendLine(" public static jQueryObject " + Utils.PascalCase(entry.Name) + "(this jQueryObject q, " + Utils.PascalCase(entry.Name) + "Options options) {"); methods.AppendLine(" return null;"); methods.AppendLine(" }"); Utils.CreateFile(Path.Combine(DestinationPath, "Position"), "PositionExtensions", string.Format(content, methods.ToString())); }
private void RenderOptions(Entry entry) { if (entry.Events.Count == 0 && entry.Options.Count == 0) { return; } string className = Utils.PascalCase(entry.Name) + @"Options"; string content = @"using System; using System.Html; using System.Runtime.CompilerServices; namespace {3} {{ [Imported] [IgnoreNamespace] [Serializable] public sealed class {0} {{ {1}{2} }} }}"; StringBuilder eventsContent = new StringBuilder(); foreach (Event @event in entry.Events.OrderBy(e => e.Name)) { if ([email protected]("function")) { if (!string.IsNullOrEmpty(@event.Description)) { eventsContent.Append( @" /// <summary> /// " + Utils.FormatXmlComment(@event.Description.Replace("<entryname />", entry.Name)) + @" /// </summary>"); } string eventType; if (string.IsNullOrEmpty(@event.Type)) { eventType = "jQueryEventHandler"; } else { if (@event.Arguments.All(a => a.Properties.Count == 0)) { eventType = "jQueryUIEventHandler<jQueryObject>"; } else { eventType = "jQueryUIEventHandler<" + Utils.PascalCase(@event.Type.Replace(@event.Name.ToLowerInvariant(), Utils.PascalCase(@event.Name))) + "Event" + ">"; } } eventsContent.AppendLine( @" [ScriptName(""" + @event.Name + @""")] public " + eventType + " On" + Utils.PascalCase(@event.Name) + @" { get; set; }"); } } StringBuilder optionsContent = new StringBuilder(); foreach (var option in entry.Options .OrderBy(o => o.Name) .GroupBy(o => o.Name)) { if (!string.IsNullOrEmpty(option.Min(o => o.Description))) { optionsContent.Append( @" /// <summary> /// " + Utils.FormatXmlComment(option.Min(o => o.Description).Replace("<entryname />", entry.Name)) + @" /// </summary>"); } optionsContent.AppendLine(@" public " + Utils.MapDataType(option.Select(o => o.Type), className, option.Min(o => o.Description)) + @" " + Utils.PascalCase(option.Key) + @" { get; set; }"); } Utils.CreateFile(Path.Combine(DestinationPath, Utils.PascalCase(entry.Category), Utils.PascalCase(entry.Name)), className , string.Format(content , className , optionsContent.ToString() , eventsContent.ToString() , Utils.GetNamespace(entry))); }
private void RenderObject(Entry entry) { string className = Utils.PascalCase(entry.Name) + @"Object"; string content = @"using System; using System.Html; using System.Runtime.CompilerServices; namespace {5} {{ /// <summary> /// {1} /// </summary> /// <remarks> /// {2} /// </remarks> [Imported] [IgnoreNamespace]{4} public sealed class {0} {{ private {0}() {{ }}{3} [ScriptSkip] public static explicit operator jQueryObject({0} o) {{ return null; }} [ScriptSkip] public static explicit operator {0}(jQueryObject o) {{ return null; }} }} }}"; StringBuilder methodsContent = new StringBuilder(); foreach (var method in entry.Methods // exclude the jQuery methods as they will be inherit .Where(m => entry.Name.ToLowerInvariant() == "widget" || !excludeJQueryMethods.Contains(m.Name.ToLowerInvariant())) .OrderBy(m => m.Name)) { methodsContent.AppendLine(); methodsContent.AppendLine(); methodsContent.AppendLine(" /// <summary>"); methodsContent.AppendLine(" /// " + Utils.FormatXmlComment(method.Description.Replace("<entryname />", entry.Name))); methodsContent.AppendLine(" /// </summary>"); methodsContent.AppendLine(" [InlineCode(\"{this}." + entry.Name + "('" + method.Name + "'" + string.Join("", method.Arguments.Select(a => ", {" + a.Name + "}")) + ")\")]"); methodsContent.Append(" public " + (string.IsNullOrEmpty(method.ReturnType) ? "void" : Utils.MapDataType(method.ReturnType, className, "")) + " " + Utils.PascalCase(method.Name) + "("); List<string> args = new List<string>(); foreach (Argument arg in method.Arguments) { args.Add(Utils.MapDataType(arg.Type, className, arg.Description) + " " + (arg.Name == "event" ? "@event" : arg.Name)); } methodsContent.AppendLine(string.Join(", ", args) + ") {"); if (!string.IsNullOrEmpty(method.ReturnType)) { methodsContent.AppendLine(" return " + Utils.GetDefaultValue(method.ReturnType) + ";"); } methodsContent.AppendLine(" }"); } foreach (var option in entry.Options .OrderBy(o => o.Name) .GroupBy(o => o.Name)) { string name = (entry.Methods.Any(m => m.Name == option.Key) ? "Option" : "") + Utils.PascalCase(option.Key); string description = option.Min(o => o.Description); methodsContent.AppendLine(); methodsContent.AppendLine(); if (!string.IsNullOrEmpty(description)) { methodsContent.AppendLine(" /// <summary>"); methodsContent.AppendLine(" /// " + Utils.FormatXmlComment(description.Replace("<entryname />", entry.Name))); methodsContent.AppendLine(" /// </summary>"); } methodsContent.AppendLine(" public " + Utils.MapDataType(option.Select(o => o.Type), className, option.Min(o => o.Description)) + " " + name + " {"); methodsContent.AppendLine(" [InlineCode(\"{this}." + entry.Name + "('option', '" + option.Key + "')\")]"); methodsContent.AppendLine(" get;"); methodsContent.AppendLine(" [InlineCode(\"{this}." + entry.Name + "('option', '" + option.Key + "', {value})\")]"); methodsContent.AppendLine(" set;"); methodsContent.AppendLine(" }"); } foreach (Event @event in entry.Events.Where(e => !e.Type.StartsWith("function")).OrderBy(e => e.Name)) { string eventType; if (string.IsNullOrEmpty(@event.Type)) { eventType = "jQueryEventHandler"; } else if (@event.Arguments.All(a => a.Properties.Count == 0)) { eventType = "jQueryUIEventHandler<jQueryObject>"; } else { eventType = "jQueryUIEventHandler<" + Utils.PascalCase(@event.Type.Replace(@event.Name.ToLowerInvariant(), Utils.PascalCase(@event.Name))) + "Event" + ">"; } methodsContent.AppendLine(); methodsContent.AppendLine(); methodsContent.AppendLine(" /// <summary>"); methodsContent.AppendLine(" /// " + Utils.FormatXmlComment(@event.Description.Replace("<entryname />", entry.Name))); methodsContent.AppendLine(" /// </summary>"); methodsContent.AppendLine(" public event " + eventType + " On" + Utils.PascalCase(@event.Name) + " {"); methodsContent.AppendLine(" [InlineCode(\"{this}.bind('" + @event.Name + "', {value})\")]"); methodsContent.AppendLine(" add {"); methodsContent.AppendLine(" }"); methodsContent.AppendLine(" [InlineCode(\"{this}.unbind('" + @event.Name + "', {value})\")]"); methodsContent.AppendLine(" remove {"); methodsContent.AppendLine(" }"); methodsContent.AppendLine(" }"); } string formatedContent = string.Format(content , className , Utils.FormatXmlComment(entry.Description.Replace("<entryname />", entry.Name)) , Utils.FormatXmlComment(entry.LongDescription.Replace("<entryname />", entry.Name)) , methodsContent.ToString() , string.Empty , Utils.GetNamespace(entry)); Utils.CreateFile(Path.Combine(DestinationPath, Utils.PascalCase(entry.Category), Utils.PascalCase(entry.Name)), className, formatedContent); }
private void RenderEvents(Entry entry) { if (entry.Events.Count == 0) { return; } string content = @"using System; using System.Html; using System.Runtime.CompilerServices; namespace {2} {{ [Imported] [IgnoreNamespace] [Serializable] public sealed class {0} {{{1} }} }}"; string property = @" public {1} {0} {{ get; set; }}"; string className; foreach (var @event in entry.Events .OrderBy(e => e.Name)) { if (@event.Type.StartsWith("function")) continue; if (string.IsNullOrEmpty(@event.Type)) continue; string eventType = @event.Type.Replace(@event.Name.ToLowerInvariant(), Utils.PascalCase(@event.Name)); foreach (Argument arg in @event.Arguments) { if (arg.Name != "ui") continue; if (arg.Properties.Count == 0) continue; className = Utils.PascalCase(eventType) + "Event"; StringBuilder properties = new StringBuilder(); foreach (Property prop in arg.Properties.OrderBy(p => p.Name)) { properties.Append(string.Format(property, Utils.PascalCase(prop.Name), Utils.MapDataType(prop.Type, className, prop.Description))); } Utils.CreateFile(Path.Combine(DestinationPath, Utils.PascalCase(entry.Category), Utils.PascalCase(entry.Name)) , className , string.Format(content, className, properties.ToString(), Utils.GetNamespace(entry))); } } }
private void RenderEntry(Entry entry, Entry baseEntry = null) { if (entry == null || entry.Name == "widget") { return; } if (entry.Category != "effect" && entry.Name != "position") { RenderObject(entry); } RenderOptions(entry); RenderEvents(entry); }
private void RenderOptions(Entry entry) { if (entry.Events.Count == 0 && entry.Options.Count == 0) { return; } string className = Utils.PascalCase(entry.Name) + @"Options"; string content = @"using System; using System.Runtime.CompilerServices; namespace {4} {{ /// <summary> /// Options used to initialize or customize {5}. /// </summary> [Imported] [IgnoreNamespace] [ScriptName(""Object"")] public {3} class {0} {{ public {0}() {{ }} public {0}(params object[] nameValuePairs) {{ }} {1}{2} }} }}"; StringBuilder eventsContent = new StringBuilder(); foreach (Event @event in entry.Events.AsQueryable().OrderBy(e => e.Name)) { if ([email protected]("function")) { if (!string.IsNullOrEmpty(@event.Description)) { eventsContent.Append( @" /// <summary> /// " + Utils.FormatXmlComment(@event.Description.Replace("<entryname />", entry.Name)) + @" /// </summary>"); } string eventType; if (string.IsNullOrEmpty(@event.Type)) { eventType = "jQueryEventHandler"; } else { if (@event.Arguments.All(a => a.Properties.Count == 0)) { eventType = "jQueryUIEventHandler<jQueryObject>"; } else { eventType = "jQueryUIEventHandler<" + Utils.PascalCase(@event.Type.Replace(@event.Name.ToLower(), Utils.PascalCase(@event.Name))) + "Event" + ">"; } } eventsContent.AppendLine( @" [IntrinsicProperty] public " + eventType + " " + Utils.PascalCase(@event.Name) + " {" + @" get { return null; } set { } }"); } } StringBuilder optionsContent = new StringBuilder(); foreach (var option in entry.Options.AsQueryable() .OrderBy(o => o.Name) .GroupBy(o => o.Name)) { if (!string.IsNullOrEmpty(option.Min(o => o.Description))) { optionsContent.Append( @" /// <summary> /// " + Utils.FormatXmlComment(option.Min(o => o.Description).Replace("<entryname />", entry.Name)) + @" /// </summary>"); } optionsContent.AppendLine(@" [IntrinsicProperty] public " + Utils.MapDataType(option.Min(o => o.Type)) + @" " + Utils.PascalCase(option.Key) + @" {" + @" get { return " + Utils.GetDefaultValue(option.Min(o => o.Type)) + @"; } set { } }"); } string name = Utils.PascalCase(entry.Name); if ((name == "jQueryUI") || (name == "Position")) { name = ""; } string category = Utils.PascalCase(entry.Category); if ((category == "Widgets") && (name == "Widget")) { category = ""; name = ""; } Utils.CreateFile(Path.Combine(DestinationPath, category, name), className , string.Format(content , className , eventsContent.ToString() , optionsContent.ToString() , (entry.Name.ToLower() != "widget") ? "sealed" : string.Empty , Utils.GetNamespace(entry) , Utils.PascalCase(entry.Name))); }
private void RenderOptionEnum(Entry entry, Entry baseEntry = null) { if (entry.Options.Count == 0) { return; } string className = Utils.PascalCase(entry.Name) + @"Option"; string content = @"using System; using System.Runtime.CompilerServices; namespace {2} {{ /// <summary> /// Options for use with {3}. /// </summary> [Imported] [IgnoreNamespace] [NamedValues] public enum {0} {{{1} }} }}"; StringBuilder enumValues = new StringBuilder(); IEnumerable<Option> options = entry.Options.AsQueryable(); if (baseEntry != null) { options = options.Union(baseEntry.Options); } foreach (var option in options.OrderBy(o => o.Name) .GroupBy(o => o.Name)) { enumValues.AppendLine(); enumValues.AppendLine(); if (!string.IsNullOrEmpty(option.Min(o => o.Description))) { enumValues.AppendLine(" /// <summary>"); enumValues.AppendLine(" /// " + Utils.FormatXmlComment(option.Min(o => o.Description).Replace("<entryname />", entry.Name))); enumValues.AppendLine(" /// </summary>"); } enumValues.Append(" " + Utils.PascalCase(option.Key) + ","); } string name = Utils.PascalCase(entry.Name); if ((name == "jQueryUI") || (name == "Position")) { name = ""; } string category = Utils.PascalCase(entry.Category); if ((category == "Widgets") && (name == "Widget")) { category = ""; name = ""; } Utils.CreateFile(Path.Combine(DestinationPath, category, name), className , string.Format(content, className, enumValues.ToString().Trim(','), Utils.GetNamespace(entry), Utils.PascalCase(entry.Name))); }
private void RenderObject(Entry entry) { if (entry.Type == "method") { return; // Skip over the method entry. These objects will be added to the jQueryUI object } string className = Utils.PascalCase(entry.Name) + @"Object"; string content = @"using System; using System.Runtime.CompilerServices; namespace {8} {{ /// <summary> /// {1} /// </summary> /// <remarks> /// {2} /// </remarks> [Imported] [IgnoreNamespace]{7} public " + (abstractObjects.Contains(entry.Name.ToLower()) ? "abstract" : "sealed") + @" class {0} : " + (string.IsNullOrEmpty(entry.Type) ? "jQuery" : Utils.PascalCase(entry.Type)) + @"Object {{ " + (abstractObjects.Contains(entry.Name.ToLower()) ? "protected" : "private") + @" {0}() {{ }}{3}{4}{5}{6} }} }}"; string overload1 = @" [ScriptName(""{0}"")] public {2}{1}Object {1}() {{ return null; }}"; string overload2 = @" [ScriptName(""{0}"")] public {1}Object {1}({1}Options options) {{ return null; }}"; string overload3 = @" [ScriptName(""{0}"")] public object {1}({1}Method method, params object[] options) {{ return null; }}"; string overload4 = @" [IntrinsicProperty] public static object Prototype {{ get {{ return null; }} }} [IntrinsicProperty] public jQueryObject Element {{ get {{ return null; }} }} [IntrinsicProperty] public object Options {{ get {{ return null; }} }} [IntrinsicProperty] [ScriptName(""_setOption"")] public Function SetOption {{ get {{ return null; }} }} [IntrinsicProperty] [ScriptName(""_setOptions"")] public Function SetOptions {{ get {{ return null; }} }} "; StringBuilder methodsContent = new StringBuilder(); foreach (var method in entry.Methods // exclude the jQuery methods as they will be inherit .Where(m => entry.Name.ToLower() == "widget" || !excludeJQueryMethods.Contains(m.Name.ToLower())) .OrderBy(m => m.Name)) { // TODO: temporal solution for SetOption & SetOptions. Cannot have property and Method with the same name. if (method.Name.ToLower() == "_setoption" || method.Name.ToLower() == "_setoptions") { continue; } methodsContent.AppendLine(); methodsContent.AppendLine(); methodsContent.AppendLine(" /// <summary>"); methodsContent.AppendLine(" /// " + Utils.FormatXmlComment(method.Description.Replace("<entryname />", entry.Name))); methodsContent.AppendLine(" /// </summary>"); if (Utils.PascalCase(method.Name).ToLower() != method.Name.ToLower()) { methodsContent.AppendLine(" [ScriptName(\"" + method.Name + "\")]"); } methodsContent.Append(" public " + (method.Name.ToLower() == "length" ? "new " : string.Empty) + (string.IsNullOrEmpty(method.ReturnType) ? "void" : Utils.MapDataType(method.ReturnType)) + " " + Utils.PascalCase(method.Name) + "("); List<string> args = new List<string>(); foreach (Argument arg in method.Arguments) { args.Add(Utils.MapDataType(arg.Type) + " " + (arg.Name == "event" ? "@event" : arg.Name)); } methodsContent.AppendLine(string.Join(", ", args) + ") {"); if (!string.IsNullOrEmpty(method.ReturnType)) { methodsContent.AppendLine(" return " + Utils.GetDefaultValue(method.ReturnType) + ";"); } methodsContent.AppendLine(" }"); } string formatedContent = string.Empty; if (entry.Name.ToLower() == "widget") { formatedContent = string.Format(content , className , Utils.FormatXmlComment(entry.Description.Replace("<entryname />", entry.Name)) , Utils.FormatXmlComment(entry.LongDescription.Replace("<entryname />", entry.Name)) , string.Empty , string.Empty , string.Empty , string.Format(overload4, entry.Name, Utils.PascalCase(entry.Name)) + methodsContent.ToString() , @" [ScriptName(""$.Widget"")]" , Utils.GetNamespace(entry)); } else { formatedContent = string.Format(content , className , Utils.FormatXmlComment(entry.Description.Replace("<entryname />", entry.Name)) , Utils.FormatXmlComment(entry.LongDescription.Replace("<entryname />", entry.Name)) , string.Format(overload1, entry.Name, Utils.PascalCase(entry.Name), (entry.Name.ToLower() == "position" || entry.Name.ToLower() == "size") ? "new " : string.Empty) , (entry.Options.Count > 0) ? string.Format(overload2, entry.Name, Utils.PascalCase(entry.Name)) : string.Empty , (entry.Methods.Where(m => !excludeJQueryMethods.Contains(m.Name.ToLower())).Count() > 0) ? string.Format(overload3, entry.Name, Utils.PascalCase(entry.Name)) : string.Empty , methodsContent.ToString() , string.Empty , Utils.GetNamespace(entry)); } string name = Utils.PascalCase(entry.Name); if ((name == "jQueryUI") || (name == "Position")) { name = ""; } string category = Utils.PascalCase(entry.Category); if ((category == "Widgets") && (name == "Widget")) { category = ""; name = ""; } Utils.CreateFile(Path.Combine(DestinationPath, category, name), className, formatedContent); }
private void RenderMethodEnum(Entry entry, Entry baseEntry = null) { if (entry.Methods.Count == 0) { return; } if (entry.Methods.Where(m => !excludeJQueryMethods.Contains(m.Name.ToLower())).Count() == 0) { return; } string className = Utils.PascalCase(entry.Name) + @"Method"; string content = @"using System; using System.Runtime.CompilerServices; namespace {2} {{ /// <summary> /// Operations supported by {3}. /// </summary> [Imported] [IgnoreNamespace] [NamedValues] public enum {0} {{{1} }} }}"; StringBuilder enumValues = new StringBuilder(); IEnumerable<Method> methods = entry.Methods.AsQueryable(); if (baseEntry != null) { methods = methods.Union(baseEntry.Methods // filter private methods from the base entry .Where(m => !m.Name.StartsWith("_"))) .Where(m => !excludeJQueryMethods.Contains(m.Name.ToLower())); } foreach (var method in methods.OrderBy(m => m.Name) .GroupBy(m => m.Name)) { enumValues.AppendLine(); enumValues.AppendLine(); if (!string.IsNullOrEmpty(method.Min(m => m.Description))) { enumValues.AppendLine(" /// <summary>"); enumValues.AppendLine(" /// " + Utils.FormatXmlComment(method.Min(m => m.Description).Replace("<entryname />", entry.Name))); enumValues.AppendLine(" /// </summary>"); } if (Utils.PascalCase(method.Key).ToLower() != method.Key.ToLower()) { enumValues.AppendLine(" [ScriptName(\"" + method.Key + "\")]"); } enumValues.Append(" " + Utils.PascalCase(method.Key) + ","); } string name = Utils.PascalCase(entry.Name); if ((name == "jQueryUI") || (name == "Position")) { name = ""; } string category = Utils.PascalCase(entry.Category); if ((category == "Widgets") && (name == "Widget")) { category = ""; name = ""; } Utils.CreateFile(Path.Combine(DestinationPath, category, name), className , string.Format(content, className, enumValues.ToString().Trim(','), Utils.GetNamespace(entry), Utils.PascalCase(entry.Name))); }
private void RenderEntry(Entry entry, Entry baseEntry = null) { if (entry == null) { return; } RenderObject(entry); RenderOptions(entry); RenderEvents(entry); RenderOptionEnum(entry, baseEntry); RenderEventsEnum(entry, baseEntry); RenderMethodEnum(entry, baseEntry); }