summaryrefslogtreecommitdiff
path: root/xmpp/element_buffer.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-07-04 22:10:55 +0200
committerxengineering <me@xengineering.eu>2023-07-04 22:15:17 +0200
commit48811e7d2487ebc3db49b8af7e20f57db4ac28f4 (patch)
treea317f8dc44ac9828ae5806e1fa1dee7547118619 /xmpp/element_buffer.go
parent5570ccd1d6b50042acbf2fad3793afa6dde79ca2 (diff)
parentd9fe0a4360770b1e4b6b4fb3686c3275ad1b6e6e (diff)
downloadlimox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar
limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar.zst
limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.zip
Merge branch 'element-handling'
This moves away from the concept to parse each individual XML token from the token and group them as a []xml.Token slice for further processing. While this is still possible, receiving aswell as sending has switched to define structs with XML tags which can be marshalled and unmarshalled with the xml.Encoder.EncodeElement() and xml.Decoder.DecodeElement() functions. Further documentation can be found at https://pkg.go.dev/encoding/xml#Unmarshal and https://pkg.go.dev/encoding/xml#Marshal. This merge reduces with its changes the number of code lines in the XMPP implementation by around 41 percent!
Diffstat (limited to 'xmpp/element_buffer.go')
-rw-r--r--xmpp/element_buffer.go61
1 files changed, 0 insertions, 61 deletions
diff --git a/xmpp/element_buffer.go b/xmpp/element_buffer.go
deleted file mode 100644
index 7792db2..0000000
--- a/xmpp/element_buffer.go
+++ /dev/null
@@ -1,61 +0,0 @@
-package xmpp
-
-import (
- "encoding/xml"
-)
-
-// elementBuffer is a struct to store multiple values of type xml.Token until
-// they form a complete XML element with opening and closing tag which is
-// suitable to be passed to an appropriate handler function.
-type elementBuffer struct {
- tokens []xml.Token
- end xml.EndElement
- level int
-}
-
-// newElementBuffer returns a new initialized elementBuffer struct.
-func newElementBuffer() elementBuffer {
- buf := elementBuffer{}
- buf.reset()
- return buf
-}
-
-// FIXME this function needs essential error handling for corner cases!
-//
-// add is able to add a new xml.Token to the buffer. There are some rules
-// checked to ensure a correct and consistent elementBuffer which are checked.
-// If one of these checks fail the token is not added and a corresponding error
-// is returned.
-func (e *elementBuffer) add(t xml.Token) error {
- switch t.(type) {
- case xml.StartElement:
- e.level += 1
- case xml.EndElement:
- e.level -= 1
- }
- e.tokens = append(e.tokens, t)
- return nil
-}
-
-// FIXME isComplete would be true if a stream with only one XML comment is
-// passed to the buffer. This might be unexpected behaviour.
-//
-// isComplete returns true if the buffer contains a slice of XML tokens which
-// form a complete XML element starting with an xml.StartElement and closing
-// with the corresponding xml.EndElement.
-func (e *elementBuffer) isComplete() bool {
- return (len(e.tokens) > 0 && e.level == 0)
-}
-
-// reset returns the content of the buffer as a slice of XML tokens and resets
-// the buffer to the initial state. This function can be used to initialize the
-// elementBuffer struct. In that case the return value can be ignored.
-func (e *elementBuffer) reset() (buf []xml.Token) {
- retval := e.tokens
-
- e.tokens = make([]xml.Token, 0)
- e.end = xml.EndElement{}
- e.level = 0
-
- return retval
-}