structs.go
changeset 38 2839fece923e
parent 37 fbda8e925fdf
child 40 33e7f25f1fd2
equal deleted inserted replaced
37:fbda8e925fdf 38:2839fece923e
    92 	// The xml:lang attribute.
    92 	// The xml:lang attribute.
    93 	XLang() string
    93 	XLang() string
    94 	// A nested error element, if any.
    94 	// A nested error element, if any.
    95 	XError() *Error
    95 	XError() *Error
    96 	// A (non-error) nested element, if any.
    96 	// A (non-error) nested element, if any.
    97 	XChild() *Generic
    97 	XNested() interface{}
       
    98 	setNested(interface{})
       
    99 	generic() *Generic
    98 	innerxml() string
   100 	innerxml() string
    99 }
       
   100 
       
   101 type ExtendedStanza interface {
       
   102 	Stanza
       
   103 	InnerMarshal(io.Writer) os.Error
       
   104 }
   101 }
   105 
   102 
   106 // message stanza
   103 // message stanza
   107 type Message struct {
   104 type Message struct {
   108 	To string `xml:"attr"`
   105 	To string `xml:"attr"`
   114 	Error *Error
   111 	Error *Error
   115 	Subject *Generic
   112 	Subject *Generic
   116 	Body *Generic
   113 	Body *Generic
   117 	Thread *Generic
   114 	Thread *Generic
   118 	Any *Generic
   115 	Any *Generic
       
   116 	Nested interface{}
   119 }
   117 }
   120 var _ xml.Marshaler = &Message{}
   118 var _ xml.Marshaler = &Message{}
   121 var _ Stanza = &Message{}
   119 var _ Stanza = &Message{}
   122 var _ ExtendedStanza = &Message{}
       
   123 
   120 
   124 // presence stanza
   121 // presence stanza
   125 type Presence struct {
   122 type Presence struct {
   126 	To string `xml:"attr"`
   123 	To string `xml:"attr"`
   127 	From string `xml:"attr"`
   124 	From string `xml:"attr"`
   132 	Error *Error
   129 	Error *Error
   133 	Show *Generic
   130 	Show *Generic
   134 	Status *Generic
   131 	Status *Generic
   135 	Priority *Generic
   132 	Priority *Generic
   136 	Any *Generic
   133 	Any *Generic
       
   134 	Nested interface{}
   137 }
   135 }
   138 var _ xml.Marshaler = &Presence{}
   136 var _ xml.Marshaler = &Presence{}
   139 var _ Stanza = &Presence{}
   137 var _ Stanza = &Presence{}
   140 var _ ExtendedStanza = &Presence{}
       
   141 
   138 
   142 // iq stanza
   139 // iq stanza
   143 type Iq struct {
   140 type Iq struct {
   144 	To string `xml:"attr"`
   141 	To string `xml:"attr"`
   145 	From string `xml:"attr"`
   142 	From string `xml:"attr"`
   147 	Type string `xml:"attr"`
   144 	Type string `xml:"attr"`
   148 	Lang string `xml:"attr"`
   145 	Lang string `xml:"attr"`
   149 	Innerxml string `xml:"innerxml"`
   146 	Innerxml string `xml:"innerxml"`
   150 	Error *Error
   147 	Error *Error
   151 	Any *Generic
   148 	Any *Generic
       
   149 	Nested interface{}
   152 }
   150 }
   153 var _ xml.Marshaler = &Iq{}
   151 var _ xml.Marshaler = &Iq{}
   154 var _ Stanza = &Iq{}
   152 var _ Stanza = &Iq{}
   155 
   153 
   156 // Describes an XMPP stanza error. See RFC 3920, Section 9.3.
   154 // Describes an XMPP stanza error. See RFC 3920, Section 9.3.
   293 	}
   291 	}
   294 	if st.XLang() != "" {
   292 	if st.XLang() != "" {
   295 		writeField(buf, "xml:lang", st.XLang())
   293 		writeField(buf, "xml:lang", st.XLang())
   296 	}
   294 	}
   297 	buf.WriteString(">")
   295 	buf.WriteString(">")
   298 	if ext, ok := st.(ExtendedStanza) ; ok {
   296 
   299 		if st.XError() != nil {
   297 	if st.XNested() != nil {
   300 			bytes, _ := st.XError().MarshalXML()
   298 		xml.Marshal(buf, st.XNested())
   301 			buf.WriteString(string(bytes))
   299 	} else if st.generic() != nil {
   302 		}
   300 		xml.Marshal(buf, st.generic())
   303 		err := ext.InnerMarshal(buf)
   301 	} else if st.innerxml() != "" {
   304 		if err != nil {
   302 		buf.WriteString(st.innerxml())
   305 			return nil, err
   303 	}
   306 		}
   304 
   307 	} else {
       
   308 		inner := st.innerxml()
       
   309 		if inner == "" {
       
   310 			xml.Marshal(buf, st.XChild())
       
   311 		} else {
       
   312 			buf.WriteString(st.innerxml())
       
   313 		}
       
   314 	}
       
   315 	buf.WriteString("</")
   305 	buf.WriteString("</")
   316 	buf.WriteString(st.XName())
   306 	buf.WriteString(st.XName())
   317 	buf.WriteString(">")
   307 	buf.WriteString(">")
   318 	return buf.Bytes(), nil
   308 	return buf.Bytes(), nil
   319 }
   309 }
   361 
   351 
   362 func (m *Message) XError() *Error {
   352 func (m *Message) XError() *Error {
   363 	return m.Error
   353 	return m.Error
   364 }
   354 }
   365 
   355 
   366 func (m *Message) XChild() *Generic {
   356 func (m *Message) XNested() interface{} {
       
   357 	return m.Nested
       
   358 }
       
   359 
       
   360 func (m *Message) setNested(n interface{}) {
       
   361 	m.Nested = n
       
   362 }
       
   363 
       
   364 func (m *Message) generic() *Generic {
   367 	return m.Any
   365 	return m.Any
   368 }
   366 }
   369 
   367 
   370 func (m *Message) innerxml() string {
   368 func (m *Message) innerxml() string {
   371 	return m.Innerxml
   369 	return m.Innerxml
   417 
   415 
   418 func (p *Presence) XError() *Error {
   416 func (p *Presence) XError() *Error {
   419 	return p.Error
   417 	return p.Error
   420 }
   418 }
   421 
   419 
   422 func (p *Presence) XChild() *Generic {
   420 func (p *Presence) XNested() interface{} {
       
   421 	return p.Nested
       
   422 }
       
   423 
       
   424 func (p *Presence) setNested(n interface{}) {
       
   425 	p.Nested = n
       
   426 }
       
   427 
       
   428 func (p *Presence) generic() *Generic {
   423 	return p.Any
   429 	return p.Any
   424 }
   430 }
   425 
   431 
   426 func (p *Presence) innerxml() string {
   432 func (p *Presence) innerxml() string {
   427 	return p.Innerxml
   433 	return p.Innerxml
   473 
   479 
   474 func (iq *Iq) XError() *Error {
   480 func (iq *Iq) XError() *Error {
   475 	return iq.Error
   481 	return iq.Error
   476 }
   482 }
   477 
   483 
   478 func (iq *Iq) XChild() *Generic {
   484 func (iq *Iq) XNested() interface{} {
       
   485 	return iq.Nested
       
   486 }
       
   487 
       
   488 func (iq *Iq) setNested(n interface{}) {
       
   489 	iq.Nested = n
       
   490 }
       
   491 
       
   492 func (iq *Iq) generic() *Generic {
   479 	return iq.Any
   493 	return iq.Any
   480 }
   494 }
   481 
   495 
   482 func (iq *Iq) innerxml() string {
   496 func (iq *Iq) innerxml() string {
   483 	return iq.Innerxml
   497 	return iq.Innerxml