private void AddParameterElements(XElement complexType, ParameterInfo[] parameters, string[] requiredParameters) { XElement sequence = new XElement(_Namespace + "sequence"); foreach (var parameter in from parameter in parameters let typeName = MagickScriptTypes.GetXsdElementType(parameter) where typeName != null orderby parameter.Name select new { Name = parameter.Name, TypeName = typeName, IsRequired = requiredParameters.Contains(parameter.Name) }) { XElement element = new XElement(_Namespace + "element", new XAttribute("name", parameter.Name)); if (!parameter.IsRequired) { element.Add(new XAttribute("minOccurs", "0")); } element.Add(new XAttribute("type", parameter.TypeName)); sequence.Add(element); } if (sequence.HasElements) { complexType.Add(sequence); } }
private bool IsStatic(MethodBase[] methods) { if (methods == null) { return(false); } foreach (var method in methods) { var parameters = method.GetParameters(); foreach (ParameterInfo parameter in parameters) { string xsdTypeName = MagickScriptTypes.GetXsdAttributeType(parameter); if (xsdTypeName != null) { return(false); } string typeName = GetName(parameter); if (!HasStaticCreateMethod(typeName)) { return(false); } } } return(true); }
private XElement CreateElement(PropertyInfo property) { string name = MagickScriptTypes.GetXsdName(property); string attributeTypeName = MagickScriptTypes.GetXsdAttributeType(property); if (attributeTypeName != null) { XElement complexType = new XElement(_Namespace + "complexType"); complexType.Add(new XElement(_Namespace + "attribute", new XAttribute("name", "value"), new XAttribute("use", "required"), new XAttribute("type", attributeTypeName))); return(new XElement(_Namespace + "element", new XAttribute("name", name), complexType)); } else { string elementTypeName = MagickScriptTypes.GetXsdElementType(property); if (string.IsNullOrEmpty(elementTypeName)) { throw new NotImplementedException("CreateElement: " + name); } return(new XElement(_Namespace + "element", new XAttribute("name", name), new XAttribute("type", elementTypeName))); } }
private void AddMethods(XElement element, IEnumerable <MethodBase> methods) { ParameterInfo[] parameters = (from method in methods from parameter in method.GetParameters() select parameter).ToArray(); if (parameters.Length == 0) { element.Add(new XAttribute("type", "empty")); return; } if (methods.Count() == 1 && IsTypedElement(methods.First(), parameters)) { string elementTypeName = MagickScriptTypes.GetXsdElementType(parameters[0]); if (string.IsNullOrEmpty(elementTypeName)) { throw new NotImplementedException("AddMethods: " + methods.First().Name); } element.Add(new XAttribute("type", elementTypeName)); return; } XElement complexType = new XElement(_Namespace + "complexType"); AddArguments(complexType, methods); element.Add(complexType); }
private void AddParameterAttributes(XElement complexType, ParameterInfo[] parameters, string[] requiredParameters) { foreach (var parameter in from parameter in parameters let typeName = MagickScriptTypes.GetXsdAttributeType(parameter) where typeName != null orderby parameter.Name select new { Name = parameter.Name, TypeName = typeName, IsRequired = requiredParameters.Contains(parameter.Name) }) { XElement attribute = new XElement(_Namespace + "attribute", new XAttribute("name", parameter.Name)); if (parameter.IsRequired) { attribute.Add(new XAttribute("use", "required")); } attribute.Add(new XAttribute("type", parameter.TypeName)); complexType.Add(attribute); } }
private XElement CreateElement(IEnumerable <MethodBase> methods) { XElement element = new XElement(_Namespace + "element", new XAttribute("name", MagickScriptTypes.GetXsdName(methods.First()))); AddMethods(element, methods); return(element); }
public XsdGenerator(QuantumDepth depth) { _Depth = depth; _Types = new MagickScriptTypes(depth); _Namespaces = new XmlNamespaceManager(new NameTable()); _Namespaces.AddNamespace("xs", _Namespace.ToString()); }
private void ReplaceIReadDefines(XElement annotation) { annotation.ReplaceWith( from type in _Types.GetInterfaceTypes("IReadDefines") let name = MagickScriptTypes.GetXsdName(type) select new XElement(_Namespace + "element", new XAttribute("name", name), new XAttribute("type", name))); }
public void Write(MagickScriptTypes types) { Types = types; WriteStart("ImageMagick"); WriteLine("public sealed partial class MagickScript"); WriteStartColon(); WriteCode(); WriteEndColon(); WriteEnd(); }
private void WriteCallMethods() { foreach (MethodBase method in Methods) { string xsdMethodName = MagickScriptTypes.GetXsdName(method); ParameterInfo[] parameters = method.GetParameters(); Write("XmlElement "); Write(xsdMethodName); Write(" = (XmlElement)element.SelectSingleNode(\""); Write(xsdMethodName); WriteLine("\");"); Write("if ("); Write(xsdMethodName); WriteLine(" != null)"); WriteStartColon(); foreach (ParameterInfo parameter in parameters) { string typeName = GetName(parameter); Write(typeName); Write(" "); Write(parameter.Name); Write("_ = XmlHelper.GetAttribute<"); Write(typeName); Write(">("); Write(xsdMethodName); Write(", \""); Write(parameter.Name); WriteLine("\");"); } Write("result."); Write(method.Name); Write("("); for (int i = 0; i < parameters.Length; i++) { if (i > 0) { Write(","); } Write(parameters[i].Name); Write("_"); } WriteLine(");"); WriteEndColon(); } }
private void ReplaceDefines(XElement annotation) { List <XElement> types = new List <XElement>(); foreach (Type interfaceType in _Types.GetInterfaceTypes("IDefines")) { XElement complexType = new XElement(_Namespace + "complexType", new XAttribute("name", MagickScriptTypes.GetXsdName(interfaceType))); AddClass(complexType, interfaceType); types.Add(complexType); } annotation.ReplaceWith(types.ToArray()); }
private void WriteCode(string className) { Write("private "); Write(className); Write(" Create"); Write(className); WriteLine("(XmlElement parent)"); WriteStartColon(); WriteCheckNull("parent"); WriteLine("XmlElement element = (XmlElement)parent.FirstChild;"); WriteCheckNull("element"); WriteSwitch(from type in Types.GetInterfaceTypes(className) select MagickScriptTypes.GetXsdName(type)); WriteEndColon(); }
protected void WriteGetValue(PropertyInfo property) { string typeName = GetName(property); string xsdTypeName = MagickScriptTypes.GetXsdAttributeType(property); if (xsdTypeName != null) { WriteGetElementValue(typeName, "value"); } else { WriteCreateMethod(typeName); Write("("); WriteSelectElement(typeName, null); WriteLine(");"); } }
private void WriteGetValue(ParameterInfo parameter) { string typeName = GetName(parameter); string xsdTypeName = MagickScriptTypes.GetXsdAttributeType(parameter); if (xsdTypeName != null) { WriteGetElementValue(typeName, parameter.Name); } else { WriteCreateMethod(typeName); Write("("); WriteSelectElement(typeName, parameter.Name); WriteLine(");"); } }
private void Write() { string folder = MagickScriptTypes.GetFolderName(_Depth); string outputFile = PathHelper.GetFullPath(@"src\Magick.NET\Resources\" + folder + @"\MagickScript.xsd"); Console.WriteLine("Creating: " + outputFile); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = " "; using (XmlWriter writer = XmlWriter.Create(outputFile, settings)) { _Document.Save(writer); } }
private void AddClassAttributes(XElement complexType, IEnumerable <PropertyInfo> properties) { foreach (var property in from property in properties let typeName = MagickScriptTypes.GetXsdAttributeType(property) where typeName != null let name = MagickScriptTypes.GetXsdName(property) orderby name select new { Name = name, TypeName = typeName }) { complexType.Add(new XElement(_Namespace + "attribute", new XAttribute("name", property.Name), new XAttribute("type", property.TypeName))); } }
private void WriteAttributeForEach(ParameterInfo[] allParameters) { ParameterInfo[] parameters = allParameters.Where(p => MagickScriptTypes.GetXsdAttributeType(p) != null).ToArray(); if (parameters.Length == 0) { return; } parameters = parameters.OrderBy(p => p.Name).ToArray(); WriteLine("foreach (XmlAttribute attribute in element.Attributes)"); WriteStartColon(); if (parameters.DistinctBy(p => GetName(p)).Count() == 1) { Write("arguments[attribute.Name] = GetValue<"); Write(GetName(parameters[0])); WriteLine(">(attribute);"); } else { for (int i = 0; i < parameters.Length; i++) { string xsdName = MagickScriptTypes.GetXsdName(parameters[i]); if (i > 0) { Write("else "); } Write("if (attribute.Name == \""); Write(xsdName); WriteLine("\")"); Indent++; Write("arguments[\""); Write(xsdName); Write("\"] = "); WriteGetAttributeValue(GetName(parameters[i])); Indent--; } } WriteEndColon(); }
private void WriteElementForEach(ParameterInfo[] allParameters) { ParameterInfo[] parameters = allParameters.Where(p => MagickScriptTypes.GetXsdAttributeType(p) == null).ToArray(); if (parameters.Length == 0) { return; } WriteLine("foreach (XmlElement elem in element.SelectNodes(\"*\"))"); WriteStartColon(); if (parameters.DistinctBy(p => GetName(p)).Count() == 1) { Write("arguments[elem.Name] = "); WriteCreateMethod(GetName(parameters[0])); WriteLine("(elem);"); } else { for (int i = 0; i < parameters.Length; i++) { string xsdName = MagickScriptTypes.GetXsdName(parameters[i]); if (i > 0) { Write("else "); } Write("if (elem.Name == \""); Write(xsdName); WriteLine("\")"); Indent++; Write("arguments[\""); Write(xsdName); Write("\"] = "); WriteCreateMethod(GetName(parameters[i])); WriteLine("(elem);"); Indent--; } } WriteEndColon(); }
private void AddClassElements(XElement complexType, IEnumerable <PropertyInfo> properties, IEnumerable <MethodInfo> methods) { XElement sequence = new XElement(_Namespace + "sequence"); foreach (var property in from property in properties let typeName = MagickScriptTypes.GetXsdElementType(property) where typeName != null let name = MagickScriptTypes.GetXsdName(property) orderby name select new { Name = name, TypeName = typeName }) { XElement element = new XElement(_Namespace + "element", new XAttribute("name", property.Name), new XAttribute("minOccurs", "0")); element.Add(new XAttribute("type", property.TypeName)); sequence.Add(element); } if (methods.Count() > 0) { foreach (MethodBase method in methods) { XElement element = new XElement(_Namespace + "element", new XAttribute("name", MagickScriptTypes.GetXsdName(method)), new XAttribute("minOccurs", "0"), new XAttribute("maxOccurs", "unbounded")); AddMethods(element, new MethodBase[] { method }); sequence.Add(element); } } if (sequence.HasElements) { complexType.Add(sequence); } }
private void WriteExecute() { Write("private "); Write(ReturnType); Write(" Execute"); Write(ExecuteName); Write("(XmlElement element, "); Write(ExecuteArgument); WriteLine(")"); WriteStartColon(); IEnumerable <string> names = (from property in Properties select MagickScriptTypes.GetXsdName(property)).Concat( from method in Methods select MagickScriptTypes.GetXsdName(method[0])).Concat( CustomMethods); WriteSwitch(names); WriteEndColon(); }
protected sealed override void WriteCase(string name) { MemberInfo member = (from property in Properties where MagickScriptTypes.GetXsdName(property).Equals(name, StringComparison.OrdinalIgnoreCase) select property).FirstOrDefault(); if (member == null) { member = (from overloads in Methods let method = overloads[overloads.Length - 1] where MagickScriptTypes.GetXsdName(method).Equals(name, StringComparison.OrdinalIgnoreCase) select method).FirstOrDefault(); } if (ReturnType != "void") { Write("return "); } Write("Execute"); if (member == null) { Write(char.ToUpper(name[0], CultureInfo.InvariantCulture)); Write(name.Substring(1)); } else { Write(GetName(member)); } Write("("); if (member == null || !HasParameters(member)) { Write("element, "); } Write(ExecuteArgument.Split(' ').Last()); WriteLine(");"); if (ReturnType == "void") { WriteLine("return;"); } }
private void WriteExecute() { WriteLine("[SuppressMessage(\"Microsoft.Maintainability\", \"CA1502:AvoidExcessiveComplexity\")]"); WriteLine("[SuppressMessage(\"Microsoft.Maintainability\", \"CA1505:AvoidUnmaintainableCode\")]"); Write("private "); Write(ReturnType); Write(" Execute"); Write(ExecuteName); Write("(XmlElement element, "); Write(ExecuteArgument); WriteLine(")"); WriteStartColon(); IEnumerable <string> names = (from property in Properties select MagickScriptTypes.GetXsdName(property)).Concat( from method in Methods select MagickScriptTypes.GetXsdName(method[0])).Concat( CustomMethods); WriteSwitch(names); WriteEndColon(); }
private MagickScriptGenerator() { _OutputFolder = SetOutputFolder(@"Source\Magick.NET\Core\Script\Generated"); _Types = new MagickScriptTypes(QuantumDepth.Q16HDRI); }
private void WriteInvalidCombinations(MethodBase[] methods) { WriteLine("else"); Indent++; Write("throw new ArgumentException(\"Invalid argument combination for '" + MagickScriptTypes.GetXsdName(methods[0]) + "', allowed combinations are:"); foreach (MethodBase method in methods) { Write(" ["); ParameterInfo[] parameters = method.GetParameters(); for (int i = 0; i < parameters.Length; i++) { Write(parameters[i].Name); if (i != parameters.Length - 1) { Write(", "); } } Write("]"); } WriteLine("\");"); Indent--; }
protected static string GetName(MemberInfo member) { return(MagickScriptTypes.GetName(member)); }
public void WriteCode(MagickScriptTypes types) { Types = types; WriteCode(); }
public XsdGenerator(QuantumDepth depth) { _Depth = depth; _Types = new MagickScriptTypes(depth); _Namespaces = new XmlNamespaceManager(new NameTable()); _Namespaces.AddNamespace("xs", _Namespace.ToString()); }
private void AddClass(XElement parent, Type type) { AddClassElements(parent, MagickScriptTypes.GetProperties(type), MagickScriptTypes.GetMethods(type)); AddClassAttributes(parent, MagickScriptTypes.GetProperties(type)); }
public void Write(MagickScriptTypes types) { Types = types; WriteStart("ImageMagick"); WriteLine("public sealed partial class MagickScript"); WriteStartColon(); WriteCode(); WriteEndColon(); WriteEnd(); }
public void WriteCode(MagickScriptTypes types) { Types = types; WriteCode(); }
private MagickScriptGenerator() { _OutputFolder = SetOutputFolder(@"src\Magick.NET\Shared\Script\Generated"); _Types = new MagickScriptTypes(QuantumDepth.Q16HDRI); }
private MagickScriptGenerator() { _OutputFolder = SetOutputFolder(@"Source\Magick.NET\Core\Script\Generated"); _Types = new MagickScriptTypes(QuantumDepth.Q16HDRI); }