예제 #1
0
        /// <summary>
        /// Provides example code for the Load(XmlReader) method
        /// </summary>
        public static void LoadXmlReaderExample()
        {
            #region Load(XmlReader reader)
            BlogMLDocument document = new BlogMLDocument();

            using (Stream stream = new FileStream("BlogMLDocument.xml", FileMode.Open, FileAccess.Read))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments   = true;
                settings.IgnoreWhitespace = true;

                using (XmlReader reader = XmlReader.Create(stream, settings))
                {
                    document.Load(reader);

                    foreach (BlogMLPost post in document.Posts)
                    {
                        if (post.ApprovalStatus == BlogMLApprovalStatus.Approved)
                        {
                            //  Perform some processing on the blog post
                        }
                    }
                }
            }
            #endregion
        }
예제 #2
0
        public async Task <bool> Import(
            int userId,
            string fileName,
            int blogRootNode,
            bool overwrite,
            string regexMatch,
            string regexReplace,
            bool publishAll,
            bool exportDisqusXml  = false,
            bool importFirstImage = false)
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException("File not found: " + fileName);
                }

                var root = _contentService.GetById(blogRootNode);
                if (root == null)
                {
                    throw new InvalidOperationException("No node found with id " + blogRootNode);
                }
                if (!root.ContentType.Alias.InvariantEquals("Articulate"))
                {
                    throw new InvalidOperationException("The node with id " + blogRootNode + " is not an Articulate root node");
                }

                using (var stream = File.OpenRead(fileName))
                {
                    var document = new BlogMLDocument();
                    document.Load(stream);

                    stream.Position = 0;
                    var xdoc = XDocument.Load(stream);

                    var authorIdsToName = ImportAuthors(userId, root, document.Authors);

                    var imported = await ImportPosts(userId, xdoc, root, document.Posts, document.Authors.ToArray(), document.Categories.ToArray(), authorIdsToName, overwrite, regexMatch, regexReplace, publishAll, importFirstImage);

                    if (exportDisqusXml)
                    {
                        var xDoc = _disqusXmlExporter.Export(imported, document);

                        using (var memStream = new MemoryStream())
                        {
                            xDoc.Save(memStream);
                            _fileSystem.AddFile("DisqusXmlExport.xml", memStream, true);
                        }
                    }
                }

                return(false);
            }
            catch (Exception ex)
            {
                _logger.Error <BlogMlImporter>(ex, "Importing failed with errors");
                return(true);
            }
        }
예제 #3
0
        private BlogMLDocument GetDocument(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("File not found: " + fileName);
            }

            using (var stream = File.OpenRead(fileName))
            {
                var document = new BlogMLDocument();
                document.Load(stream);
                return(document);
            }
        }
예제 #4
0
        /// <summary>
        /// Provides example code for the Load(Uri, ICredentials, IWebProxy) method
        /// </summary>
        public static void LoadUriExample()
        {
            BlogMLDocument document = new BlogMLDocument();
            Uri            source   = new Uri("http://www.example.org/blog/blogML.axd");

            document.Load(source, CredentialCache.DefaultNetworkCredentials, null);

            foreach (BlogMLPost post in document.Posts)
            {
                if (post.ApprovalStatus == BlogMLApprovalStatus.Approved)
                {
                    //  Perform some processing on the blog post
                }
            }
        }
예제 #5
0
        /// <summary>
        /// Provides example code for the Load(IXPathNavigable) method
        /// </summary>
        public static void LoadIXPathNavigableExample()
        {
            XPathDocument source = new XPathDocument("http://www.example.org/blog/blogML.axd");

            BlogMLDocument document = new BlogMLDocument();

            document.Load(source);

            foreach (BlogMLPost post in document.Posts)
            {
                if (post.ApprovalStatus == BlogMLApprovalStatus.Approved)
                {
                    //  Perform some processing on the blog post
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Provides example code for the Load(Stream) method
        /// </summary>
        public static void LoadStreamExample()
        {
            BlogMLDocument document = new BlogMLDocument();

            using (Stream stream = new FileStream("BlogMLDocument.xml", FileMode.Open, FileAccess.Read))
            {
                document.Load(stream);

                foreach (BlogMLPost post in document.Posts)
                {
                    if (post.ApprovalStatus == BlogMLApprovalStatus.Approved)
                    {
                        //  Perform some processing on the blog post
                    }
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Instantiates a <see cref="ISyndicationResource"/> that conforms to the specified <see cref="SyndicationContentFormat"/> using the supplied <see cref="Stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> used to load the syndication resource.</param>
        /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration value that indicates the type syndication resource the <paramref name="stream"/> represents.</param>
        /// <returns>
        ///     An <see cref="ISyndicationResource"/> object that conforms to the specified <paramref name="format"/>, initialized using the supplied <paramref name="stream"/>.
        ///     If the <paramref name="format"/> is not supported by the provider, returns a <b>null</b> reference.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception>
        private static ISyndicationResource BuildResource(SyndicationContentFormat format, Stream stream)
        {
            Guard.ArgumentNotNull(stream, "stream");

            if (format == SyndicationContentFormat.Apml)
            {
                ApmlDocument document = new ApmlDocument();
                document.Load(stream);
                return(document);
            }
            else if (format == SyndicationContentFormat.Atom)
            {
                XPathDocument  document  = new XPathDocument(stream);
                XPathNavigator navigator = document.CreateNavigator();
                navigator.MoveToRoot();
                navigator.MoveToChild(XPathNodeType.Element);

                if (String.Compare(navigator.LocalName, "entry", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    AtomEntry entry = new AtomEntry();
                    entry.Load(navigator);
                    return(entry);
                }
                else if (String.Compare(navigator.LocalName, "feed", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    AtomFeed feed = new AtomFeed();
                    feed.Load(navigator);
                    return(feed);
                }
                else
                {
                    return(null);
                }
            }
            else if (format == SyndicationContentFormat.BlogML)
            {
                BlogMLDocument document = new BlogMLDocument();
                document.Load(stream);
                return(document);
            }
            else if (format == SyndicationContentFormat.Opml)
            {
                OpmlDocument document = new OpmlDocument();
                document.Load(stream);
                return(document);
            }
            else if (format == SyndicationContentFormat.Rsd)
            {
                RsdDocument document = new RsdDocument();
                document.Load(stream);
                return(document);
            }
            else if (format == SyndicationContentFormat.Rss)
            {
                RssFeed feed = new RssFeed();
                feed.Load(stream);
                return(feed);
            }
            else
            {
                return(null);
            }
        }
예제 #8
0
        public async Task Import(
            int userId,
            string fileName,
            int blogRootNode,
            bool overwrite,
            string regexMatch,
            string regexReplace,
            bool publishAll,
            bool exportDisqusXml = false)
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    throw new FileNotFoundException("File not found: " + fileName);
                }

                var root = _applicationContext.Services.ContentService.GetById(blogRootNode);
                if (root == null)
                {
                    throw new InvalidOperationException("No node found with id " + blogRootNode);
                }
                if (!root.ContentType.Alias.InvariantEquals("Articulate"))
                {
                    throw new InvalidOperationException("The node with id " + blogRootNode + " is not an Articulate root node");
                }

                using (var stream = File.OpenRead(fileName))
                {
                    var document = new BlogMLDocument();
                    document.Load(stream);

                    stream.Position = 0;
                    var xdoc = XDocument.Load(stream);

                    var authorIdsToName = ImportAuthors(userId, root, document.Authors);

                    var imported = await ImportPosts(userId, xdoc, root, document.Posts, document.Authors.ToArray(), document.Categories.ToArray(), authorIdsToName, overwrite, regexMatch, regexReplace, publishAll);

                    if (exportDisqusXml)
                    {
                        var exporter = new DisqusXmlExporter();
                        var xDoc     = exporter.Export(imported, document);

                        using (var memStream = new MemoryStream())
                        {
                            xDoc.Save(memStream);

                            var mediaFs = FileSystemProviderManager.Current.GetFileSystemProvider <MediaFileSystem>();

                            mediaFs.AddFile("Articulate/DisqusXmlExport.xml", memStream, true);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HasErrors = true;
                LogHelper.Error <BlogMlImporter>("Importing failed with errors", ex);
            }
        }