Exemplo n.º 1
0
        private IModelChange CreateListInsertionReference(IModelElement element, Uri absoluteUri, string propertyName, int startingIndex, List <Uri> newItemsUris)
        {
            var collectionType = element.GetType().GetProperty(propertyName).PropertyType;
            var itemType       = GetCollectionItemType(collectionType);

            var genericType = typeof(ListInsertionAssociation <>).MakeGenericType(itemType);

            return((IModelChange)Activator.CreateInstance(genericType, absoluteUri, propertyName, startingIndex, newItemsUris));
        }
Exemplo n.º 2
0
        private IModelChange CreateListInsertionContainment(IModelElement element, Uri absoluteUri, string propertyName, int startingIndex, IList newItems)
        {
            var collectionType = element.GetType().GetProperty(propertyName).PropertyType;
            var itemType       = GetCollectionItemType(collectionType);

            var listType = typeof(List <>).MakeGenericType(itemType);
            var list     = Activator.CreateInstance(listType) as IList;

            foreach (var item in newItems)
            {
                list.Add(item);
            }

            var genericType = typeof(ListInsertionComposition <>).MakeGenericType(itemType);

            return((IModelChange)Activator.CreateInstance(genericType, absoluteUri, propertyName, startingIndex, list));
        }
Exemplo n.º 3
0
        private IModelChange CreatePropertyChange(IModelElement element, Uri absoluteUri, string propertyName, object newValue, Uri newValueUri)
        {
            var propertyType = element.GetType().GetProperty(propertyName).PropertyType;

            if (!propertyType.GetInterfaces().Contains(typeof(IModelElement))) // only model elements can be references
            {
                return(CreatePropertyChangeAttribute(propertyType, absoluteUri, propertyName, newValue));
            }
            else if (GetAllReferences(element.GetClass()).Any(a => a.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase))) // a reference
            {
                return(CreatePropertyChangeReference(propertyType, absoluteUri, propertyName, newValueUri));
            }
            else // a recently created model element
            {
                return(CreatePropertyChangeAttribute(propertyType, absoluteUri, propertyName, newValue));
            }
        }
Exemplo n.º 4
0
        public override object Resolve(string id, Type type, bool exactType = false, 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 (exactType)
                    {
                        if (resolved.GetType() == type)
                        {
                            return(resolved);
                        }
                        else
                        {
                            throw new InvalidOperationException($"The model element with the uri {id} has not the expected type {type.Name} but is a {resolved.GetType().Name} instead.");
                        }
                    }
                    else
                    {
                        if (type.IsInstanceOfType(resolved))
                        {
                            return(resolved);
                        }
                        else
                        {
                            throw new InvalidOperationException($"The model element with the uri {id} has not the expected type {type.Name} but is a {resolved.GetType().Name}.");
                        }
                    }
                }
                else
                {
                    return(resolved);
                }
            }
            return(base.Resolve(id, type, exactType, failOnConflict, source));
        }
Exemplo n.º 5
0
        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);
        }