public XmlShortcutFile(string filePath) { _doc = new XmlDocument(); _doc.Load(filePath); _globalMacros = new XmlMacroCollection(); _globalMacros.AddMacros(_doc.DocumentElement); }
/** * Construct a new macro collection that copies from an existing one */ public XmlMacroCollection(XmlMacroCollection CopyFrom) { if (CopyFrom._nodes != null) { _nodes = new Dictionary<XmlNode, bool>(CopyFrom._nodes); } if (CopyFrom._macros != null) { _macros = new Dictionary<string, XmlMacroValue>(CopyFrom._macros); } }
/** * Enumerates through the given node and upwards through his template hierarchy */ IEnumerable<XmlNode> NodeTemplateTree(XmlNode node, XmlMacroCollection macros) { // check if we define this, or any of our templates for (var templateNode = node; templateNode != null; templateNode = GetNodeTemplate(templateNode, macros)) { yield return templateNode; } }
public XmlVirtualShortcut( string inName, XmlNode inContextNode, XmlNode inShortcutNode, XmlMacroCollection inMacros ) { JArgumentEmptyException.Check(inName, "inName"); JArgumentNullException.Check(inContextNode, "inContextNode"); JArgumentNullException.Check(inShortcutNode, "inShortcutNode"); JArgumentNullException.Check(inMacros, "inMacros"); Name = inName; ContextNode = inContextNode; ShortcutNode = inShortcutNode; Macros = inMacros; }
/** * Gets the node the given node inherits from, if it exists. */ XmlNode GetNodeTemplate(XmlNode node, XmlMacroCollection macros) { string templateName; if (GetAttributeValue(node, macros, "inherits", out templateName)) { if (!String.IsNullOrEmpty(templateName)) { var templateNode = FindTemplate(node.ParentNode, macros, templateName); if (templateNode != null) { return templateNode; } } throw new JRunXmlException(node, "Could not find template \"{0}\"", templateName); } return null; }
/** * Retrieve the macro-expanded text value of a child of an xml node. * Properties are inheritable from templates. * Returns true if the property is defined. */ bool GetPropertyValue(XmlNode node, XmlMacroCollection macros, string propertyName, out string value) { value = null; // check if we define this, or any of our templates foreach (var templateNode in NodeTemplateTree(node, macros)) { var childNode = templateNode.SelectSingleNode(propertyName); if (childNode != null) { value = childNode.InnerText; break; } } if (value != null) { try { value = macros.GetExpandedValue(value); } catch (Exception ex) { throw new JRunXmlException(node, ex, "Could not expand macro value for property \"{0}\"", propertyName); } } return value != null; }
/** * Returns all shortcut nodes that are in the given node and the node's templates. */ IEnumerable<XmlNode> GetAllShortcutNodes(XmlNode listNode, XmlMacroCollection macros) { foreach (var template in NodeTemplateTree(listNode, macros)) { foreach (var node in template.SelectXmlNodes("Shortcut")) { yield return node; } } }
/** * Calculates the macro-expanded value of an attribute on the node. * Returns true if the attribute is defined. */ bool GetAttributeValue(XmlNode node, XmlMacroCollection macros, string attribName, out string value) { value = node.GetAttributeValue(attribName); if (value != null) { try { value = macros.GetExpandedValue(value); } catch (Exception ex) { throw new JRunXmlException(node, ex, "Could not expand macro value for attribute \"{0}\"", attribName); } } return value != null; }
/** * Searches up the parent node tree to find a template by the given name */ XmlNode FindTemplate(XmlNode start, XmlMacroCollection macros, string templateName) { foreach (var nodeObj in start.ChildNodes) { var node = nodeObj as XmlNode; if (node != null) { if (AttributeValueMatches(node, macros, "name", templateName)) { return node; } } } if (start.ParentNode != null) { return FindTemplate(start.ParentNode, macros, templateName); } return null; }
/** * Creates a virtual shortcut for the given context and node */ XmlVirtualShortcut CreateVirtualShortcut(XmlNode context, XmlNode shortcutNode, XmlMacroCollection outerMacros) { var nodeName = shortcutNode.GetAttributeValue("name"); if (!String.IsNullOrEmpty(nodeName)) { var realName = outerMacros.GetExpandedValue(nodeName); if (!String.IsNullOrEmpty(realName)) { var newMacros = new XmlMacroCollection(outerMacros); AddTemplateMacros(shortcutNode, newMacros); return new XmlVirtualShortcut(realName, context, shortcutNode, newMacros); } } return null; }
/** * Gets the macro-expanded value of the named attribute and returns true * if it matches the given value check (ignores case) */ bool AttributeValueMatches(XmlNode node, XmlMacroCollection macros, string attribName, string valueCheck) { string nodeValue; if (GetAttributeValue(node, macros, attribName, out nodeValue)) { if (StringComparer.OrdinalIgnoreCase.Equals(nodeValue, valueCheck)) { return true; } } return false; }
/** * Recursively add the node and his template's macros */ void AddTemplateMacros(XmlNode node, XmlMacroCollection macros) { var templateNode = GetNodeTemplate(node, macros); if (templateNode != null) { AddTemplateMacros(templateNode, macros); } macros.AddMacros(node); }
/** * Retrieves all shortcut xml nodes from the given xml file and profile name. */ public IEnumerable<XmlVirtualShortcut> GetAllVirtualShortcuts(string profileName) { List<XmlVirtualShortcut> shortCuts = new List<XmlVirtualShortcut>(); foreach (var profile in _doc.DocumentElement.SelectXmlNodes("Profile")) { if (String.IsNullOrEmpty(profileName) || AttributeValueMatches(profile, _globalMacros, "name", profileName)) { var profileMacros = new XmlMacroCollection(_globalMacros); AddTemplateMacros(profile, profileMacros); // get all shortcut nodes defined directly in the profile foreach (var shortcutNode in GetAllShortcutNodes(profile, profileMacros)) { var vShortcut = CreateVirtualShortcut(shortcutNode, shortcutNode, profileMacros); if (vShortcut != null) { shortCuts.Add(vShortcut); } } // get all shortcut nodes defined in lists foreach (var profileTemplate in NodeTemplateTree(profile, profileMacros)) { foreach (var listNode in profileTemplate.SelectXmlNodes("List")) { var listMacros = new XmlMacroCollection(profileMacros); AddTemplateMacros(listNode, listMacros); foreach (var shortcutNode in GetAllShortcutNodes(listNode, listMacros)) { var vShortcut = CreateVirtualShortcut(shortcutNode, shortcutNode, listMacros); if (vShortcut != null) { shortCuts.Add(vShortcut); } } } } break; } } return shortCuts; }