|
using System ;
using System.IO ;
using System.Xml ;
using System.Xml.XPath ;
namespace Slogger.Utilities
{
public class SettingsReader
{
XPathNavigator _navigator;
protected XPathNavigator Navigator { get { return _navigator ; } }
public XPathNavigator CloneNavigator( ) { return _navigator.Clone( ) ; }
#region Constructors
public SettingsReader( ) { }
public SettingsReader( XPathNavigator xpathNavigator )
{
Initialise( xpathNavigator ) ;
}
public SettingsReader( string filename)
{
Initialise( filename ) ;
}
public SettingsReader(XPathDocument xpathDocument )
{
Initialise( xpathDocument ) ;
}
public SettingsReader( StreamReader streamReader )
{
Initialise( streamReader ) ;
}
#endregion
#region Initialisers
public void Initialise( XPathNavigator xpathNavigator )
{
if ( xpathNavigator == null )
throw new NullReferenceException( "Cannot initialise SettingsReader with a null XPathNavigator" ) ;
_navigator = xpathNavigator ;
}
public void Initialise( StreamReader streamReader )
{
XmlTextReader r = new XmlTextReader( streamReader ) ;
r.WhitespaceHandling=WhitespaceHandling.Significant;
XPathDocument document = new XPathDocument(r ) ;
_navigator= document.CreateNavigator( ) ;
}
public void Initialise( XPathDocument xpathDocument )
{
_navigator = xpathDocument.CreateNavigator( ) ;
}
public void Initialise( string filename )
{
Initialise ( new StreamReader( filename ) ) ;
}
#endregion
public bool SetRoot( string root )
{
XPathNodeIterator it = _navigator.Select( root ) ;
if ( it.Count==0)
return false ;
it.MoveNext( ) ;
_navigator = it.Current.Clone( ) ;
return true;
}
public void SetRoot( )
{
_navigator.MoveToRoot( ) ;
}
public void SetRoot( XPathNavigator navigator )
{
_navigator = navigator.Clone( ) ;
}
public bool Exists(string path)
{
string s = path.Replace(".", "/" ) ;
XPathNavigator nav = _navigator.Clone( ) ;
XPathNodeIterator it = nav.Select( s ) ;
return it.Count == 0 ? false : true ;
}
public object GetValue(string path)
{
string s = path.Replace(".", "/" ) ;
XPathNodeIterator it = _navigator.Select( s ) ;
if ( it.Count== 0 )
throw new Exception(string.Format("Cannot find the value of node: {0}", path ) ) ;
it.MoveNext( ) ;
return it.Current.Value;
}
public object this[string path]
{
get
{
//SEE IF THERE'S AN @ IN THERE TO GET AN ATTRIBUTE
int n = path.LastIndexOf("@");
if ( n != -1 )
return GetAttribute( path.Substring(0,n)+path.Substring(n+1) ) ;
return GetValue( path ) ;
}
}
public object GetAttribute(string path)
{
string s = path.Replace(".", "/" ) ;
int n = s.LastIndexOf("/");
if ( n == -1 )
return Navigator.GetAttribute( s, "" ) ;
string s2 = s.Substring(0,n);
string satt=s.Substring(n+1 ) ;
XPathNodeIterator it = _navigator.Select( s2 ) ;
if ( it.Count== 0 )
throw new Exception(string.Format("Cannot get attribute. Node:{0} not found", s2 ) ) ;
it.MoveNext( ) ;
return it.Current.GetAttribute(satt, "" ) ;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I was born at a very young age. My hobbies include reading pornography and killing squirrels.