示例#1
0
        /// <summary>
        /// Add namespace prefix for any names of the element in metadata document.
        /// </summary>
        /// <param name="targetName">The target element's name.</param>
        /// <param name="appliesType">The applies type of the element. (Related to the element's local-name.)</param>
        /// <param name="metadataDoc">The metadata document.</param>
        /// <returns>Returns the name with the namespace of the element.</returns>
        public static string AddNamespace(this string targetName, AppliesToType appliesType)
        {
            if (string.IsNullOrEmpty(targetName))
            {
                return(string.Empty);
            }

            string   target   = targetName.GetLastSegment();
            XElement metadata = XElement.Parse(ServiceStatus.GetInstance().MetadataDocument);
            string   xpath    = string.Format(@"//*[local-name()='{0}' and @Name='{1}']", appliesType, target);
            var      element  = metadata.XPathSelectElement(xpath, ODataNamespaceManager.Instance);

            if (null == element)
            {
                return(string.Empty);
            }

            AliasNamespacePair aliasNamespace = element.GetAliasAndNamespace();

            if (string.IsNullOrEmpty(aliasNamespace.Namespace))
            {
                return(string.Empty);
            }

            return(string.Format("{0}.{1}", aliasNamespace.Namespace, target));
        }
示例#2
0
 /// <summary>
 /// Initialize the Term class.
 /// </summary>
 /// <param name="aliasNamespace">The alias and namespace pair.</param>
 /// <param name="name">The name of the term.</param>
 /// <param name="type">The type of the term.</param>
 /// <param name="appliesTo">The element is applied by the term.</param>
 /// <param name="defaultValue">The default value of the term.</param>
 public TermElement(AliasNamespacePair aliasNamespace, string name, string type, string[] appliesTo, object defaultValue)
 {
     this.Name         = name;
     this.Alias        = aliasNamespace.Alias;
     this.Namespace    = aliasNamespace.Namespace;
     this.Type         = type;
     this.AppliesTo    = appliesTo;
     this.DefaultValue = defaultValue;
 }
        /// <summary>
        /// Whether the json property is function or action property.
        /// </summary>
        /// <param name="FucOrActNames">The function or action names from metadata</param>
        /// <param name="jProp">The json property</param>
        /// <param name="metadataSchema">The metadata Schema XML element.</param>
        /// <returns>true if function or action property; false otherwise</returns>
        public static bool isFunctionOrActionProperty(List <string> FucOrActNames, JProperty jProp, XElement metadataSchema)
        {
            bool isImportName = false;

            AliasNamespacePair aliasNamespacePair = MetadataHelper.GetAliasAndNamespace(metadataSchema);

            foreach (string name in FucOrActNames)
            {
                if (!string.IsNullOrEmpty(name) &&
                    (jProp.Name.Equals("#" + aliasNamespacePair.Alias + "." + name) ||
                     jProp.Name.Equals("#" + aliasNamespacePair.Namespace + "." + name)))
                {
                    isImportName = true;
                    break;
                }
            }

            return(isImportName);
        }
        /// <summary>
        /// Verify Metadata.Core.4204
        /// </summary>
        /// <param name="context">Service context</param>
        /// <param name="info">out parameter to return violation information when rule fail</param>
        /// <returns>true if rule passes; false otherwise</returns>
        public override bool? Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool? passed = null;
            info = null;

            // Load MetadataDocument into XMLDOM
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(context.MetadataDocument);
            string xpath = "//*[local-name()='Reference']";
            XmlNodeList refNodeList = xmlDoc.SelectNodes(xpath);

            // Add all included reference namespace alias pair in aliasNamespacePairList.
            foreach (XmlNode reference in refNodeList)
            {
                foreach (XmlNode child in reference.ChildNodes)
                {
                    if (child.Attributes == null) continue; // the comment nodes do not contain Attributes collection.
                    if (child.Name.Equals("edmx:Include"))
                    {
                        string namespaceString = string.Empty;
                        string aliasString = string.Empty;
                        if (child.Attributes == null) continue; // the comment nodes do not contain Attributes collection.
                        if (child.Attributes["Namespace"] != null)
                        {
                            namespaceString = child.Attributes["Namespace"].Value;
                        }

                        if (child.Attributes["Alias"] != null)
                        {
                            aliasString = child.Attributes["Alias"].Value;
                        }

                        AliasNamespacePair referenceAliasNamespace = new AliasNamespacePair(aliasString, namespaceString);

                        aliasNamespacePairList.Add(referenceAliasNamespace);
                    }
                }
            }

            XElement metaXml = XElement.Parse(context.MetadataDocument);
            xpath = "//*[local-name()='ComplexType']";
            IEnumerable<XElement> complexTypeElements = metaXml.XPathSelectElements(xpath, ODataNamespaceManager.Instance);

            foreach (XElement complexTypeElement in complexTypeElements)
            {
                passed = true;

                HashSet<string> descendantsSet = new HashSet<string>(StringComparer.Ordinal);

                AliasNamespacePair aliasNameSpace = complexTypeElement.GetAliasAndNamespace();

                if (!string.IsNullOrEmpty(aliasNameSpace.Namespace))
                {
                    descendantsSet.Add(aliasNameSpace.Namespace+ "." + complexTypeElement.Attribute("Name").Value);
                }

                if (!string.IsNullOrEmpty(aliasNameSpace.Alias))
                {
                    descendantsSet.Add(aliasNameSpace.Alias + "." + complexTypeElement.Attribute("Name").Value);
                }

                if (!aliasNamespacePairList.Contains(aliasNameSpace))
                {
                    aliasNamespacePairList.Add(aliasNameSpace);
                }

                if (this.IsComplextTypeBaseDeadCycled(complexTypeElement, context, ref descendantsSet))
                {
                    passed = true;
                }
                else
                {
                    passed = false;
                    info = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, context.ResponsePayload);
                    break;
                }
            }

            return passed;
        }