private void CloseCDataSection()
        {
            var cdataNode = new XCData(cData);

            if (current == null)
            {
                root?.Add(cdataNode);
            }
            else
            {
                current.Add(cdataNode);
            }

            cData = string.Empty;
        }
示例#2
0
        internal void Publish()
        {
            // TODO: need this to roll up existing ones as well though
            XmppXElement pubkeys = new XmppXElement("urn:xmpp:openpgp:0", "pubkeys");
            XmppXElement pubkey  = new XmppXElement("urn:xmpp:openpgp:0", "pubkey")
            {
                Value = Convert.ToBase64String(this.secretKey.PublicKey.GetEncoded())
            };

            pubkeys.Add(pubkey);
            var item = new Matrix.Xmpp.PubSub.Item();

            item.Add(pubkeys);
            this.xmpp.pubsubManager.PublishItem("", "urn:xmpp:openpgp:0", item);
        }
        /// <summary>
        /// Write bytes into the parser.
        /// </summary>
        /// <param name="buf">The bytes to put into the parse stream</param>
        /// <param name="offset">Offset into buf to start at</param>
        /// <param name="length">Number of bytes to write</param>
        /// <exception cref="System.NotImplementedException">Token type not implemented:  + tok</exception>
        public void Write(byte[] buf, int offset, int length)
        {
            // or assert, really, but this is a little nicer.
            if (length == 0)
            {
                return;
            }

            // No locking is required.  Read() won't get called again
            // until this method returns.

            // TODO: only do this copy if we have a partial token at the
            // end of parsing.
            var copy = new byte[length];

            Buffer.BlockCopy(buf, offset, copy, 0, length);
            bufferAggregate.Write(copy);

            byte[] b   = bufferAggregate.GetBuffer();
            int    off = 0;
            var    ct  = new ContentToken();

            try
            {
                while (off < b.Length)
                {
                    Tokens tok;
                    if (isCData)
                    {
                        tok = utf8Encoding.TokenizeCdataSection(b, off, b.Length, ct);
                    }
                    else
                    {
                        tok = utf8Encoding.TokenizeContent(b, off, b.Length, ct);
                    }

                    switch (tok)
                    {
                    case Tokens.PartialToken:
                    case Tokens.PartialChar:
                    case Tokens.ExtensibleToken:
                        return;

                    case Tokens.EmptyElementNoAtts:
                    case Tokens.EmptyElementWithAtts:
                        StartTag(b, off, ct, tok);
                        EndTag(b, off, ct, tok);
                        break;

                    case Tokens.StartTagNoAtts:
                    case Tokens.StartTagWithAtts:
                        StartTag(b, off, ct, tok);
                        break;

                    case Tokens.EndTag:
                        EndTag(b, off, ct, tok);
                        break;

                    case Tokens.DataChars:
                    case Tokens.DataNewline:
                        AddText(utf.GetString(b, off, ct.TokenEnd - off));
                        break;

                    case Tokens.CharReference:
                    case Tokens.MagicEntityReference:
                        AddText(new string(new[] { ct.RefChar1 }));
                        break;

                    case Tokens.CharPairReference:
                        AddText(new string(new[] { ct.RefChar1,
                                                   ct.RefChar2 }));
                        break;

                    case Tokens.Comment:
                        if (current != null)
                        {
                            // <!-- 4
                            //  --> 3
                            int start = off + 4 * utf8Encoding.MinBytesPerChar;
                            int end   = ct.TokenEnd - off -
                                        7 * utf8Encoding.MinBytesPerChar;
                            string text = utf.GetString(b, start, end);
                            current.Add(text);
                        }
                        break;

                    case Tokens.CdataSectOpen:
                        isCData = true;
                        break;

                    case Tokens.CdataSectClose:
                        CloseCDataSection();
                        isCData = false;
                        break;

                    case Tokens.XmlDeclaration:
                        // thou shalt use UTF8, and XML version 1.
                        // i shall ignore evidence to the contrary...

                        // TODO: Throw an exception if these assuptions are
                        // wrong
                        break;

                    case Tokens.EntityReference:
                    case Tokens.ProcessingInstruction:

                        throw new NotImplementedException("Token type not implemented: " + tok);
                    }
                    off = ct.TokenEnd;
                }
            }
            catch (Exception ex)
            {
                OnStreamError?.Invoke(ex);
            }
            finally
            {
                bufferAggregate.RemoveFirst(off);
            }
        }
        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;
            }
        }