internal static ClassificationTypeDefinition OperatorClassificationDefinition = null; // Set via MEF

        #endregion

        #region IDlrClassifierProvider

        public IClassifier GetClassifier(ITextBuffer buffer)
        {
            if (_categoryMap == null)
            {
                _categoryMap = FillCategoryMap(_classificationRegistry);
            }

            Genero4glClassifier res = null;

            if (buffer != null && buffer.Properties != null)
            {
                if (!buffer.Properties.TryGetProperty <Genero4glClassifier>(typeof(Genero4glClassifier), out res) &&
                    ContentTypes != null &&
                    ContentTypes.Any(x => x != null &&
                                     buffer.ContentType != null &&
                                     buffer.ContentType.IsOfType(x.TypeName)))
                {
                    res = new Genero4glClassifier(this, buffer);
                    buffer.Properties.AddProperty(typeof(Genero4glClassifier), res);
                }
            }

            if (VSGeneroPackage.Instance != null)
            {
                if (VSGeneroPackage.Instance.GlobalFunctionProvider == null)
                {
                    VSGeneroPackage.Instance.GlobalFunctionProvider = _PublicFunctionProvider;
                }

                if (VSGeneroPackage.Instance.ProgramFileProvider == null)
                {
                    VSGeneroPackage.Instance.ProgramFileProvider = _ProgramFileProvider;
                }

                if (VSGeneroPackage.Instance.GlobalDatabaseProvider == null)
                {
                    VSGeneroPackage.Instance.GlobalDatabaseProvider = _DatabaseInfoProvider;
                }

                if (VSGeneroPackage.Instance.BuildTaskProvider == null)
                {
                    VSGeneroPackage.Instance.BuildTaskProvider = _BuildTaskProvider;
                }

                if (VSGeneroPackage.Instance.CommentValidators == null)
                {
                    VSGeneroPackage.Instance.CommentValidators = _CommentValidators;
                }
            }

            if (res == null)
            {
                return(GetClassifier());
            }
            return(res);
        }
        /// <summary>
        /// Verifies the semantic rule
        /// </summary>
        /// <param name="context">The Interop service context</param>
        /// <param name="info">out parameter to return violation information when rule does not pass</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");
            }

            info = null;
            bool?passed = false;

            string contentType = context.ResponseHttpHeaders.GetHeaderValue("Content-Type");

            if (!string.IsNullOrEmpty(contentType))
            {
                string mediaType = null;
                string subType   = null;

                string[] parts = contentType.Split(';');
                if (parts.Length >= 1)
                {
                    mediaType = parts[0].Trim().ToLowerInvariant();

                    for (int i = 1; i < parts.Length; i++)
                    {
                        string[] pair = parts[i].Split(new char[] { '=' }, 2);
                        if (pair.Length == 2)
                        {
                            if (pair[0].Equals("type", StringComparison.OrdinalIgnoreCase))
                            {
                                subType = pair[1].ToLowerInvariant();
                                break;
                            }
                        }
                    }
                }

                passed = ContentTypes.Any(x => mediaType.Equals(x, StringComparison.Ordinal));
                if (passed.Value)
                {
                    if (!string.IsNullOrEmpty(subType) && mediaType.Equals("application/atom+xml", StringComparison.Ordinal))
                    {
                        passed = ODataSubtypes.Any(x => subType.Equals(x, StringComparison.Ordinal));
                    }
                }
            }

            if (!passed.Value)
            {
                info = new ExtensionRuleViolationInfo(this.ErrorMessage, context.Destination, contentType);
            }

            return(passed);
        }
Beispiel #3
0
        private void SaveRelationships(OpcRelationships relationships)
        {
            if (!ContentTypes.Any(ct => string.Equals(ct.Extension, "rels", StringComparison.OrdinalIgnoreCase)))
            {
                ContentTypes.Add(new OpcContentType("rels", OpcKnownMimeTypes.OpenXmlRelationship, OpcContentTypeMode.Default));
            }
            var path  = relationships.DocumentUri.ToPackagePath();
            var entry = Archive.GetEntry(path) ?? Archive.CreateEntry(path);

            using (var stream = entry.Open())
            {
                stream.SetLength(0L);
                var newXml = relationships.ToXml();
                newXml.Save(stream, SaveOptions.None);
            }
        }
Beispiel #4
0
        public virtual IOutputFormatter SelectFormatter(
            OutputFormatterContext formatterContext,
            IEnumerable <IOutputFormatter> formatters)
        {
            var logger = formatterContext.HttpContext.RequestServices.GetRequiredService <ILogger <ObjectResult> >();

            // Check if any content-type was explicitly set (for example, via ProducesAttribute
            // or Url path extension mapping). If yes, then ignore content-negotiation and use this content-type.
            if (ContentTypes.Count == 1)
            {
                logger.LogVerbose(
                    "Skipped content negotiation as content type '{ContentType}' is explicitly set for the response.",
                    ContentTypes[0]);

                return(SelectFormatterUsingAnyAcceptableContentType(formatterContext,
                                                                    formatters,
                                                                    ContentTypes));
            }

            var sortedAcceptHeaderMediaTypes = GetSortedAcceptHeaderMediaTypes(formatterContext);

            IOutputFormatter selectedFormatter = null;

            if (ContentTypes == null || ContentTypes.Count == 0)
            {
                // Check if we have enough information to do content-negotiation, otherwise get the first formatter
                // which can write the type.
                MediaTypeHeaderValue requestContentType = null;
                MediaTypeHeaderValue.TryParse(
                    formatterContext.HttpContext.Request.ContentType,
                    out requestContentType);
                if (!sortedAcceptHeaderMediaTypes.Any() && requestContentType == null)
                {
                    logger.LogVerbose("No information found on request to perform content negotiation.");

                    return(SelectFormatterBasedOnTypeMatch(formatterContext, formatters));
                }

                //
                // Content-Negotiation starts from this point on.
                //

                // 1. Select based on sorted accept headers.
                if (sortedAcceptHeaderMediaTypes.Any())
                {
                    selectedFormatter = SelectFormatterUsingSortedAcceptHeaders(
                        formatterContext,
                        formatters,
                        sortedAcceptHeaderMediaTypes);
                }

                // 2. No formatter was found based on accept headers, fall back on request Content-Type header.
                if (selectedFormatter == null && requestContentType != null)
                {
                    selectedFormatter = SelectFormatterUsingAnyAcceptableContentType(
                        formatterContext,
                        formatters,
                        new[] { requestContentType });
                }

                // 3. No formatter was found based on Accept and request Content-Type headers, so
                // fallback on type based match.
                if (selectedFormatter == null)
                {
                    logger.LogVerbose("Could not find an output formatter based on content negotiation.");

                    // Set this flag to indicate that content-negotiation has failed to let formatters decide
                    // if they want to write the response or not.
                    formatterContext.FailedContentNegotiation = true;

                    return(SelectFormatterBasedOnTypeMatch(formatterContext, formatters));
                }
            }
            else
            {
                if (sortedAcceptHeaderMediaTypes.Any())
                {
                    // Filter and remove accept headers which cannot support any of the user specified content types.
                    var filteredAndSortedAcceptHeaders = sortedAcceptHeaderMediaTypes
                                                         .Where(acceptHeader =>
                                                                ContentTypes.Any(contentType =>
                                                                                 contentType.IsSubsetOf(acceptHeader)));

                    selectedFormatter = SelectFormatterUsingSortedAcceptHeaders(
                        formatterContext,
                        formatters,
                        filteredAndSortedAcceptHeaders);
                }

                if (selectedFormatter == null)
                {
                    // Either there were no acceptHeaders that were present OR
                    // There were no accept headers which matched OR
                    // There were acceptHeaders which matched but there was no formatter
                    // which supported any of them.
                    // In any of these cases, if the user has specified content types,
                    // do a last effort to find a formatter which can write any of the user specified content type.
                    selectedFormatter = SelectFormatterUsingAnyAcceptableContentType(
                        formatterContext,
                        formatters,
                        ContentTypes);
                }
            }

            return(selectedFormatter);
        }
Beispiel #5
0
        public virtual IOutputFormatter SelectFormatter(OutputFormatterContext formatterContext,
                                                        IEnumerable <IOutputFormatter> formatters)
        {
            var incomingAcceptHeaderMediaTypes = formatterContext.ActionContext.HttpContext.Request.GetTypedHeaders().Accept ??
                                                 new MediaTypeHeaderValue[] { };

            // By default we want to ignore considering accept headers for content negotiation when
            // they have a media type like */* in them. Browsers typically have these media types.
            // In these cases we would want the first formatter in the list of output formatters to
            // write the response. This default behavior can be changed through options, so checking here.
            var options = formatterContext.ActionContext.HttpContext
                          .RequestServices
                          .GetRequiredService <IOptions <MvcOptions> >()
                          .Options;

            var respectAcceptHeader = true;

            if (options.RespectBrowserAcceptHeader == false &&
                incomingAcceptHeaderMediaTypes.Any(mediaType => mediaType.MatchesAllTypes))
            {
                respectAcceptHeader = false;
            }

            IEnumerable <MediaTypeHeaderValue> sortedAcceptHeaderMediaTypes = null;

            if (respectAcceptHeader)
            {
                sortedAcceptHeaderMediaTypes = SortMediaTypeHeaderValues(incomingAcceptHeaderMediaTypes)
                                               .Where(header => header.Quality != HeaderQuality.NoMatch);
            }

            IOutputFormatter selectedFormatter = null;

            if (ContentTypes == null || ContentTypes.Count == 0)
            {
                if (respectAcceptHeader)
                {
                    // Select based on sorted accept headers.
                    selectedFormatter = SelectFormatterUsingSortedAcceptHeaders(
                        formatterContext,
                        formatters,
                        sortedAcceptHeaderMediaTypes);
                }

                if (selectedFormatter == null)
                {
                    var requestContentType = formatterContext.ActionContext.HttpContext.Request.ContentType;

                    // No formatter found based on accept headers, fall back on request contentType.
                    MediaTypeHeaderValue incomingContentType = null;
                    MediaTypeHeaderValue.TryParse(requestContentType, out incomingContentType);

                    // In case the incomingContentType is null (as can be the case with get requests),
                    // we need to pick the first formatter which
                    // can support writing this type.
                    var contentTypes = new[] { incomingContentType };
                    selectedFormatter = SelectFormatterUsingAnyAcceptableContentType(
                        formatterContext,
                        formatters,
                        contentTypes);

                    // This would be the case when no formatter could write the type base on the
                    // accept headers and the request content type. Fallback on type based match.
                    if (selectedFormatter == null)
                    {
                        foreach (var formatter in formatters)
                        {
                            var supportedContentTypes = formatter.GetSupportedContentTypes(
                                formatterContext.DeclaredType,
                                formatterContext.Object?.GetType(),
                                contentType: null);

                            if (formatter.CanWriteResult(formatterContext, supportedContentTypes?.FirstOrDefault()))
                            {
                                return(formatter);
                            }
                        }
                    }
                }
            }
            else if (ContentTypes.Count == 1)
            {
                // There is only one value that can be supported.
                selectedFormatter = SelectFormatterUsingAnyAcceptableContentType(
                    formatterContext,
                    formatters,
                    ContentTypes);
            }
            else
            {
                if (respectAcceptHeader)
                {
                    // Filter and remove accept headers which cannot support any of the user specified content types.
                    var filteredAndSortedAcceptHeaders = sortedAcceptHeaderMediaTypes
                                                         .Where(acceptHeader =>
                                                                ContentTypes
                                                                .Any(contentType =>
                                                                     contentType.IsSubsetOf(acceptHeader)));

                    selectedFormatter = SelectFormatterUsingSortedAcceptHeaders(
                        formatterContext,
                        formatters,
                        filteredAndSortedAcceptHeaders);
                }

                if (selectedFormatter == null)
                {
                    // Either there were no acceptHeaders that were present OR
                    // There were no accept headers which matched OR
                    // There were acceptHeaders which matched but there was no formatter
                    // which supported any of them.
                    // In any of these cases, if the user has specified content types,
                    // do a last effort to find a formatter which can write any of the user specified content type.
                    selectedFormatter = SelectFormatterUsingAnyAcceptableContentType(
                        formatterContext,
                        formatters,
                        ContentTypes);
                }
            }

            return(selectedFormatter);
        }
Beispiel #6
0
        public virtual IOutputFormatter SelectFormatter(OutputFormatterContext formatterContext,
                                                        IEnumerable <IOutputFormatter> formatters)
        {
            var incomingAcceptHeader = HeaderParsingHelpers.GetAcceptHeaders(
                formatterContext.ActionContext.HttpContext.Request.Accept);
            var sortedAcceptHeaders = SortMediaTypeWithQualityHeaderValues(incomingAcceptHeader)
                                      .Where(header => header.Quality != HttpHeaderUtilitites.NoMatch)
                                      .ToArray();

            IOutputFormatter selectedFormatter = null;

            if (ContentTypes == null || ContentTypes.Count == 0)
            {
                // Select based on sorted accept headers.
                selectedFormatter = SelectFormatterUsingSortedAcceptHeaders(
                    formatterContext,
                    formatters,
                    sortedAcceptHeaders);

                if (selectedFormatter == null)
                {
                    var requestContentType = formatterContext.ActionContext.HttpContext.Request.ContentType;

                    // No formatter found based on accept headers, fall back on request contentType.
                    MediaTypeHeaderValue incomingContentType = null;
                    MediaTypeHeaderValue.TryParse(requestContentType, out incomingContentType);

                    // In case the incomingContentType is null (as can be the case with get requests),
                    // we need to pick the first formatter which
                    // can support writing this type.
                    var contentTypes = new[] { incomingContentType };
                    selectedFormatter = SelectFormatterUsingAnyAcceptableContentType(
                        formatterContext,
                        formatters,
                        contentTypes);

                    // This would be the case when no formatter could write the type base on the
                    // accept headers and the request content type. Fallback on type based match.
                    if (selectedFormatter == null)
                    {
                        foreach (var formatter in formatters)
                        {
                            var supportedContentTypes = formatter.GetSupportedContentTypes(
                                formatterContext.DeclaredType,
                                formatterContext.Object?.GetType(),
                                contentType: null);

                            if (formatter.CanWriteResult(formatterContext, supportedContentTypes?.FirstOrDefault()))
                            {
                                return(formatter);
                            }
                        }
                    }
                }
            }
            else if (ContentTypes.Count == 1)
            {
                // There is only one value that can be supported.
                selectedFormatter = SelectFormatterUsingAnyAcceptableContentType(
                    formatterContext,
                    formatters,
                    ContentTypes);
            }
            else
            {
                // Filter and remove accept headers which cannot support any of the user specified content types.
                var filteredAndSortedAcceptHeaders = sortedAcceptHeaders
                                                     .Where(acceptHeader =>
                                                            ContentTypes
                                                            .Any(contentType =>
                                                                 contentType.IsSubsetOf(acceptHeader)))
                                                     .ToArray();

                if (filteredAndSortedAcceptHeaders.Length > 0)
                {
                    selectedFormatter = SelectFormatterUsingSortedAcceptHeaders(
                        formatterContext,
                        formatters,
                        filteredAndSortedAcceptHeaders);
                }

                if (selectedFormatter == null)
                {
                    // Either there were no acceptHeaders that were present OR
                    // There were no accept headers which matched OR
                    // There were acceptHeaders which matched but there was no formatter
                    // which supported any of them.
                    // In any of these cases, if the user has specified content types,
                    // do a last effort to find a formatter which can write any of the user specified content type.
                    selectedFormatter = SelectFormatterUsingAnyAcceptableContentType(
                        formatterContext,
                        formatters,
                        ContentTypes);
                }
            }

            return(selectedFormatter);
        }