- using System;
- using System.Xml;
- using System.Xml.Xsl;
- using System.Xml.XPath;
- using System.Collections.Generic;
-
- namespace PersonsXPath
- {
- class CustomContext : XsltContext
- {
- #region Constructeurs
-
- public CustomContext()
- : base()
- {
- }
-
- public CustomContext(NameTable table)
- : base(table)
- {
- }
-
- #endregion Constructeurs
-
- #region Champs
-
- /// <summary>
- /// Contient les variables.
- /// </summary>
- private Dictionary<String, IXsltContextVariable> _variables
- = new Dictionary<String, IXsltContextVariable>();
-
- #endregion Champs
-
- #region Implémentation de XsltContext
-
- public override int CompareDocument(String baseUri, String nextbaseUri)
- {
- // Nous utilisons simplement une comparaison de chaîne.
- return String.CompareOrdinal(baseUri, nextbaseUri);
- }
-
- public override bool PreserveWhitespace(System.Xml.XPath.XPathNavigator node)
- {
- return true;
- }
-
- public override bool Whitespace
- {
- get
- {
- return true;
- }
- }
-
- public override IXsltContextFunction ResolveFunction(
- String prefix,
- String name,
- XPathResultType[] ArgTypes)
- {
- // Inutile dans notre cas, nous ne nous intéressons qu'aux variables.
- return null;
- }
-
- public override IXsltContextVariable ResolveVariable(String prefix, String name)
- {
- IXsltContextVariable var = null;
- this._variables.TryGetValue(name, out var);
- return var;
- }
-
- #endregion Implémentation de XsltContext
-
- #region Gestion des paramètres
-
- public void AddVariable(String name, Object value)
- {
- #region Vérification des paramètres
-
- if (name == null)
- throw new ArgumentNullException("name");
- else if (name.Length < 0)
- throw new ArgumentException("name");
-
- if (value == null)
- throw new ArgumentNullException("value");
-
- #endregion Vérification des paramètres
-
- this._variables[name] = new Variable(name, value);
- }
-
- public Boolean RemoveVariable(String name)
- {
- #region Vérification des paramètres
-
- if (name == null)
- throw new ArgumentNullException("name");
- else if (name.Length < 0)
- throw new ArgumentException("name");
-
- #endregion Vérification des paramètres
-
- return this._variables.Remove(name);
- }
-
- public void ClearVariables()
- {
- this._variables.Clear();
- }
-
- #endregion Gestion des paramètres
-
- private class Variable : IXsltContextVariable
- {
- #region Constructeur
-
- /// <summary>
- /// Initialise une nouvelle instance de <see cref="Variable"/>.
- /// </summary>
- /// <param name="name"></param>
- /// <param name="value"></param>
- /// <exception cref="ArgumentNullException">
- /// <c>name</c> est une référence nulle.
- /// ou
- /// <c>value</c> est une référence nulle.
- /// </exception>
- /// <exception cref="ArgumentException">
- /// <c>name</c> est une chaîne vide.
- /// </exception>
- public Variable(String name, Object value)
- {
- #region Vérification des paramètres
-
- if (name == null)
- throw new ArgumentNullException("name");
- else if (name.Length < 0)
- throw new ArgumentException("name");
-
- if (value == null)
- throw new ArgumentNullException("value");
-
- #endregion Vérification des paramètres
-
- this._name = name;
- this._value = value;
-
- this.DetermineType();
- }
-
- #endregion Constructeur
-
- #region Champs
-
- /// <summary>
- /// Nom de la variable.
- /// </summary>
- private String _name;
-
- /// <summary>
- /// Valeur de la variable.
- /// </summary>
- private Object _value;
-
- /// <summary>
- /// Type de la variable.
- /// </summary>
- private XPathResultType _type;
-
- #endregion Champs
-
- #region Propriétés
-
- /// <summary>
- /// Obtient ou définit le nom de la variable.
- /// </summary>
- public String Name
- {
- get
- {
- return this._name;
- }
-
- set
- {
- this._name = value;
- }
- }
-
- /// <summary>
- /// Obtient ou définit la valeur de la variable.
- /// </summary>
- public Object Value
- {
- get
- {
- return this._value;
- }
-
- set
- {
- this._value = value;
- }
- }
-
- #endregion Propriétés
-
- #region Implémentation de IXsltContextVariable
-
- public object Evaluate(XsltContext xsltContext)
- {
- return this._value;
- }
-
- public Boolean IsLocal
- {
- get
- {
- return false;
- }
- }
-
- public Boolean IsParam
- {
- get
- {
- return false;
- }
- }
-
- public XPathResultType VariableType
- {
- get
- {
- return this._type;
- }
- }
-
- #endregion Implémentation de IXsltContextVariable
-
- #region Méthodes utilitaires
-
- /// <summary>
- /// Permet de déterminer le type de la valeur.
- /// </summary>
- private void DetermineType()
- {
- // Détermination des types :
- // String : XPathResultType.String
- // Boolean : XPathResultType.Boolean
- // XPathNavigator : XPathResultType.Navigator
- // XPathNodeIterator : XPathResultType.NodeSet
- // Double : XPathResultType.Number
- // Convertible en Double : XPathResultType.Number
- // Autres : XPathResultType.Any
-
- if (this._value is String)
- this._type = XPathResultType.String;
- else if (this._value is Boolean)
- this._type = XPathResultType.Boolean;
- else if (this._value is XPathNavigator)
- this._type = XPathResultType.Navigator;
- else if (this._value is XPathNodeIterator)
- this._type = XPathResultType.NodeSet;
- else if (this._value is Double)
- this._type = XPathResultType.Number;
- else if (this._value is IConvertible)
- {
- // Si le type n'est pas double mais implémente IConvertible,
- // nous tentons la conversion.
- try
- {
- this._value = Convert.ToDouble(this._value);
- this._type = XPathResultType.Number;
- }
- catch
- {
- this._type = XPathResultType.Any;
- }
- }
- else
- this._type = XPathResultType.Any;
- }
-
- #endregion Méthodes utilitaires
- }
- }
- }
using System;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Collections.Generic;
namespace PersonsXPath
{
class CustomContext : XsltContext
{
#region Constructeurs
public CustomContext()
: base()
{
}
public CustomContext(NameTable table)
: base(table)
{
}
#endregion Constructeurs
#region Champs
/// <summary>
/// Contient les variables.
/// </summary>
private Dictionary<String, IXsltContextVariable> _variables
= new Dictionary<String, IXsltContextVariable>();
#endregion Champs
#region Implémentation de XsltContext
public override int CompareDocument(String baseUri, String nextbaseUri)
{
// Nous utilisons simplement une comparaison de chaîne.
return String.CompareOrdinal(baseUri, nextbaseUri);
}
public override bool PreserveWhitespace(System.Xml.XPath.XPathNavigator node)
{
return true;
}
public override bool Whitespace
{
get
{
return true;
}
}
public override IXsltContextFunction ResolveFunction(
String prefix,
String name,
XPathResultType[] ArgTypes)
{
// Inutile dans notre cas, nous ne nous intéressons qu'aux variables.
return null;
}
public override IXsltContextVariable ResolveVariable(String prefix, String name)
{
IXsltContextVariable var = null;
this._variables.TryGetValue(name, out var);
return var;
}
#endregion Implémentation de XsltContext
#region Gestion des paramètres
public void AddVariable(String name, Object value)
{
#region Vérification des paramètres
if (name == null)
throw new ArgumentNullException("name");
else if (name.Length < 0)
throw new ArgumentException("name");
if (value == null)
throw new ArgumentNullException("value");
#endregion Vérification des paramètres
this._variables[name] = new Variable(name, value);
}
public Boolean RemoveVariable(String name)
{
#region Vérification des paramètres
if (name == null)
throw new ArgumentNullException("name");
else if (name.Length < 0)
throw new ArgumentException("name");
#endregion Vérification des paramètres
return this._variables.Remove(name);
}
public void ClearVariables()
{
this._variables.Clear();
}
#endregion Gestion des paramètres
private class Variable : IXsltContextVariable
{
#region Constructeur
/// <summary>
/// Initialise une nouvelle instance de <see cref="Variable"/>.
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <exception cref="ArgumentNullException">
/// <c>name</c> est une référence nulle.
/// ou
/// <c>value</c> est une référence nulle.
/// </exception>
/// <exception cref="ArgumentException">
/// <c>name</c> est une chaîne vide.
/// </exception>
public Variable(String name, Object value)
{
#region Vérification des paramètres
if (name == null)
throw new ArgumentNullException("name");
else if (name.Length < 0)
throw new ArgumentException("name");
if (value == null)
throw new ArgumentNullException("value");
#endregion Vérification des paramètres
this._name = name;
this._value = value;
this.DetermineType();
}
#endregion Constructeur
#region Champs
/// <summary>
/// Nom de la variable.
/// </summary>
private String _name;
/// <summary>
/// Valeur de la variable.
/// </summary>
private Object _value;
/// <summary>
/// Type de la variable.
/// </summary>
private XPathResultType _type;
#endregion Champs
#region Propriétés
/// <summary>
/// Obtient ou définit le nom de la variable.
/// </summary>
public String Name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}
/// <summary>
/// Obtient ou définit la valeur de la variable.
/// </summary>
public Object Value
{
get
{
return this._value;
}
set
{
this._value = value;
}
}
#endregion Propriétés
#region Implémentation de IXsltContextVariable
public object Evaluate(XsltContext xsltContext)
{
return this._value;
}
public Boolean IsLocal
{
get
{
return false;
}
}
public Boolean IsParam
{
get
{
return false;
}
}
public XPathResultType VariableType
{
get
{
return this._type;
}
}
#endregion Implémentation de IXsltContextVariable
#region Méthodes utilitaires
/// <summary>
/// Permet de déterminer le type de la valeur.
/// </summary>
private void DetermineType()
{
// Détermination des types :
// String : XPathResultType.String
// Boolean : XPathResultType.Boolean
// XPathNavigator : XPathResultType.Navigator
// XPathNodeIterator : XPathResultType.NodeSet
// Double : XPathResultType.Number
// Convertible en Double : XPathResultType.Number
// Autres : XPathResultType.Any
if (this._value is String)
this._type = XPathResultType.String;
else if (this._value is Boolean)
this._type = XPathResultType.Boolean;
else if (this._value is XPathNavigator)
this._type = XPathResultType.Navigator;
else if (this._value is XPathNodeIterator)
this._type = XPathResultType.NodeSet;
else if (this._value is Double)
this._type = XPathResultType.Number;
else if (this._value is IConvertible)
{
// Si le type n'est pas double mais implémente IConvertible,
// nous tentons la conversion.
try
{
this._value = Convert.ToDouble(this._value);
this._type = XPathResultType.Number;
}
catch
{
this._type = XPathResultType.Any;
}
}
else
this._type = XPathResultType.Any;
}
#endregion Méthodes utilitaires
}
}
}