func (o *OSM) marshalInnerXML(e *xml.Encoder) error { if o == nil { return nil } if err := e.Encode(o.Bounds); err != nil { return err } if err := e.Encode(o.Nodes); err != nil { return err } if err := e.Encode(o.Ways); err != nil { return err } if err := e.Encode(o.Relations); err != nil { return err } if err := e.Encode(o.Changesets); err != nil { return err } return nil }
func (node *Optiongroup) MarshalXML(e *xml.Encoder, start xml.StartElement) error { var err error startElt := xml.StartElement{Name: xml.Name{Local: "optiongroup"}} err = e.EncodeToken(startElt) if err != nil { return err } err = marshalShortDescription("shortdescription", e, node.ShortDescription) if err != nil { return err } err = e.Encode(node.Classoption) if err != nil { return err } err = e.EncodeToken(xml.EndElement{Name: startElt.Name}) if err != nil { return err } return nil }
func (c *Command) MarshalXML(e *xml.Encoder, start xml.StartElement) error { var err error if c.Level == "" { c.Level = "beginner" } cmdstartelt := xml.StartElement{Name: xml.Name{Local: "command"}} cmdstartelt.Attr = []xml.Attr{ xml.Attr{Name: xml.Name{Local: "name"}, Value: c.Name}, xml.Attr{Name: xml.Name{Local: "label"}, Value: strings.Join(c.Label, ",")}, xml.Attr{Name: xml.Name{Local: "level"}, Value: c.Level}, } err = e.EncodeToken(cmdstartelt) if err != nil { return err } err = marshalShortDescription("shortdescription", e, c.ShortDescription) if err != nil { return err } err = marshalDescription("description", e, c.Description) if err != nil { return err } err = e.Encode(c.Variant) if err != nil { return err } return e.EncodeToken(xml.EndElement{Name: cmdstartelt.Name}) }
func (people People) MarshalXML(enc *xml.Encoder, start xml.StartElement) error { name := xml.Name{Space: "", Local: "People"} enc.EncodeToken(xml.StartElement{Name: name}) for _, person := range people { enc.Encode(person) } enc.EncodeToken(xml.EndElement{Name: name}) return nil }
// MarshalXML marshals only the fields needed for creating/updating adjustments // with the recurly API. func (a Adjustment) MarshalXML(e *xml.Encoder, start xml.StartElement) error { am := adjustmentMarshaler{ Description: a.Description, AccountingCode: a.AccountingCode, UnitAmountInCents: a.UnitAmountInCents, Quantity: a.Quantity, Currency: a.Currency, TaxCode: a.TaxCode, TaxExempt: a.TaxExempt, } return e.Encode(am) }
func (node *Package) MarshalXML(e *xml.Encoder, start xml.StartElement) error { var err error startElt := xml.StartElement{Name: xml.Name{Local: "package"}} if node.Level == "" { node.Level = "beginner" } startElt.Attr = []xml.Attr{ xml.Attr{Name: xml.Name{Local: "name"}, Value: node.Name}, xml.Attr{Name: xml.Name{Local: "level"}, Value: node.Level}, xml.Attr{Name: xml.Name{Local: "label"}, Value: strings.Join(node.Label, ",")}, xml.Attr{Name: xml.Name{Local: "loadspackages"}, Value: strings.Join(node.LoadsPackages, ",")}, } err = e.EncodeToken(startElt) if err != nil { return err } err = marshalShortDescription("shortdescription", e, node.ShortDescription) if err != nil { return err } err = marshalDescription("description", e, node.Description) if err != nil { return err } err = e.Encode(node.Options) if err != nil { return err } err = e.Encode(node.Commands) if err != nil { return err } err = e.EncodeToken(xml.EndElement{Name: startElt.Name}) if err != nil { return err } return nil }
func (v *Variant) MarshalXML(e *xml.Encoder, start xml.StartElement) error { variantStartElt := xml.StartElement{Name: xml.Name{Local: "variant"}} variantStartElt.Attr = append(variantStartElt.Attr, xml.Attr{Name: xml.Name{Local: "name"}, Value: v.Name}) err := e.EncodeToken(variantStartElt) if err != nil { return err } e.Encode(v.Arguments) marshalDescription("description", e, v.Description) err = e.EncodeToken(xml.EndElement{Name: variantStartElt.Name}) if err != nil { return err } return nil }
func subscribe(xmlEncoder *xml.Encoder, xmlDecoder *xml.Decoder, fullJID string) error { var bareJID string if barePosition := strings.Index(fullJID, "/"); barePosition < 0 { return fmt.Errorf("Can't split JID %s", fullJID) } else { bareJID = fullJID[:barePosition] } var subscribe struct { XMLName xml.Name `xml:"jabber:client iq"` Type string `xml:"type,attr"` To string `xml:"to,attr"` ID string `xml:"id,attr"` Subscribe struct { XMLName xml.Name `xml:"google:push subscribe"` Item struct { XMLName xml.Name `xml:"item"` Channel string `xml:"channel,attr"` From string `xml:"from,attr"` } } } subscribe.Type = "set" subscribe.To = bareJID subscribe.ID = "3" subscribe.Subscribe.Item.Channel = "cloudprint.google.com" subscribe.Subscribe.Item.From = "cloudprint.google.com" if err := xmlEncoder.Encode(&subscribe); err != nil { return fmt.Errorf("XMPP subscription request failed: %s", err) } var subscription struct { XMLName xml.Name `xml:"jabber:client iq"` To string `xml:"to,attr"` From string `xml:"from,attr"` } if err := xmlDecoder.Decode(&subscription); err != nil { return fmt.Errorf("XMPP subscription response invalid: %s", err) } else if fullJID != subscription.To || bareJID != subscription.From { return errors.New("XMPP subscription failed") } return nil }
func WrapList(e *xml.Encoder, listName xml.Name, list interface{}) error { listVal := reflect.ValueOf(list) token := xml.StartElement{ Name: listName, } err := e.EncodeToken(token) if err != nil { return err } count := listVal.Len() for index := 0; index < count; index++ { err = e.Encode(listVal.Index(index).Interface()) if err != nil { return err } } return e.EncodeToken(token.End()) }
func (t *Tag) MarshalXML(e *xml.Encoder, start xml.StartElement) error { start.Name = t.Name start.Attr = t.Attr e.EncodeToken(start) for _, v := range t.Children { switch v.(type) { case *Tag: child := v.(*Tag) if err := e.Encode(child); err != nil { return err } case xml.CharData: e.EncodeToken(v.(xml.CharData)) case xml.Comment: e.EncodeToken(v.(xml.Comment)) } } e.EncodeToken(start.End()) return nil }
func (x *domm) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) { start.Name.Local = x.model.Name() for k, v := range x.model.AttrAsMap() { start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: k}, Value: v}) } e.EncodeToken(start) for _, _l := range x.model.Children() { switch l := _l.(type) { case dom.Element: child := &domm{} child.model = l e.Encode(child) case dom.Text: e.EncodeToken(xml.CharData(l.Data())) default: halt.As(100, reflect.TypeOf(l)) } } e.EncodeToken(start.End()) return }
func (fc *FeatureContainer) MarshalXML(e *xml.Encoder, start xml.StartElement) error { start = xml.StartElement{Name: xml.Name{Local: "stream:features"}} if err := e.EncodeToken(start); err != nil { return err } fs := make([]Feature, 0, len(fc.features)) for _, v := range fc.features { fs = append(fs, v) } if err := e.Encode(fs); err != nil { return err } if err := e.EncodeToken(start.End()); err != nil { return err } return e.Flush() }
func (l *Ltxref) MarshalXML(e *xml.Encoder, start xml.StartElement) error { eltname := xml.Name{Local: "ltxref", Space: "urn:speedata.de:2015:latexref"} startelt := xml.StartElement{Name: eltname} startelt.Attr = append(startelt.Attr, xml.Attr{Name: xml.Name{Local: "version"}, Value: l.Version}) e.Indent("", " ") err := e.EncodeToken(startelt) if err != nil { return err } err = e.Encode(l.Commands) if err != nil { return err } err = e.Encode(l.Environments) if err != nil { return err } err = e.Encode(l.DocumentClasses) if err != nil { return err } err = e.Encode(l.Packages) if err != nil { return err } err = e.EncodeToken(xml.EndElement{Name: eltname}) if err != nil { return err } if false { return errors.New("dummy error") } return nil }
func marshalTwiml(t twimlResponse, e *xml.Encoder, start *xml.StartElement) error { return e.Encode(t.getContents()) }
func xmppHandshake(xmlEncoder *xml.Encoder, xmlDecoder *xml.Decoder, domain, proxyName string) (string, error) { handshake := xml.StartElement{ Name: xml.Name{"jabber:client", "stream:stream"}, Attr: []xml.Attr{ xml.Attr{xml.Name{Local: "to"}, domain}, xml.Attr{xml.Name{Local: "xml:lang"}, "en"}, xml.Attr{xml.Name{Local: "version"}, "1.0"}, xml.Attr{xml.Name{Local: "xmlns:stream"}, "http://etherx.jabber.org/streams"}, }, } if err := xmlEncoder.EncodeToken(handshake); err != nil { return "", fmt.Errorf("Failed to write SASL handshake: %s", err) } if err := xmlEncoder.Flush(); err != nil { return "", fmt.Errorf("Failed to flush encoding stream: %s", err) } if startElement, err := readStartElement(xmlDecoder); err != nil { return "", err } else if startElement.Name.Space != "http://etherx.jabber.org/streams" || startElement.Name.Local != "stream" { return "", fmt.Errorf("Read unexpected XMPP XML stanza: %s", startElement.Name.Local) } var features struct { XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"` Bind *struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"` } Session *struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-session session"` } } if err := xmlDecoder.Decode(&features); err != nil { return "", fmt.Errorf("Read unexpected XMPP XML element: %s", err) } else if features.Bind == nil || features.Session == nil { return "", errors.New("XMPP bind or session missing from handshake") } var resource struct { XMLName xml.Name `xml:"jabber:client iq"` Type string `xml:"type,attr"` ID string `xml:"id,attr"` Bind struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"` Resource struct { XMLName xml.Name `xml:"resource"` ResourceName string `xml:",chardata"` } } } resource.Type = "set" resource.ID = "0" resource.Bind.Resource.ResourceName = proxyName if err := xmlEncoder.Encode(&resource); err != nil { return "", fmt.Errorf("Failed to set resource during XMPP handshake: %s", err) } var jid struct { XMLName xml.Name `xml:"jabber:client iq"` Bind *struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"` JID string `xml:"jid"` } } if err := xmlDecoder.Decode(&jid); err != nil { return "", err } else if jid.Bind == nil || jid.Bind.JID == "" { return "", errors.New("Received unexpected XML element during XMPP handshake") } fullJID := jid.Bind.JID var session struct { XMLName xml.Name `xml:"jabber:client iq"` Type string `xml:"type,attr"` ID string `xml:"id,attr"` Session struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-session session"` } } session.Type = "set" session.ID = "1" if err := xmlEncoder.Encode(&session); err != nil { return "", fmt.Errorf("Failed to complete XMPP handshake: %s", err) } var done struct { XMLName xml.Name `xml:"jabber:client iq"` ID string `xml:"id,attr"` } if err := xmlDecoder.Decode(&done); err != nil { return "", err } else if done.ID != "1" { return "", errors.New("Received unexpected result at end of XMPP handshake") } return fullJID, nil }
func saslHandshake(xmlEncoder *xml.Encoder, xmlDecoder *xml.Decoder, domain, user, accessToken string) error { handshake := xml.StartElement{ Name: xml.Name{"jabber:client", "stream:stream"}, Attr: []xml.Attr{ xml.Attr{xml.Name{Local: "to"}, domain}, xml.Attr{xml.Name{Local: "xml:lang"}, "en"}, xml.Attr{xml.Name{Local: "version"}, "1.0"}, xml.Attr{xml.Name{Local: "xmlns:stream"}, "http://etherx.jabber.org/streams"}, }, } if err := xmlEncoder.EncodeToken(handshake); err != nil { return fmt.Errorf("Failed to write SASL handshake: %s", err) } if err := xmlEncoder.Flush(); err != nil { return fmt.Errorf("Failed to flush encoding stream: %s", err) } if startElement, err := readStartElement(xmlDecoder); err != nil { return err } else if startElement.Name.Space != "http://etherx.jabber.org/streams" || startElement.Name.Local != "stream" { return fmt.Errorf("Read unexpected SASL XML stanza: %s", startElement.Name.Local) } var features struct { XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"` Mechanisms *struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl mechanisms"` } } if err := xmlDecoder.Decode(&features); err != nil { return fmt.Errorf("Read unexpected SASL XML element: %s", err) } else if features.Mechanisms == nil { return errors.New("SASL mechanisms missing from handshake") } credential := base64.StdEncoding.EncodeToString([]byte("\x00" + user + "\x00" + accessToken)) var auth struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl auth"` Mechanism string `xml:"mechanism,attr"` Service string `xml:"auth:service,attr"` Allow string `xml:"auth:allow-generated-jid,attr"` FullBind string `xml:"auth:client-uses-full-bind-result,attr"` XMLNS string `xml:"xmlns:auth,attr"` Credential string `xml:",chardata"` } auth.Mechanism = "X-OAUTH2" auth.Service = "chromiumsync" auth.Allow = "true" auth.FullBind = "true" auth.XMLNS = "http://www.google.com/talk/protocol/auth" auth.Credential = credential if err := xmlEncoder.Encode(auth); err != nil { return fmt.Errorf("Failed to write SASL credentials: %s", err) } var success struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl success"` } if err := xmlDecoder.Decode(&success); err != nil { return fmt.Errorf("Failed to complete SASL handshake: %s", err) } return nil }
// MarshalXML is a weird and stunted version of xml.Marshal which is used by the // REST-XML request types to get around a bug in encoding/xml which doesn't // allow us to marshal pointers to zero values: // // https://github.com/golang/go/issues/5452 func MarshalXML(v interface{}, e *xml.Encoder, start xml.StartElement) error { value := reflect.ValueOf(v) t := value.Type() switch value.Kind() { case reflect.Ptr: if !value.IsNil() { return MarshalXML(value.Elem().Interface(), e, start) } case reflect.Struct: var rootInfo xmlFieldInfo // detect xml.Name, if any for i := 0; i < value.NumField(); i++ { f := t.Field(i) v := value.Field(i) if f.Type == xmlName { rootInfo = parseXMLTag(f.Tag.Get("xml")) if rootInfo.name == "" { // name not in tag, try value name := v.Interface().(xml.Name) rootInfo = xmlFieldInfo{ name: name.Local, ns: name.Space, } } } } for _, start := range rootInfo.start(t.Name()) { if err := e.EncodeToken(start); err != nil { return err } } for i := 0; i < value.NumField(); i++ { ft := value.Type().Field(i) if ft.Type == xmlName { continue } fv := value.Field(i) fi := parseXMLTag(ft.Tag.Get("xml")) if fi.name == "-" { continue } if fi.omit { switch fv.Kind() { case reflect.Ptr: if fv.IsNil() { continue } case reflect.Slice, reflect.Map: if fv.Len() == 0 { continue } default: if !fv.IsValid() { continue } } } starts := fi.start(ft.Name) for _, start := range starts[:len(starts)-1] { if err := e.EncodeToken(start); err != nil { return err } } start := starts[len(starts)-1] if err := e.EncodeElement(fv.Interface(), start); err != nil { return err } for _, end := range fi.end(ft.Name)[1:] { if err := e.EncodeToken(end); err != nil { return err } } } for _, end := range rootInfo.end(t.Name()) { if err := e.EncodeToken(end); err != nil { return err } } default: return e.Encode(v) } return nil }
func (fw *Feature) MarshalXML(e *xml.Encoder, _ xml.StartElement) error { return e.Encode(fw.featureElement) }
func (u *extern) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) { switch x := u.x.(type) { case *ir.Unit: start.Name.Local = "unit" u.attr(&start, "name", x.Name) err = e.EncodeToken(start) for _, c := range x.Const { n := &extern{x: c} e.Encode(n) } for _, v := range x.Variables { n := &extern{x: v} e.Encode(n) } if len(x.Infix) > 0 { inf := xml.StartElement{} inf.Name.Local = "infix" u.attr(&inf, "num", len(x.Infix)) for i, v := range x.Infix { u.attr(&inf, "arg"+strconv.Itoa(i), v.Name) } e.EncodeToken(inf) e.EncodeToken(inf.End()) } for _, v := range x.Pre { pre := xml.StartElement{} pre.Name.Local = "precondition" e.EncodeToken(pre) n := &extern{x: v} e.Encode(n) e.EncodeToken(pre.End()) } for _, v := range x.Post { post := xml.StartElement{} post.Name.Local = "postcondition" e.EncodeToken(post) n := &extern{x: v} e.Encode(n) e.EncodeToken(post.End()) } err = e.EncodeToken(start.End()) case ir.ForeignType: start.Name.Local = "definition" u.attr(&start, "name", x.Name()) err = e.EncodeToken(start) for _, v := range x.Variables() { n := &extern{x: v, shallow: true} e.Encode(n) } for _, v := range x.Imports() { imp := xml.StartElement{} imp.Name.Local = "import" u.attr(&imp, "name", v) e.EncodeToken(imp) e.EncodeToken(imp.End()) } if len(x.Infix()) > 0 { inf := xml.StartElement{} inf.Name.Local = "infix" u.attr(&inf, "num", len(x.Infix())) for i, v := range x.Infix() { u.attr(&inf, "arg"+strconv.Itoa(i), v.Name) } e.EncodeToken(inf) e.EncodeToken(inf.End()) } err = e.EncodeToken(start.End()) case *ir.Variable: switch x.Modifier { case mods.IN: start.Name.Local = "in" case mods.OUT: start.Name.Local = "out" case mods.REG: start.Name.Local = "reg" default: start.Name.Local = "var" } u.attr(&start, "name", x.Name) u.attr(&start, "builtin", x.Type.Basic) if x.Type.Basic { u.attr(&start, "type", x.Type.Builtin.Code.String()) } else { u.attr(&start, "type", x.Type.Foreign.Name()) } e.EncodeToken(start) if x.Type.Basic && !u.shallow { if r := x.Unit.Rules[x.Name]; r != nil { n := &extern{x: r} e.Encode(n) } } else if !u.shallow { if rr := x.Unit.ForeignRules[x.Name]; rr != nil { for k, v := range rr { rs := xml.StartElement{} rs.Name.Local = "foreign" u.attr(&rs, "id", k) e.EncodeToken(rs) n := &extern{x: v} e.Encode(n) e.EncodeToken(rs.End()) } } } e.EncodeToken(start.End()) case *ir.AssignRule: start.Name.Local = "becomes" e.EncodeToken(start) n := &extern{x: x.Expr} e.Encode(n) e.EncodeToken(start.End()) case *ir.Const: start.Name.Local = "constant" u.attr(&start, "name", x.Name) e.EncodeToken(start) n := &extern{x: x.Expr} e.Encode(n) e.EncodeToken(start.End()) case *ir.ConstExpr: start.Name.Local = "constant-expression" u.attr(&start, "type", x.Type) e.EncodeToken(start) e.EncodeToken(u.data(x.Type, x.Value)) e.EncodeToken(start.End()) case *ir.AtomExpr: start.Name.Local = "atom-expression" u.attr(&start, "value", x.Value) e.EncodeToken(start) e.EncodeToken(start.End()) case *ir.SelectExpr: start.Name.Local = "selector-expression" if x.Var != nil { u.attr(&start, "unit", x.Var.Unit.Name) u.attr(&start, "variable", x.Var.Name) if x.Foreign != nil { u.attr(&start, "foreign", x.Foreign.Name) } } else if x.Const != nil { u.attr(&start, "unit", x.Const.Unit.Name) u.attr(&start, "constant", x.Const.Name) } else { halt.As(100) } u.attr(&start, "inner", x.Inner.String()) e.EncodeToken(start) for _, v := range x.ExprList { n := &extern{x: v} e.Encode(n) } e.EncodeToken(start.End()) case *ir.Monadic: start.Name.Local = "monadic-expression" u.attr(&start, "op", x.Op.String()) e.EncodeToken(start) { n := &extern{x: x.Expr} e.Encode(n) } e.EncodeToken(start.End()) case *ir.TypeTest: start.Name.Local = "type-test-expression" assert.For(x.Typ.Basic, 20) u.attr(&start, "type", x.Typ.Builtin.Code.String()) e.EncodeToken(start) { n := &extern{x: x.Operand} e.Encode(n) } e.EncodeToken(start.End()) case *ir.Dyadic: start.Name.Local = "dyadic-expression" u.attr(&start, "op", x.Op.String()) e.EncodeToken(start) { n := &extern{x: x.Left} e.Encode(n) } { n := &extern{x: x.Right} e.Encode(n) } e.EncodeToken(start.End()) case *ir.Ternary: start.Name.Local = "ternary-expression" e.EncodeToken(start) { n := &extern{x: x.If} e.Encode(n) } { n := &extern{x: x.Then} e.Encode(n) } { n := &extern{x: x.Else} e.Encode(n) } e.EncodeToken(start.End()) case *ir.InfixExpr: start.Name.Local = "infix-expression" u.attr(&start, "unit", x.Unit.Name()) e.EncodeToken(start) for _, v := range x.Args { n := &extern{x: v} e.Encode(n) } e.EncodeToken(start.End()) case *ir.ListExpr: start.Name.Local = "list-expression" e.EncodeToken(start) for _, v := range x.Expr { n := &extern{x: v} e.Encode(n) } e.EncodeToken(start.End()) case *ir.SetExpr: start.Name.Local = "set-expression" e.EncodeToken(start) for _, v := range x.Expr { n := &extern{x: v} e.Encode(n) } e.EncodeToken(start.End()) case *ir.MapExpr: start.Name.Local = "map-expression" e.EncodeToken(start) for i, v := range x.Key { key := xml.StartElement{} u.attr(&key, "index", i) key.Name.Local = "key" e.EncodeToken(key) n := &extern{x: v} e.Encode(n) e.EncodeToken(key.End()) } for i, v := range x.Value { val := xml.StartElement{} u.attr(&val, "index", i) val.Name.Local = "value" e.EncodeToken(val) n := &extern{x: v} e.Encode(n) e.EncodeToken(val.End()) } e.EncodeToken(start.End()) default: halt.As(100, reflect.TypeOf(x)) } return }