private void Normalize()
{
var parts = Split();
var newParts = new List<string>();
foreach (var part in parts)
{
// handle (and remove) relative directory directives
if (part.Length == 0)
{
// empty parts might be double slashes and can be skipped. start and end are
// handled separately below
continue;
}
if (part.Equals("..") && newParts.Count > 0 && !newParts.Last().Equals(".."))
{
newParts.RemoveAt(newParts.Count - 1);
}
else if (part.Equals("."))
{
// "." can just be skipped if not at the beginning
continue;
}
else
{
newParts.Add(part);
}
}
// absolute path means we shouldn't have a "." or ".." at the beginning
if (IsAbsolutePath())
{
if (newParts.Count == 0)
{
_path = CorrectSlash;
return;
}
if (newParts[0].Equals(".."))
{
// "/../foo" is invalid
var msg = String.Format("Invalid path '{0}' goes beyond root directory",
String.Join(CorrectSlash, newParts));
throw new CmisPathException(msg);
}
// makes sure we get a CorrectSlash infront when joining
newParts.Insert(0, "");
}
// check if we should prepend a "."
else if (parts.Length > 0 && parts[0].Equals(".") &&
newParts.Count > 0 && !newParts[0].Equals(".."))
{
newParts.Insert(0, ".");
}
var newPath = String.Join(CorrectSlash, newParts);
// check for a trailing slash to preserve
if (HasTrailingSlash())
{
newPath += CorrectSlash;
}
_path = newPath;
}
}