예제 #1
0
        /// <summary>
        /// Determines whether this instance matches the given namespace and attribute name or not.
        /// </summary>
        /// <returns><c>true</c> if this instance matches the specified namespace and name; otherwise, <c>false</c>.</returns>
        /// <param name="nspace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        public override bool IsMatch(ZptNamespace nspace, string name)
        {
            if(nspace == null)
              {
            throw new ArgumentNullException(nameof(nspace));
              }

              return ZptHtmlElement.GetNameWithPrefix(nspace, name) == this.Name;
        }
예제 #2
0
        /// <summary>
        /// Determines whether this instance matches the given namespace and attribute name or not.
        /// </summary>
        /// <returns><c>true</c> if this instance matches the specified namespace and name; otherwise, <c>false</c>.</returns>
        /// <param name="nspace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        public override bool IsMatch(ZptNamespace nspace, string name)
        {
            if(nspace == null)
              {
            throw new ArgumentNullException(nameof(nspace));
              }

              return (((_original.Name.Namespace.NamespaceName == null
                && nspace.Uri == null)
               || (_original.Name.Namespace.NamespaceName == nspace.Uri
                   || (String.IsNullOrEmpty(nspace.Uri) && String.IsNullOrEmpty(_original.Name.Namespace.NamespaceName))))
              && _original.Name.LocalName == name);
        }
예제 #3
0
        /// <summary>
        /// Gets an attribute which matches the given criteria, or a <c>null</c> reference is no matching attribute is
        /// found.
        /// </summary>
        /// <returns>The attribute, or a <c>null</c> reference.</returns>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        public override IZptAttribute GetAttribute(ZptNamespace attributeNamespace, string name)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }
              if(name == null)
              {
            throw new ArgumentNullException(nameof(name));
              }

              var attribName = this.IsInNamespace(attributeNamespace)? name : GetNameWithPrefix(attributeNamespace, name);
              var htmlAttribute = this.Node.Attributes
            .FirstOrDefault(x => {
              bool output;
              string nameWithPrefix = GetNameWithPrefix(attributeNamespace, name);

              if(this.IsInNamespace(attributeNamespace))
              {
            output = (x.Name == attribName || x.Name == nameWithPrefix);
              }
              else
              {
            output = x.Name == nameWithPrefix;
              }

              return output;
            });

              return (htmlAttribute != null)? new ZptHtmlAttribute(htmlAttribute) : null;
        }
예제 #4
0
 /// <summary>
 /// Gets an attribute matching the given namespace and attribute name.
 /// </summary>
 /// <returns>The attribute, or a <c>null</c> reference if no attribute is found.</returns>
 /// <param name="nspace">The attribute namespace.</param>
 /// <param name="attributeName">The attribute name.</param>
 public virtual IZptAttribute GetAttribute(ZptNamespace nspace, string attributeName)
 {
     return this.OriginalAttributes
     .Where(x => x.IsMatch(nspace, attributeName)
        || (this.Element.IsInNamespace(nspace) && x.IsMatch(ZptNamespace.Default, attributeName)))
     .FirstOrDefault();
 }
예제 #5
0
 /// <summary>
 /// Initializes the <see cref="T:CSF.Zpt.ZptConstants+Tal"/> class.
 /// </summary>
 static Tal()
 {
     _namespace = new ZptNamespace("tal", "http://xml.zope.org/namespaces/tal");
 }
예제 #6
0
        /// <summary>
        /// Sets the value of an attribute.
        /// </summary>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        /// <param name="value">The attribute value.</param>
        public override void SetAttribute(ZptNamespace attributeNamespace, string name, string value)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }
              if(name == null)
              {
            throw new ArgumentNullException(nameof(name));
              }

              this.Node.SetAttributeValue(XName.Get(name, attributeNamespace.Uri?? XNamespace.None.NamespaceName), value);
        }
예제 #7
0
        /// <summary>
        /// Removes a named attribute.
        /// </summary>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        public override void RemoveAttribute(ZptNamespace attributeNamespace, string name)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }
              if(name == null)
              {
            throw new ArgumentNullException(nameof(name));
              }

              var attribs = this.Node.Attributes()
            .Cast<XAttribute>()
            .Where(x => x.Name.LocalName == name
               && (attributeNamespace.Uri == null
                   || x.Name.Namespace.NamespaceName == attributeNamespace.Uri))
            .ToArray();

              foreach(var attrib in attribs)
              {
            attrib.Remove();
              }
        }
예제 #8
0
        /// <summary>
        /// Recursively searches for attributes with a given namespace or prefix and removes them from their parent
        /// element.
        /// </summary>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        public override void PurgeAttributes(ZptNamespace attributeNamespace)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }

              var elements = ((IEnumerable) this.Node.XPathEvaluate(".//*"))
            .Cast<XElement>()
            .Union(new [] { (XElement) this.Node })
            .ToArray();

              var toRemove = (from ele in elements
                      from attrib in ele.Attributes().Cast<XAttribute>()
                      where
                        attrib.Name.Namespace.NamespaceName == attributeNamespace.Uri
                        || (attrib.Name.Namespace.NamespaceName == "http://www.w3.org/2000/xmlns/"
                            && attrib.Value == attributeNamespace.Uri)
                      select new { Element = ele, Attribute = attrib })
            .ToArray();

              foreach(var item in toRemove)
              {
            item.Attribute.Remove();
              }
        }
예제 #9
0
        /// <summary>
        /// Gets the prefix from a given namespace.
        /// </summary>
        /// <returns>The prefix.</returns>
        /// <param name="nSpace">An element or attribute namespace.</param>
        internal static string GetPrefix(ZptNamespace nSpace)
        {
            if(nSpace == null)
              {
            throw new ArgumentNullException(nameof(nSpace));
              }

              return (nSpace.Prefix != null)? String.Concat(nSpace.Prefix, PREFIX_SEPARATOR) : String.Empty;
        }
예제 #10
0
        /// <summary>
        /// Gets an element or attribute name, adding a namespace prefix where appropriate.
        /// </summary>
        /// <returns>The formatted name.</returns>
        /// <param name="nSpace">A namespace.</param>
        /// <param name="name">An element or attribute name.</param>
        internal static string GetNameWithPrefix(ZptNamespace nSpace, string name)
        {
            if(nSpace == null)
              {
            throw new ArgumentNullException(nameof(nSpace));
              }
              EnforceNameNotEmpty(name);

              return (nSpace.Prefix != null)? String.Concat(nSpace.Prefix, PREFIX_SEPARATOR, name) : name;
        }
예제 #11
0
        /// <summary>
        /// Sets the value of an attribute.
        /// </summary>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        /// <param name="value">The attribute value.</param>
        public override void SetAttribute(ZptNamespace attributeNamespace, string name, string value)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }
              if(name == null)
              {
            throw new ArgumentNullException(nameof(name));
              }

              var formattedName = GetNameWithPrefix(attributeNamespace, name);
              this.Node.SetAttributeValue(formattedName, value);
        }
예제 #12
0
        /// <summary>
        /// Recursively searches the children of the current instance, returning a collection of elements which have an
        /// attribute matching the given criteria.
        /// </summary>
        /// <returns>The matching child elements.</returns>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        public override IZptElement[] SearchChildrenByAttribute(ZptNamespace attributeNamespace, string name)
        {
            string
            attribName = GetNameWithPrefix(attributeNamespace, name),
            prefix = GetPrefix(attributeNamespace);

              return (from node in this.Node.Descendants()
              from attrib in node.Attributes
              where
                attrib.Name == attribName
                || (node.Name.StartsWith(prefix)
                    && attrib.Name == name)
              select new ZptHtmlElement(node, this.SourceFile, this.OwnerDocument))
            .ToArray();
        }
예제 #13
0
        /// <summary>
        /// Removes a named attribute.
        /// </summary>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        public override void RemoveAttribute(ZptNamespace attributeNamespace, string name)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }
              if(name == null)
              {
            throw new ArgumentNullException(nameof(name));
              }

              var formattedName = GetNameWithPrefix(attributeNamespace, name);
              var attribs = this.Node.Attributes.Where(x => x.Name == formattedName).ToArray();
              foreach(var attrib in attribs)
              {
            attrib.Remove();
              }
        }
예제 #14
0
        /// <summary>
        /// Recursively searches for elements with a given namespace or prefix and removes them using the
        /// <see cref="Omit"/> behaviour.
        /// </summary>
        /// <param name="elementNamespace">The element namespace.</param>
        public override void PurgeElements(ZptNamespace elementNamespace)
        {
            var toRemove = this.Node
            .Descendants()
            .Union(new [] { this.Node })
            .Where(x => IsInNamespace(elementNamespace, x))
            .ToArray();

              foreach(var item in toRemove)
              {
            new ZptHtmlElement(item, this.SourceFile, this.OwnerDocument).Omit();
              }
        }
예제 #15
0
        /// <summary>
        /// Recursively searches for attributes with a given namespace or prefix and removes them from their parent
        /// element.
        /// </summary>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        public override void PurgeAttributes(ZptNamespace attributeNamespace)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }

              var nodes = this.Node
            .Descendants()
            .Union(new [] { this.Node })
            .ToArray();

              var toRemove = (from node in nodes
                      from attrib in node.Attributes
                      where
                        attrib.Name.StartsWith(String.Concat(attributeNamespace.Prefix, PREFIX_SEPARATOR))
                        || attrib.Name == String.Concat(XMLNS_ATTRIBUTE, PREFIX_SEPARATOR, attributeNamespace.Prefix)
                      select attrib)
            .ToArray();

              foreach(var item in toRemove)
              {
            item.Remove();
              }
        }
예제 #16
0
        /// <summary>
        /// Gets an attribute which matches the given criteria, or a <c>null</c> reference is no matching attribute is
        /// found.
        /// </summary>
        /// <returns>The attribute, or a <c>null</c> reference.</returns>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        public override IZptAttribute GetAttribute(ZptNamespace attributeNamespace, string name)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }
              EnforceNameNotEmpty(name);

              string query;
              var nsManager = new XmlNamespaceManager(new NameTable());

              if(String.IsNullOrEmpty(attributeNamespace.Uri))
              {
            query = String.Concat("@", name);
              }
              else if(this.IsInNamespace(attributeNamespace))
              {
            nsManager.AddNamespace("search", attributeNamespace.Uri);
            query = String.Concat("@search:", name, "|", name);
              }
              else
              {
            nsManager.AddNamespace("search", attributeNamespace.Uri);
            query = String.Concat("@search:", name);
              }

              var xmlAttribute = ((IEnumerable) this.Node.XPathEvaluate(query, nsManager))
            .Cast<XAttribute>()
            .FirstOrDefault();

              return (xmlAttribute != null)? new ZptXmlLinqAttribute(xmlAttribute) : null;
        }
예제 #17
0
 /// <summary>
 /// Determines whether or not the current instance is in the specified namespace.
 /// </summary>
 /// <returns>
 /// <c>true</c> if this instance is in the specified namespace; otherwise, <c>false</c>.
 /// </returns>
 /// <param name="nSpace">The namespace for which to test.</param>
 public override bool IsInNamespace(ZptNamespace nSpace)
 {
     return this.IsInNamespace(nSpace, this.Node);
 }
예제 #18
0
        private bool IsInNamespace(ZptNamespace nSpace, HtmlNode node)
        {
            if(nSpace == null)
              {
            throw new ArgumentNullException(nameof(nSpace));
              }

              bool output;

              if(nSpace.Prefix != null)
              {
            output = node.Name.StartsWith(String.Concat(nSpace.Prefix, PREFIX_SEPARATOR));
              }
              else
              {
            output = !node.Name.Contains(PREFIX_SEPARATOR);
              }

              return output;
        }
예제 #19
0
        /// <summary>
        /// Recursively searches for elements with a given namespace or prefix and removes them using the
        /// <see cref="Omit"/> behaviour.
        /// </summary>
        /// <param name="elementNamespace">The element namespace.</param>
        public override void PurgeElements(ZptNamespace elementNamespace)
        {
            var toRemove = ((IEnumerable) this.Node.XPathEvaluate(".//*"))
            .Cast<XElement>()
            .Union(new [] { this.Node })
            .Where(x => IsInNamespace(elementNamespace, x))
            .ToArray();

              foreach(var item in toRemove)
              {
            new ZptXmlLinqElement(item, this.SourceFile, this.OwnerDocument).Omit();
              }
        }
        /// <summary>
        /// Handle the related attribute types which exist upon the element exposed by the given context, if any.
        /// </summary>
        /// <returns>A response type providing information about the result of this operation.</returns>
        /// <param name="context">The rendering context, which exposes a ZPT element.</param>
        public AttributeHandlingResult Handle(IRenderingContext context)
        {
            if(context == null)
              {
            throw new ArgumentNullException(nameof(context));
              }

              var attrib = context.GetTalAttribute(ZptConstants.Tal.AttributesAttribute);

              if(attrib != null)
              {
            var itemMatches = ItemMatcher
              .Matches(attrib.Value)
              .Cast<Match>()
              .Select(x => Attribute.Match(x.Groups[1].Value));

            if(itemMatches.Any(x => !x.Success))
            {
              string message = String.Format(Resources.ExceptionMessages.ZptAttributeParsingError,
                                         ZptConstants.Tal.Namespace,
                                         ZptConstants.Tal.AttributesAttribute,
                                         attrib.Value);
              throw new ParserException(message) {
            SourceElementName = context.Element.Name,
            SourceAttributeName = attrib.Name,
            SourceAttributeValue = attrib.Value
              };
            }

            var items = itemMatches
              .Select(x => new {  Prefix = x.Groups[1].Value,
                              AttributeName = x.Groups[2].Value,
                              Expression = x.Groups[3].Value })
              .ToArray();

            foreach(var item in items)
            {
              ExpressionResult result;
              var unescapedExpression = this.UnescapeSemicolons(item.Expression);

              try
              {
            result = context.TalModel.Evaluate(unescapedExpression, context);
              }
              catch(Exception ex)
              {
            string message = String.Format(Resources.ExceptionMessages.ExpressionEvaluationException,
                                           ZptConstants.Tal.Namespace,
                                           ZptConstants.Tal.AttributesAttribute,
                                           item.Expression,
                                           context.Element.Name);
            throw new ModelEvaluationException(message, ex) {
              ExpressionText = item.Expression,
              ElementName = context.Element.Name,
            };
              }

              if(!result.CancelsAction)
              {
            var nspace = new ZptNamespace(prefix: item.Prefix);
            if(result.Value == null)
            {
              if(String.IsNullOrEmpty(item.Prefix))
              {
                context.Element.RemoveAttribute(item.AttributeName);
              }
              else
              {
                context.Element.RemoveAttribute(nspace, item.AttributeName);
              }
            }
            else
            {
              if(String.IsNullOrEmpty(item.Prefix))
              {
                context.Element.SetAttribute(item.AttributeName, result.Value.ToString());
              }
              else
              {
                context.Element.SetAttribute(nspace, item.AttributeName, result.Value.ToString());
              }
            }
              }
            }
              }

              return new AttributeHandlingResult(new [] { context }, true);
        }
예제 #21
0
        /// <summary>
        /// Recursively searches the children of the current instance, returning a collection of elements which have an
        /// attribute matching the given criteria.
        /// </summary>
        /// <returns>The matching child elements.</returns>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        public override IZptElement[] SearchChildrenByAttribute(ZptNamespace attributeNamespace, string name)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }
              EnforceNameNotEmpty(name);

              string query;
              var nsManager = new XmlNamespaceManager(new NameTable());

              if(String.IsNullOrEmpty(attributeNamespace.Uri))
              {
            query = String.Concat(".//*[@", name, "]");
              }
              else
              {
            nsManager.AddNamespace("search", attributeNamespace.Uri);
            query = String.Concat(".//*[@search:", name, "]");
              }

              return ((IEnumerable) this.Node.XPathEvaluate(query, nsManager))
            .Cast<XElement>()
            .Select(x => new ZptXmlLinqElement(x, this.SourceFile, this.OwnerDocument))
            .ToArray();
        }
예제 #22
0
        /// <summary>
        /// Recursively searches for attributes with a given namespace or prefix and removes them from their parent
        /// element.
        /// </summary>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        public override void PurgeAttributes(ZptNamespace attributeNamespace)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }

              var elements = this.Node
            .SelectNodes(".//*")
            .Cast<System.Xml.XmlElement>()
            .Union(new [] { (System.Xml.XmlElement) this.Node })
            .ToArray();

              var toRemove = (from ele in elements
                      from attrib in ele.Attributes.Cast<System.Xml.XmlAttribute>()
                      where
                        attrib.NamespaceURI == attributeNamespace.Uri
                        || (attrib.NamespaceURI == "http://www.w3.org/2000/xmlns/"
                            && attrib.Value == attributeNamespace.Uri)
                      select new { Element = ele, Attribute = attrib })
            .ToArray();

              foreach(var item in toRemove)
              {
            item.Element.RemoveAttributeNode(item.Attribute);
              }
        }
예제 #23
0
        private bool IsInNamespace(ZptNamespace nSpace, XElement node)
        {
            if(nSpace == null)
              {
            throw new ArgumentNullException(nameof(nSpace));
              }

              bool output;

              if(nSpace.Uri != null)
              {
            output = node.Name.Namespace.NamespaceName == nSpace.Uri;
              }
              else
              {
            output = String.IsNullOrEmpty(node.Name.Namespace.NamespaceName);
              }

              return output;
        }
예제 #24
0
        /// <summary>
        /// Recursively searches for elements with a given namespace or prefix and removes them using the
        /// <see cref="Omit"/> behaviour.
        /// </summary>
        /// <param name="elementNamespace">The element namespace.</param>
        public override void PurgeElements(ZptNamespace elementNamespace)
        {
            var toRemove = this.Node
            .SelectNodes(".//*")
            .Cast<System.Xml.XmlElement>()
            .Union(new [] { this.Node })
            .Where(x => IsInNamespace(elementNamespace, x))
            .ToArray();

              foreach(var item in toRemove)
              {
            new ZptXmlElement(item, this.SourceFile, this.OwnerDocument).Omit();
              }
        }
예제 #25
0
 /// <summary>
 /// Initializes the <see cref="T:CSF.Zpt.ZptConstants+Metal"/> class.
 /// </summary>
 static Metal()
 {
     _namespace = new ZptNamespace("metal", "http://xml.zope.org/namespaces/metal");
 }
예제 #26
0
        /// <summary>
        /// Recursively searches the children of the current instance, returning a collection of elements which have an
        /// attribute matching the given criteria.
        /// </summary>
        /// <returns>The matching child elements.</returns>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        public override IZptElement[] SearchChildrenByAttribute(ZptNamespace attributeNamespace, string name)
        {
            EnforceNameNotEmpty(name);

              string query;
              var nsManager = new XmlNamespaceManager(new NameTable());

              if(String.IsNullOrEmpty(attributeNamespace.Uri))
              {
            query = String.Concat(".//*[@", name, "]");
              }
              else
              {
            nsManager.AddNamespace("search", attributeNamespace.Uri);
            query = String.Concat(".//*[@search:", name, "]");
              }

              return this.Node.SelectNodes(query, nsManager)
            .Cast<XmlNode>()
            .Select(x => new ZptXmlElement(x, this.SourceFile, this.OwnerDocument))
            .ToArray();
        }
예제 #27
0
        /// <summary>
        /// Sets the value of an attribute.
        /// </summary>
        /// <param name="attributeNamespace">The attribute namespace.</param>
        /// <param name="name">The attribute name.</param>
        /// <param name="value">The attribute value.</param>
        public override void SetAttribute(ZptNamespace attributeNamespace, string name, string value)
        {
            if(attributeNamespace == null)
              {
            throw new ArgumentNullException(nameof(attributeNamespace));
              }
              if(name == null)
              {
            throw new ArgumentNullException(nameof(name));
              }

              var attribs = this.Node.Attributes.Cast<XmlAttribute>()
            .Where(x => x.LocalName == name
                    && (attributeNamespace.Uri == null
                        || x.NamespaceURI == attributeNamespace.Uri))
            .ToArray();

              if(!attribs.Any())
              {
            attribs = new [] { this.Node.OwnerDocument.CreateAttribute(attributeNamespace.Prefix, name, attributeNamespace.Uri) };
            this.Node.Attributes.Append(attribs[0]);
              }

              foreach(var attrib in attribs)
              {
            attrib.Value = value;
              }
        }
예제 #28
0
 /// <summary>
 /// Determines whether this instance matches the given namespace and attribute name or not.
 /// </summary>
 /// <returns><c>true</c> if this instance matches the specified namespace and name; otherwise, <c>false</c>.</returns>
 /// <param name="nspace">The attribute namespace.</param>
 /// <param name="name">The attribute name.</param>
 public abstract bool IsMatch(ZptNamespace nspace, string name);