diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2009-09-20 21:18:59 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2009-09-21 08:06:22 -0700 |
commit | bbe3f9cc9c8a1e5909273c1a162a63ea7a66afd8 (patch) | |
tree | c91f66e642c4d26fca266e226b3f2765f546d700 /tools/Sandcastle/Source/BuildAssembler/BuildComponents/ForEachComponent.cs | |
parent | 627014f0bbc3fd576277375e70f8391d150b0a67 (diff) | |
download | DotNetOpenAuth-bbe3f9cc9c8a1e5909273c1a162a63ea7a66afd8.zip DotNetOpenAuth-bbe3f9cc9c8a1e5909273c1a162a63ea7a66afd8.tar.gz DotNetOpenAuth-bbe3f9cc9c8a1e5909273c1a162a63ea7a66afd8.tar.bz2 |
Switched out the Sandcastle binaries for the source code.
Diffstat (limited to 'tools/Sandcastle/Source/BuildAssembler/BuildComponents/ForEachComponent.cs')
-rw-r--r-- | tools/Sandcastle/Source/BuildAssembler/BuildComponents/ForEachComponent.cs | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/tools/Sandcastle/Source/BuildAssembler/BuildComponents/ForEachComponent.cs b/tools/Sandcastle/Source/BuildAssembler/BuildComponents/ForEachComponent.cs new file mode 100644 index 0000000..d073daf --- /dev/null +++ b/tools/Sandcastle/Source/BuildAssembler/BuildComponents/ForEachComponent.cs @@ -0,0 +1,101 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Xml; +using System.Xml.XPath; + +namespace Microsoft.Ddue.Tools { + + public class ForEachComponent : BuildComponent { + + public ForEachComponent (BuildAssembler assembler, XPathNavigator configuration) : base(assembler, configuration) { + + // set up the context + XPathNodeIterator context_nodes = configuration.Select("context"); + foreach (XPathNavigator context_node in context_nodes) + { + string prefix = context_node.GetAttribute("prefix", String.Empty); + string name = context_node.GetAttribute("name", String.Empty); + context.AddNamespace(prefix, name); + } + + // load the expression format + XPathNavigator variable_node = configuration.SelectSingleNode("variable"); + if (variable_node == null) throw new ConfigurationErrorsException("When instantiating a ForEach component, you must specify a variable using the <variable> element."); + string xpath_format = variable_node.GetAttribute("expression", String.Empty); + if ((xpath_format == null) || (xpath_format.Length == 0)) throw new ConfigurationErrorsException("When instantiating a ForEach component, you must specify a variable expression using the expression attribute"); + xpath = XPathExpression.Compile(xpath_format); + + // load the subcomponents + WriteMessage(MessageLevel.Info, "Loading subcomponents."); + XPathNavigator components_node = configuration.SelectSingleNode("components"); + if (components_node == null) throw new ConfigurationErrorsException("When instantiating a ForEach component, you must specify subcomponents using the <components> element."); + + components = BuildAssembler.LoadComponents(components_node); + + WriteMessage(MessageLevel.Info, String.Format("Loaded {0} subcomponents.", components.Count)); + + } + + // the format string for the variable expression + private XPathExpression xpath; + + // the xpath context + private CustomContext context = new CustomContext(); + + // the subcomponents + private ICollection<BuildComponent> components; + + // the work of the component + + public override void Apply (XmlDocument document, string key) { + + // adjust the context + context["key"] = key; + + // evaluate the condition + XPathExpression xpath_local = xpath.Clone(); + xpath_local.SetContext(context); + + Object result = document.CreateNavigator().Evaluate(xpath_local); + + // try to intrepret the result as a node set + XPathNodeIterator result_node_iterator = result as XPathNodeIterator; + + if (result_node_iterator != null) { + XPathNavigator[] result_nodes = BuildComponentUtilities.ConvertNodeIteratorToArray(result_node_iterator); + //Console.WriteLine("{0} node-set result", result_nodes.Length); + // if it is, apply the child components to each node value + foreach (XPathNavigator result_node in result_nodes) { + // Console.WriteLine(result_node.Value); + ApplyComponents(document, result_node.Value); + } + } else { + //Console.WriteLine("non-node-set result"); + // if it isn't, apply the child components to the string value of the result + ApplyComponents(document, result.ToString()); + + } + + + } + + private void ApplyComponents (XmlDocument document, string key) { + foreach (BuildComponent component in components) { + component.Apply(document, key); + } + } + + + public override void Dispose () { + foreach (BuildComponent component in components) { + component.Dispose(); + } + base.Dispose(); + } + + } + +}
\ No newline at end of file |