Beispiel #1
0
 private static void ValidateContentTypeProvider(IContentTypeProvider provider)
 {
     if (provider == null)
     {
         throw new InvalidContentTypeProviderException("External content type provider cannot be null");
     }
     if (!provider.CanRead && !provider.CanWrite)
     {
         throw new InvalidContentTypeProviderException($"Provider '{provider.GetType().RESTarTypeName()}' cannot read or write");
     }
 }
        public TContentData Deserialize <TContentData>(string filePath) where TContentData : class, IContentData
        {
            using (var fileStream = File.OpenRead(filePath))
            {
                Type contentType;

                // leave stream open so that the next streamReader can read the fileStream.
                using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, 1024, leaveOpen: true))
                {
                    var            eventReader = GetEventReader(streamReader);
                    YamlLayoutData layoutData;

                    try
                    {
                        // deserialize to YamlLayoutData so that we can find out which Layout to use.
                        layoutData = _yamlDeserializer.Deserialize <YamlLayoutData>(eventReader);
                    }
                    catch (YamlException ex)
                    {
                        throw new FrontMatterException("YAML Front Matter is missing or could not be parsed from the document: \r\n" + (ex.InnerException != null ? ex.InnerException.Message : ex.Message) + "\r\n\r\nFile: " + filePath);
                    }

                    contentType = layoutData != null && !string.IsNullOrEmpty(layoutData.Layout) ?
                                  _contentTypeProvider.GetType(layoutData.Layout) :
                                  typeof(ContentData);
                }

                fileStream.Seek(0, SeekOrigin.Begin);

                using (var streamReader = new StreamReader(fileStream))
                {
                    var eventReader = GetEventReader(streamReader);

                    var contentData = (TContentData)_yamlDeserializer.Deserialize(eventReader, contentType ?? typeof(ContentData));

                    if (contentData == null)
                    {
                        contentData = (TContentData)(IContentData) new ContentData();
                    }

                    contentData.Content = streamReader.ReadToEnd();

                    return(contentData);
                }
            }
        }
 private void ValidateContentTypeProvider(IContentTypeProvider provider)
 {
     if (provider == null)
     {
         throw new InvalidContentTypeProviderException("External content type provider cannot be null");
     }
     if (!provider.CanRead && !provider.CanWrite)
     {
         throw new InvalidContentTypeProviderException($"Content type provider '{provider.GetType().GetRESTableTypeName()}' can't read nor write. A content type provider " +
                                                       "must be able to either read or write.");
     }
 }