Пример #1
0
        /// <summary>
        /// Parse CSDL xml doc into CsdlModel, error messages are stored in this.errors.
        /// </summary>
        /// <param name="csdlVersion">The csdlVersion out.</param>
        /// <param name="csdlModel">The CsdlModel out.</param>
        /// <returns>True if succeeded.</returns>
        private bool TryParseCsdlFileToCsdlModel(out Version csdlVersion, out CsdlModel csdlModel)
        {
            csdlVersion = null;
            csdlModel   = null;
            try
            {
                // Advance to root element
                if (this.reader.NodeType != XmlNodeType.Element)
                {
                    while (this.reader.Read() && this.reader.NodeType != XmlNodeType.Element)
                    {
                    }
                }

                // There must be a root element for all current artifacts
                if (this.reader.EOF)
                {
                    this.RaiseEmptyFile();
                    return(false);
                }

                if (this.reader.LocalName != CsdlConstants.Element_Edmx ||
                    !CsdlConstants.SupportedEdmxNamespaces.TryGetValue(this.reader.NamespaceURI, out csdlVersion))
                {
                    this.RaiseError(EdmErrorCode.UnexpectedXmlElement, Edm.Strings.XmlParser_UnexpectedRootElement(this.reader.Name, CsdlConstants.Element_Edmx));
                    return(false);
                }

                this.ParseEdmxElement(csdlVersion);
                IEnumerable <EdmError> err;
                if (!this.csdlParser.GetResult(out csdlModel, out err))
                {
                    this.errors.AddRange(err);
                    if (this.HasIntolerableError())
                    {
                        return(false);
                    }
                }
            }
            catch (XmlException e)
            {
                this.errors.Add(new EdmError(new CsdlLocation(this.source, e.LineNumber, e.LinePosition), EdmErrorCode.XmlError, e.Message));
                return(false);
            }

            csdlModel.AddCurrentModelReferences(this.edmReferences);
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Parse CSDL-JSON doc into CsdlModel, error messages are stored in <see cref="JsonParserContext"/>
        /// </summary>
        /// <param name="jsonReader">The JSON reader.</param>
        /// <param name="context">The parser context.</param>
        /// <returns>Null or parsed <see cref="CsdlModel"/>.</returns>
        internal static CsdlModel ParseCsdlDocument(ref Utf8JsonReader jsonReader, JsonParserContext context)
        {
            Debug.Assert(context != null);

            JsonDocument jsonDocument = GetJsonDocument(ref jsonReader, context);

            if (jsonDocument == null)
            {
                return(null);
            }

            // make sure to dispose the JsonDocument.
            using (jsonDocument)
            {
                JsonElement rootElement = jsonDocument.RootElement;

                // A CSDL JSON document consists of a single JSON object.
                if (!rootElement.ValidateValueKind(JsonValueKind.Object, context))
                {
                    return(null);
                }

                // This document object MUST contain the member $Version.
                Version version = rootElement.ProcessRequiredProperty("$Version", context, ParseVersion);
                if (version == null)
                {
                    return(null);
                }

                CsdlModel csdlModel = new CsdlModel
                {
                    CsdlVersion = version
                };

                IList <IEdmReference> references = null;
                rootElement.ParseAsObject(context, (propertyName, propertyValue) =>
                {
                    switch (propertyName)
                    {
                    case "$Version":
                        // skip, because processed
                        break;

                    case "$EntityContainer":
                        // The value of $EntityContainer is the namespace-qualified name of the entity container of that service.
                        // So far, i don't know how to use it. So skip it.
                        break;

                    case "$Reference":
                        // The document object MAY contain the member $Reference to reference other CSDL documents.
                        references = ParseReferences(propertyValue, context);
                        break;

                    default:
                        // CSDL document also MAY contain members for schemas.
                        // Each schema's value is an object.
                        if (propertyValue.ValueKind == JsonValueKind.Object)
                        {
                            CsdlSchema schema = SchemaJsonParser.ParseCsdlSchema(propertyName, csdlModel.CsdlVersion, propertyValue, context);
                            if (schema != null)
                            {
                                csdlModel.AddSchema(schema);
                                break;
                            }
                        }

                        context.ReportError(EdmErrorCode.UnexpectedElement, Strings.CsdlJsonParser_UnexpectedJsonMember(context.Path, propertyValue.ValueKind));
                        break;
                    }
                });


                if (references != null)
                {
                    csdlModel.AddCurrentModelReferences(references);
                }

                return(csdlModel);
            }
        }