Esempio n. 1
0
        private void StartTag(byte[] buf, int offset,
                              ContentToken ct, Tokens tok)
        {
            depth++;
            int    colon;
            string name;
            string prefix;

            var attributes = new Dictionary <string, string>();

            nsStack.Push();

            // if i have attributes
            if ((tok == Tokens.StartTagWithAtts) ||
                (tok == Tokens.EmptyElementWithAtts))
            {
                int    start;
                int    end;
                string val;
                for (int i = 0; i < ct.GetAttributeSpecifiedCount(); i++)
                {
                    start = ct.GetAttributeNameStart(i);
                    end   = ct.GetAttributeNameEnd(i);
                    name  = utf.GetString(buf, start, end - start);

                    start = ct.GetAttributeValueStart(i);
                    end   = ct.GetAttributeValueEnd(i);
                    //val = _utf.GetString(buf, start, end - start);

                    val = NormalizeAttributeValue(buf, start, end - start);
                    // <foo b='&amp;'/>
                    // <foo b='&amp;amp;'
                    // TODO: if val includes &amp;, it gets double-escaped
                    if (name.StartsWith("xmlns:"))
                    {
                        // prefixed namespace declaration
                        colon  = name.IndexOf(':');
                        prefix = name.Substring(colon + 1);
                        nsStack.AddNamespace(prefix, val);
                        attributes.Add(name, val);
                    }
                    else if (name == "xmlns")
                    {
                        // namespace declaration
                        nsStack.AddNamespace(string.Empty, val);
                        attributes.Add(name, val);
                    }
                    else
                    {
                        // normal attribute
                        attributes.Add(name, val);
                    }
                }
            }

            name = utf.GetString(buf,
                                 offset + utf8Encoding.MinBytesPerChar,
                                 ct.NameEnd - offset - utf8Encoding.MinBytesPerChar);

            colon = name.IndexOf(':');
            string ns;

            prefix = null;
            if (colon > 0)
            {
                prefix = name.Substring(0, colon);
                name   = name.Substring(colon + 1);
                ns     = nsStack.LookupNamespace(prefix);
            }
            else
            {
                ns = nsStack.DefaultNamespace;
            }

            XmppXElement newel = Factory.GetElement(prefix, name, ns);

            foreach (string attrname in attributes.Keys)
            {
                colon = attrname.IndexOf(':');
                if (colon > 0)
                {
                    prefix = attrname.Substring(0, colon);
                    name   = attrname.Substring(colon + 1);
                    ns     = nsStack.LookupNamespace(prefix);
                    if (attrname.StartsWith("xmlns:"))
                    {
                        // Namespace Declaration
                        newel.SetAttributeValue(XName.Get(name, ns), attributes[attrname]);
                    }
                    else
                    {
                        // prefixed attribute
                        newel.SetAttributeValue("{" + ns + "}" + name, attributes[attrname]);
                    }
                }
                else
                {
                    newel.SetAttributeValue(XName.Get(attrname, string.Empty), attributes[attrname]);
                }
            }

            if (root == null)
            {
                root = newel;
                OnStreamStart?.Invoke(root);
            }
            else
            {
                current?.Add(newel);
                current = newel;
            }
        }