public static Uri ResolveSchemaIdAndScopeId(Uri idScope, Uri schemaId, string path, out Uri newScope)
        {
            Uri knownSchemaId;

            if (schemaId != null)
            {
                newScope = SchemaDiscovery.ResolveSchemaId(idScope, schemaId);

                knownSchemaId = newScope;
            }
            else
            {
                if (idScope == null)
                {
                    knownSchemaId = new Uri(path, UriKind.RelativeOrAbsolute);
                }
                else
                {
                    knownSchemaId = SchemaDiscovery.ResolveSchemaId(idScope, new Uri(path, UriKind.RelativeOrAbsolute));
                }

                newScope = idScope;
            }

            return(knownSchemaId);
        }
Exemple #2
0
        private void DiscoverInternal(JSchema schema, string latestPath)
        {
            if (schema.Reference != null)
            {
                return;
            }

            if (_knownSchemas.Any(s => s.Schema == schema))
            {
                return;
            }

            string currentPath;

            if (schema.Id == null)
            {
                Uri currentScopeId = _pathStack.First().Id;
                currentPath = string.Join("/", _pathStack.Where(p => p.Id == currentScopeId && !string.IsNullOrEmpty(p.Path)).Select(p => p.Path));

                if (!string.IsNullOrEmpty(currentPath))
                {
                    currentPath += "/";
                }

                currentPath += latestPath;
            }
            else
            {
                latestPath  = "#";
                currentPath = "#";
            }

            Uri newScopeId;
            Uri schemaKnownId = SchemaDiscovery.ResolveSchemaIdAndScopeId(_pathStack.First().Id, schema.Id, currentPath, out newScopeId);

            _knownSchemas.Add(new KnownSchema(schemaKnownId, schema, _state));

            _pathStack.Push(new SchemaPath(newScopeId, latestPath));

            // discover should happen in the same order as writer except extension data (e.g. definitions)
            if (schema._extensionData != null)
            {
                foreach (KeyValuePair <string, JToken> valuePair in schema._extensionData)
                {
                    DiscoverTokenSchemas(valuePair.Key, valuePair.Value);
                }
            }

            DiscoverSchema(Constants.PropertyNames.AdditionalProperties, schema.AdditionalProperties);
            DiscoverSchema(Constants.PropertyNames.AdditionalItems, schema.AdditionalItems);
            DiscoverDictionarySchemas(Constants.PropertyNames.Properties, schema._properties);
            DiscoverDictionarySchemas(Constants.PropertyNames.PatternProperties, schema._patternProperties);
            DiscoverArraySchemas(Constants.PropertyNames.Items, schema._items);
            DiscoverArraySchemas(Constants.PropertyNames.AllOf, schema._allOf);
            DiscoverArraySchemas(Constants.PropertyNames.AnyOf, schema._anyOf);
            DiscoverArraySchemas(Constants.PropertyNames.OneOf, schema._oneOf);
            DiscoverSchema(Constants.PropertyNames.Not, schema.Not);

            _pathStack.Pop();
        }
        private Uri GetSchemaIdAndNewScopeId(JSchema schema, ref string latestPath, out Uri newScopeId)
        {
            string currentPath;

            if (schema.Id == null)
            {
                Uri currentScopeId = _pathStack.First().Id;
                currentPath = StringHelpers.Join("/", _pathStack.Where(p => p.Id == currentScopeId && !string.IsNullOrEmpty(p.Path)).Reverse().Select(p => p.Path));

                if (!string.IsNullOrEmpty(currentPath))
                {
                    currentPath += "/";
                }

                currentPath += latestPath;
            }
            else
            {
                latestPath  = "#";
                currentPath = "#";
            }

            Uri schemaKnownId = SchemaDiscovery.ResolveSchemaIdAndScopeId(_pathStack.First().Id, schema.Id, currentPath, out newScopeId);

            return(schemaKnownId);
        }
        private Uri GetSchemaIdAndNewScopeId(JSchema schema, ref string latestPath, out Uri newScopeId)
        {
            Uri currentScopeId = _pathStack.First().Id;

            string currentPath;

            if (schema.Id == null)
            {
                currentPath = StringHelpers.Join("/", _pathStack.Where(p => p.Id == currentScopeId && !string.IsNullOrEmpty(p.Path)).Reverse().Select(p => p.Path));

                if (!string.IsNullOrEmpty(currentScopeId.OriginalString) &&
                    currentPath != "#" &&
                    !currentPath.StartsWith("#/", StringComparison.Ordinal))
                {
                    currentPath = "#/" + currentPath;
                }

                if (!string.IsNullOrEmpty(currentPath) &&
                    !currentPath.EndsWith('/'))
                {
                    currentPath += "/";
                }

                currentPath += latestPath;
            }
            else
            {
                bool parentHash = _pathStack.Any(p => p.Id == currentScopeId && p.Path != null && p.Path.IndexOf('#') != -1);
                if (parentHash)
                {
                    latestPath  = string.Empty;
                    currentPath = string.Empty;
                }
                else
                {
                    latestPath  = "#";
                    currentPath = "#";
                }
            }

            Uri schemaKnownId = SchemaDiscovery.ResolveSchemaIdAndScopeId(_pathStack.First().Id, schema.Id, currentPath, out newScopeId);

            return(schemaKnownId);
        }
Exemple #5
0
        public static Uri ResolveSchemaIdAndScopeId(Uri idScope, JSchema schema, string path, out Uri newScope)
        {
            Uri schemaId = schema.Id;
            Uri knownSchemaId;

            if (schemaId != null)
            {
                try
                {
                    newScope = SchemaDiscovery.ResolveSchemaId(idScope, schemaId);
                }
                catch (Exception ex)
                {
                    string message = "Error resolving schema ID '{0}' in the current scope. The resolved ID must be a valid URI.".FormatWith(CultureInfo.InvariantCulture, schemaId);

                    throw new JSchemaException(JSchemaException.FormatMessage(schema, schema.Path, message), ex);
                }

                knownSchemaId = newScope;
            }
            else
            {
                if (idScope == null)
                {
                    knownSchemaId = new Uri(path, UriKind.RelativeOrAbsolute);
                }
                else
                {
                    knownSchemaId = SchemaDiscovery.ResolveSchemaId(idScope, new Uri(path, UriKind.RelativeOrAbsolute));
                }

                newScope = idScope;
            }

            return(knownSchemaId);
        }