public async Task <IXDocumentHandler> ConnectAsync(IDocumentId documentId) { var pageHandler = new OneNoteDocumentHandler(documentId, this.oneNoteApp, this.logger); await pageHandler.LoadPageContentAsync(); return(pageHandler); }
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); }
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); }
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}."); } }
public void Init(IDocumentProviderInfo documentProvider, IDocumentId documentId) { paragraphParser.Init(documentProvider, docParseContext); docParseContext.Init(documentId); docParseContext.EnterHierarchyElement(ElementType.Root); }
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); }
public bool DocumentIsEmpty(IDocumentId documentId) { if (documentId == null) { return(true); } return(DocumentSize(documentId) == 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}'"); }
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() ?? ""}'"); }
public OneNoteDocumentHandler( IDocumentId documentId, IOneNoteAppWrapper oneNoteApp, ILogger logger) { this.logger = logger; this.oneNoteApp = oneNoteApp; DocumentId = documentId; }
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)); }
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); }
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); }
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; } }
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); }
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| )([^<])", "$1 $3"); // todo: разобраться, нужно ли это сейчас } if (xml != null) { return(XDocument.Parse(xml)); } throw new NotSupportedException(documentId.GetType().Name); }
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); }
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); }
public INamedStream SharedReadDocument(IDocumentId documentId) { return(OpenLazyDocument(documentId, FileAccess.Read, FileShare.Read)); }
public WordDocumentHandler(IDocumentId documentId) { DocumentId = documentId; }
public string DocumentName(IDocumentId documentId) { return(GetDocumentInfo(documentId).Name); }
public IDocumentParser Create(IDocumentProviderInfo documentProvider, IDocumentId documentId) { this.documentParser.Init(documentProvider, documentId); return(documentParser); }
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(); }
public void RenameDocument(IDocumentId documentId, string name) { var documentInfo = GetDocumentInfo(documentId); documentInfo.Name = name; }
public INamedStream ExclusiveReadWriteDocument(IDocumentId documentId) { return(OpenLazyDocument(documentId, FileAccess.ReadWrite, FileShare.None)); }
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)); }
public HtmlDocumentHandler(IDocumentId documentId) { DocumentId = documentId; }