xmpp/stream.go
changeset 128 8342afcffc92
parent 127 a8f9a0c07fc8
child 129 cccf2b2fe34d
equal deleted inserted replaced
127:a8f9a0c07fc8 128:8342afcffc92
    18 	"encoding/xml"
    18 	"encoding/xml"
    19 	"fmt"
    19 	"fmt"
    20 	"io"
    20 	"io"
    21 	"math/big"
    21 	"math/big"
    22 	"net"
    22 	"net"
       
    23 	"reflect"
    23 	"regexp"
    24 	"regexp"
    24 	"strings"
    25 	"strings"
    25 	"time"
    26 	"time"
    26 )
    27 )
    27 
    28 
    74 		}
    75 		}
    75 	}
    76 	}
    76 }
    77 }
    77 
    78 
    78 func readXml(r io.Reader, ch chan<- interface{},
    79 func readXml(r io.Reader, ch chan<- interface{},
    79 	extStanza map[string]func(*xml.Name) interface{}) {
    80 	extStanza map[xml.Name]reflect.Type) {
    80 	if _, ok := Debug.(*noLog); !ok {
    81 	if _, ok := Debug.(*noLog); !ok {
    81 		pr, pw := io.Pipe()
    82 		pr, pw := io.Pipe()
    82 		go tee(r, pw, "S: ")
    83 		go tee(r, pw, "S: ")
    83 		r = pr
    84 		r = pr
    84 	}
    85 	}
   160 		// Put it on the channel.
   161 		// Put it on the channel.
   161 		ch <- obj
   162 		ch <- obj
   162 	}
   163 	}
   163 }
   164 }
   164 
   165 
   165 func parseExtended(st *Header, extStanza map[string]func(*xml.Name) interface{}) error {
   166 func parseExtended(st *Header, extStanza map[xml.Name]reflect.Type) error {
   166 	// Now parse the stanza's innerxml to find the string that we
   167 	// Now parse the stanza's innerxml to find the string that we
   167 	// can unmarshal this nested element from.
   168 	// can unmarshal this nested element from.
   168 	reader := strings.NewReader(st.Innerxml)
   169 	reader := strings.NewReader(st.Innerxml)
   169 	p := xml.NewDecoder(reader)
   170 	p := xml.NewDecoder(reader)
   170 	for {
   171 	for {
   174 		}
   175 		}
   175 		if err != nil {
   176 		if err != nil {
   176 			return err
   177 			return err
   177 		}
   178 		}
   178 		if se, ok := t.(xml.StartElement); ok {
   179 		if se, ok := t.(xml.StartElement); ok {
   179 			if con, ok := extStanza[se.Name.Space]; ok {
   180 			if typ, ok := extStanza[se.Name]; ok {
   180 				// Call the indicated constructor.
   181 				nested := reflect.New(typ).Interface()
   181 				nested := con(&se.Name)
       
   182 
   182 
   183 				// Unmarshal the nested element and
   183 				// Unmarshal the nested element and
   184 				// stuff it back into the stanza.
   184 				// stuff it back into the stanza.
   185 				err := p.DecodeElement(nested, &se)
   185 				err := p.DecodeElement(nested, &se)
   186 				if err != nil {
   186 				if err != nil {