Ejemplo n.º 1
0
        public void WriteSchema(JSchema schema)
        {
            ValidationUtils.ArgumentNotNull(schema, "schema");

            _knownSchemas.Clear();

            JSchemaDiscovery discovery;

            if (_externalSchemas != null)
            {
                foreach (ExternalSchema externalSchema in _externalSchemas)
                {
                    discovery = new JSchemaDiscovery(_knownSchemas, KnownSchemaState.External);
                    discovery.Discover(externalSchema.Schema, externalSchema.Uri);
                }
            }

            discovery = new JSchemaDiscovery(_knownSchemas, KnownSchemaState.InlinePending);
            discovery.Discover(schema, null);

            KnownSchema rootKnownSchema = _knownSchemas.Single(s => s.Schema == schema);

            rootKnownSchema.State = KnownSchemaState.InlineWritten;

            WriteSchemaInternal(schema);
        }
Ejemplo n.º 2
0
        private void ReferenceOrWriteSchema(JSchema context, JSchema schema, string propertyName, bool isDefinitions = false)
        {
            KnownSchema knownSchema = _knownSchemas.GetByJSchema(schema);

            if (knownSchema == null)
            {
#if DEBUG
                _writer.WriteValue("UNKNOWN SCHEMA: " + schema.ResolvedId);
#endif
                return;
            }

            if (propertyName != null)
            {
                _writer.WritePropertyName(propertyName);
            }

            if (ShouldWriteReference(knownSchema, isDefinitions))
            {
                Uri reference = ResolveReference(context, knownSchema);

                WriteReferenceObject(reference);
                return;
            }

            knownSchema.State = KnownSchemaState.Written;

            WriteSchemaInternal(schema);
        }
Ejemplo n.º 3
0
        private bool ShouldWriteReference(KnownSchema knownSchema, bool isDefinitions)
        {
            if (knownSchema.State == KnownSchemaState.DefinitionPending)
            {
                // only write schemas found in dependencies in the dependencies object
                // otherwise write reference
                return(!isDefinitions);
            }

            if (knownSchema.State != KnownSchemaState.InlinePending)
            {
                if (_referenceHandling == JSchemaWriterReferenceHandling.Always)
                {
                    return(true);
                }

                bool isRecursive = _schemaStack.Contains(knownSchema.Schema);

                if (isRecursive)
                {
                    if (_referenceHandling == JSchemaWriterReferenceHandling.Auto)
                    {
                        return(true);
                    }

                    throw new JSchemaException("Error writing schema because writing schema references has been disabled and the schema contains a circular reference.");
                }

                return(false);
            }

            return(false);
        }
Ejemplo n.º 4
0
        public void WriteSchema(JSchema schema)
        {
            ValidationUtils.ArgumentNotNull(schema, nameof(schema));

            _rootSchema = schema;

            _knownSchemas.Clear();

            JSchemaDiscovery discovery;

            if (_externalSchemas != null)
            {
                foreach (ExternalSchema externalSchema in _externalSchemas)
                {
                    discovery = new JSchemaDiscovery(schema, _knownSchemas, KnownSchemaState.External);
                    discovery.Discover(externalSchema.Schema, externalSchema.Uri);
                }
            }

            if (_version == SchemaVersion.Unset)
            {
                _version = SchemaVersionHelpers.MapSchemaUri(schema.SchemaVersion);
            }

            discovery = new JSchemaDiscovery(schema, _knownSchemas, KnownSchemaState.InlinePending);
            discovery.Discover(schema, null);

            KnownSchema rootKnownSchema = _knownSchemas.Single(s => s.Schema == schema);

            rootKnownSchema.State = KnownSchemaState.Written;

            WriteSchemaInternal(schema);
        }
Ejemplo n.º 5
0
        private Uri ResolveReference(JSchema context, KnownSchema knownSchema)
        {
            KnownSchema currentKnownSchema = _knownSchemas.GetByJSchema(context);

            Uri reference;

            // Id is fully qualified
            // make it relative to the current schema
            if (currentKnownSchema.Id.IsAbsoluteUri)
            {
                if (currentKnownSchema.Id.IsBaseOf(knownSchema.Id))
                {
                    reference = currentKnownSchema.Id.MakeRelativeUri(knownSchema.Id);

                    // MakeRelativeUri escapes the result, need to unescape
                    reference = new Uri(Uri.UnescapeDataString(reference.OriginalString), UriKind.RelativeOrAbsolute);
                }
                else if (knownSchema.Id == currentKnownSchema.Id)
                {
                    reference = new Uri("#", UriKind.RelativeOrAbsolute);
                }
                else
                {
                    reference = knownSchema.Id;
                }
            }
            else
            {
                reference = knownSchema.Id;
            }

            return(reference);
        }
Ejemplo n.º 6
0
        private void PopulateSchemaId(ValidationError error)
        {
            Uri schemaId = null;

            for (int i = 0; i < Schema.KnownSchemas.Count; i++)
            {
                KnownSchema s = Schema.KnownSchemas[i];
                if (s.Schema == error.Schema)
                {
                    schemaId = s.Id;
                    break;
                }
            }

            error.SchemaId = schemaId;

            // property getter will lazy create a new list
            // don't create child error list unneeded
            if (error._childErrors != null)
            {
                for (int i = 0; i < error._childErrors.Count; i++)
                {
                    ValidationError validationError = error._childErrors[i];
                    PopulateSchemaId(validationError);
                }
            }
        }
Ejemplo n.º 7
0
        private bool ShouldWriteReference(KnownSchema knownSchema)
        {
            if (knownSchema.State != KnownSchemaState.InlinePending)
            {
                if (_referenceHandling == JSchemaWriterReferenceHandling.Always)
                {
                    return(true);
                }

                bool isRecursive = _schemaStack.Contains(knownSchema.Schema);

                if (isRecursive)
                {
                    if (_referenceHandling == JSchemaWriterReferenceHandling.Auto)
                    {
                        return(true);
                    }

                    throw new JSchemaException("Error writing schema because writing schema references has been disabled and the schema contains a circular reference.");
                }

                return(false);
            }

            return(false);
        }
Ejemplo n.º 8
0
        private void ReferenceOrWriteSchema(JSchema context, JSchema schema, string propertyName)
        {
            KnownSchema knownSchema = _knownSchemas.SingleOrDefault(s => s.Schema == schema);

            if (knownSchema == null)
            {
                return;
            }

            if (propertyName != null)
            {
                _writer.WritePropertyName(propertyName);
            }

            if (knownSchema.State != KnownSchemaState.InlinePending)
            {
                KnownSchema currentKnownSchema = _knownSchemas.Single(s => s.Schema == context);

                Uri reference;

                // Id is fully qualified
                // make it relative to the current schema
                if (currentKnownSchema.Id.IsAbsoluteUri)
                {
                    if (currentKnownSchema.Id.IsBaseOf(knownSchema.Id))
                    {
                        reference = currentKnownSchema.Id.MakeRelativeUri(knownSchema.Id);

                        // MakeRelativeUri escapes the result, need to unescape
                        reference = new Uri(Uri.UnescapeDataString(reference.OriginalString), UriKind.RelativeOrAbsolute);
                    }
                    else if (knownSchema.Id == currentKnownSchema.Id)
                    {
                        reference = new Uri("#", UriKind.RelativeOrAbsolute);
                    }
                    else
                    {
                        reference = knownSchema.Id;
                    }
                }
                else
                {
                    reference = knownSchema.Id;
                }

                WriteReferenceObject(reference);
                return;
            }

            knownSchema.State = KnownSchemaState.InlineWritten;

            WriteSchemaInternal(schema);
        }
        private void ReferenceOrWriteSchema(JSchema schema)
        {
            KnownSchema knownSchema = _knownSchemas.Single(s => s.Schema == schema);

            if (knownSchema.State != KnownSchemaState.InlinePending)
            {
                WriteReferenceObject(knownSchema.Id);
                return;
            }

            knownSchema.State = KnownSchemaState.InlineWritten;

            WriteSchemaInternal(schema);
        }
Ejemplo n.º 10
0
        private void WriteReference(JSchema context, JSchema schema, string propertyName)
        {
            KnownSchema knownSchema = _knownSchemas.GetByJSchema(schema);

            if (knownSchema == null)
            {
                return;
            }

            if (propertyName != null)
            {
                _writer.WritePropertyName(propertyName);
            }

            Uri reference = ResolveReference(context, knownSchema);

            _writer.WriteValue(reference);
        }
Ejemplo n.º 11
0
        public void RaiseValidationErrors()
        {
            if (_validationErrors != null)
            {
                _schemaDiscovery.Discover(RootSchema, null);

                foreach (ValidationError error in _validationErrors)
                {
                    KnownSchema knownSchema = _schemaDiscovery.KnownSchemas.SingleOrDefault(s => s.Schema == error.Schema);
                    if (knownSchema != null)
                    {
                        error.SchemaId = knownSchema.Id;
                    }

                    _validationEventHandler(this, new SchemaValidationEventArgs(error));
                }

                _validationErrors.Clear();
            }
        }