public ModuleResult ProcessElement(XmlElement moduleElement, ViewConfiguration configuration)
{
SageContext context = configuration.Context;
ModuleResult result = new ModuleResult(moduleElement);
XmlNode pathNode = moduleElement.SelectSingleNode("mod:config/mod:source/mod:path", nm);
XmlNode selectionNode = moduleElement.SelectSingleNode("mod:config/mod:source/mod:xpath", nm);
XmlNodeList namespaceNodes = moduleElement.SelectNodes("mod:config/mod:source/mod:namespaces/mod:namespace", nm);
string xpath = selectionNode != null ? selectionNode.InnerText : null;
if (pathNode == null)
{
result.Status = ModuleResultStatus.None;
return result;
}
string path = pathNode.InnerText.Trim();
if (string.IsNullOrEmpty(path))
{
log.WarnFormat("The path element shouldn't be empty.");
result.Status = ModuleResultStatus.ModuleWarning;
return result;
}
path = context.Path.Substitute(path);
if (!Path.IsPathRooted(path))
{
string configDirectory = Path.GetDirectoryName(configuration.Info.ConfigPath);
path = Path.Combine(configDirectory, path);
}
string resolved = context.Path.Resolve(path);
if (!File.Exists(resolved))
{
log.WarnFormat("The specified xml path '{0}' ('{1}') doesn't exist", path, resolved);
result.Status = ModuleResultStatus.ModuleWarning;
return result;
}
XmlNode selection;
try
{
selection = context.Resources.LoadXml(resolved);
}
catch (Exception ex)
{
log.ErrorFormat("Failed to add specified file '{0}' ('{1}') as XML document: {2}",
path, resolved, ex.Message);
result.Status = ModuleResultStatus.ModuleError;
return result;
}
if (!string.IsNullOrEmpty(xpath))
{
try
{
XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
manager.AddNamespace("default", XmlNamespaces.XHtmlNamespace);
xpath = defaultNamespaceNodes.Replace(xpath, "$1default:$2$3");
foreach (XmlElement node in namespaceNodes)
{
string prefix = node.GetAttribute("prefix");
manager.AddNamespace(prefix, node.InnerText.Trim());
}
selection = selection.SelectSingleNode(xpath, manager);
if (selection == null)
{
log.WarnFormat("The xpath expression '{0}' yielded no result.", xpath);
result.Status = ModuleResultStatus.ModuleWarning;
return result;
}
}
catch (Exception ex)
{
log.ErrorFormat("Attempting to select using xpath expression '{0}' resulted in error: {1}.",
xpath, ex.Message);
result.Status = ModuleResultStatus.ModuleError;
return result;
}
}
else if (selectionNode != null)
{
log.WarnFormat("The selection element shouldn't be empty.");
result.Status = ModuleResultStatus.ModuleWarning;
}
result.AppendDataElement(selection);
return result;
}