protected override void InitializeElementProperties(XmlReader reader, ref object obj, ITypeSerializationInfo info, XmlSerializationContext context) { var model = obj as Model; if (model == null) { base.InitializeElementProperties(reader, ref obj, info, context); } else { int currentDepth = reader.Depth; while (reader.Depth < currentDepth || reader.Read()) { if (reader.Depth == currentDepth) { break; } else if (reader.Depth < currentDepth) { return; } if (reader.NodeType == XmlNodeType.Element) { var element = CreateRoot(reader) as IModelElement; Initialize(reader, element, context); if (element != null) { model.RootElements.Add(element); } } } } }
protected override bool WriteIdentifiedObject(XmlWriter writer, object obj, XmlIdentificationMode identificationMode, ITypeSerializationInfo info, XmlSerializationContext context) { if (identificationMode == XmlIdentificationMode.Identifier) { var model = context.Root as Model; if (model != null) { var modelElement = obj as IModelElement; if (modelElement != null) { Uri uri; if (modelElement.Model == model) { uri = modelElement.RelativeUri; } else { uri = MakeShortUri(modelElement.AbsoluteUri, model.ModelUri); } if (uri != null) { writer.WriteString(uri.ConvertToString()); return true; } } } } return base.WriteIdentifiedObject(writer, obj, identificationMode, info, context); }
protected override void HandleUnknownAttribute(XmlReader reader, object obj, ITypeSerializationInfo info, XmlSerializationContext context) { if (reader.NamespaceURI == XmiArtificialIdAttribute.Instance.Namespace && reader.LocalName == XmiArtificialIdAttribute.Instance.ElementName) { XmiArtificialIdAttribute.Instance.SetValue(obj, reader.Value, context); } else { base.HandleUnknownAttribute(reader, obj, info, context); } }
protected override void WriteBeginElement(System.Xml.XmlWriter writer, object obj, ITypeSerializationInfo info) { string prefix = info.NamespacePrefix; if (prefix == null) { prefix = writer.LookupPrefix(info.Namespace); } else if (writer.LookupPrefix(info.Namespace) == null) { writer.WriteAttributeString("xmlns", prefix, null, info.Namespace); } string value = prefix + ":" + info.ElementName; writer.WriteAttributeString(XMLSchemaInstancePrefix, TypeField, XMLSchemaInstanceNamespace, value); }
public static string GetTypeName(this ITypeSerializationInfo info) { switch (info) { case IStructureDefinitionReference tr: return(tr.ReferredType); case IStructureDefinitionSummary ct: return(ct.TypeName); default: throw Error.NotSupported($"Don't know how to derive type information from type {info.GetType()}"); } }
public virtual object Resolve(string id, ITypeSerializationInfo type) { Dictionary <string, object> dict; object obj; if (idStore.TryGetValue(type, out dict) && dict.TryGetValue(id, out obj)) { return(obj); } else { return(null); } }
protected override void WriteElementProperties(XmlWriter writer, object obj, ITypeSerializationInfo info, XmlSerializationContext context) { var model = obj as Model; if (model == null) { base.WriteElementProperties(writer, obj, info, context); } else { foreach (var element in model.RootElements) { Serialize(element, writer, null, true, XmlIdentificationMode.FullObject, context); } } }
protected override string GetAttributeValue(object value, ITypeSerializationInfo info, XmlSerializationContext context) { var model = context.Root as Model; var modelElement = value as ModelElement; if (modelElement != null && model != null) { Uri uri = model.CreateUriForElement(modelElement); if (uri != null) { return(uri.ConvertToString()); } } return(base.GetAttributeValue(value, info, context)); }
protected override void InitializeTypeSerializationInfo(Type type, ITypeSerializationInfo serializationInfo) { base.InitializeTypeSerializationInfo(type, serializationInfo); if (!serializationInfo.IsIdentified && !serializationInfo.IsCollection && serializationInfo is XmlTypeSerializationInfo) { var id = IdAttribute; if (id != null && serializationInfo is XmlTypeSerializationInfo info) { if (!info.AttributeProperties.Contains(id)) { info.DeclaredAttributeProperties.Add(id); } info.IdentifierProperty = id; } } }
private void WriteTypeQualifier(XmlWriter writer, ITypeSerializationInfo type) { if (type.NamespacePrefix != null) { if (writer.LookupPrefix(type.Namespace) != type.NamespacePrefix) { writer.WriteAttributeString("xmlns", type.NamespacePrefix, null, type.Namespace); } writer.WriteAttributeString(XMLSchemaInstancePrefix, TypeField, XMLSchemaInstanceNamespace, type.NamespacePrefix + ":" + type.ElementName); } else { writer.WriteStartAttribute(XMLSchemaInstancePrefix, TypeField, XMLSchemaInstanceNamespace); writer.WriteQualifiedName(type.ElementName, type.Namespace); writer.WriteEndAttribute(); } }
private List <Dictionary <string, object> > GetOrCreateTypeStores(ITypeSerializationInfo type) { // we have to search all paths List <Dictionary <string, object> > paths; if (!pathStore.TryGetValue(type, out paths)) { paths = new List <Dictionary <string, object> >(); foreach (var pair in idStore) { if (type.IsAssignableFrom(pair.Key)) { paths.Add(pair.Value); } } pathStore.Add(type, paths); } return(paths); }
protected override string GetAttributeValue(object value, ITypeSerializationInfo info, XmlSerializationContext context) { var model = context.Root as Model; var modelElement = value as ModelElement; if (modelElement != null && model != null) { Uri uri; if (modelElement.Model == model) { uri = modelElement.RelativeUri; } else { uri = MakeShortUri(modelElement.AbsoluteUri, model.ModelUri); } if (uri != null) { return(uri.ConvertToString()); } } return(base.GetAttributeValue(value, info, context)); }
protected override void WriteElementProperties(XmlWriter writer, object obj, ITypeSerializationInfo info, XmlSerializationContext context) { var model = obj as Model; if (model == null) { base.WriteElementProperties(writer, obj, info, context); } else { foreach (var element in model.RootElements) { if (element == null) { continue; } var typeInfo = GetSerializationInfo(element.GetType(), true); writer.WriteStartElement(typeInfo.NamespacePrefix ?? RootPrefix, typeInfo.ElementName, typeInfo.Namespace); Serialize(element, writer, null, false, XmlIdentificationMode.FullObject, context); writer.WriteEndElement(); } } }
private void InsertIntoIdStore(string id, object value, ITypeSerializationInfo type) { Dictionary<string, object> dict; if (!idStore.TryGetValue(type, out dict)) { dict = new Dictionary<string, object>(); idStore.Add(type, dict); } if (!dict.ContainsKey(id)) { dict.Add(id, value); } else { dict[id] = value; } if (type.BaseTypes != null) { foreach (var baseType in type.BaseTypes) { InsertIntoIdStore(id, value, baseType); } } }
protected override object OnNameClash(string id, ITypeSerializationInfo type, IEnumerable <object> candidates, object source) { var modelElement = source as IModelElement; if (modelElement != null) { var newCandidates = candidates.OfType <IModelElement>(); if (newCandidates.Count() == 1) { return(newCandidates.First()); } var siblingsOfCurrent = newCandidates.Where(c => c.Parent == modelElement.Parent); if (siblingsOfCurrent.Count() == 1) { return(siblingsOfCurrent.First()); } var childrenOfCurrent = newCandidates.Where(c => c.Parent == modelElement); if (childrenOfCurrent.Count() == 1) { return(childrenOfCurrent.First()); } } return(base.OnNameClash(id, type, candidates, source)); }
public virtual object Resolve(string id, ITypeSerializationInfo type, Type minType = null, bool failOnConflict = true, object source = null) { if (id == null) { return(null); } Dictionary <string, object> dict; object obj; if (idStore.TryGetValue(type, out dict) && dict.TryGetValue(id, out obj)) { var clash = obj as NameClash; if (clash != null) { if (failOnConflict) { return(OnNameClash(id, type, clash.Objects.AsReadOnly(), source)); } else { return(null); } } return(obj); } else { object result = null; List <Dictionary <string, object> > paths = GetOrCreateTypeStores(type); foreach (var typeStore in paths) { if (typeStore.TryGetValue(id, out obj)) { if (result == null) { result = obj; } else { var clash = result as NameClash; if (clash == null) { clash = new NameClash(); clash.Objects.Add(result); result = clash; } clash.Objects.Add(obj); } } } var resultClash = result as NameClash; if (resultClash != null) { if (failOnConflict) { return(OnNameClash(id, type, resultClash.Objects.AsReadOnly(), source)); } else { return(null); } } else { return(result); } } }
protected override void WriteBeginRootElement(System.Xml.XmlWriter writer, object root, ITypeSerializationInfo info) { writer.WriteStartElement(info.NamespacePrefix ?? RootPrefix, info.ElementName, info.Namespace); writer.WriteAttributeString(XMIPrefix, "version", XMINamespace, "2.0"); writer.WriteAttributeString("xmlns", XMLSchemaInstancePrefix, null, XMLSchemaInstanceNamespace); }
protected virtual object OnNameClash(string id, ITypeSerializationInfo type, IEnumerable <object> candidates, object source) { throw new XmlResolveNameClashException(id, type, candidates); }
protected override void WriteEndRootElement(System.Xml.XmlWriter writer, object root, ITypeSerializationInfo info) { writer.WriteEndElement(); }
protected override void WriteElementProperties(XmlWriter writer, object obj, ITypeSerializationInfo info, XmlSerializationContext context) { var model = obj as Model; if (model == null) { base.WriteElementProperties(writer, obj, info, context); } else { foreach (var element in model.RootElements) { if (element == null) continue; var typeInfo = GetSerializationInfo(element.GetType(), true); writer.WriteStartElement(typeInfo.NamespacePrefix ?? RootPrefix, typeInfo.ElementName, typeInfo.Namespace); Serialize(element, writer, null, false, XmlIdentificationMode.FullObject, context); writer.WriteEndElement(); } } }
public XmlAddToCollectionDelay(ITypeSerializationInfo type) { Type = type; }
protected override void WriteEndElement(System.Xml.XmlWriter writer, object obj, ITypeSerializationInfo info) { }
public DynamicAttributeSerializationInfo(IAttribute attribute, ITypeSerializationInfo type) { Attribute = attribute; PropertyType = type; }
protected override bool WriteIdentifiedObject(XmlWriter writer, object obj, XmlIdentificationMode identificationMode, ITypeSerializationInfo info, XmlSerializationContext context) { if (identificationMode == XmlIdentificationMode.Identifier) { var model = context.Root as Model; if (model != null) { var modelElement = obj as IModelElement; if (modelElement != null) { Uri uri; if (modelElement.Model == model) { uri = modelElement.RelativeUri; } else { uri = MakeShortUri(modelElement.AbsoluteUri, model.ModelUri); } if (uri != null) { writer.WriteString(uri.ConvertToString()); return(true); } } } } return(base.WriteIdentifiedObject(writer, obj, identificationMode, info, context)); }
protected override bool WriteIdentifiedObject(XmlWriter writer, object obj, XmlIdentificationMode identificationMode, ITypeSerializationInfo info, XmlSerializationContext context) { return false; }
protected override void WriteElementProperties(System.Xml.XmlWriter writer, object obj, ITypeSerializationInfo info, XmlSerializationContext context) { foreach (XmlPropertySerializationInfo pi in info.ElementProperties) { var value = pi.GetValue(obj, context); if (pi.ShouldSerializeValue(obj, value)) { if (pi.PropertyType.IsCollection) { var collectionType = pi.PropertyType.CollectionItemType.Type; foreach (object item in value as IEnumerable) { writer.WriteStartElement(pi.NamespacePrefix, pi.ElementName, pi.Namespace); var itemType = item.GetType(); var itemInfo = GetSerializationInfo(itemType, true); if (itemType != collectionType) { WriteTypeQualifier(writer, itemInfo); } if (itemInfo.IsStringConvertible) { writer.WriteString(itemInfo.ConvertToString(item)); } else { Serialize(item, writer, pi, false, pi.IdentificationMode, context); } writer.WriteEndElement(); } } else { writer.WriteStartElement(pi.NamespacePrefix, pi.ElementName, pi.Namespace); if (value != null) { var type = value.GetType(); if (type != pi.PropertyType.Type.GetImplementationType()) { WriteTypeQualifier(writer, GetSerializationInfo(type, true)); } } else { throw new NotImplementedException(); } Serialize(value, writer, pi, false, pi.IdentificationMode, context); writer.WriteEndElement(); } } } }
public bool IsAssignableFrom(ITypeSerializationInfo specializedType) { return(specializedType is XmiStringSerializationInfo); }
public virtual object Resolve(string id, ITypeSerializationInfo type) { Dictionary<string, object> dict; object obj; if (idStore.TryGetValue(type, out dict) && dict.TryGetValue(id, out obj)) { return obj; } else { return null; } }
public bool IsAssignableFrom(ITypeSerializationInfo specializedType) { return(specializedType == this); }
protected override void InitializeElementProperties(System.Xml.XmlReader reader, ref object obj, ITypeSerializationInfo info, XmlSerializationContext context) { int currentDepth = reader.Depth; while (reader.Depth < currentDepth || reader.Read()) { if (reader.Depth == currentDepth) { break; } else if (reader.Depth < currentDepth) { return; } if (reader.NodeType == XmlNodeType.Element) { var found = false; foreach (IPropertySerializationInfo p in info.ElementProperties) { if (IsPropertyElement(reader, p)) { ReadElementFromProperty(reader, obj, context, p); found = true; break; } } if (!found) { foreach (IPropertySerializationInfo p in info.AttributeProperties) { if (IsPropertyElement(reader, p)) { ReadElementFromProperty(reader, obj, context, p); found = true; break; } } if (!found) { OnUnknownElement(new UnknownElementEventArgs(obj, reader.ReadOuterXml())); } } } } }
public bool IsAssignableFrom(ITypeSerializationInfo specializedType) { return(specializedType is XmlTypeSerializationInfo typeInfo && Type.IsAssignableFrom(typeInfo.Type)); }
protected override string GetAttributeValue(object value, ITypeSerializationInfo info, XmlSerializationContext context) { var model = context.Root as Model; var modelElement = value as ModelElement; if (modelElement != null && model != null) { Uri uri; if (modelElement.Model == model) { uri = modelElement.RelativeUri; } else { uri = MakeShortUri(modelElement.AbsoluteUri, model.ModelUri); } if (uri != null) { return uri.ConvertToString(); } } return base.GetAttributeValue(value, info, context); }
protected override void WriteElementProperties(System.Xml.XmlWriter writer, object obj, ITypeSerializationInfo info, XmlSerializationContext context) { if (info.DefaultProperty != null) { var content = info.DefaultProperty.GetValue(obj, context); if (info.DefaultProperty.ShouldSerializeValue(obj, content)) { writer.WriteString(GetAttributeValue(content, info.DefaultProperty.PropertyType, false, context)); } } foreach (XmlPropertySerializationInfo pi in info.ElementProperties) { var value = pi.GetValue(obj, context); if (pi.ShouldSerializeValue(obj, value)) { if (pi.PropertyType.IsCollection) { var collectionType = pi.PropertyType.CollectionItemType.Type; foreach (object item in value as IEnumerable) { writer.WriteStartElement(pi.NamespacePrefix, pi.ElementName, pi.Namespace); var itemType = item.GetType(); var itemInfo = GetSerializationInfo(itemType, true); if (itemType != collectionType) { WriteTypeQualifier(writer, itemInfo); } if (itemInfo.IsStringConvertible) { writer.WriteString(itemInfo.ConvertToString(item)); } else { Serialize(item, writer, pi, false, pi.IdentificationMode, context); } writer.WriteEndElement(); } } else { writer.WriteStartElement(pi.NamespacePrefix, pi.ElementName, pi.Namespace); if (value != null) { var type = value.GetType(); if (type != pi.PropertyType.Type.GetImplementationType()) { WriteTypeQualifier(writer, GetSerializationInfo(type, true)); } } else { throw new NotImplementedException(); } Serialize(value, writer, pi, false, pi.IdentificationMode, context); writer.WriteEndElement(); } } } }
public override object Resolve(string id, ITypeSerializationInfo type, Type minType = null, bool failOnConflict = true, object source = null) { if (string.IsNullOrEmpty(id)) { return(null); } var match = colonRegex.Match(id); if (match.Success) { id = id.Substring(match.Length); } Uri uri; IModelElement resolved = null; int hashIndex = id.IndexOf('#'); if (hashIndex != -1) { if (hashIndex == 0) { resolved = Model.Resolve(id); } else if (Uri.TryCreate(id, UriKind.Absolute, out uri)) { resolved = Repository.Resolve(uri); } else { if (Model.ModelUri != null) { var newUri = new Uri(Model.ModelUri, id); resolved = Repository.Resolve(newUri); } else { throw new NotImplementedException(); } } } else { resolved = Model.Resolve(id); } if (resolved != null) { if (failOnConflict) { if ((minType != null && minType.IsInstanceOfType(resolved)) || type.IsInstanceOf(resolved)) { return(resolved); } else { throw new InvalidOperationException($"The model element with the uri {id} has not the expected type {type} but is a {resolved.GetType().Name} instead."); } } else { return(resolved); } } var baseResolved = base.Resolve(id, type, minType, failOnConflict, source); if (baseResolved != null) { return(baseResolved); } if (Model.ModelUri != null && Model.ModelUri.IsAbsoluteUri && Model.ModelUri.IsFile) { var fileUri = new Uri(Model.ModelUri, id); if (System.IO.File.Exists(fileUri.AbsolutePath)) { return(Repository.Resolve(fileUri)); } } return(null); }
protected override void InitializeElementProperties(System.Xml.XmlReader reader, ref object obj, ITypeSerializationInfo info, XmlSerializationContext context) { int currentDepth = reader.Depth; while (reader.Depth < currentDepth || reader.Read()) { if (reader.Depth == currentDepth) { break; } else if (reader.Depth < currentDepth) { return; } if (reader.NodeType == XmlNodeType.Element) { var found = false; foreach (IPropertySerializationInfo p in info.ElementProperties) { if (IsPropertyElement(reader, p)) { ReadElementFromProperty(reader, obj, context, p); found = true; break; } } if (!found) { foreach (IPropertySerializationInfo p in info.AttributeProperties) { if (IsPropertyElement(reader, p)) { ReadElementFromProperty(reader, obj, context, p); found = true; break; } } if (!found) { OnUnknownElement(new UnknownElementEventArgs(obj, reader.ReadOuterXml())); } } } else if ((reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.CDATA)) { if (info.DefaultProperty == null) { throw new InvalidOperationException("Simple content unexpected for type " + info.Type.FullName); } InitializePropertyFromText(info.DefaultProperty, obj, reader.Value, context); } } }
protected override void InitializeTypeSerializationInfo(ITypeSerializationInfo serializationInfo) { base.InitializeTypeSerializationInfo(serializationInfo); if (!serializationInfo.IsIdentified && !serializationInfo.IsCollection && serializationInfo is XmlTypeSerializationInfo) { XmlTypeSerializationInfo info = serializationInfo as XmlTypeSerializationInfo; var id = IdAttribute; if (id != null && info != null) { if (!info.AttributeProperties.Contains(id)) { info.DeclaredAttributeProperties.Add(id); } info.IdentifierProperty = id; } } }
protected override bool WriteIdentifiedObject(XmlWriter writer, object obj, XmlIdentificationMode identificationMode, ITypeSerializationInfo info, XmlSerializationContext context) { if (identificationMode == XmlIdentificationMode.Identifier) { var model = context.Root as Model; if (model != null) { var modelElement = obj as IModelElement; if (modelElement != null) { Uri uri = model.CreateUriForElement(modelElement); if (uri != null) { writer.WriteString(uri.ConvertToString()); return(true); } } } } return(base.WriteIdentifiedObject(writer, obj, identificationMode, info, context)); }
public virtual bool ContainsId(string id, ITypeSerializationInfo type) { if (idStore.ContainsKey(type)) { return idStore[type].ContainsKey(id); } else { return false; } }
/// <inheritdoc /> protected override bool WriteIdentifiedObject(XmlWriter writer, object obj, XmlIdentificationMode identificationMode, ITypeSerializationInfo info, XmlSerializationContext context) { return(false); }
public DynamicReferenceSerializationInfo(IReference reference, ITypeSerializationInfo type) { Reference = reference; PropertyType = type; }