Skip to content
16 changes: 10 additions & 6 deletions ToxyFramework/Base/BaseTextParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;

namespace Toxy.Base
{
Expand All @@ -20,7 +21,6 @@ private protected BaseTextParser(ParserContext context)
Context = context;
}

#nullable enable
internal virtual void ValidateContext()
{
Utility.ValidateContext(Context);
Expand All @@ -35,15 +35,20 @@ internal virtual void ValidateContext()
public string Parse()
{
ValidateContext();
IDisposable? disposable = null;
Stream stream = Utility.GetStream(Context);
try
{
return ParseText(out disposable);
return ParseText(stream);
}
finally
{
// we don't care if it throws just dispose if needed
disposable?.Dispose();
// Streams passed by the User should not be disposed
// this should be the only place to do so
if (!Context.IsStreamContext)
{
stream.Dispose();
}
}
}

Expand All @@ -53,7 +58,6 @@ public string Parse()
/// <param name="disposable">An optional <see cref="IDisposable"/>, which should be disposed at the end of parsing.
/// It will be disposed if an <see cref="Exception"/> will be thrown.</param>
/// <returns>Returns the extracted Text.</returns>
internal abstract string ParseText(out IDisposable? disposable);
#nullable disable
internal abstract string ParseText(Stream stream);
}
}
110 changes: 54 additions & 56 deletions ToxyFramework/ParserContext.cs
Original file line number Diff line number Diff line change
@@ -1,65 +1,63 @@
using DocumentFormat.OpenXml.InkML;
using DocumentFormat.OpenXml.Math;
using FileSignatures;
using FileSignatures;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Toxy
{
public class ParserContext
{
Stream _filestream;
public ParserContext(string path):this(path, null)
{

}
public ParserContext(string path, Encoding encoding)
{
this.Path = path;
this.Properties = new Dictionary<string, string>();
this.Encoding = encoding;
}
public ParserContext(Stream stream):this(null, Encoding.UTF8)
{
this._filestream = stream;
}

public bool IsStreamContext { get=> this._filestream != null; }
public Stream Stream { get => this._filestream; }
public string Path { get; set; }
public class ParserContext
{
private Stream _filestream;
public bool IsStreamContext { get => _filestream != null; }
public Stream Stream { get => _filestream; }
public string Path { get; set; }

public string Extension
{
get
{
return Utility.GetFileExtention(this);
}
}
public string MimeType
{
get
{
FileFormat? fileformat = null;
FileFormatInspector inspector = new FileFormatInspector();
if (this.IsStreamContext)
{
this.Stream.Position = 0;
fileformat = inspector.DetermineFileFormat(this.Stream);
}
else
{
using var stream = File.OpenRead(this.Path);
fileformat = inspector.DetermineFileFormat(stream);
}
if (fileformat == null)
throw new InvalidDataException("File format could not be determined for the input stream");
return fileformat.MediaType;
public string Extension
{
get
{
return Utility.GetFileExtention(this);
}
}
public string MimeType
{
get
{
FileFormat fileformat = null;
FileFormatInspector inspector = new FileFormatInspector();
if (IsStreamContext)
{
Stream.Position = 0;
fileformat = inspector.DetermineFileFormat(Stream);
}
else
{
using var stream = File.OpenRead(Path);
fileformat = inspector.DetermineFileFormat(stream);
}
if (fileformat == null)
{
throw new InvalidDataException("File format could not be determined for the input stream");
}
return fileformat.MediaType;

}
}
public Encoding Encoding { get; set; }

public Dictionary<string, string> Properties { get; set; }
}
}
}
public Encoding Encoding { get; set; }

public Dictionary<string, string> Properties { get; set; }

public ParserContext(string path) : this(path, null)
{ }
public ParserContext(string path, Encoding encoding)
{
Path = path;
Properties = new Dictionary<string, string>();
Encoding = encoding;
}
public ParserContext(Stream stream) : this(null, Encoding.UTF8)
{
_filestream = stream;
}
}
}
20 changes: 11 additions & 9 deletions ToxyFramework/Parsers/EPUB/EPUBTextParser.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using HtmlAgilityPack;
using DocumentFormat.OpenXml.InkML;
using HtmlAgilityPack;
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Toxy.Base;
using VersOne.Epub;

namespace Toxy.Parsers
{
public sealed class EPUBTextParser : ITextParser
public sealed class EPUBTextParser : BaseTextParser
{
public EPUBTextParser(ParserContext context)
{
Context = context;
}
public ParserContext Context { get; set; }
public string Parse()
public EPUBTextParser(ParserContext context) : base(context)
{ }

internal override string ParseText(Stream stream)
{
EpubBook book = EPUBHelper.GetEpubBook(Context);
EpubBook book = EpubReader.ReadBook(stream);
StringBuilder result = new StringBuilder();
HtmlDocument html = new HtmlDocument();
Regex whiteSpaceStart = new Regex("^\\s*", RegexOptions.Multiline);
Expand Down
4 changes: 1 addition & 3 deletions ToxyFramework/Parsers/Email/EMLTextParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public class EMLTextParser : BaseTextParser
public EMLTextParser(ParserContext context) : base(context)
{ }

internal override string ParseText(out IDisposable disposable)
internal override string ParseText(Stream stream)
{
Stream stream = Utility.GetStream(Context);
disposable = Context.IsStreamContext ? null : stream;
using (MimeMessage message = MimeMessage.Load(stream))
{
return MimeMessageHelper.ConvertToText(message);
Expand Down
28 changes: 12 additions & 16 deletions ToxyFramework/Parsers/Email/MsgTextParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.IO;
using System.Text;
using Toxy.Base;

namespace Toxy.Parsers
{
Expand All @@ -12,21 +13,15 @@ namespace Toxy.Parsers
/// <remarks>
/// http://www.codeproject.com/Articles/19571/MsgReader-DLL
/// </remarks>
public class MsgTextParser : PlainTextParser
public class MsgTextParser : BaseTextParser
{
public MsgTextParser(ParserContext context)
: base(context)
{
this.Context = context;
}
public MsgTextParser(ParserContext context) : base(context)
{ }

public override string Parse()
internal override string ParseText(Stream stream)
{
Utility.ValidateContext(Context);

StringBuilder result = new StringBuilder();
using (var stream = Utility.GetStream(Context))
using (var reader = new Storage.Message(stream))
using (Storage.Message reader = new Storage.Message(stream, FileAccess.Read, Context.IsStreamContext))
{
if (reader.Sender != null)
{
Expand All @@ -39,7 +34,7 @@ public override string Parse()
StringBuilder recipientTo = new StringBuilder();
StringBuilder recipientCc = new StringBuilder();
StringBuilder recipientBcc = new StringBuilder();
foreach (var recipient in reader.Recipients)
foreach (Storage.Recipient recipient in reader.Recipients)
{
string sRecipient = null;
if (string.IsNullOrEmpty(recipient.DisplayName))
Expand Down Expand Up @@ -79,12 +74,13 @@ public override string Parse()
result.Append("[Bcc] ");
result.AppendLine(recipientBcc.ToString());
}

}
if (!string.IsNullOrEmpty(reader.Subject))
result.AppendFormat("[Subject] {0}{1}", reader.Subject, Environment.NewLine);
if (!string.IsNullOrEmpty(reader.Subject))
{
result.AppendFormat("[Subject] {0}{1}", reader.Subject, Environment.NewLine);
}

result.AppendLine();
result.AppendLine();
result.AppendLine(reader.BodyText);
}
return result.ToString();
Expand Down
104 changes: 104 additions & 0 deletions ToxyFramework/Parsers/Office/PowerPoint2007TextParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using PasswordProtectedChecker;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using Toxy.Base;

namespace Toxy.Parsers
{
public class Powerpoint2007TextParser : BaseTextParser
{
public Powerpoint2007TextParser(ParserContext context) : base(context)
{ }

internal override void ValidateContext()
{
base.ValidateContext();
Utility.ThrowIfProtected(Context);
}

internal override string ParseText(Stream stream)
{
StringBuilder sb = new StringBuilder();
// closes the Stream anyways
using (Package pkg = Package.Open(stream, FileMode.Open, FileAccess.Read))
{
using PresentationDocument ppt = PresentationDocument.Open(pkg);
// Get the relationship ID of the first slide.
PresentationPart part = ppt.PresentationPart;
OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
for (int index = 0; index < slideIds.Count; index++)
{
string relId = (slideIds[index] as SlideId).RelationshipId;

// Get the slide part from the relationship ID.
SlidePart slide = (SlidePart)part.GetPartById(relId);
string[] texts = GetAllTextInSlide(slide);
foreach (string text in texts)
{
sb.AppendLine(text);
}
}
}
return sb.ToString();
}

public static string[] GetAllTextInSlide(SlidePart slidePart)
{
// Verify that the slide part exists.
#if NET8_0_OR_GREATER
ArgumentNullException.ThrowIfNull(slidePart, nameof(slidePart));
#else
if (slidePart == null)
{
throw new ArgumentNullException("slidePart");
}
#endif

// Create a new linked list of strings.
LinkedList<string> texts = new LinkedList<string>();

// If the slide exists...
if (slidePart.Slide != null)
{
// Iterate through all the paragraphs in the slide.
foreach (DocumentFormat.OpenXml.Drawing.Paragraph paragraph in
slidePart.Slide.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>())
{
// Create a new string builder.
StringBuilder paragraphText = new StringBuilder();

// Iterate through the lines of the paragraph.
foreach (DocumentFormat.OpenXml.Drawing.Text text in
paragraph.Descendants<DocumentFormat.OpenXml.Drawing.Text>())
{
// Append each line to the previous lines.
paragraphText.Append(text.Text);
}

if (paragraphText.Length > 0)
{
// Add each paragraph to the linked list.
texts.AddLast(paragraphText.ToString());
}
}
}

if (texts.Count > 0)
{
// Return an array of strings.
return texts.ToArray();
}
else
{
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Text;


namespace Toxy.Parsers
{
public class Word2003TextParser : ITextParser
Expand Down
Loading
Loading