//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation, Scott Hanselman. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Xrds {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.XPath;
using DotNetOpenAuth.OpenId;
///
/// The Service element in an XRDS document.
///
internal class ServiceElement : XrdsNode, IComparable {
///
/// Initializes a new instance of the class.
///
/// The service element.
/// The parent.
public ServiceElement(XPathNavigator serviceElement, XrdElement parent) :
base(serviceElement, parent) {
}
///
/// Gets the XRD parent element.
///
public XrdElement Xrd {
get { return (XrdElement)ParentNode; }
}
///
/// Gets the priority.
///
public int? Priority {
get {
XPathNavigator n = Node.SelectSingleNode("@priority", XmlNamespaceResolver);
return n != null ? n.ValueAsInt : (int?)null;
}
}
///
/// Gets the URI child elements.
///
public IEnumerable UriElements {
get {
List uris = new List();
foreach (XPathNavigator node in Node.Select("xrd:URI", XmlNamespaceResolver)) {
uris.Add(new UriElement(node, this));
}
uris.Sort();
return uris;
}
}
///
/// Gets the type child elements.
///
/// The type elements.
public IEnumerable TypeElements {
get {
foreach (XPathNavigator node in Node.Select("xrd:Type", XmlNamespaceResolver)) {
yield return new TypeElement(node, this);
}
}
}
///
/// Gets the type child element's URIs.
///
public string[] TypeElementUris {
get {
return this.TypeElements.Select(type => type.Uri).ToArray();
}
}
///
/// Gets the OP Local Identifier.
///
public Identifier ProviderLocalIdentifier {
get {
var n = Node.SelectSingleNode("xrd:LocalID", XmlNamespaceResolver)
?? Node.SelectSingleNode("openid10:Delegate", XmlNamespaceResolver);
if (n != null && n.Value != null) {
string value = n.Value.Trim();
if (value.Length > 0) {
return n.Value;
}
}
return null;
}
}
#region IComparable Members
///
/// Compares the current object with another object of the same type.
///
/// An object to compare with this object.
///
/// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings:
/// Value
/// Meaning
/// Less than zero
/// This object is less than the parameter.
/// Zero
/// This object is equal to .
/// Greater than zero
/// This object is greater than .
///
public int CompareTo(ServiceElement other) {
if (other == null) {
return -1;
}
if (this.Priority.HasValue && other.Priority.HasValue) {
return this.Priority.Value.CompareTo(other.Priority.Value);
} else {
if (this.Priority.HasValue) {
return -1;
} else if (other.Priority.HasValue) {
return 1;
} else {
return 0;
}
}
}
#endregion
}
}