Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
 /// <summary> Go through the &lt;common-regexps&gt; section of the policy file.</summary>
 /// <param name="commonRegularExpressionListNode">Top level of &lt;common-regexps&gt;.</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);
         }
     }
 }
Esempio n. 3
0
 /// <summary> Go through &lt;directives&gt; section of the policy file.</summary>
 /// <param name="directiveListNode">Top level of &lt;directives&gt;</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);
         }
     }
 }
Esempio n. 4
0
        /// <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);
        }
Esempio n. 5
0
        /// <summary> Private method for parsing the &lt;tag-rules&gt; from the XML file.</summary>
        /// <param name="tagAttributeListNode">The top level of &lt;tag-rules&gt;</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);
            }
        }
Esempio n. 6
0
 /// <summary> Go through &lt;global-tag-attributes&gt; section of the policy file.</summary>
 /// <param name="globalAttributeListNode">Top level of &lt;global-tag-attributes&gt;</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>");
         }
     }
 }
Esempio n. 7
0
 /// <summary> Go through &lt;dynamic-tag-attributes&gt; section of the policy file.</summary>
 /// <param name="dynamicAttributeListNode">Top level of &lt;dynamic-tag-attributes&gt;</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>");
         }
     }
 }
Esempio n. 8
0
 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);
     }
 }
Esempio n. 9
0
        /// <summary> Go through the &lt;common-attributes&gt; section of the policy file.</summary>
        /// <param name="commonAttributeListNode">Top level of &lt;common-attributes&gt;.</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);
            }
        }
Esempio n. 10
0
        /// <summary> Go through the &lt;css-rules&gt; section of the policy file.</summary>
        /// <param name="cssNodeList">Top level of &lt;css-rules&gt;.</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);
            }
        }
Esempio n. 11
0
        /// <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);
        }