2014-07-26 07:12:56 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-08-31 11:41:24 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Collections.Specialized;
|
2014-09-12 15:22:24 +00:00
|
|
|
|
using System.ComponentModel;
|
2014-08-30 07:24:01 +00:00
|
|
|
|
using System.Diagnostics;
|
2014-09-12 15:22:24 +00:00
|
|
|
|
using System.IO;
|
2014-07-26 07:12:56 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml.Serialization;
|
2014-08-31 11:41:24 +00:00
|
|
|
|
using RainmeterStudio.Core.Utils;
|
2014-07-26 07:12:56 +00:00
|
|
|
|
|
2014-08-12 13:33:13 +00:00
|
|
|
|
namespace RainmeterStudio.Core.Model
|
2014-07-26 07:12:56 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reference to a file or folder
|
|
|
|
|
/// </summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
[DebuggerDisplay("QualifiedName = {QualifiedName}, StoragePath = {StoragePath}")]
|
2014-09-12 15:22:24 +00:00
|
|
|
|
public class Reference : INotifyCollectionChanged, INotifyPropertyChanged
|
2014-07-26 07:12:56 +00:00
|
|
|
|
{
|
2014-09-12 15:22:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The kind of item the reference points to
|
|
|
|
|
/// </summary>
|
|
|
|
|
public enum ReferenceTargetKind
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invalid state
|
|
|
|
|
/// </summary>
|
|
|
|
|
None,
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reference points to a file
|
|
|
|
|
/// </summary>
|
|
|
|
|
File,
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reference points to a directory
|
|
|
|
|
/// </summary>
|
|
|
|
|
Directory,
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reference points to a project
|
|
|
|
|
/// </summary>
|
|
|
|
|
Project
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-31 11:41:24 +00:00
|
|
|
|
private Dictionary<string, Reference> _children;
|
2014-09-12 15:22:24 +00:00
|
|
|
|
private Reference _parent;
|
|
|
|
|
private string _name, _storagePath;
|
|
|
|
|
private ReferenceTargetKind _kind;
|
|
|
|
|
|
|
|
|
|
#region Events
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Triggered when children are added or removed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Triggered when a property changes its value
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
|
|
|
|
#endregion
|
2014-08-31 11:41:24 +00:00
|
|
|
|
|
|
|
|
|
#region Properties
|
2014-08-30 07:24:01 +00:00
|
|
|
|
|
2014-08-16 14:09:08 +00:00
|
|
|
|
/// <summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// Gets or sets the parent of this reference
|
2014-08-16 14:09:08 +00:00
|
|
|
|
/// </summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
[XmlIgnore]
|
2014-09-12 15:22:24 +00:00
|
|
|
|
public Reference Parent
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _parent;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
// Unsubscribe from old parent
|
|
|
|
|
if (_parent != null)
|
|
|
|
|
_parent.PropertyChanged -= Parent_PropertyChanged;
|
|
|
|
|
|
|
|
|
|
// Set new parent
|
|
|
|
|
_parent = value;
|
|
|
|
|
|
|
|
|
|
// Subscribe to new parent
|
|
|
|
|
if (_parent != null)
|
|
|
|
|
_parent.PropertyChanged += Parent_PropertyChanged;
|
|
|
|
|
|
|
|
|
|
// Notify
|
|
|
|
|
if (PropertyChanged != null)
|
|
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs("Parent"));
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-31 11:41:24 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the children references
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlIgnore]
|
|
|
|
|
public ReadOnlyDictionary<string, Reference> ChildrenDictionary
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-09-12 15:22:24 +00:00
|
|
|
|
return new ReadOnlyDictionary<string, Reference>(_children);
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// Gets or sets children
|
2014-08-30 07:24:01 +00:00
|
|
|
|
/// </summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
[XmlArray("children")]
|
|
|
|
|
public Reference[] Children
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
return _children.Values.ToArray();
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
2014-08-31 11:41:24 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
Clear();
|
|
|
|
|
value.ForEach(Add);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the name of the reference
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlAttribute("name")]
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
2014-09-12 15:22:24 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _name;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_name = value;
|
|
|
|
|
|
|
|
|
|
if (PropertyChanged != null)
|
|
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
|
|
|
|
|
}
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
2014-07-26 07:12:56 +00:00
|
|
|
|
|
2014-08-16 14:09:08 +00:00
|
|
|
|
/// <summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// Gets the full qualified name of this reference
|
2014-08-16 14:09:08 +00:00
|
|
|
|
/// </summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
[XmlIgnore]
|
|
|
|
|
public string QualifiedName
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
if (Parent == null)
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
// Return name
|
|
|
|
|
return Name;
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
2014-08-31 11:41:24 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// If it has a parent, get the parent's name
|
|
|
|
|
return Parent.QualifiedName + '/' + Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-30 07:24:01 +00:00
|
|
|
|
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the parts of the full qualified name of this reference
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlIgnore]
|
|
|
|
|
public IEnumerable<string> QualifiedParts
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Parent == null)
|
|
|
|
|
{
|
|
|
|
|
return Enumerable.Repeat(Name, 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Parent.QualifiedParts.Append(Name);
|
|
|
|
|
}
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-26 07:12:56 +00:00
|
|
|
|
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the path to the file on the disk. If reference is in a project, the path should be relative.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlAttribute("storagePath")]
|
|
|
|
|
public string StoragePath
|
|
|
|
|
{
|
2014-09-12 15:22:24 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _storagePath;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_storagePath = value;
|
|
|
|
|
|
|
|
|
|
if (PropertyChanged != null)
|
|
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs("StoragePath"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-12 16:20:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the target kind
|
|
|
|
|
/// </summary>
|
2014-09-12 15:22:24 +00:00
|
|
|
|
[XmlAttribute("targetKind")]
|
|
|
|
|
public ReferenceTargetKind TargetKind
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _kind;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_kind = value;
|
|
|
|
|
|
|
|
|
|
if (PropertyChanged != null)
|
|
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs("Kind"));
|
|
|
|
|
}
|
2014-08-31 11:41:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
2014-08-16 14:09:08 +00:00
|
|
|
|
/// <summary>
|
2014-09-12 15:22:24 +00:00
|
|
|
|
/// Initializes the reference as a file reference
|
2014-08-16 14:09:08 +00:00
|
|
|
|
/// </summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
public Reference()
|
2014-09-12 15:22:24 +00:00
|
|
|
|
: this(null, null, ReferenceTargetKind.File)
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
}
|
2014-08-30 07:24:01 +00:00
|
|
|
|
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the reference
|
|
|
|
|
/// </summary>
|
2014-09-12 15:22:24 +00:00
|
|
|
|
/// <param name="name">Name of this reference</param>
|
|
|
|
|
/// <param name="kind">Reference kind</param>
|
|
|
|
|
public Reference(string name, ReferenceTargetKind kind)
|
|
|
|
|
: this(name, null, kind)
|
2014-08-31 11:41:24 +00:00
|
|
|
|
{
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-09-12 15:22:24 +00:00
|
|
|
|
/// Initializes the reference.
|
|
|
|
|
/// Kind is inferred by testing the file on disk.
|
2014-08-30 07:24:01 +00:00
|
|
|
|
/// </summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// <param name="name">Name of reference</param>
|
|
|
|
|
/// <param name="storagePath">Path to item on disk</param>
|
|
|
|
|
public Reference(string name, string storagePath)
|
2014-09-12 15:22:24 +00:00
|
|
|
|
: this(name, storagePath, InferKind(storagePath))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the reference
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Name of reference</param>
|
|
|
|
|
/// <param name="storagePath">Path to item on disk</param>
|
|
|
|
|
/// <param name="kind">Reference kind</param>
|
|
|
|
|
public Reference(string name, string storagePath, ReferenceTargetKind kind)
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
StoragePath = storagePath;
|
|
|
|
|
Name = name;
|
2014-09-12 15:22:24 +00:00
|
|
|
|
TargetKind = kind;
|
2014-08-31 11:41:24 +00:00
|
|
|
|
_children = new Dictionary<string, Reference>();
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-31 11:41:24 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2014-09-12 15:22:24 +00:00
|
|
|
|
#region Exists
|
|
|
|
|
|
2014-08-30 07:24:01 +00:00
|
|
|
|
/// <summary>
|
2014-09-12 15:22:24 +00:00
|
|
|
|
/// Checks if the file exists
|
2014-08-30 07:24:01 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2014-09-12 15:22:24 +00:00
|
|
|
|
public bool ExistsOnStorage()
|
2014-07-26 07:12:56 +00:00
|
|
|
|
{
|
2014-09-12 15:22:24 +00:00
|
|
|
|
if (StoragePath != null)
|
|
|
|
|
{
|
|
|
|
|
return File.Exists(StoragePath) || Directory.Exists(StoragePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2014-08-31 11:41:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-12 15:22:24 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Children/parenting operations
|
|
|
|
|
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a child reference
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reference"></param>
|
|
|
|
|
public void Add(Reference reference)
|
|
|
|
|
{
|
|
|
|
|
// Make sure object is not parented yet
|
|
|
|
|
if (reference.Parent != null)
|
|
|
|
|
throw new ArgumentException("Reference must be removed from its current parent first.");
|
|
|
|
|
|
|
|
|
|
// Add and parent
|
|
|
|
|
_children.Add(reference.Name, reference);
|
2014-09-12 16:20:21 +00:00
|
|
|
|
reference.Parent = this;
|
2014-08-31 11:41:24 +00:00
|
|
|
|
|
|
|
|
|
// Trigger event
|
|
|
|
|
if (CollectionChanged != null)
|
|
|
|
|
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, reference));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes a reference
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reference">Reference to remove</param>
|
|
|
|
|
/// <returns>True if removed successfully</returns>
|
|
|
|
|
public bool Remove(Reference reference)
|
|
|
|
|
{
|
|
|
|
|
// Make sure we are the parent
|
|
|
|
|
if (reference.Parent != this)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Remove
|
|
|
|
|
reference.Parent = null;
|
|
|
|
|
bool res = _children.Remove(reference.Name);
|
|
|
|
|
|
|
|
|
|
// Trigger event
|
|
|
|
|
if (CollectionChanged != null)
|
|
|
|
|
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, reference));
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes this reference from its parent
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if unparented successfully</returns>
|
|
|
|
|
public bool Unparent()
|
|
|
|
|
{
|
|
|
|
|
if (Parent != null)
|
|
|
|
|
return Parent.Remove(this);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes all children
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
// Unparent
|
|
|
|
|
foreach (var pair in _children)
|
|
|
|
|
{
|
|
|
|
|
pair.Value.Parent = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear
|
|
|
|
|
_children.Clear();
|
|
|
|
|
|
|
|
|
|
// Trigger event
|
|
|
|
|
if (CollectionChanged != null)
|
|
|
|
|
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the number of children
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Count
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _children.Count;
|
|
|
|
|
}
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-12 15:22:24 +00:00
|
|
|
|
private void Parent_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (PropertyChanged != null && (e.PropertyName == "Parent" || e.PropertyName == "Name" || e.PropertyName == "QualifiedName"))
|
|
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs("QualifiedName"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Object overrides
|
|
|
|
|
|
2014-08-16 14:09:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Compares a reference to another objects
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="obj">Another object</param>
|
|
|
|
|
/// <returns>True if objects are equal</returns>
|
2014-07-26 07:12:56 +00:00
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
2014-08-30 07:24:01 +00:00
|
|
|
|
if (obj is Reference)
|
|
|
|
|
{
|
|
|
|
|
Reference other = (Reference)obj;
|
2014-08-31 11:41:24 +00:00
|
|
|
|
return (String.Equals(QualifiedName, other.QualifiedName));
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-16 14:09:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Obtains the hash code of this reference
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Hash code</returns>
|
2014-07-26 07:12:56 +00:00
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
return QualifiedName.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the string representation of this reference
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>String representation</returns>
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return QualifiedName;
|
|
|
|
|
}
|
2014-08-30 07:24:01 +00:00
|
|
|
|
|
2014-09-12 15:22:24 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Helper methods
|
|
|
|
|
|
|
|
|
|
private static ReferenceTargetKind InferKind(string storagePath)
|
|
|
|
|
{
|
|
|
|
|
ReferenceTargetKind kind = ReferenceTargetKind.None;
|
|
|
|
|
|
|
|
|
|
if (Path.GetExtension(storagePath) == ".rsproj")
|
|
|
|
|
kind = ReferenceTargetKind.Project;
|
|
|
|
|
|
|
|
|
|
else if (File.Exists(storagePath))
|
|
|
|
|
kind = ReferenceTargetKind.File;
|
|
|
|
|
|
|
|
|
|
else if (Directory.Exists(storagePath))
|
|
|
|
|
kind = ReferenceTargetKind.Directory;
|
|
|
|
|
|
|
|
|
|
return kind;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2014-08-31 11:41:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides useful methods for references
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class ReferenceExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tries to get a reference from the same tree having specified qualified name
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="this">Reference contained in the tree</param>
|
|
|
|
|
/// <param name="qualifiedName">Full qualified name</param>
|
|
|
|
|
/// <param name="output">Found reference</param>
|
|
|
|
|
/// <returns>True if succeeded to find the reference</returns>
|
|
|
|
|
public static bool TryGetReference(this Reference @this, string qualifiedName, out Reference output)
|
|
|
|
|
{
|
|
|
|
|
var thisQualifiedName = @this.QualifiedName;
|
|
|
|
|
|
|
|
|
|
// Am I the reference? return myself
|
|
|
|
|
if (qualifiedName.Equals(thisQualifiedName))
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
output = @this;
|
|
|
|
|
return true;
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
2014-08-31 11:41:24 +00:00
|
|
|
|
|
|
|
|
|
// Qualified name is a child, look child up
|
|
|
|
|
else if (qualifiedName.StartsWith(thisQualifiedName))
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
int startIndex = thisQualifiedName.Length + 1;
|
|
|
|
|
int endIndex = qualifiedName.IndexOf('/', startIndex);
|
|
|
|
|
|
|
|
|
|
string child;
|
|
|
|
|
Reference childRef;
|
|
|
|
|
|
|
|
|
|
if (endIndex < 0)
|
|
|
|
|
{
|
|
|
|
|
child = qualifiedName.Substring(startIndex);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
child = qualifiedName.Substring(startIndex, endIndex - startIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to get child
|
|
|
|
|
if (@this.ChildrenDictionary.TryGetValue(child, out childRef))
|
|
|
|
|
{
|
|
|
|
|
return childRef.TryGetReference(qualifiedName, out output);
|
|
|
|
|
}
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
2014-07-26 07:12:56 +00:00
|
|
|
|
|
2014-08-31 11:41:24 +00:00
|
|
|
|
// Qualified name is not a child and not 'this', so ask parent to find it
|
|
|
|
|
else if (@this.Parent != null)
|
|
|
|
|
{
|
|
|
|
|
return @this.Parent.TryGetReference(qualifiedName, out output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Failed to find child
|
|
|
|
|
output = null;
|
|
|
|
|
return false;
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
2014-08-30 07:24:01 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// Gets a reference from the same tree having specified qualified name
|
2014-08-30 07:24:01 +00:00
|
|
|
|
/// </summary>
|
2014-08-31 11:41:24 +00:00
|
|
|
|
/// <param name="this">Reference contained in the tree</param>
|
|
|
|
|
/// <param name="qualifiedName">Full qualified name</param>
|
|
|
|
|
/// <returns>Found reference</returns>
|
|
|
|
|
/// <exception cref="ArgumentException">If qualified name not found</exception>
|
|
|
|
|
public static Reference GetReference(this Reference @this, string qualifiedName)
|
|
|
|
|
{
|
|
|
|
|
Reference res;
|
|
|
|
|
|
|
|
|
|
if (TryGetReference(@this, qualifiedName, out res))
|
|
|
|
|
{
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Could not find reference.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if a reference is in the same tree as this
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="this">Reference that is in the tree we search in</param>
|
|
|
|
|
/// <param name="other">Reference to search</param>
|
|
|
|
|
/// <returns>True if the tree contains the reference.</returns>
|
|
|
|
|
public static bool TreeContains(this Reference @this, Reference reference)
|
2014-08-30 07:24:01 +00:00
|
|
|
|
{
|
2014-08-31 11:41:24 +00:00
|
|
|
|
Reference temp;
|
|
|
|
|
return TryGetReference(@this, reference.QualifiedName, out temp);
|
2014-08-30 07:24:01 +00:00
|
|
|
|
}
|
2014-07-26 07:12:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|