diff options
-rw-r--r-- | xmpp/element_buffer.go | 61 | ||||
-rw-r--r-- | xmpp/element_buffer_test.go | 55 |
2 files changed, 0 insertions, 116 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 -} diff --git a/xmpp/element_buffer_test.go b/xmpp/element_buffer_test.go deleted file mode 100644 index af3d5c2..0000000 --- a/xmpp/element_buffer_test.go +++ /dev/null @@ -1,55 +0,0 @@ -package xmpp - -import ( - "encoding/xml" - "strings" - "testing" -) - -// bufTest is a struct containing a test point for the -// xengineering.eu/limox/xmpp.elementBuffer. It contains a test XML string -// which has to be exactly one XML element and an array of indentation levels -// which have to be checked after each token which is parsed. -type bufTest struct { - xml string - levels []int -} - -func TestElementBuffer(t *testing.T) { - tests := []bufTest{ - bufTest{`<stream></stream>`, []int{1, 0}}, - bufTest{`<stream/>`, []int{1, 0}}, - bufTest{`<a><b>testing</b></a>`, []int{1, 2, 2, 1, 0}}, - bufTest{`<a><!-- comment --><b>testing</b></a>`, []int{1, 1, 2, 2, 1, 0}}, - bufTest{`<!-- comment --><a><b>testing</b></a>`, []int{0, 1, 2, 2, 1, 0}}, - } - - for _, v := range tests { - r := strings.NewReader(v.xml) - d := xml.NewDecoder(r) - b := newElementBuffer() - - i := 0 - - for { - token, err := d.Token() - if err != nil { - if i != len(v.levels) { - t.Fatalf("Stopped parsing at unexpected index due to error `%v`!\n", err) - } - break - } - - err = b.add(token) - if err != nil { - t.Fatalf("add(token) failed with error `%v`!\n", err) - } - - if b.level != v.levels[i] { - t.Fatalf("Indent level of xmpp.elementBuffer %d does not match value given by test data %d!\n", b.level, v.levels[i]) - } - - i += 1 - } - } -} |