protected override void ProcessSchemaElement(IEdmSchemaElement element)
        {
            EdmSchema edmSchema  = null;
            string    @namespace = element.Namespace;

            if (EdmUtil.IsNullOrWhiteSpaceInternal(@namespace))
            {
                @namespace = string.Empty;
            }
            if (!this.modelSchemas.TryGetValue(@namespace, out edmSchema))
            {
                edmSchema = new EdmSchema(@namespace);
                this.modelSchemas.Add(@namespace, edmSchema);
            }
            edmSchema.AddSchemaElement(element);
            this.activeSchema = edmSchema;
            base.ProcessSchemaElement(element);
        }
        /// <summary>
        /// Sets the serialization alias for a given namespace(including current model's schemas namespace-alias, and referenced models' schemas namespace-alias)
        /// TODO: REF make sure no duplicated alias.
        /// </summary>
        /// <param name="model">Model that will be serialized.</param>
        /// <param name="namespaceName">The namespace to set the alias for.</param>
        /// <param name="alias">The alias for that namespace.</param>
        public static void SetNamespaceAlias(this IEdmModel model, string namespaceName, string alias)
        {
            VersioningDictionary <string, string> mappings = model.GetAnnotationValue <VersioningDictionary <string, string> >(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation);

            if (mappings == null)
            {
                mappings = VersioningDictionary <string, string> .Create(string.CompareOrdinal);
            }

            if (EdmUtil.IsNullOrWhiteSpaceInternal(alias))
            {
                string val;
                if (mappings.TryGetValue(namespaceName, out val))
                {
                    mappings = mappings.Remove(namespaceName);
                }
            }
            else
            {
                mappings = mappings.Set(namespaceName, alias);
            }

            model.SetAnnotationValue(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation, mappings);

            var list = model.GetAnnotationValue <VersioningList <string> >(model, EdmConstants.InternalUri, CsdlConstants.UsedNamespacesAnnotation);

            if (list == null)
            {
                list = VersioningList <string> .Create();
            }

            if (!string.IsNullOrEmpty(namespaceName) && !list.Contains(namespaceName))
            {
                list = list.Add(namespaceName);
            }

            model.SetAnnotationValue(model, EdmConstants.InternalUri, CsdlConstants.UsedNamespacesAnnotation, list);
        }
        protected override void ProcessSchemaElement(IEdmSchemaElement element)
        {
            string namespaceName = element.Namespace;

            // Put all of the namespaceless stuff into one schema.
            if (EdmUtil.IsNullOrWhiteSpaceInternal(namespaceName))
            {
                namespaceName = string.Empty;
            }

            EdmSchema schema;

            if (!this.modelSchemas.TryGetValue(namespaceName, out schema))
            {
                schema = new EdmSchema(namespaceName);
                this.modelSchemas.Add(namespaceName, schema);
            }

            schema.AddSchemaElement(element);
            this.activeSchema = schema;

            base.ProcessSchemaElement(element);
        }
Exemplo n.º 4
0
        internal static bool ValidateValueCanBeWrittenAsXmlElementAnnotation(IEdmValue value, string annotationNamespace, string annotationName, out EdmError error)
        {
            IEdmStringValue edmStringValue = value as IEdmStringValue;

            if (edmStringValue == null)
            {
                error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNotIEdmStringValue);
                return(false);
            }

            string rawString = edmStringValue.Value;

            XmlReader reader = XmlReader.Create(new StringReader(rawString));

            try
            {
                // Skip to root element.
                if (reader.NodeType != XmlNodeType.Element)
                {
                    while (reader.Read() && reader.NodeType != XmlNodeType.Element)
                    {
                    }
                }

                // The annotation must be an element.
                if (reader.EOF)
                {
                    error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml);
                    return(false);
                }

                // The root element must corespond to the term of the annotation
                string elementNamespace = reader.NamespaceURI;
                string elementName      = reader.LocalName;

                if (EdmUtil.IsNullOrWhiteSpaceInternal(elementNamespace) || EdmUtil.IsNullOrWhiteSpaceInternal(elementName))
                {
                    error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNullNamespaceOrName);
                    return(false);
                }

                if (!((annotationNamespace == null || elementNamespace == annotationNamespace) && (annotationName == null || elementName == annotationName)))
                {
                    error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationMismatchedTerm);
                    return(false);
                }

                // Parse the entire fragment to determine if the XML is valid
                while (reader.Read())
                {
                }

                error = null;
                return(true);
            }
            catch (Exception)
            {
                error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml);
                return(false);
            }
        }
Exemplo n.º 5
0
        private void ProcessNode()
        {
            Debug.Assert(!this.reader.EOF, "ProcessNode should not be called after reader reaches EOF");

            // If this is a text node, accumulate the text and return so that sequences of text nodes are coalesced into a single text value.
            if (this.IsTextNode)
            {
                if (this.currentText == null)
                {
                    this.currentText         = new StringBuilder();
                    this.currentTextLocation = this.Location;
                }

                this.currentText.Append(this.reader.Value);
                return;
            }

            // If this is not a text node and text has been accumulated, set the text on the current parser before control moves to the parser for this new sub-element.
            if (this.currentText != null)
            {
                string       textValue    = this.currentText.ToString();
                CsdlLocation textLocation = this.currentTextLocation;
                this.currentText         = null;
                this.currentTextLocation = default(CsdlLocation);

                if (!EdmUtil.IsNullOrWhiteSpaceInternal(textValue) && !string.IsNullOrEmpty(textValue))
                {
                    this.currentScope.AddChildValue(new XmlTextValue(textLocation, textValue));
                }
            }

            switch (this.reader.NodeType)
            {
            // we ignore these childless elements
            case XmlNodeType.Whitespace:
            case XmlNodeType.XmlDeclaration:
            case XmlNodeType.Comment:
            case XmlNodeType.Notation:
            case XmlNodeType.ProcessingInstruction:
            {
                return;
            }

            // we ignore these elements that can have children
            case XmlNodeType.DocumentType:
            case XmlNodeType.EntityReference:
            {
                this.reader.Skip();
                return;
            }

            case XmlNodeType.Element:
            {
                this.ProcessElement();
                return;
            }

            case XmlNodeType.EndElement:
            {
                this.EndElement();
                return;
            }

            default:
            {
                this.ReportUnexpectedNodeType(this.reader.NodeType);
                this.reader.Skip();
                return;
            }
            }
        }
Exemplo n.º 6
0
        private void ProcessNode()
        {
            if (!this.IsTextNode)
            {
                if (this.currentText != null)
                {
                    string       str          = this.currentText.ToString();
                    CsdlLocation csdlLocation = this.currentTextLocation;
                    this.currentText         = null;
                    this.currentTextLocation = null;
                    if (!EdmUtil.IsNullOrWhiteSpaceInternal(str) && !string.IsNullOrEmpty(str))
                    {
                        this.currentScope.AddChildValue(new XmlTextValue(csdlLocation, str));
                    }
                }
                XmlNodeType nodeType = this.reader.NodeType;
                switch (nodeType)
                {
                case XmlNodeType.Element:
                {
                    this.ProcessElement();
                    return;
                }

                case XmlNodeType.Attribute:
                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                case XmlNodeType.Entity:
                case XmlNodeType.Document:
                case XmlNodeType.DocumentFragment:
                case XmlNodeType.SignificantWhitespace:
                case XmlNodeType.EndEntity:
                {
                    this.ReportUnexpectedNodeType(this.reader.NodeType);
                    this.reader.Skip();
                    return;
                }

                case XmlNodeType.EntityReference:
                case XmlNodeType.DocumentType:
                {
                    this.reader.Skip();
                    return;
                }

                case XmlNodeType.ProcessingInstruction:
                case XmlNodeType.Comment:
                case XmlNodeType.Notation:
                case XmlNodeType.Whitespace:
                case XmlNodeType.XmlDeclaration:
                {
                    return;
                }

                case XmlNodeType.EndElement:
                {
                    this.EndElement();
                    return;
                }

                default:
                {
                    this.ReportUnexpectedNodeType(this.reader.NodeType);
                    this.reader.Skip();
                    return;
                }
                }
            }
            else
            {
                if (this.currentText == null)
                {
                    this.currentText         = new StringBuilder();
                    this.currentTextLocation = this.Location;
                }
                this.currentText.Append(this.reader.Value);
                return;
            }
        }
Exemplo n.º 7
0
        internal static bool ValidateValueCanBeWrittenAsXmlElementAnnotation(IEdmValue value, string annotationNamespace, string annotationName, out EdmError error)
        {
            bool            flag;
            IEdmStringValue edmStringValue = value as IEdmStringValue;

            if (edmStringValue != null)
            {
                string    str       = edmStringValue.Value;
                XmlReader xmlReader = XmlReader.Create(new StringReader(str));
                try
                {
                    if (xmlReader.NodeType != XmlNodeType.Element)
                    {
                        while (xmlReader.Read() && xmlReader.NodeType != XmlNodeType.Element)
                        {
                        }
                    }
                    if (!xmlReader.EOF)
                    {
                        string namespaceURI = xmlReader.NamespaceURI;
                        string localName    = xmlReader.LocalName;
                        if (EdmUtil.IsNullOrWhiteSpaceInternal(namespaceURI) || EdmUtil.IsNullOrWhiteSpaceInternal(localName))
                        {
                            error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNullNamespaceOrName);
                            flag  = false;
                        }
                        else
                        {
                            if ((annotationNamespace == null || namespaceURI == annotationNamespace) && (annotationName == null || localName == annotationName))
                            {
                                while (xmlReader.Read())
                                {
                                }
                                error = null;
                                flag  = true;
                            }
                            else
                            {
                                error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationMismatchedTerm);
                                flag  = false;
                            }
                        }
                    }
                    else
                    {
                        error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml);
                        flag  = false;
                    }
                }
                catch (Exception exception)
                {
                    error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml);
                    flag  = false;
                }
                return(flag);
            }
            else
            {
                error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNotIEdmStringValue);
                return(false);
            }
        }