/// <summary>Get the allowed regular expressions defined in the provided <see cref="XmlElement"/>. Used for tag rules or CSS rules.</summary> /// <param name="node">The node to retrieve the values from.</param> /// <param name="elementName">The name of the element which has regular expressions defined.</param> /// <param name="parseContext">The parse context.</param> /// <returns>A list with the allowed regular expressions.</returns> private static List <string> GetAllowedRegexpsForRules(XmlElement node, string elementName, ParseContext parseContext) { var allowedList = new List <string>(); foreach (XmlElement regExNode in PolicyParserUtil.GetGrandchildrenByTagNames(node, "regexp-list", "regexp")) { string regExName = XmlUtil.GetAttributeValue(regExNode, "name"); string value = XmlUtil.GetAttributeValue(regExNode, "value"); /* * Look up common regular expression specified by the "name" field. They can put a common * name in the "name" field or provide a custom value in the "value" field. They must choose * one or the other, not both. */ if (!string.IsNullOrEmpty(regExName)) { string pattern = regExName == null ? null : parseContext.commonRegularExpressions.GetValueOrTypeDefault(regExName); if (pattern != null) { allowedList.Add(pattern); } else { throw new PolicyException($"Regular expression '{regExName}' was referenced as a common regexp in definition of '{elementName}', but does not exist in <common-regexp>."); } } else if (!string.IsNullOrEmpty(value)) { // TODO: See if I need to reimplement pattern.compile allowedList.Add(value); } } return(allowedList); }
/// <summary> Go through the <common-regexps> section of the policy file.</summary> /// <param name="commonRegularExpressionListNode">Top level of <common-regexps>.</param> /// <param name="parseContext">The <see cref="ParseContext"/> containing the common regular expressions dictionary to fill.</param> private static void ParseCommonRegExps(XmlNode commonRegularExpressionListNode, ParseContext parseContext) { foreach (XmlElement node in PolicyParserUtil.GetChildrenByTagName(commonRegularExpressionListNode, "regexp")) { string name = XmlUtil.GetAttributeValue(node, "name"); if (!parseContext.commonRegularExpressions.ContainsKey(name)) { string value = XmlUtil.GetAttributeValue(node, "value"); parseContext.commonRegularExpressions.Add(name, value); } } }
/// <summary> Go through <directives> section of the policy file.</summary> /// <param name="directiveListNode">Top level of <directives></param> /// <param name="parseContext">The <see cref="ParseContext"/> containing the directives dictionary to fill.</param> private static void ParseDirectives(XmlNode directiveListNode, ParseContext parseContext) { foreach (XmlElement node in PolicyParserUtil.GetChildrenByTagName(directiveListNode, "directive")) { string name = XmlUtil.GetAttributeValue(node, "name"); if (!parseContext.directives.ContainsKey(name)) { string value = XmlUtil.GetAttributeValue(node, "value"); parseContext.directives.Add(name, value); } } }
/// <summary>Get the allowed attributes defined in the provided tag <see cref="XmlElement"/>.</summary> /// <param name="tagNode">The node to retrieve the values from.</param> /// <param name="tagName">The name of the tag which has attributes defined.</param> /// <param name="parseContext">The parse context.</param> /// <returns>A dictionary with the allowed attributes.</returns> private static Dictionary <string, Attribute> GetTagAllowedAttributes(XmlElement tagNode, string tagName, ParseContext parseContext) { var allowedAttributes = new Dictionary <string, Attribute>(); foreach (XmlElement attributeNode in PolicyParserUtil.GetChildrenByTagName(tagNode, "attribute")) { string attributeName = XmlUtil.GetAttributeValue(attributeNode, "name"); if (!attributeNode.HasChildNodes) { /* All they provided was the name, so they must want a common attribute. */ Attribute attribute = parseContext.commonAttributes.GetValueOrTypeDefault(attributeName.ToLowerInvariant()); if (attribute != null) { /* If they provide onInvalid/description values here they will override the common values. */ string onInvalid = XmlUtil.GetAttributeValue(attributeNode, "onInvalid"); string description = XmlUtil.GetAttributeValue(attributeNode, "description"); if (!string.IsNullOrEmpty(onInvalid)) { attribute.OnInvalid = onInvalid; } if (!string.IsNullOrEmpty(description)) { attribute.Description = description; } allowedAttributes.Add(attributeName, attribute.Clone() as Attribute); } else { throw new PolicyException($"Attribute '{XmlUtil.GetAttributeValue(attributeNode, "name")}' was referenced as a common attribute in definition of '{tagName}', but does not exist in <common-attributes>"); } } else { /* Custom attribute for this tag */ var attribute = new Attribute(XmlUtil.GetAttributeValue(attributeNode, "name")) { AllowedValues = PolicyParserUtil.GetAttributeOrValueFromGrandchildren(attributeNode, "literal-list", "literal", "value"), AllowedRegExp = GetAllowedRegexpsForRules(attributeNode, tagName, parseContext), Description = XmlUtil.GetAttributeValue(attributeNode, "description"), OnInvalid = XmlUtil.GetAttributeValue(attributeNode, "onInvalid") }; allowedAttributes.Add(attributeName, attribute); } } return(allowedAttributes); }
/// <summary> Private method for parsing the <tag-rules> from the XML file.</summary> /// <param name="tagAttributeListNode">The top level of <tag-rules></param> /// <param name="parseContext">The <see cref="ParseContext"/> containing the tag rules dictionary to fill.</param> private static void ParseTagRules(XmlNode tagAttributeListNode, ParseContext parseContext) { foreach (XmlElement tagNode in PolicyParserUtil.GetChildrenByTagName(tagAttributeListNode, "tag")) { string tagName = XmlUtil.GetAttributeValue(tagNode, "name"); var tag = new Tag(tagName) { Action = XmlUtil.GetAttributeValue(tagNode, "action"), AllowedAttributes = GetTagAllowedAttributes(tagNode, tagName, parseContext) }; parseContext.tagRules.Add(tagName.ToLowerInvariant(), tag); } }
/// <summary> Go through <global-tag-attributes> section of the policy file.</summary> /// <param name="globalAttributeListNode">Top level of <global-tag-attributes></param> /// <param name="parseContext">The <see cref="ParseContext"/> containing the global attributes dictionary to fill.</param> private static void ParseGlobalAttributes(XmlNode globalAttributeListNode, ParseContext parseContext) { foreach (XmlElement node in PolicyParserUtil.GetChildrenByTagName(globalAttributeListNode, "attribute")) { string name = XmlUtil.GetAttributeValue(node, "name"); Attribute toAdd = parseContext.commonAttributes.GetValueOrTypeDefault(name.ToLowerInvariant()); if (toAdd != null) { parseContext.globalAttributes.Add(name.ToLowerInvariant(), toAdd); } else { throw new PolicyException($"Global attribute '{name}' was not defined in <common-attributes>"); } } }
/// <summary> Go through <dynamic-tag-attributes> section of the policy file.</summary> /// <param name="dynamicAttributeListNode">Top level of <dynamic-tag-attributes></param> /// <param name="parseContext">The <see cref="ParseContext"/> containing the dynamic attributes dictionary to fill.</param> private static void ParseDynamicAttributes(XmlNode dynamicAttributeListNode, ParseContext parseContext) { foreach (XmlElement node in PolicyParserUtil.GetChildrenByTagName(dynamicAttributeListNode, "attribute")) { string name = XmlUtil.GetAttributeValue(node, "name"); Attribute toAdd = parseContext.commonAttributes.GetValueOrTypeDefault(name.ToLowerInvariant()); if (toAdd != null) { string attributeName = name.ToLowerInvariant().Substring(0, name.Length - 1); parseContext.dynamicAttributes.Add(attributeName, toAdd); } else { throw new PolicyException($"Dynamic attribute '{name}' was not defined in <common-attributes>"); } } }
private static void ParseTagListWithLiterals(XmlNode nodeList, List <string> tagListToFill, List <string> defaultTagsList) { if (nodeList != null) { foreach (XmlElement element in PolicyParserUtil.GetGrandchildrenByTagNames(nodeList as XmlElement, "literal-list", "literal")) { string value = XmlUtil.GetAttributeValue(element, "value"); if (!string.IsNullOrEmpty(value)) { tagListToFill.Add(value); } } } else { tagListToFill.AddRange(defaultTagsList); } }
/// <summary> Go through the <common-attributes> section of the policy file.</summary> /// <param name="commonAttributeListNode">Top level of <common-attributes>.</param> /// <param name="parseContext">The <see cref="ParseContext"/> containing the common attributes dictionary to fill.</param> private static void ParseCommonAttributes(XmlNode commonAttributeListNode, ParseContext parseContext) { foreach (XmlElement node in PolicyParserUtil.GetChildrenByTagName(commonAttributeListNode, "attribute")) { // TODO: Throw exception if onInvalid is defined but is not an expected option? string onInvalid = XmlUtil.GetAttributeValue(node, "onInvalid"); string name = XmlUtil.GetAttributeValue(node, "name"); var attribute = new Attribute(name) { AllowedRegExp = GetAllowedRegexpsForCommonAttributes(node, parseContext), AllowedValues = PolicyParserUtil.GetAttributeOrValueFromGrandchildren(node, "literal-list", "literal", "value"), Description = XmlUtil.GetAttributeValue(node, "description"), OnInvalid = string.IsNullOrEmpty(onInvalid) ? Constants.DEFAULT_ONINVALID : onInvalid, }; parseContext.commonAttributes.Add(name.ToLowerInvariant(), attribute); } }
/// <summary> Go through the <css-rules> section of the policy file.</summary> /// <param name="cssNodeList">Top level of <css-rules>.</param> /// <param name="parseContext">The <see cref="ParseContext"/> containing the CSS rules dictionary to fill.</param> private static void ParseCssRules(XmlNode cssNodeList, ParseContext parseContext) { foreach (XmlElement propertyNode in PolicyParserUtil.GetChildrenByTagName(cssNodeList, "property")) { string name = XmlUtil.GetAttributeValue(propertyNode, "name"); string description = XmlUtil.GetAttributeValue(propertyNode, "description"); string onInvalid = XmlUtil.GetAttributeValue(propertyNode, "onInvalid"); var property = new Property(name) { Description = description, OnInvalid = string.IsNullOrEmpty(onInvalid) ? Constants.DEFAULT_ONINVALID : onInvalid, AllowedRegExp = GetAllowedRegexpsForRules(propertyNode, name, parseContext), AllowedValues = PolicyParserUtil.GetAttributeOrValueFromGrandchildren(propertyNode, "literal-list", "literal", "value"), ShorthandRefs = PolicyParserUtil.GetAttributeOrValueFromGrandchildren(propertyNode, "shorthand-list", "shorthand", "name"), }; parseContext.cssRules.Add(name.ToLowerInvariant(), property); } }
/// <summary>Get the allowed regular expressions defined in the provided <see cref="XmlElement"/>.</summary> /// <param name="node">The node to retrieve the values from.</param> /// <param name="parseContext">The parse context.</param> /// <returns>A list with the allowed regular expressions.</returns> private static List <string> GetAllowedRegexpsForCommonAttributes(XmlElement node, ParseContext parseContext) { var allowedList = new List <string>(); foreach (XmlElement regExNode in PolicyParserUtil.GetGrandchildrenByTagNames(node, "regexp-list", "regexp")) { string regExName = XmlUtil.GetAttributeValue(regExNode, "name"); string value = XmlUtil.GetAttributeValue(regExNode, "value"); string allowedRegEx; if (string.IsNullOrEmpty(regExName)) { allowedRegEx = value; } else { allowedRegEx = regExName == null ? null : parseContext.commonRegularExpressions.GetValueOrTypeDefault(regExName); } allowedList.Add(allowedRegEx); } return(allowedList); }