/// <summary>
        /// Parse a XElement type parameter to a Property type instance.
        /// </summary>
        /// <param name="propertyValueElement">A PropertyValue element in metadata document.</param>
        /// <param name="complexTypeElement">A related ComplexType element in metadata document.</param>
        /// <returns>Return a Property type instance.</returns>
        public static ODataProperty Parse(XElement propertyValueElement, ComplexTypeElement complexTypeElement)
        {
            if (null == propertyValueElement || null == complexTypeElement)
            {
                throw new ArgumentNullException("The value of parameter 'propertyValueElement' or 'complexTypeElement' cannot be null.");
            }

            // Set the property name.
            string propName = null != propertyValueElement.Attribute("Property") ?
                              propertyValueElement.Attribute("Property").Value :
                              null;

            // Set the property type.
            string propType = null;

            foreach (var prop in complexTypeElement.Properties)
            {
                if (prop.Name == propName)
                {
                    propType = prop.Type;
                    break;
                }
            }

            if (null == propType)
            {
                throw new NullReferenceException("The property type does not contain in the input parameter 'complexTypeElement'.");
            }

            // Set the property value.
            object propVal = null;

            if (propType.StartsWith("Collection("))
            {
                string subType = propType.RemoveCollectionFlag().RemoveEdmDotPrefix();

                if (string.Empty != propertyValueElement.Value)
                {
                    string        xPath    = string.Format("./*[local-name()='Collection']/*[local-name()='{0}']", subType);
                    var           elements = propertyValueElement.XPathSelectElements(xPath, ODataNamespaceManager.Instance).ToList();
                    List <string> vals     = new List <string>();
                    elements.ForEach(e =>
                    {
                        vals.Add(e.Value);
                    });
                    propVal = vals;
                }
            }
            else
            {
                propVal = ODataProperty.GetPropertyValue(propertyValueElement, propType);
            }

            return(new ODataProperty(propName, propVal, propType));
        }
Пример #2
0
        /// <summary>
        /// Parse a XElement type parameter to a Property type instance.
        /// </summary>
        /// <param name="propertyValueElement">A PropertyValue element in metadata document.</param>
        /// <param name="complexTypeElement">A related ComplexType element in metadata document.</param>
        /// <returns>Return a Property type instance.</returns>
        public static ODataProperty Parse(XElement propertyValueElement, ComplexTypeElement complexTypeElement)
        {
            if (null == propertyValueElement || null == complexTypeElement)
            {
                throw new ArgumentNullException("The value of parameter 'propertyValueElement' or 'complexTypeElement' cannot be null.");
            }

            // Set the property name.
            string propName = null != propertyValueElement.Attribute("Property") ?
                propertyValueElement.Attribute("Property").Value :
                null;

            // Set the property type.
            string propType = null;

            foreach (var prop in complexTypeElement.Properties)
            {
                if (prop.Name == propName)
                {
                    propType = prop.Type;
                    break;
                }
            }

            if (null == propType)
            {
                throw new NullReferenceException("The property type does not contain in the input parameter 'complexTypeElement'.");
            }

            // Set the property value.
            object propVal = null;

            if (propType.StartsWith("Collection("))
            {
                string subType = propType.RemoveCollectionFlag().RemoveEdmDotPrefix();

                if (string.Empty != propertyValueElement.Value)
                {
                    string xPath = string.Format("./*[local-name()='Collection']/*[local-name()='{0}']", subType);
                    var elements = propertyValueElement.XPathSelectElements(xPath, ODataNamespaceManager.Instance).ToList();
                    List<string> vals = new List<string>();
                    elements.ForEach(e =>
                    {
                        vals.Add(e.Value);
                    });
                    propVal = vals;
                }
            }
            else
            {
                propVal = ODataProperty.GetPropertyValue(propertyValueElement, propType);
            }

            return new ODataProperty(propName, propVal, propType);
        }
        /// <summary>
        /// Gets the supportive feature information of all the metadata elements.
        /// Note: This method is only used to validate all the terms with complex type.
        /// </summary>
        /// <param name="targetShortName">The short name of target element.</param>
        /// <param name="termElement">The term information which will be validated.</param>
        /// <param name="complexTypeElement">The complex type template.</param>
        /// <param name="metadataDoc">The metadata document.</param>
        /// <returns>Returns the validation.</returns>
        public static ODataComplexType GetSupportiveFeatureInfo(string targetShortName, TermElement termElement, ComplexTypeElement complexTypeElement, string metadataDoc)
        {
            if (string.IsNullOrEmpty(targetShortName) || null == termElement || null == complexTypeElement || !metadataDoc.IsXmlPayload())
            {
                return null;
            }

            ODataComplexType complexType = new ODataComplexType(complexTypeElement.Name);
            var metadata = XElement.Parse(metadataDoc);
            string aliasTermName = string.Format("{0}.{1}", termElement.Alias, termElement.Name);
            string namespaceTermName = string.Format("{0}.{1}", termElement.Namespace, termElement.Name);

            for (int i = 0; i < termElement.AppliesTo.Length; i++)
            {
                #region Gets the inside annotation.
                // Gets the specified annotation which is defined in entity-set element.
                string xPath = string.Format("//*[local-name()='{0}' and @Name='{1}']/*[local-name()='Annotation'][@Term='{2}' or @Term='{3}']",
                        termElement.AppliesTo[i],
                        targetShortName,
                        aliasTermName,
                        namespaceTermName);
                var annotationInside = metadata.XPathSelectElement(xPath, ODataNamespaceManager.Instance);
                #endregion

                #region Gets the outside annotation.
                // Gets the specified annotation which is defined in annotations element.
                XElement annotationOutside = null;
                xPath = string.Format("//*[local-name()='{0}' and @Name='{1}']", termElement.AppliesTo[i], targetShortName);
                var entitySetElement = metadata.XPathSelectElement(xPath, ODataNamespaceManager.Instance);

                if (null != entitySetElement)
                {
                    var localAliasNamespace = MetadataHelper.GetAliasAndNamespace(entitySetElement);

                    xPath = string.Format("//*[local-name()='Annotations'][@Target='{0}' or @Target='{1}' or @Target='{2}']/*[local-name()='Annotation'][@Term='{3}' or @Term='{4}']",
                        targetShortName,
                        string.Format("{0}.{1}", localAliasNamespace.Alias, targetShortName),
                        string.Format("{0}.{1}", localAliasNamespace.Namespace, targetShortName),
                        aliasTermName,
                        namespaceTermName);
                    annotationOutside = metadata.XPathSelectElement(xPath, ODataNamespaceManager.Instance);
                }
                #endregion

                if (null != annotationInside)
                {
                    // Get the annotation information from target element in metadata. (e.g. EntitySet)
                    xPath = ".//*[local-name()='PropertyValue']";
                    var propertyValueElements = annotationInside.XPathSelectElements(xPath, ODataNamespaceManager.Instance);

                    if (null != propertyValueElements && 0 != propertyValueElements.Count())
                    {
                        foreach (var pV in propertyValueElements)
                        {
                            complexType.AddProperty(ODataProperty.Parse(pV, complexTypeElement));
                        }
                    }
                }

                if (null != annotationOutside)
                {
                    // Get the annotation information from Annotations element in metadata.
                    var propertyValueElements = annotationOutside.XPathSelectElements(xPath, ODataNamespaceManager.Instance);

                    if (null != propertyValueElements)
                    {
                        foreach (var pV in propertyValueElements)
                        {
                            complexType.AddProperty(ODataProperty.Parse(pV, complexTypeElement));
                        }
                    }
                }

                if (complexType.GetProperties().Any())
                {
                    return complexType;
                }
            }

            foreach (var prop in complexTypeElement.Properties)
            {
                if (null != prop.DefaultValue && prop.Type.StartsWith("Edm."))
                {
                    complexType.AddProperty(new ODataProperty(prop.Name, prop.DefaultValue, prop.Type));
                }
            }

            return complexType;
        }