Skip to content

Commit 89eb406

Browse files
committed
Added additional Load function that accepts a URI.
1 parent 6586929 commit 89eb406

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

BeanIO/Internal/Config/Xml/XmlMappingParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ protected XmlMapping ImportConfiguration(XElement element)
256256
throw new BeanIOConfigurationException(string.Format("No scheme specified for resource '{0}'", resource));
257257

258258
var url = new Uri(resource);
259-
ISchemeHandler handler;
260-
if (!Settings.Instance.SchemeHandlers.TryGetValue(url.Scheme, out handler))
259+
var handler = Settings.Instance.GetSchemeHandler(url, false);
260+
if (handler == null)
261261
throw new BeanIOConfigurationException(
262262
string.Format(
263263
"Scheme of import resource name {0} must one of: {1}",

BeanIO/Internal/Util/Settings.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Reflection;
45

56
using BeanIO.Config;
@@ -257,6 +258,27 @@ public void Add(ISchemeHandler handler)
257258
_schemeHandlers[handler.Scheme] = handler;
258259
}
259260

261+
public ISchemeHandler GetSchemeHandler(Uri url, bool throwIfMissing)
262+
{
263+
return GetSchemeHandler(url.Scheme, throwIfMissing);
264+
}
265+
266+
public ISchemeHandler GetSchemeHandler(string scheme, bool throwIfMissing)
267+
{
268+
ISchemeHandler handler;
269+
if (!SchemeHandlers.TryGetValue(scheme, out handler))
270+
{
271+
if (!throwIfMissing)
272+
return null;
273+
throw new BeanIOConfigurationException(
274+
string.Format(
275+
"Scheme '{0}' must one of: {1}",
276+
scheme,
277+
string.Join(", ", SchemeHandlers.Keys.Select(x => string.Format("'{0}'", x)))));
278+
}
279+
return handler;
280+
}
281+
260282
private static IPropertiesProvider CreateDefaultProvider()
261283
{
262284
var defaultProvider = new PropertiesStreamProvider(typeof(Settings).GetTypeInfo().Assembly.GetManifestResourceStream(DEFAULT_CONFIGURATION_PATH));

BeanIO/StreamFactory.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,27 @@ public virtual void Load(System.IO.Stream input)
101101
Load(input, null);
102102
}
103103

104+
/// <summary>
105+
/// Loads a BeanIO mapping, and adds the configured streams to this factory.
106+
/// </summary>
107+
/// <param name="source">The source to read the mapping file from</param>
108+
public virtual void Load(Uri source)
109+
{
110+
Load(source, null);
111+
}
112+
113+
/// <summary>
114+
/// Loads a BeanIO mapping file, and adds the configured streams to this factory.
115+
/// </summary>
116+
/// <param name="source">The source to read the mapping file from</param>
117+
/// <param name="properties">user <see cref="Properties"/> for property substitution</param>
118+
public virtual void Load(Uri source, Properties properties)
119+
{
120+
var handler = Settings.Instance.GetSchemeHandler(source, true);
121+
using (var input = handler.Open(source))
122+
Load(input, properties);
123+
}
124+
104125
/// <summary>
105126
/// Loads a BeanIO mapping file, and adds the configured streams to this factory.
106127
/// </summary>

0 commit comments

Comments
 (0)