Ejemplo n.º 1
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);
        }