XmlObjectExtension
Extension methods to help out generating XMLObject structure to schema.
using Namotion.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NJsonSchema.Infrastructure
{
public static class XmlObjectExtension
{
public static void GenerateXmlObjectForType(this JsonSchema schema, Type type)
{
Attribute[] typeAttributes = ContextualTypeExtensions.ToCachedType(type).get_TypeAttributes();
if (typeAttributes.Any()) {
dynamic val = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>((IEnumerable<Attribute>)typeAttributes, "System.Xml.Serialization.XmlTypeAttribute", 1);
if (val != null)
XmlObjectExtension.GenerateXmlObject(val.TypeName, val.Namespace, false, false, schema);
}
}
public static void GenerateXmlObjectForArrayType(this JsonSchema schema)
{
if (schema.IsArray && schema.ParentSchema == null)
GenerateXmlObject("ArrayOf" + schema.Item.Xml.Name, null, true, false, schema);
}
public static void GenerateXmlObjectForItemType(this JsonSchema schema, CachedType type)
{
dynamic val = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>((IEnumerable<Attribute>)type.get_TypeAttributes(), "System.Xml.Serialization.XmlTypeAttribute", 1);
string name = GetXmlItemName(type.get_OriginalType());
if (val != null)
name = (string)val.TypeName;
GenerateXmlObject(name, null, false, false, schema);
}
public static void GenerateXmlObjectForProperty(this JsonSchemaProperty propertySchema, ContextualType type, string propertyName)
{
string text = null;
string namespace = null;
bool flag = false;
if (propertySchema.IsArray) {
dynamic val = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(type.get_Attributes(), "System.Xml.Serialization.XmlArrayAttribute", 1);
if (val != null) {
text = (string)val.ElementName;
namespace = (string)val.Namespace;
}
dynamic val2 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(type.get_Attributes(), "System.Xml.Serialization.XmlArrayItemAttribute", 1);
if (val2 != null) {
dynamic val3 = val2.ElementName;
dynamic val4 = val2.Namespace;
XmlObjectExtension.GenerateXmlObject(val3, val4, true, false, propertySchema.Item);
}
flag = true;
}
dynamic val5 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(type.get_Attributes(), "System.Xml.Serialization.XmlElementAttribute", 1);
if (val5 != null) {
text = (string)val5.ElementName;
namespace = (string)val5.Namespace;
}
dynamic val6 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>(type.get_Attributes(), "System.Xml.Serialization.XmlAttributeAttribute", 1);
if (val6 != null) {
if ((!string.IsNullOrEmpty(val6.AttributeName)))
text = (string)val6.AttributeName;
if ((!string.IsNullOrEmpty(val6.Namespace)))
namespace = (string)val6.Namespace;
}
if (string.IsNullOrEmpty(text) && propertySchema.Type == JsonObjectType.None) {
dynamic val7 = EnumerableExtensions.FirstAssignableToTypeNameOrDefault<Attribute>((IEnumerable<Attribute>)type.get_TypeAttributes(), "System.Xml.Serialization.XmlTypeAttribute", 1);
if (val7 != null)
text = propertyName;
}
if (!string.IsNullOrEmpty(text) | flag)
GenerateXmlObject(text, namespace, flag, (val6 != null) ? true : false, propertySchema);
}
private static void GenerateXmlObject(string name, string namespace, bool wrapped, bool isAttribute, JsonSchema schema)
{
schema.Xml = new JsonXmlObject {
Name = name,
Wrapped = wrapped,
Namespace = namespace,
ParentSchema = schema,
Attribute = isAttribute
};
}
private static string GetXmlItemName(Type type)
{
if (type == typeof(int))
return "int";
if (type == typeof(string))
return "string";
if (type == typeof(double))
return "double";
if (type == typeof(decimal))
return "decimal";
return type.Name;
}
}
}