internal static XmlNode ProcessSageLinkElement(SageContext context, XmlNode node) { Contract.Requires<ArgumentNullException>(node != null); if (node.SelectSingleElement("ancestor::sage:literal", XmlNamespaces.Manager) != null) return node; XmlElement linkElem = (XmlElement) node; string linkName = context.ProcessText(linkElem.GetAttribute("ref")); bool rawString = linkElem.GetAttribute("raw").EqualsAnyOf("1", "yes", "true"); if (!string.IsNullOrEmpty(linkName) && rawString) { if (context.Url.Links.ContainsKey(linkName)) { linkElem.InnerText = context.Url.Links[linkName].Value; } return linkElem; } string linkHref = context.Url.GetUrl(linkElem); if (!string.IsNullOrEmpty(linkHref)) { linkElem.SetAttribute("href", linkHref); } foreach (XmlNode child in node.ChildNodes) { XmlNode processed = NodeEvaluator.GetNodeHandler(child)(context, child); if (processed != null) node.ReplaceChild(processed, child); else node.RemoveChild(child); } return linkElem; }
internal static XmlNode ProcessSageUrlElement(SageContext context, XmlNode node) { Contract.Requires<ArgumentNullException>(node != null); if (node.SelectSingleElement("ancestor::sage:literal", XmlNamespaces.Manager) != null) return node; string linkHref = context.Url.GetUrl((XmlElement) node); if (!string.IsNullOrEmpty(linkHref)) { if (node.NodeType == XmlNodeType.Element) return node.OwnerDocument.CreateTextNode(linkHref); } return node; }
internal static XmlNode ProcessHrefAttribute(SageContext context, XmlNode node) { Contract.Requires<ArgumentNullException>(node != null); if (node.SelectSingleElement("ancestor::sage:literal", XmlNamespaces.Manager) != null) return node; node.InnerText = context.ProcessText(node.InnerText); return node; }
internal static XmlNode ProcessSageBaseHrefElement(SageContext context, XmlNode node) { Contract.Requires<ArgumentNullException>(node != null); if (node.SelectSingleElement("ancestor::sage:literal", XmlNamespaces.Manager) != null) return node; XmlElement result = node.OwnerDocument.CreateElement("base", XmlNamespaces.XHtmlNamespace); result.SetAttribute("href", context.BaseHref); return result; }
internal static XmlNode ProcessContextSwitchNode(SageContext context, XmlNode switchNode) { if (switchNode.SelectSingleElement("ancestor::sage:literal", XmlNamespaces.Manager) != null) return switchNode; XmlElement switchElem = (XmlElement) switchNode; string propName = switchElem.GetAttribute("property"); string key = switchElem.GetAttribute("key"); if (string.IsNullOrEmpty(propName)) { log.ErrorFormat("The switch node in document '{0}' is missing the required property attribute", switchNode.OwnerDocument.BaseURI); return switchNode; } string propValue = SageContext.GetContextProperty(context, propName, key); int caseNum = 0; bool caseFound = false; XmlDocumentFragment result = switchNode.OwnerDocument.CreateDocumentFragment(); foreach (XmlElement caseNode in switchNode.SelectNodes("context:case", XmlNamespaces.Manager)) { string testValue = caseNode.GetAttribute("test"); if (string.IsNullOrEmpty(testValue)) { log.WarnFormat( "The case node with index {0} of switch node '{1}' in document '{2}' didn't specify a test condition.", caseNum, propName, switchNode.OwnerDocument.BaseURI); continue; } if (testValue.Equals(propValue, StringComparison.InvariantCultureIgnoreCase)) { foreach (XmlNode node in caseNode.SelectNodes("node()")) result.AppendChild(context.ProcessNode(node)); caseFound = true; break; } caseNum += 1; } if (!caseFound) { XmlNode defaultNode = switchNode.SelectSingleNode("context:default", XmlNamespaces.Manager); if (defaultNode != null) { foreach (XmlNode node in defaultNode.SelectNodes("node()")) result.AppendChild(context.ProcessNode(node)); } } return result; }
internal static XmlNode ProcessContextIfNode(SageContext context, XmlNode node) { if (node.SelectSingleElement("ancestor::sage:literal", XmlNamespaces.Manager) != null) return node; XmlElement element = (XmlElement) node; XmlDocumentFragment result = node.OwnerDocument.CreateDocumentFragment(); string expression = element.GetAttribute("expression"); string propName = element.GetAttribute("property"); string key = element.GetAttribute("key"); string equals = element.GetAttribute("equals"); string notEquals = element.GetAttribute("not-equals"); bool nodeValid = false; if (!string.IsNullOrWhiteSpace(expression)) { var text = context.textEvaluator.Process(expression); nodeValid = !string.IsNullOrWhiteSpace(text); } else { string propValue = SageContext.GetContextProperty(context, propName, key); if (notEquals != string.Empty) nodeValid = !propValue.Equals(notEquals, StringComparison.InvariantCultureIgnoreCase); else if (equals != string.Empty) nodeValid = propValue.Equals(equals, StringComparison.InvariantCultureIgnoreCase); } if (!nodeValid) return null; foreach (XmlNode child in node.SelectNodes("node()")) result.AppendChild(context.ProcessNode(child)); return result; }
internal static XmlNode ProcessBaseHrefNode(SageContext context, XmlNode node) { if (node.SelectSingleElement("ancestor::sage:literal", XmlNamespaces.Manager) != null) return node; XmlElement result = node.OwnerDocument.CreateElement("base", XmlNamespaces.XHtmlNamespace); result.SetAttribute("href", context.BaseHref); return result; }
internal static XmlNode ProcessSageVersionNode(SageContext context, XmlNode node) { if (node.SelectSingleElement("ancestor::sage:literal", XmlNamespaces.Manager) != null) return node; string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); return node.OwnerDocument.CreateTextNode(version); }
internal static XmlNode ProcessContextValueNode(SageContext context, XmlNode node) { if (node.SelectSingleElement("ancestor::sage:literal", XmlNamespaces.Manager) != null) return node; XmlElement valueElem = (XmlElement) node; string xpath = valueElem.GetAttribute("xpath"); if (!string.IsNullOrWhiteSpace(xpath)) { XmlNode value = node.SelectSingleNode(xpath, XmlNamespaces.Manager); node.OwnerDocument.CreateTextNode(value.InnerText); } string propName = valueElem.GetAttribute("property"); if (string.IsNullOrWhiteSpace(propName)) return node.OwnerDocument.CreateTextNode(context.textEvaluator.Process(valueElem.InnerText)); string propKey = valueElem.GetAttribute("key"); string propValue = SageContext.GetContextProperty(context, propName, propKey); if (propValue != null) return node.OwnerDocument.CreateTextNode(propValue); // default: return node as is return node; }