/// <summary> /// Parse the <see cref="JsonElement"/> to a <see cref="IEdmReference"/>. /// </summary> /// <param name="url">The reference Url string.</param> /// <param name="element">The input JSON element.</param> /// <param name="context">The parser context.</param> /// <returns>null or parsed <see cref="IEdmReference"/>.</returns> internal static IEdmReference ParseReference(string url, JsonElement element, JsonParserContext context) { // The value of each reference object is an object. if (!element.ValidateValueKind(JsonValueKind.Object, context)) { return(null); } IList <IEdmInclude> includes = null; IList <IEdmIncludeAnnotations> includeAnnotations = null; element.ParseAsObject(context, (propertyName, propertyValue) => { // The reference object MAY contain the members $Include and $IncludeAnnotations as well as annotations. switch (propertyName) { case "$Include": // The value of $Include is an array. // Array items are objects that MUST contain the member $Namespace and MAY contain the member $Alias. includes = propertyValue.ParseAsArray(context, ParseInclude); break; case "$IncludeAnnotations": // The value of $IncludeAnnotations is an array. // Array items are objects that MUST contain the member $TermNamespace and MAY contain the members $Qualifier and $TargetNamespace. includeAnnotations = propertyValue.ParseAsArray(context, ParseIncludeAnnotations); break; default: // The reference objects MAY contain annotations.However, EdmReference doesn't support annotation. // So, skip the annotation. context.ReportError(EdmErrorCode.UnexpectedElement, Strings.CsdlJsonParser_UnexpectedJsonMember(context.Path, propertyValue.ValueKind)); break; } }); EdmReference edmReference = new EdmReference(new Uri(url, UriKind.RelativeOrAbsolute)); includes.ForEach(i => edmReference.AddInclude(i)); includeAnnotations.ForEach(i => edmReference.AddIncludeAnnotations(i)); return(edmReference); }
/// <summary> /// TODO: use XmlDocumentParser /// </summary> private void ParseReferenceElement() { // read 'Uri' attribute EdmReference result = new EdmReference(new Uri(this.GetAttributeValue(null, CsdlConstants.Attribute_Uri), UriKind.RelativeOrAbsolute)); if (this.reader.IsEmptyElement) { this.reader.Read(); this.edmReferences.Add(result); return; } this.reader.Read(); while (this.reader.NodeType != XmlNodeType.EndElement) { while (this.reader.NodeType == XmlNodeType.Whitespace && this.reader.Read()) { // read white spaces. can be an extension method. } if (this.reader.NodeType != XmlNodeType.Element) { break; } if (this.reader.LocalName == CsdlConstants.Element_Include) { // parse: <edmx:Include Alias="IoTDeviceModel" Namespace="Microsoft.IntelligentSystems.DeviceModel.Vocabulary.V1"/> IEdmInclude tmp = new EdmInclude(this.GetAttributeValue(null, CsdlConstants.Attribute_Alias), this.GetAttributeValue(null, CsdlConstants.Attribute_Namespace)); result.AddInclude(tmp); } else if (this.reader.LocalName == CsdlConstants.Element_IncludeAnnotations) { // parse: <edmx:IncludeAnnotations TermNamespace="org.example.hcm" Qualifier="Tablet" TargetNamespace="com.contoso.Person" /> IEdmIncludeAnnotations tmp = new EdmIncludeAnnotations(this.GetAttributeValue(null, CsdlConstants.Attribute_TermNamespace), this.GetAttributeValue(null, CsdlConstants.Attribute_Qualifier), this.GetAttributeValue(null, CsdlConstants.Attribute_TargetNamespace)); result.AddIncludeAnnotations(tmp); } else if (this.reader.LocalName == CsdlConstants.Element_Annotation) { this.reader.Skip(); this.RaiseError(EdmErrorCode.UnexpectedXmlElement, Edm.Strings.XmlParser_UnexpectedElement(this.reader.LocalName)); continue; } else { this.RaiseError(EdmErrorCode.UnexpectedXmlElement, Edm.Strings.XmlParser_UnexpectedElement(this.reader.LocalName)); } if (!this.reader.IsEmptyElement) { this.reader.Read(); while (this.reader.NodeType == XmlNodeType.Whitespace && this.reader.Read()) { // read white spaces. can be an extension method. } Debug.Assert(this.reader.NodeType == XmlNodeType.EndElement, "The XmlReader should be at the end of element"); } this.reader.Read(); } Debug.Assert(this.reader.NodeType == XmlNodeType.EndElement, "The XmlReader should be at the end of element"); this.reader.Read(); this.edmReferences.Add(result); }