|
// <copyright file="ConfigurationPath.cs" company="MetaPropeller" author="Vladimir Klisic">
//
// Copyright 2005 Vladimir Klisic
// mailto:vladimir.klisic@free.fr
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any
// damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute
// it freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must
// not claim that you wrote the original software. If you use this
// software in a product, an acknowledgment in the product documentation
// would be appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must
// not be misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// </copyright>
//
// <history>
// <change who=�Vladimir Klisic� date=�2005.02.16�>Initial version</change>
// <history>
using System;
using System.Text;
namespace MetaPropeller.Configuration
{
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="T:ConfigurationPath"]/*'/>
public class ConfigurationPath
{
#region Class members
private string[] _path = null;
private int _currentSegmentIdx = -1;
private char[] _separators = { '/', '.', '\\' };
#endregion
#region Constructors
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="M:ConfigurationPath"]/*'/>
internal ConfigurationPath(string path)
{
SetPath(path);
}
#endregion
#region Properties
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="P:Separators"]/*'/>
public char[] Separators
{
get { return _separators; }
set { _separators = value; }
}
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="P:FirstSegment"]/*'/>
public string FirstSegment
{
get
{
if (_path.Length > 0)
return _path[0];
else
throw new InvalidOperationException("No path set");
}
}
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="P:CurrentSegment"]/*'/>
public string CurrentSegment
{
get
{
if (_currentSegmentIdx != -1 && _currentSegmentIdx < _path.Length)
return _path[_currentSegmentIdx];
else
throw new InvalidOperationException("No path set");
}
}
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="P:LastSegment"]/*'/>
public string LastSegment
{
get
{
if (_path.Length > 0)
return _path[_path.Length-1];
else
throw new InvalidOperationException("No path set");
}
}
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="P:Length"]/*'/>
public uint Length
{
get { return _path != null? ((uint) _path.Length) : 0; }
}
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="P:EndOfPath"]/*'/>
public bool EndOfPath
{
get { return _currentSegmentIdx == -1 || _path == null || _currentSegmentIdx >= _path.Length; }
}
#endregion
#region Methods
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="M:SetPath"]/*'/>
public void SetPath(string path)
{
string s = path.TrimStart(_separators);
string[] segments = s.Split(_separators);
if (segments.Length > 0)
{
_path = segments;
_currentSegmentIdx = _path.Length > 0? 0 : - 1;
}
else
_currentSegmentIdx = -1;
}
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="M:MoveToFirstSegment"]/*'/>
public void MoveToFirstSegment()
{
if (_path.Length > 0)
_currentSegmentIdx = 0;
else
throw new InvalidOperationException("No path set");
}
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="M:MoveToPrevSegment"]/*'/>
public void MoveToPrevSegment()
{
if (_currentSegmentIdx > 0)
_currentSegmentIdx--;
else
throw new InvalidOperationException("No path set");
}
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="M:MoveToNextSegment"]/*'/>
public void MoveToNextSegment()
{
if (_currentSegmentIdx < _path.Length)
_currentSegmentIdx++;
else
throw new InvalidOperationException("No path set");
}
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="M:MoveToLastSegment"]/*'/>
public void MoveToLastSegment()
{
if (_path != null && _path.Length > 0)
_currentSegmentIdx = _path.Length - 1;
else
throw new InvalidOperationException("No path set");
}
/// <include file='docs/ConfigurationPathDoc.xml' path='doc/members/member[@name="M:GetCurrentSegmentParentPath"]/*'/>
public string GetCurrentSegmentParentPath()
{
if (_path == null || _path.Length == 0 || _currentSegmentIdx == -1)
return String.Empty;
// All parent nodes that lead to this one: "/root/parent1/parent2"
string separator = _separators.Length > 0? _separators[0].ToString() : "/";
StringBuilder sb = new StringBuilder(separator);
for (int i = 0; i < _currentSegmentIdx; i++)
{
sb.Append(_path[i]);
if (_currentSegmentIdx > i + 1)
sb.Append(separator);
}
return sb.ToString();
}
#endregion
}
}
|
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.
Vladimir Klisic is a half-human, half-software development engineer, currently working for Trema Laboratories in southeastern France (Sophia-Antipolis).