1
0
Fork 0
mirror of https://github.com/sbrl/PolyFeed.git synced 2024-06-27 09:44:55 +00:00

Compare commits

...

3 commits

Author SHA1 Message Date
Starbeamrainbowlabs 0afff60345
Start refactoring the html provider into it's own class, but it's not finished yet. 2019-08-10 18:56:48 +01:00
Starbeamrainbowlabs 44fe2472c3
Add IParserProvider interface.
Next up: decouple the existing HTML provider from FeedBuilder
2019-08-10 18:14:50 +01:00
Starbeamrainbowlabs 72a9f401ba
Add reflection helpers
Imported from the SVM ACW for Component-based Architectures
2019-08-10 18:05:34 +01:00
4 changed files with 147 additions and 0 deletions

View file

@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.IO;
namespace PolyFeed.Helpers
{
internal static class ReflectionUtilities
{
public static bool Verbose = true;
public static IEnumerable<Assembly> IterateLoadedAssemblies()
{
return AppDomain.CurrentDomain.GetAssemblies();
}
public static IEnumerable<Type> IterateAllLoadedTypes()
{
return IterateLoadedAssemblies()
.SelectMany((Assembly nextAssembly) => nextAssembly.GetTypes());
}
public static IEnumerable<Type> IterateLoadedTypes(Assembly targetAssembly)
{
return targetAssembly.GetTypes();
}
public static void LoadAssembliesDirectory(string directory)
{
foreach (string nextDll in Directory.EnumerateFiles(directory, "*.dll"))
{
AssemblyName assemblyName = AssemblyName.GetAssemblyName(nextDll);
// Check that a strongname is actually present
if (assemblyName.GetPublicKeyToken().Length < 8)
continue;
// Verify the strongname is valid.
/*try
{
bool isOk = false;
NativeMethods.StrongNameSignatureVerificationEx(nextDll, 0xFF, ref isOk);
if (!isOk)
continue;
}
catch (DllNotFoundException error)
{
Console.Error.WriteLine($"Warning: Unable to verify the integrity of the StrongName for '{nextDll}' (DLL not found: {error.Message}).");
}*/
try
{
Assembly.LoadFrom(nextDll);
// FUTURE: Consider using Assembly.ReflectionOnlyLoadFrom in a separate AppDomain to figure out if there's anything useful in an assembly before loading it for reals
}
catch (BadImageFormatException error)
{
if (Verbose) Console.Error.WriteLine($"Error loading '{nextDll}': {error.Message}");
}
}
}
/// <summary>
/// Searches the types present in the specified assembly to find a type that implements
/// the specified interface.
/// </summary>
/// <param name="targetInterface">The target interface that returned types should implement.</param>
/// <param name="assemblyToSearch">The assembly to search through for matching types.</param>
public static IEnumerable<Type> IterateImplementingTypes(Type targetInterface, Assembly assemblyToSearch)
{
if (!targetInterface.IsInterface)
throw new ArgumentException($"Error: The specified type {targetInterface} is not an " +
"interface, so it can't be used to search for implementing types.");
// FUTURE: Add caching here? Reflection is slow
foreach (Type nextType in IterateAllLoadedTypes())
{
// Make sure it implements the specified interface
if (!targetInterface.IsAssignableFrom(nextType))
continue;
yield return nextType;
}
}
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Net;
using Microsoft.SyndicationFeed.Atom;
namespace PolyFeed.ParserProviders
{
public class HtmlParserProvider : IParserProvider
{
public HtmlParserProvider()
{
}
public void ParseWebResponse(FeedSource source, WebResponse response)
{
throw new NotImplementedException();
}
public void SetOutputFeed(AtomFeedWriter feed)
{
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Net;
using Microsoft.SyndicationFeed.Atom;
namespace PolyFeed.ParserProviders
{
/// <summary>
/// Defines the functionality that a source parser should provide.
/// Sources are represented by a <see cref="FeedSource" /> object, and source parsers
/// are responsible for parsing it and populating a given atom feed.
/// </summary>
public interface IParserProvider
{
/// <summary>
/// The identifier of this provider.
/// Used in the .toml configuration file to specify which parser to use.
/// </summary>
string Identifier { get; }
/// <summary>
/// Sets the output feed that parsed output should be written to.
/// </summary>
/// <param name="feed">The output feed writer that output should be written to.</param>
void SetOutputFeed(AtomFeedWriter feed);
/// <summary>
/// Parses a web response that's paired with a given <see cref="FeedSource" />.
/// </summary>
/// <param name="source">The <see cref="FeedSource"/> object that the <paramref name="response"/> was generated from.</param>
/// <param name="response">The <see cref="WebResponse"/> in question needs parsing.</param>
void ParseWebResponse(FeedSource source, WebResponse response);
}
}

View file

@ -151,6 +151,9 @@
<Compile Include="SnakeCasePropertySelector.cs" />
<Compile Include="Helpers\HtmlHelpers.cs" />
<Compile Include="Helpers\UserAgentHelper.cs" />
<Compile Include="Helpers\ReflectionHelpers.cs" />
<Compile Include="ParserProviders\IParserProvider.cs" />
<Compile Include="ParserProviders\HtmlParserProvider.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
@ -158,6 +161,7 @@
<ItemGroup>
<Folder Include="Salamander.Core\" />
<Folder Include="Helpers\" />
<Folder Include="ParserProviders\" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets" Condition="Exists('..\packages\NETStandard.Library.2.0.3\build\netstandard2.0\NETStandard.Library.targets')" />