public async Task <IXDocumentHandler> ConnectAsync(IDocumentId documentId)
        {
            var pageHandler = new OneNoteDocumentHandler(documentId, this.oneNoteApp, this.logger);
            await pageHandler.LoadPageContentAsync();

            return(pageHandler);
        }
Exemplo n.º 2
0
        private void OnCloseArenaDeleteDocument(IDocumentId document)
        {
            var documentPath = document.DocumentId;

            ExceptionHelper.ExceptionCatcher(() => File.Delete(documentPath),
                                             where : $"Error while disposing arena {document.Arena}. Probably documentId '{document.DocumentId}' still opened because some stream not disposed.");
        }
        public async Task <IWordDocumentHandler> ConnectAsync(IDocumentId documentId)
        {
            var pageHandler = new WordDocumentHandler(documentId);

            pageHandler.LoadPageContent();
            return(pageHandler);
        }
Exemplo n.º 4
0
        public IDocumentId MakeDocument(IContentManagerArenaId arena, string filePath,
                                        FileUsingMode mode = FileUsingMode.AutoDetect, bool deleteSourceFile = false, string fileName = null)
        {
            if (arena is null)
            {
                throw new ArgumentNullException(nameof(arena));
            }
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentException(nameof(filePath));
            }
            if (!File.Exists(filePath))
            {
                throw new ArgumentException($"{nameof(filePath)} = '{filePath}' does not exists.");
            }
            fileName = fileName ?? Path.GetFileName(filePath);
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentException(nameof(fileName) + " is empty.");
            }
            if (mode == FileUsingMode.AutoDetect)
            {
                mode = PathIsNetworkPath(filePath) ? FileUsingMode.CopySourceFile : FileUsingMode.UseSourceFile;
            }
            IDocumentId          documentId      = null;
            IContentProvider     contentProvider = null;
            Action <IDocumentId> onCloseArena    = OnCloseArenaDeleteDocument;

            switch (mode)
            {
            case FileUsingMode.UseSourceFile:
            {
                if (!deleteSourceFile)
                {
                    onCloseArena = null;
                }
                documentId = new DocumentIdImpl(arena, filePath);
            } break;

            case FileUsingMode.CopySourceFile:
            {
                var arenaFilePath = Path.GetTempFileName();
                if (deleteSourceFile)
                {
                    File.Move(filePath, arenaFilePath, true);
                    documentId = new DocumentIdImpl(arena, arenaFilePath);
                }
                else
                {
                    contentProvider = new FileContentProvider(contentDestinationPath => File.Copy(filePath, contentDestinationPath, true), () => File.Length(filePath));
                    documentId      = new DocumentIdImpl(arena, arenaFilePath);
                }
            } break;

            default:
                throw new NotImplementedException($"{nameof(mode)}={mode}");
            }
            AddDocumentInfo(documentId, new DocumentInfo(fileName, onCloseArena, contentProvider));
            return(documentId);
        }
Exemplo n.º 5
0
 private void AddDocumentInfo(ArenaInfo arenaInfo, IDocumentId documentId, DocumentInfo documentInfo)
 {
     if (!arenaInfo.TryAdd(documentId, documentInfo))
     {
         throw new Exception($"Content manager document id {documentId} duplicated in arena {documentId.Arena}.");
     }
 }
Exemplo n.º 6
0
        public void Init(IDocumentProviderInfo documentProvider, IDocumentId documentId)
        {
            paragraphParser.Init(documentProvider, docParseContext);

            docParseContext.Init(documentId);
            docParseContext.EnterHierarchyElement(ElementType.Root);
        }
Exemplo n.º 7
0
        public async Task <IHtmlDocumentHandler> ConnectAsync(IDocumentId documentId)
        {
            var pageConnector = new HtmlDocumentHandler(documentId);
            await pageConnector.LoadPageContentAsync();

            return(pageConnector);
        }
        private static async Task <HtmlDocument> ReadDocumentAsync(IDocumentId documentId)
        {
            string fileContent = null;

            if (documentId is FileDocumentId fileDocumentId)
            {
                var filePath = fileDocumentId.FilePath;
                fileContent = File.ReadAllText(filePath);

                if (!StringUtils.ContainsHtml(fileContent))
                {
                    fileContent = string.Concat(
                        fileContent.Split(new string[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries)
                        .Select(p => $"<p>{p}</p>"));

                    documentId.SetReadonly();
                }
            }
            else if (documentId is WebDocumentId webDocumentId)
            {
                throw new NotImplementedException();  // todo async?
            }
            else
            {
                throw new NotSupportedException(documentId.GetType().Name);
            }

            var htmlDoc = new HtmlDocument();

            htmlDoc.LoadHtml(fileContent);
            return(htmlDoc);
        }
Exemplo n.º 9
0
 public bool DocumentIsEmpty(IDocumentId documentId)
 {
     if (documentId == null)
     {
         return(true);
     }
     return(DocumentSize(documentId) == 0);
 }
Exemplo n.º 10
0
 private DocumentInfo GetDocumentInfo(ArenaInfo arenaInfo, IDocumentId documentId)
 {
     if (arenaInfo.TryGetValue(documentId, out var currentDocumentInfo))
     {
         return(currentDocumentInfo);
     }
     throw new ArgumentException($"Not found document id '{documentId}' on arena '{documentId.Arena}'");
 }
Exemplo n.º 11
0
 private ArenaInfo GetArenaInfo(IContentManagerArenaId arena, IDocumentId documentId = null /*for logging only*/)
 {
     if (Arenas.TryGetValue(arena, out var currentArenaInfo))
     {
         return(currentArenaInfo);
     }
     throw new ArgumentException($"Working with disposed arena {arena} for document id '{documentId?.ToString() ?? ""}'");
 }
Exemplo n.º 12
0
        public OneNoteDocumentHandler(
            IDocumentId documentId,
            IOneNoteAppWrapper oneNoteApp,
            ILogger logger)
        {
            this.logger     = logger;
            this.oneNoteApp = oneNoteApp;

            DocumentId = documentId;
        }
Exemplo n.º 13
0
        private static WordprocessingDocument ReadDocument(IDocumentId documentId)
        {
            if (!(documentId is FileDocumentId fileDocumentId))
            {
                throw new NotSupportedException(documentId.GetType().Name);
            }

            var filePath = fileDocumentId.FilePath;

            return(WordprocessingDocument.Open(filePath, !documentId.IsReadonly));
        }
Exemplo n.º 14
0
        private INamedStream OpenLazyDocument(IDocumentId documentId, FileAccess access, FileShare share)
        {
            var documentInfo = GetDocumentInfo(documentId);
            var result       = new Document
            {
                Name   = documentInfo.Name,
                Stream = new LazyConstructableStream(
                    streamFactory: () => OpenDocumentStream(documentId, access, share),
                    optimizedSizeGetterWithoutStreamCreating: () => documentInfo.ContentProvider?.OptimizedContentLengthProvider)
            };

            return(result);
        }
Exemplo n.º 15
0
        public void Init()
        {
            this.documentParseContext = new DocumentParseContext();

            base.Init(services => services.AddScoped(sp => documentParseContext));

            this.documentProvider = new MockDocumentProviderInfo(ServiceProvider.GetService <IVerseLinkService>())
            {
                IsReadonly = true
            };
            this.documentParserFactory = ServiceProvider.GetService <IDocumentParserFactory>();

            this.mockDocumentId = new FileDocumentId(0, null, true);
        }
Exemplo n.º 16
0
        private Stream OpenDocumentStream(IDocumentId documentId, FileAccess access, FileShare share)
        {
            GetDocumentInfo(documentId).ContentProvider?.ContentPreparation(documentId.DocumentId);
            var file = File.Open(documentId.DocumentId, FileMode.Open, access, share);

            try
            {
                return(SmartLowMemoryStream.Open(file));
            }
            catch
            {
                file?.Dispose();
                throw;
            }
        }
Exemplo n.º 17
0
        private static XDocument ReadDocument(IDocumentId documentId)
        {
            string xml = null;

            if (documentId is FileDocumentId)
            {
                var filePath = ((FileDocumentId)documentId).FilePath;
                var ext      = Path.GetExtension(filePath);
                xml = File.ReadAllText(filePath);
            }

            if (xml != null)
            {
                return(XDocument.Parse(xml));
            }

            throw new NotSupportedException(documentId.GetType().Name);
        }
Exemplo n.º 18
0
        private async Task <XDocument> ReadDocumentAsync(IDocumentId documentId)
        {
            string xml = null;

            if (documentId is OneNoteDocumentId)
            {
                xml = await this.oneNoteApp.GetPageContentAsync(((OneNoteDocumentId)documentId).PageId);

                //html = Regex.Replace(html, "([^>])(\\n|&nbsp;)([^<])", "$1 $3");      // todo: разобраться, нужно ли это сейчас
            }

            if (xml != null)
            {
                return(XDocument.Parse(xml));
            }

            throw new NotSupportedException(documentId.GetType().Name);
        }
Exemplo n.º 19
0
        public async Task <DocumentParseResult> ParseDocumentAsync(IDocumentId documentId)
        {
            DocumentParseResult result;

            await using (var docHandler = await oneNoteDocumentConnector.ConnectAsync(documentId))
            {
                using (var docParser = documentParserFactory.Create(this, documentId))
                {
                    ParseNode(docParser, docHandler.Document.Root);
                    result = docParser.DocumentParseResult;
                }

                if (result.IsValuable)
                {
                    docHandler.SetDocumentChanged();
                }
            }

            return(result);
        }
Exemplo n.º 20
0
        public async Task <DocumentParseResult> ParseDocumentAsync(IDocumentId documentId)
        {
            DocumentParseResult result;

            await using (var docHandler = await wordDocumentConnector.ConnectAsync(documentId))
            {
                using (var docParser = documentParserFactory.Create(this, documentId))
                {
                    ParseNode(docParser, docHandler.WordDocument.MainDocumentPart.Document.Body);
                    result = docParser.DocumentParseResult;
                }

                if (result.IsValuable && !documentId.IsReadonly)
                {
                    docHandler.SetDocumentChanged();
                }
            }

            return(result);
        }
Exemplo n.º 21
0
 public INamedStream SharedReadDocument(IDocumentId documentId)
 {
     return(OpenLazyDocument(documentId, FileAccess.Read, FileShare.Read));
 }
Exemplo n.º 22
0
 public WordDocumentHandler(IDocumentId documentId)
 {
     DocumentId = documentId;
 }
Exemplo n.º 23
0
 public string DocumentName(IDocumentId documentId)
 {
     return(GetDocumentInfo(documentId).Name);
 }
Exemplo n.º 24
0
 public IDocumentParser Create(IDocumentProviderInfo documentProvider, IDocumentId documentId)
 {
     this.documentParser.Init(documentProvider, documentId);
     return(documentParser);
 }
Exemplo n.º 25
0
 public bool Equals(IDocumentId other)
 {
     return((!(other is null)) && Tuple.Create(other.DocumentId, other.Arena).Equals(Tuple.Create(DocumentId, Arena)));
 }
 public void Init(IDocumentId documentId)
 {
     DocumentId          = documentId;
     DocumentParseResult = new DocumentParseResult();
 }
Exemplo n.º 27
0
        public void RenameDocument(IDocumentId documentId, string name)
        {
            var documentInfo = GetDocumentInfo(documentId);

            documentInfo.Name = name;
        }
Exemplo n.º 28
0
 public INamedStream ExclusiveReadWriteDocument(IDocumentId documentId)
 {
     return(OpenLazyDocument(documentId, FileAccess.ReadWrite, FileShare.None));
 }
Exemplo n.º 29
0
 public INamedStream MakeWritableEmptyDocument(IContentManagerArenaId arena, string name, out IDocumentId documentId)
 {
     using (var empty = System.IO.Stream.Null)
         documentId = MakeDocument(arena, new Document()
         {
             Name = name, Stream = empty
         });
     return(ExclusiveReadWriteDocument(documentId));
 }
Exemplo n.º 30
0
 public HtmlDocumentHandler(IDocumentId documentId)
 {
     DocumentId = documentId;
 }