//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Xrds {
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.XPath;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId;
///
/// An XRDS document.
///
internal class XrdsDocument : XrdsNode {
///
/// The namespace used by XML digital signatures.
///
private const string XmlDSigNamespace = "http://www.w3.org/2000/09/xmldsig#";
///
/// The namespace used by Google Apps for Domains for OpenID URI templates.
///
private const string GoogleOpenIdNamespace = "http://namespace.google.com/openid/xmlns";
///
/// Initializes a new instance of the class.
///
/// The root node of the XRDS document.
public XrdsDocument(XPathNavigator xrdsNavigator)
: base(xrdsNavigator) {
XmlNamespaceResolver.AddNamespace("xrd", XrdsNode.XrdNamespace);
XmlNamespaceResolver.AddNamespace("xrds", XrdsNode.XrdsNamespace);
XmlNamespaceResolver.AddNamespace("openid10", Protocol.V10.XmlNamespace);
XmlNamespaceResolver.AddNamespace("ds", XmlDSigNamespace);
XmlNamespaceResolver.AddNamespace("google", GoogleOpenIdNamespace);
}
///
/// Initializes a new instance of the class.
///
/// The Xml reader positioned at the root node of the XRDS document.
public XrdsDocument(XmlReader reader)
: this(new XPathDocument(reader).CreateNavigator()) { }
///
/// Initializes a new instance of the class.
///
/// The text that is the XRDS document.
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Fixing would decrease readability, and not likely avoid any finalizer on a StringReader anyway.")]
public XrdsDocument(string xml)
: this(new XPathDocument(new StringReader(xml)).CreateNavigator()) { }
///
/// Gets the XRD child elements of the document.
///
public IEnumerable XrdElements {
get {
// We may be looking at a full XRDS document (in the case of YADIS discovery)
// or we may be looking at just an individual XRD element from a larger document
// if we asked xri.net for just one.
if (Node.SelectSingleNode("/xrds:XRDS", XmlNamespaceResolver) != null) {
foreach (XPathNavigator node in Node.Select("/xrds:XRDS/xrd:XRD", XmlNamespaceResolver)) {
yield return new XrdElement(node, this);
}
} else {
XPathNavigator node = Node.SelectSingleNode("/xrd:XRD", XmlNamespaceResolver);
if (node != null) {
yield return new XrdElement(node, this);
}
}
}
}
///
/// Gets a value indicating whether all child XRD elements were resolved successfully.
///
internal bool IsXrdResolutionSuccessful {
get { return this.XrdElements.All(xrd => xrd.IsXriResolutionSuccessful); }
}
}
}