/*
OpenNxSerialization Framework
Copyright (C) 2006 - 2007 "NeXtreme Innovations"
[The Next Xtreme Innovations]
This program is free software, distributed under the terms of
the GNU General Public License Version 2. See the "License.txt" file
at the top of the source tree.
*/
using System;
using System.Configuration;
using NeXtreme.OpenNxSerialization.Surrogates;
namespace NeXtreme.OpenNxSerialization.Configuration
{
/// <summary>
/// Represents a known surrogate element within a configuration file.
/// </summary>
public class NxKnownSurrogateElement : ConfigurationElement
{
private readonly TypeNameConverter msConvertor = new TypeNameConverter();
/// <summary>
/// Default Constructor
/// </summary>
public NxKnownSurrogateElement()
{
}
/// <summary>
/// Constructs the object with the given <paramref name="typeName"/>.
/// </summary>
/// <param name="typeName">Type name</param>
/// <exception cref="ArgumentNullException"></exception>
/// <remarks>
/// The specified type must implement <see cref="INxSerializationSurrogate"/>.
/// </remarks>
public NxKnownSurrogateElement(string typeName)
{
if (typeName == null)
throw new ArgumentNullException("typeName");
TypeName = typeName;
}
/// <summary>
/// Constructs the object with the given <paramref name="typeName"/> and <paramref name="typeHandle"/>.
/// </summary>
/// <param name="typeName">Type name</param>
/// <param name="typeHandle">Type handle</param>
/// <exception cref="ArgumentNullException"></exception>
/// <remarks>
/// The specified type must implement <see cref="INxSerializationSurrogate"/>.
/// </remarks>
public NxKnownSurrogateElement(string typeName, short typeHandle)
{
if (typeName == null)
throw new ArgumentNullException("typeName");
TypeName = typeName;
TypeHandle = typeHandle;
}
/// <summary>
/// Gets the name used to identify this collection of elements in the configuration file
/// </summary>
internal static string ElementName
{
get { return "surrogate"; }
}
/// <summary>
/// Constructs the object with the given <paramref name="type"/>.
/// </summary>
/// <param name="type">Type</param>
/// <exception cref="ArgumentNullException"></exception>
/// <remarks>
/// The specified type must implement <see cref="INxSerializationSurrogate"/>.
/// </remarks>
public NxKnownSurrogateElement(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
TypeName = type.AssemblyQualifiedName;
}
/// <summary>
/// Constructs the object with the given <paramref name="type"/> and <paramref name="typeHandle"/>.
/// </summary>
/// <param name="type">Type</param>
/// <param name="typeHandle">Type handle</param>
/// <exception cref="ArgumentNullException"></exception>
/// <remarks>
/// The specified type must implement <see cref="INxSerializationSurrogate"/>.
/// </remarks>
public NxKnownSurrogateElement(Type type, short typeHandle)
{
if (type == null)
throw new ArgumentNullException("type");
TypeName = type.AssemblyQualifiedName;
TypeHandle = typeHandle;
}
/// <summary>
/// Get/Set the typeName attribute of the surrogate element in knownSurrogates.
/// </summary>
/// <value>The typename for an <see cref="INxSerializationSurrogate"/> object</value>
/// <remarks>
/// The specified type must implement <see cref="INxSerializationSurrogate"/>. A given
/// surrogate must be specified only once in the list of knownSurrogates.
/// </remarks>
[ConfigurationProperty("typeName", IsKey = true, IsRequired = true)]
public string TypeName
{
get
{
object value = base["typeName"];
if (!typeof(INxSerializationSurrogate).IsAssignableFrom((Type)msConvertor.ConvertFrom(value)))
{
throw new ConfigurationErrorsException(value + " is not a valid serialization surrogate");
}
return ((string)(value));
}
set
{
if (!typeof(INxSerializationSurrogate).IsAssignableFrom((Type) msConvertor.ConvertFrom(value)))
{
throw new ConfigurationErrorsException(value + " is not a valid serialization surrogate");
}
base["typeName"] = value;
}
}
/// <summary>
/// Get/Set the typeHandle attribute of the surrogate element in knownSurrogates.
/// </summary>
/// <value>The type handle for an <see cref="INxSerializationSurrogate"/> object</value>
/// <remarks>
/// Must be in the range <see cref="Int16.MinValue"/> and <see cref="Int16.MaxValue"/>.
/// </remarks>
[ConfigurationProperty("typeHandle", IsKey = false, IsRequired = false)]
[IntegerValidator(MinValue=Int16.MinValue, MaxValue=Int16.MaxValue)]
public int TypeHandle
{
get
{
return ((int)(base["typeHandle"]));
}
set
{
base["typeHandle"] = value;
}
}
}
/// <summary>
/// The class that holds onto each element returned by the configuration manager.
/// </summary>
public class NxKnownTypeElement : ConfigurationElement
{
private readonly TypeNameConverter sConvertor = new TypeNameConverter();
/// <summary>
/// Default Constructor
/// </summary>
public NxKnownTypeElement()
{
}
/// <summary>
/// Constructs the object with the given <paramref name="typeName"/>.
/// </summary>
/// <param name="typeName">Type name</param>
/// <exception cref="ArgumentNullException"></exception>
/// <remarks>
/// A given type must be specified only once in the list of knownTypes.
/// </remarks>
public NxKnownTypeElement(string typeName)
{
if (typeName == null)
throw new ArgumentNullException("typeName");
TypeName = typeName;
}
/// <summary>
/// Constructs the object with the given <paramref name="typeName"/> and <paramref name="typeHandle"/>.
/// </summary>
/// <param name="typeName">Type name</param>
/// <param name="typeHandle">Type handle</param>
/// <exception cref="ArgumentNullException"></exception>
/// <remarks>
/// A given type must be specified only once in the list of knownTypes.
/// </remarks>
public NxKnownTypeElement(string typeName, short typeHandle)
{
if (typeName == null)
throw new ArgumentNullException("typeName");
TypeName = typeName;
TypeHandle = typeHandle;
}
/// <summary>
/// Constructs the object with the given <paramref name="type"/>.
/// </summary>
/// <param name="type">Type</param>
/// <exception cref="ArgumentNullException"></exception>
/// <remarks>
/// A given type must be specified only once in the list of knownTypes.
/// </remarks>
public NxKnownTypeElement(Type type)
{
if (type == null)
throw new ArgumentNullException("type");
TypeName = type.AssemblyQualifiedName;
}
/// <summary>
/// Constructs the object with the given <paramref name="type"/> and <paramref name="typeHandle"/>.
/// </summary>
/// <param name="type">Type</param>
/// <param name="typeHandle">Type handle</param>
/// <exception cref="ArgumentNullException"></exception>
/// <remarks>
/// A given type must be specified only once in the list of knownTypes.
/// </remarks>
public NxKnownTypeElement(Type type, short typeHandle)
{
if (type == null)
throw new ArgumentNullException("type");
TypeName = type.AssemblyQualifiedName;
TypeHandle = typeHandle;
}
/// <summary>
/// Gets the name used to identify this collection of elements in the configuration file
/// </summary>
internal static string ElementName
{
get { return "type"; }
}
/// <summary>
/// Get/Set the typeName attribute of the type element in knownTypes.
/// </summary>
/// <value>The typename for the specified type</value>
/// <remarks>
/// A given type must be specified only once in the list of knownTypes.
/// </remarks>
[ConfigurationProperty("typeName", IsKey = true, IsRequired = true)]
public string TypeName
{
get
{
object value = base["typeName"];
try
{
sConvertor.ConvertFrom(value);
}
catch(Exception)
{
throw new ConfigurationErrorsException(value + " is not a valid typename");
}
return ((string)(value));
}
set
{
try
{
sConvertor.ConvertFrom(value);
}
catch (Exception)
{
throw new ConfigurationErrorsException(value + " is not a valid typename");
}
base["typeName"] = value;
}
}
/// <summary>
/// Get/Set the typeHandle attribute of the type element in knownTypes.
/// </summary>
/// <value>The type handle for the specified type</value>
/// <remarks>
/// Must be in the range <see cref="Int16.MinValue"/> and <see cref="Int16.MaxValue"/>.
/// </remarks>
[ConfigurationProperty("typeHandle", IsKey = false, IsRequired = false)]
[IntegerValidator(MinValue = Int16.MinValue, MaxValue = Int16.MaxValue)]
public int TypeHandle
{
get
{
return ((int)(base["typeHandle"]));
}
set
{
base["typeHandle"] = value;
}
}
}
}