private PrimitiveMapping ImportPrimitiveMapping(PrimitiveModel model, string dataType) { PrimitiveMapping mapping = new PrimitiveMapping(); mapping.IsSoap = true; if (dataType.Length > 0) { mapping.TypeDesc = _typeScope.GetTypeDesc(dataType, XmlSchema.Namespace); if (mapping.TypeDesc == null) { // try it as a non-Xsd type mapping.TypeDesc = _typeScope.GetTypeDesc(dataType, UrtTypes.Namespace); if (mapping.TypeDesc == null) { throw new InvalidOperationException(SR.Format(SR.XmlUdeclaredXsdType, dataType)); } } } else { mapping.TypeDesc = model.TypeDesc; } mapping.TypeName = mapping.TypeDesc.DataType.Name; mapping.Namespace = mapping.TypeDesc.IsXsdType ? XmlSchema.Namespace : UrtTypes.Namespace; return(mapping); }
public String GetTypeString(TypeSpec Type, String NamespaceName) { if (Type.OnTypeRef) { if (PrimitiveMapping.ContainsKey(Type.TypeRef.VersionedName())) { var PlatformName = PrimitiveMapping[Type.TypeRef.VersionedName()]; return(PlatformName); } else if (EnumDict.ContainsKey(Type.TypeRef.VersionedName())) { return(GetTypeString(EnumDict[Type.TypeRef.VersionedName()].UnderlyingType, NamespaceName)); } var Ref = Type.TypeRef; if ((Ref.NamespaceName() == NamespaceName) || NamespaceName.StartsWith(Ref.NamespaceName() + ".") || (Ref.NamespaceName() == "")) { return(GetEscapedIdentifier(Ref.SimpleName(Ref.NamespaceName()))); } else { return(GetEscapedIdentifier(String.Join(".", Ref.NamespaceName().Split('.').Select(NamespacePart => LowercaseCamelize(NamespacePart))) + "." + Ref.SimpleName(Ref.NamespaceName()))); } } else if (Type.OnGenericParameterRef) { return(GetEscapedIdentifier(Type.GenericParameterRef)); } else if (Type.OnTuple) { return(GetEscapedIdentifier(Type.SimpleName(NamespaceName))); } else if (Type.OnGenericTypeSpec) { if (Type.GenericTypeSpec.ParameterValues.Count() == 1 && Type.GenericTypeSpec.TypeSpec.OnTypeRef && Type.GenericTypeSpec.TypeSpec.TypeRef.NameMatches("Optional")) { return(GetEscapedIdentifier("Opt" + Type.GenericTypeSpec.ParameterValues.Single().SimpleName(NamespaceName))); } if (Type.GenericTypeSpec.ParameterValues.Count() > 0) { return(GetTypeString(Type.GenericTypeSpec.TypeSpec, NamespaceName) + "<" + String.Join(", ", Type.GenericTypeSpec.ParameterValues.Select(p => MapToReferenceType(GetTypeString(p, NamespaceName)))) + ">"); } else { return(GetTypeString(Type.GenericTypeSpec.TypeSpec, NamespaceName)); } } else { throw new InvalidOperationException(); } }
public String GetTypeString(TypeSpec Type, String NamespaceName) { if (Type.OnTypeRef) { if (PrimitiveMapping.ContainsKey(Type.TypeRef.VersionedName())) { var Name = Type.TypeRef.VersionedName(); if (Name.Equals("Optional", StringComparison.OrdinalIgnoreCase) || Name.Equals("List", StringComparison.OrdinalIgnoreCase) || Name.Equals("Set", StringComparison.OrdinalIgnoreCase) || Name.Equals("Map", StringComparison.OrdinalIgnoreCase)) { var PlatformName = PrimitiveMapping[Name]; return(PlatformName); } } else if (EnumDict.ContainsKey(Type.TypeRef.VersionedName())) { return(GetTypeString(EnumDict[Type.TypeRef.VersionedName()].UnderlyingType, NamespaceName)); } var Ref = Type.TypeRef; if ((Ref.NamespaceName() == NamespaceName) || NamespaceName.StartsWith(Ref.NamespaceName() + ".") || (Ref.NamespaceName() == "")) { return(GetEscapedIdentifier(Ref.SimpleName(Ref.NamespaceName()))); } else { return(GetEscapedIdentifier(String.Join(".", Ref.NamespaceName().Split('.').Select(NamespacePart => LowercaseCamelize(NamespacePart))) + "." + Ref.SimpleName(Ref.NamespaceName()))); } } else if (Type.OnGenericParameterRef) { return(GetEscapedIdentifier(Type.GenericParameterRef)); } else if (Type.OnTuple) { return(GetEscapedIdentifier(Type.SimpleName(NamespaceName))); } else if (Type.OnGenericTypeSpec) { if (Type.GenericTypeSpec.ParameterValues.Count() > 0) { return(GetTypeString(Type.GenericTypeSpec.TypeSpec, NamespaceName) + "<" + String.Join(", ", Type.GenericTypeSpec.ParameterValues.Select(p => GetTypeString(p, NamespaceName)).ToList()) + ">"); } else { return(GetTypeString(Type.GenericTypeSpec.TypeSpec, NamespaceName)); } } else { throw new InvalidOperationException(); } }
public String GetTypeString(TypeSpec Type, String NamespaceName) { if (Type.OnTypeRef) { if (PrimitiveMapping.ContainsKey(Type.TypeRef.VersionedName())) { var PlatformName = PrimitiveMapping[Type.TypeRef.VersionedName()]; if (PlatformName.StartsWith("System.Collections.Generic.")) { return(new String(PlatformName.Skip("System.Collections.Generic.".Length).ToArray())); } } var Ref = Type.TypeRef; if ((Ref.NamespaceName() == NamespaceName) || NamespaceName.StartsWith(Ref.NamespaceName() + ".") || (Ref.NamespaceName() == "")) { return(GetEscapedIdentifier(Ref.SimpleName(Ref.NamespaceName()))); } else { return(GetEscapedIdentifier(Ref.NamespaceName() + "." + Ref.SimpleName(Ref.NamespaceName()))); } } else if (Type.OnGenericParameterRef) { return(GetEscapedIdentifier(Type.GenericParameterRef)); } else if (Type.OnTuple) { return("Tuple<" + String.Join(", ", Type.Tuple.Select(t => GetTypeString(t, NamespaceName))) + ">"); } else if (Type.OnGenericTypeSpec) { if (Type.GenericTypeSpec.ParameterValues.Count() > 0) { return(GetTypeString(Type.GenericTypeSpec.TypeSpec, NamespaceName) + "<" + String.Join(", ", Type.GenericTypeSpec.ParameterValues.Select(p => GetTypeString(p, NamespaceName))) + ">"); } else { return(GetTypeString(Type.GenericTypeSpec.TypeSpec, NamespaceName)); } } else { throw new InvalidOperationException(); } }
public List <String> GetPrimitives(Schema Schema) { var Primitives = new List <String>(); foreach (var p in Schema.TypeRefs.Concat(Schema.Types).Where(c => c.OnPrimitive).Select(c => c.Primitive)) { if (PrimitiveMapping.ContainsKey(p.VersionedName())) { var Name = p.DefinitionName(); var PlatformName = PrimitiveMapping[p.VersionedName()]; if ((Name != PlatformName) && (p.GenericParameters.Count() == 0)) { Primitives.AddRange(Primitive(Name, PlatformName)); } } } return(Primitives); }
public List <String> GetPrimitives(Schema Schema) { var l = new List <String>(); foreach (var c in Schema.TypeRefs.Concat(Schema.Types)) { if (c.OnPrimitive) { if (c.VersionedName() == "Unit") { l.AddRange(Primitive_Unit()); } else if (c.Primitive.GenericParameters.Count == 0) { var Name = c.Primitive.VersionedName(); if (PrimitiveMapping.ContainsKey(Name)) { var PlatformName = PrimitiveMapping[Name]; if (Name != PlatformName) { l.AddRange(Primitive(Name, PlatformName)); } } } else { continue; } } else { continue; } l.Add(""); } if (l.Count > 0) { l = l.Take(l.Count - 1).ToList(); } return(l); }
public String GetTypeString(TypeSpec Type, String NamespaceName, bool ForceNoQuote = false) { if (Type.OnTypeRef) { if (PrimitiveMapping.ContainsKey(Type.TypeRef.VersionedName())) { return(GetEscapedIdentifier(Type.TypeRef.VersionedName())); } var Ref = Type.TypeRef; if ((Ref.NamespaceName() == NamespaceName) || NamespaceName.StartsWith(Ref.NamespaceName() + ".") || (Ref.NamespaceName() == "")) { return((ForceNoQuote ? "" : "'") + GetEscapedIdentifier(Ref.SimpleName(Ref.NamespaceName())) + (ForceNoQuote ? "" : "'")); } else { throw new NotSupportedException("PythonMultipleNamespace"); //Python不支持nested class import //return "'" + GetEscapedIdentifier(Ref.NamespaceName() + "." + Ref.SimpleName(Ref.NamespaceName())) + "'"; } } else if (Type.OnGenericParameterRef) { return(GetEscapedIdentifier(Type.GenericParameterRef)); } else if (Type.OnTuple) { return("Tuple[" + String.Join(", ", Type.Tuple.Select(t => GetTypeString(t, NamespaceName, ForceNoQuote))) + "]"); } else if (Type.OnGenericTypeSpec) { if (Type.GenericTypeSpec.ParameterValues.Count() > 0) { return(GetTypeString(Type.GenericTypeSpec.TypeSpec, NamespaceName, ForceNoQuote) + "[" + String.Join(", ", Type.GenericTypeSpec.ParameterValues.Select(p => GetTypeString(p, NamespaceName, ForceNoQuote))) + "]"); } else { return(GetTypeString(Type.GenericTypeSpec.TypeSpec, NamespaceName, ForceNoQuote)); } } else { throw new InvalidOperationException(); } }
private void WriteText(object o, TextAccessor text) { if (text.Mapping is PrimitiveMapping) { PrimitiveMapping mapping = (PrimitiveMapping)text.Mapping; string stringValue; if (text.Mapping is EnumMapping) { stringValue = WriteEnumMethod((EnumMapping)mapping, o); } else { if (!WritePrimitiveValue(mapping.TypeDesc, o, false, out stringValue)) { // #10593: Add More Tests for Serialization Code Debug.Assert(o is byte[]); } } if (o is byte[]) { WriteValue((byte[])o); } else { WriteValue(stringValue); } } else if (text.Mapping is SpecialMapping) { SpecialMapping mapping = (SpecialMapping)text.Mapping; switch (mapping.TypeDesc.Kind) { case TypeKind.Node: ((XmlNode)o).WriteTo(Writer); break; default: throw new InvalidOperationException(SR.Format(SR.XmlInternalError)); } } }
public Templates(Schema Schema) { foreach (var t in Schema.TypeRefs.Concat(Schema.Types)) { if (!t.GenericParameters().All(gp => gp.Type.OnTypeRef && gp.Type.TypeRef.NameMatches(Name => PrimitiveMapping.ContainsKey(Name) && Name == "Type"))) { throw new InvalidOperationException(String.Format("GenericParametersNotAllTypeParameter: {0}", t.VersionedName())); } } if (!Schema.TypeRefs.Concat(Schema.Types).Where(t => t.OnPrimitive && t.Primitive.NameMatches(Name => Name == "Unit")).Any()) { throw new InvalidOperationException("PrimitiveMissing: Unit"); } if (!Schema.TypeRefs.Concat(Schema.Types).Where(t => t.OnPrimitive && t.Primitive.NameMatches(Name => Name == "Boolean")).Any()) { throw new InvalidOperationException("PrimitiveMissing: Boolean"); } }
public Dictionary <String, IEnumerable <String> > GetPackageFiles(Schema Schema, String NamespaceName) { var NamespaceToClasses = new Dictionary <String, List <KeyValuePair <String, List <String> > > >(); void AddClass(String ClassNamespaceName, String ClassName, IEnumerable <String> ClassContent) { if (!NamespaceToClasses.ContainsKey(ClassNamespaceName)) { NamespaceToClasses.Add(ClassNamespaceName, new List <KeyValuePair <String, List <String> > >()); } NamespaceToClasses[ClassNamespaceName].Add(new KeyValuePair <String, List <String> >(ClassName, ClassContent.ToList())); } foreach (var c in Schema.Types) { if (c.OnPrimitive) { if (c.VersionedName() == "Unit") { AddClass(c.NamespaceName(), "Unit", Primitive_Unit()); } else if (c.VersionedName() == "Set") { AddClass(c.NamespaceName(), "Set", Primitive_Set()); } else { var p = c.Primitive; if (PrimitiveMapping.ContainsKey(p.VersionedName())) { var Name = p.VersionedName(); var PlatformName = PrimitiveMapping[Name]; if (Name != PlatformName && p.GenericParameters.Count() == 0 && PlatformName != "Error") { AddClass(c.NamespaceName(), Name, Primitive(Name, PlatformName)); } else { continue; } } } } else if (c.OnAlias) { AddClass(c.NamespaceName(), c.DefinitionName(), Alias(c.Alias)); } else if (c.OnRecord) { AddClass(c.NamespaceName(), c.DefinitionName(), Record(c.Record)); } else if (c.OnTaggedUnion) { AddClass(c.NamespaceName(), c.DefinitionName(), TaggedUnion(c.TaggedUnion)); } else if (c.OnEnum) { AddClass(c.NamespaceName(), c.DefinitionName(), Enum(c.Enum)); } else if (c.OnClientCommand) { var tc = c.ClientCommand; var RequestRef = GetSuffixedTypeRef(tc.Name, tc.Version, "Request"); var Request = new RecordDef { Name = RequestRef.Name, Version = RequestRef.Version, GenericParameters = new List <VariableDef> { }, Fields = tc.OutParameters, Attributes = tc.Attributes, Description = tc.Description }; var ReplyRef = GetSuffixedTypeRef(tc.Name, tc.Version, "Reply"); var Reply = new TaggedUnionDef { Name = ReplyRef.Name, Version = ReplyRef.Version, GenericParameters = new List <VariableDef> { }, Alternatives = tc.InParameters, Attributes = tc.Attributes, Description = tc.Description }; AddClass(c.NamespaceName(), Request.DefinitionName(), Record(Request)); AddClass(c.NamespaceName(), Reply.DefinitionName(), TaggedUnion(Reply)); } else if (c.OnServerCommand) { var tc = c.ServerCommand; var EventRef = GetSuffixedTypeRef(tc.Name, tc.Version, "Event"); var Event = new RecordDef { Name = EventRef.Name, Version = EventRef.Version, GenericParameters = new List <VariableDef> { }, Fields = tc.OutParameters, Attributes = tc.Attributes, Description = tc.Description }; AddClass(c.NamespaceName(), Event.DefinitionName(), Record(Event)); } else { throw new InvalidOperationException(); } } var scg = Schema.GetSchemaClosureGenerator(); var sc = scg.GetClosure(Schema.TypeRefs.Concat(Schema.Types), new List <TypeSpec> { }); var Tuples = sc.TypeSpecs.Where(t => t.OnTuple).ToList(); foreach (var t in Tuples) { AddClass(NamespaceName, t.SimpleName(NamespaceName), Tuple(t, NamespaceName)); } var Commands = Schema.Types.Where(t => t.OnClientCommand || t.OnServerCommand).Where(t => t.Version() == "").ToList(); if (Commands.Count > 0) { AddClass(NamespaceName, "IApplicationClient", IApplicationClient(Commands, NamespaceName)); } return(NamespaceToClasses.SelectMany(p => p.Value.Select(v => new KeyValuePair <String, IEnumerable <String> >(String.Join("/", p.Key.Split('.').Where(NamespacePart => NamespacePart != "").Select(NamespacePart => LowercaseCamelize(NamespacePart)).Concat(new String[] { v.Key })), WrapModule(p.Key, Schema.Imports, v.Value)))).ToDictionary(p => p.Key, p => p.Value)); }
public String GetTypeString(TypeSpec Type, String NamespaceName, Boolean NoElaboratedTypeSpecifier = false, Boolean ForceAsEnum = false, Boolean ForceAsValue = false) { if (Type.OnTypeRef) { if (PrimitiveMapping.ContainsKey(Type.TypeRef.VersionedName())) { var PlatformName = PrimitiveMapping[Type.TypeRef.VersionedName()]; if (Type.TypeRef.NameMatches("Optional", "List", "Set", "Map")) { return(PlatformName); } else { return(GetTypeString(Type.TypeRef, NamespaceName)); } } else if (AliasSet.Contains(Type.TypeRef.VersionedName())) { return((NoElaboratedTypeSpecifier ? "" : "class ") + GetTypeString(Type.TypeRef, NamespaceName)); } else if (EnumSet.Contains(Type.TypeRef.VersionedName())) { return((NoElaboratedTypeSpecifier ? "" : "enum ") + GetTypeString(Type.TypeRef, NamespaceName)); } if (ForceAsEnum) { return((NoElaboratedTypeSpecifier ? "" : "enum ") + GetTypeString(Type.TypeRef, NamespaceName)); } else if (ForceAsValue) { return((NoElaboratedTypeSpecifier ? "" : "class ") + GetTypeString(Type.TypeRef, NamespaceName)); } return("std::shared_ptr<class " + GetTypeString(Type.TypeRef, NamespaceName) + ">"); } else if (Type.OnGenericParameterRef) { return(GetEscapedIdentifier(Type.GenericParameterRef)); } else if (Type.OnTuple) { return("std::tuple<" + String.Join(", ", Type.Tuple.Select(t => GetTypeString(t, NamespaceName))) + ">"); } else if (Type.OnGenericTypeSpec) { if (Type.GenericTypeSpec.ParameterValues.Count() > 0) { var TypeString = GetTypeString(Type.GenericTypeSpec.TypeSpec, NamespaceName, ForceAsValue: true) + "<" + String.Join(", ", Type.GenericTypeSpec.ParameterValues.Select(p => GetTypeString(p, NamespaceName))) + ">"; if (ForceAsValue) { return(TypeString); } if (Type.GenericTypeSpec.TypeSpec.OnTypeRef && Type.GenericTypeSpec.TypeSpec.TypeRef.NameMatches("Optional") && Type.GenericTypeSpec.ParameterValues.Count == 1) { return(TypeString); } else if (Type.GenericTypeSpec.TypeSpec.OnTypeRef && Type.GenericTypeSpec.TypeSpec.TypeRef.NameMatches("List") && Type.GenericTypeSpec.ParameterValues.Count == 1) { return(TypeString); } else if (Type.GenericTypeSpec.TypeSpec.OnTypeRef && Type.GenericTypeSpec.TypeSpec.TypeRef.NameMatches("Set") && Type.GenericTypeSpec.ParameterValues.Count == 1) { return(TypeString); } else if (Type.GenericTypeSpec.TypeSpec.OnTypeRef && Type.GenericTypeSpec.TypeSpec.TypeRef.NameMatches("Map") && Type.GenericTypeSpec.ParameterValues.Count == 2) { return(TypeString); } return("std::shared_ptr<" + TypeString + ">"); } else { return(GetTypeString(Type.GenericTypeSpec.TypeSpec, NamespaceName, ForceAsValue: ForceAsValue)); } } else { throw new InvalidOperationException(); } }
private void WriteQualifiedNameElement(string name, string ns, object defaultValue, XmlQualifiedName o, bool nullable, bool isSoap, PrimitiveMapping mapping) { bool hasDefault = defaultValue != null && !Globals.IsDBNullValue(defaultValue) && mapping.TypeDesc.HasDefaultSupport; if (hasDefault && IsDefaultValue(mapping, o, defaultValue, nullable)) { return; } if (isSoap) { throw new PlatformNotSupportedException(); } if (nullable) { WriteNullableQualifiedNameLiteral(name, ns, o); } else { WriteElementQualifiedName(name, ns, o); } }
private void WriteElement(object o, ElementAccessor element, string arrayName, bool writeAccessor, XmlMapping parentMapping = null) { string name = writeAccessor ? element.Name : element.Mapping.TypeName; string ns = element.Any && element.Name.Length == 0 ? null : (element.Form == XmlSchemaForm.Qualified ? (writeAccessor ? element.Namespace : element.Mapping.Namespace) : ""); if (element.Mapping is NullableMapping) { if (o != null) { ElementAccessor e = element.Clone(); e.Mapping = ((NullableMapping)element.Mapping).BaseMapping; WriteElement(o, e, arrayName, writeAccessor); } else if (element.IsNullable) { WriteNullTagLiteral(element.Name, ns); } } else if (element.Mapping is ArrayMapping) { var mapping = (ArrayMapping)element.Mapping; if (element.IsNullable && o == null) { WriteNullTagLiteral(element.Name, element.Form == XmlSchemaForm.Qualified ? element.Namespace : ""); } else if (mapping.IsSoap) { if (mapping.Elements == null || mapping.Elements.Length != 1) { throw new InvalidOperationException(SR.XmlInternalError); } var itemElement = mapping.Elements[0]; var itemMapping = itemElement.Mapping as StructMapping; var itemName = writeAccessor ? itemElement.Name : itemMapping.TypeName; var itemNamespace = itemElement.Any && itemElement.Name.Length == 0 ? null : (itemElement.Form == XmlSchemaForm.Qualified ? (writeAccessor ? itemElement.Namespace : itemMapping.Namespace) : ""); if (!writeAccessor) { WritePotentiallyReferencingElement(name, ns, o, mapping.TypeDesc.Type, true, element.IsNullable); } else { WritePotentiallyReferencingElement(name, ns, o, null, false, element.IsNullable); } } else if (element.IsUnbounded) { TypeDesc arrayTypeDesc = mapping.TypeDesc.CreateArrayTypeDesc(); var enumerable = (IEnumerable)o; foreach (var e in enumerable) { element.IsUnbounded = false; WriteElement(e, element, arrayName, writeAccessor); element.IsUnbounded = true; } } else { if (o != null) { WriteStartElement(name, ns, false); WriteArrayItems(mapping.ElementsSortedByDerivation, null, null, mapping.TypeDesc, o); WriteEndElement(); } } } else if (element.Mapping is EnumMapping) { if (element.Mapping.IsSoap) { Writer.WriteStartElement(name, ns); WriteEnumMethod((EnumMapping)element.Mapping, o); WriteEndElement(); } else { WritePrimitive(WritePrimitiveMethodRequirement.WriteElementString, name, ns, element.Default, o, element.Mapping, false, true, element.IsNullable); } } else if (element.Mapping is PrimitiveMapping) { PrimitiveMapping mapping = (PrimitiveMapping)element.Mapping; if (mapping.TypeDesc == QnameTypeDesc) { WriteQualifiedNameElement(name, ns, element.Default, (XmlQualifiedName)o, element.IsNullable, mapping.IsSoap, mapping); } else { WritePrimitiveMethodRequirement suffixNullable = mapping.IsSoap ? WritePrimitiveMethodRequirement.Encoded : WritePrimitiveMethodRequirement.None; WritePrimitiveMethodRequirement suffixRaw = mapping.TypeDesc.XmlEncodingNotRequired ? WritePrimitiveMethodRequirement.Raw : WritePrimitiveMethodRequirement.None; WritePrimitive(element.IsNullable ? WritePrimitiveMethodRequirement.WriteNullableStringLiteral | suffixNullable | suffixRaw : WritePrimitiveMethodRequirement.WriteElementString | suffixRaw, name, ns, element.Default, o, mapping, mapping.IsSoap, true, element.IsNullable); } } else if (element.Mapping is StructMapping) { var mapping = (StructMapping)element.Mapping; if (mapping.IsSoap) { WritePotentiallyReferencingElement(name, ns, o, !writeAccessor ? mapping.TypeDesc.Type : null, !writeAccessor, element.IsNullable); } else { WriteStructMethod(mapping, name, ns, o, element.IsNullable, needType: false, parentMapping: parentMapping); } } else if (element.Mapping is SpecialMapping) { if (element.Mapping is SerializableMapping) { WriteSerializable((IXmlSerializable)o, name, ns, element.IsNullable, !element.Any); } else { // XmlNode, XmlElement var node = o as XmlNode; if (node != null) { WriteElementLiteral(node, name, ns, element.IsNullable, element.Any); } else { throw CreateInvalidAnyTypeException(o); } } } else { throw new InvalidOperationException(SR.XmlInternalError); } }