Example #1
0
// TransformXML parses the XML tree, traverses it and calls TransformFunc
// on each XML token, writing the output to the writer, resulting in a
// transformed XML tree
func TransformXML(decoder *xml.Decoder, encoder *xml.Encoder, fn TransformFunc) error {
	parentNodes := &NodeList{}
	for {
		token, err := decoder.Token()
		if err != nil {
			if err != io.EOF {
				return trace.Wrap(err)
			}
			break
		}
		for _, t := range fn(parentNodes, token) {
			if err := encoder.EncodeToken(t); err != nil {
				return err
			}
		}
		switch e := token.(type) {
		case xml.StartElement:
			parentNodes.Push(e)
		case xml.EndElement:
			parentNodes.Pop()
		}
	}
	encoder.Flush()
	return nil
}
Example #2
0
func (node *Packageoption) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	var err error
	startElt := xml.StartElement{Name: xml.Name{Local: "packageoption"}}

	var dflt string
	if node.Default {
		dflt = "yes"
	} else {
		dflt = "no"
	}
	startElt.Attr = []xml.Attr{
		xml.Attr{Name: xml.Name{Local: "name"}, Value: node.Name},
		xml.Attr{Name: xml.Name{Local: "default"}, Value: dflt},
	}

	err = e.EncodeToken(startElt)
	if err != nil {
		return err
	}

	err = marshalShortDescription("shortdescription", e, node.ShortDescription)
	if err != nil {
		return err
	}
	err = e.EncodeToken(xml.EndElement{Name: startElt.Name})
	if err != nil {
		return err
	}

	return nil
}
Example #3
0
func (a *Argument) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	var err error
	startElt := xml.StartElement{Name: xml.Name{Local: "argument"}}
	var opt string
	if a.Optional {
		opt = "yes"
	} else {
		opt = "no"
	}
	startElt.Attr = append(startElt.Attr, xml.Attr{Name: xml.Name{Local: "name"}, Value: a.Name})
	startElt.Attr = append(startElt.Attr, xml.Attr{Name: xml.Name{Local: "optional"}, Value: opt})
	startElt.Attr = append(startElt.Attr, xml.Attr{Name: xml.Name{Local: "type"}, Value: argumentTypeReveseMap[a.Type]})

	err = e.EncodeToken(startElt)
	if err != nil {
		return err
	}

	err = e.EncodeToken(xml.EndElement{Name: startElt.Name})
	if err != nil {
		return err
	}

	return nil
}
Example #4
0
// MarshalXML implements the xml.Marshaller method to allow for the
// correct wrapper/start element case and attr data.
func (c Change) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	start.Name.Local = "osmChange"
	start.Attr = []xml.Attr{
		{Name: xml.Name{Local: "version"}, Value: "0.6"},
		{Name: xml.Name{Local: "generator"}, Value: "go.osm"},
	}

	if err := e.EncodeToken(start); err != nil {
		return err
	}

	if err := marshalInnerChange(e, "create", c.Create); err != nil {
		return err
	}

	if err := marshalInnerChange(e, "modify", c.Modify); err != nil {
		return err
	}

	if err := marshalInnerChange(e, "delete", c.Delete); err != nil {
		return err
	}

	if err := e.EncodeToken(start.End()); err != nil {
		return err
	}

	return nil
}
Example #5
0
func encEle(n tree.Elem, e *xml.Encoder) error {
	ele := xml.StartElement{
		Name: n.GetToken().(xml.StartElement).Name,
	}

	attrs := n.GetAttrs()
	ele.Attr = make([]xml.Attr, len(attrs))
	for i := range attrs {
		ele.Attr[i] = attrs[i].GetToken().(xml.Attr)
	}

	err := e.EncodeToken(ele)
	if err != nil {
		return err
	}

	if x, ok := n.(tree.Elem); ok {
		for _, i := range x.GetChildren() {
			err := encTok(i, e)
			if err != nil {
				return err
			}
		}
	}

	return e.EncodeToken(ele.End())
}
Example #6
0
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})
}
Example #7
0
func encNS(ns xml.Attr, e *xml.Encoder) error {
	pi := xml.ProcInst{
		Target: "namespace",
		Inst:   ([]byte)(ns.Value),
	}
	return e.EncodeToken(pi)
}
Example #8
0
// MarshalXML impements the xml.Marshaler interface.
// Any tag present with this type = true.
func (b Bool) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if b {
		e.EncodeToken(start)
		e.EncodeToken(xml.EndElement{Name: start.Name})
	}
	return nil
}
func (bagc BiddableAdGroupCriterion) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	start.Attr = append(
		start.Attr,
		xml.Attr{
			xml.Name{"http://www.w3.org/2001/XMLSchema-instance", "type"},
			"BiddableAdGroupCriterion",
		},
	)
	e.EncodeToken(start)
	e.EncodeElement(&bagc.AdGroupId, xml.StartElement{Name: xml.Name{"", "adGroupId"}})
	criterionMarshalXML(bagc.Criterion, e)

	if bagc.UserStatus != "" {
		e.EncodeElement(&bagc.UserStatus, xml.StartElement{Name: xml.Name{"", "userStatus"}})
	}
	if bagc.DestinationUrl != "" {
		e.EncodeElement(&bagc.DestinationUrl, xml.StartElement{Name: xml.Name{"", "destinationUrl"}})
	}
	e.EncodeElement(&bagc.BiddingStrategyConfiguration, xml.StartElement{Name: xml.Name{"", "biddingStrategyConfiguration"}})
	if bagc.BidModifier != 0 {
		e.EncodeElement(&bagc.BidModifier, xml.StartElement{Name: xml.Name{"", "bidModifier"}})
	}
	e.EncodeToken(start.End())
	return nil
}
Example #10
0
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
}
Example #11
0
func (a1 AdGroupAds) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	a := a1[0]
	e.EncodeToken(start)
	switch a.(type) {
	case TextAd:
		ad := a.(TextAd)
		e.EncodeElement(ad.AdGroupId, xml.StartElement{Name: xml.Name{"", "adGroupId"}})
		e.EncodeElement(ad, xml.StartElement{
			xml.Name{"", "ad"},
			[]xml.Attr{
				xml.Attr{xml.Name{"http://www.w3.org/2001/XMLSchema-instance", "type"}, "TextAd"},
			},
		})
		e.EncodeElement(ad.Status, xml.StartElement{Name: xml.Name{"", "status"}})
		e.EncodeElement(ad.Labels, xml.StartElement{Name: xml.Name{"", "labels"}})
	case ImageAd:
		return ERROR_NOT_YET_IMPLEMENTED
	case TemplateAd:
		return ERROR_NOT_YET_IMPLEMENTED
	default:
		return fmt.Errorf("unknown Ad type -> %#v", start)
	}
	e.EncodeToken(start.End())
	return nil
}
Example #12
0
func (p PList) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	start.Name = xml.Name{Local: "plist"}
	start.Attr = []xml.Attr{xml.Attr{Name: xml.Name{Local: "version"}, Value: "1.0"}}
	e.EncodeToken(start)
	PDict(p).MarshalXML(e, xml.StartElement{})
	e.EncodeToken(start.End())
	return nil
}
Example #13
0
// MarshalXML is a custom marshaller for Duration.
func (dur *Duration) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	start.Attr = []xml.Attr{xml.Attr{Name: xml.Name{Local: "duration"}, Value: dur.Duration.String()},
		xml.Attr{Name: xml.Name{Local: "state"}, Value: dur.State},
	}
	e.EncodeToken(start)
	e.EncodeToken(xml.EndElement{Name: start.Name})
	return nil
}
Example #14
0
func (a JobCommandJobRefArguments) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	start.Attr = []xml.Attr{
		xml.Attr{xml.Name{Local: "line"}, string(a)},
	}
	e.EncodeToken(start)
	e.EncodeToken(xml.EndElement{start.Name})
	return nil
}
Example #15
0
//Print prints the XML character data in string form
func (cd *PathResCharData) Print(e *xml.Encoder) error {
	var err error
	if _, ok := cd.Value.(xml.CharData); ok {
		val := cd.Value.(xml.CharData)
		err = e.EncodeToken(val)
	}
	return err
}
Example #16
0
//Print prints the XML comment in string form
func (c *PathResComment) Print(e *xml.Encoder) error {
	var err error
	if _, ok := c.Value.(xml.Comment); ok {
		val := c.Value.(xml.Comment)
		err = e.EncodeToken(val)
	}
	return err
}
Example #17
0
func (self *Element) Encode(enc *xml.Encoder, sr SchemaRepository, ga GetAliaser, params map[string]interface{}, path ...string) (err error) {
	if self.MinOccurs != "" && self.MinOccurs == "0" && !hasPrefix(params, MakePath(append(path, self.Name))) {
		return
	}

	for hasPrefix(params, MakePath(append(path, self.Name))) {
		start := xml.StartElement{
			Name: xml.Name{
				Space: ga.Namespace(),
				Local: self.Name,
			},
		}

		err = enc.EncodeToken(start)
		if err != nil {
			return
		}

		if self.Type != "" {
			parts := strings.Split(self.Type, ":")
			switch len(parts) {
			case 1:
				err = self.Encode(enc, sr, ga, params, append(path, self.Name)...)
				if err != nil {
					return
				}
			case 2:
				var schema Schemaer
				schema, err = sr.GetSchema(ga.GetAlias(parts[0]))
				if err != nil {
					return
				}

				err = schema.EncodeType(parts[1], enc, sr, params, append(path, self.Name)...)
				if err != nil {
					return
				}
			default:
				err = fmt.Errorf("malformed type '%s' in path %q", self.Type, path)
				return
			}
		} else if self.ComplexTypes != nil {
			for _, e := range self.ComplexTypes.Sequence {
				err = e.Encode(enc, sr, ga, params, append(path, self.Name)...)
				if err != nil {
					return
				}
			}
		}

		err = enc.EncodeToken(start.End())
		if err != nil {
			return
		}
	}

	return
}
Example #18
0
func (a PArray) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	start.Name = xml.Name{Local: "array"}
	e.EncodeToken(start)
	for _, el := range a {
		el.(xml.Marshaler).MarshalXML(e, start)
	}
	e.EncodeToken(start.End())
	return nil
}
Example #19
0
func (a AlfredOutput) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	start.Name = xml.Name{Local: "items"}
	e.EncodeToken(start)
	for _, item := range a {
		e.EncodeElement(item, xml.StartElement{Name: xml.Name{Local: "item"}})
	}
	e.EncodeToken(start.End())
	return nil
}
Example #20
0
func (d PDict) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	start.Name = xml.Name{Local: "dict"}
	e.EncodeToken(start)
	for k, v := range d {
		e.EncodeElement(k, xml.StartElement{Name: xml.Name{Local: "key"}})
		v.MarshalXML(e, start)
	}
	e.EncodeToken(start.End())
	return nil
}
Example #21
0
// MarshalXML marshals ce to e. start is ignored.
func (ce *CompoundElement) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if err := e.EncodeToken(ce.StartElement); err != nil {
		return err
	}
	for _, c := range ce.children {
		if err := e.EncodeElement(c, ce.StartElement); err != nil {
			return err
		}
	}
	return e.EncodeToken(ce.End())
}
Example #22
0
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
}
Example #23
0
func (mmo MutateMembersOperand) MarshalXML(e *xml.Encoder, start xml.StartElement) error {

	mmo.encodeEmails()

	e.EncodeToken(start)
	e.EncodeElement(&mmo.UserListId, xml.StartElement{Name: xml.Name{baseRemarketingUrl, "userListId"}})
	e.EncodeElement(&mmo.DataType, xml.StartElement{Name: xml.Name{baseRemarketingUrl, "dataType"}})
	e.EncodeElement(&mmo.Members, xml.StartElement{Name: xml.Name{baseRemarketingUrl, "members"}})
	e.EncodeToken(start.End())
	return nil
}
Example #24
0
// MarshalXML implements xml.Marshaler interface
func (c StreamErrorCondition) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	t := xml.StartElement{
		Name: xml.Name{
			"urn:ietf:params:xml:ns:xmpp-streams", string(c),
		},
	}

	e.EncodeToken(t)
	e.EncodeToken(t.End())

	return nil
}
Example #25
0
//Print prints the XML attribute in string form
func (a *PathResAttribute) Print(e *xml.Encoder) error {
	attr := a.Value.(*xml.Attr)
	str := attr.Name.Local + `="` + attr.Value + `"`
	if attr.Name.Space != "" {
		str = attr.Name.Space + ":" + str
	}
	pi := xml.ProcInst{
		Target: "attribute",
		Inst:   ([]byte)(str),
	}
	return e.EncodeToken(pi)
}
Example #26
0
func makeStartXml(root *XlsObjNode, enc *xml.Encoder) {
	valueList := root.value.([]*XlsObjNode)
	token := xml.StartElement{xml.Name{"", root.key}, make([]xml.Attr, 0)}

	for _, key := range valueList {
		if key.nodeType == ATTR_TYPE {
			attr := xml.Attr{xml.Name{"", key.key}, key.value.(string)}
			token.Attr = append(token.Attr, attr)
		}
	}
	enc.EncodeToken(token)
}
Example #27
0
// MarshalXML is a custom XML marshaler for metadataMap, mapping each metadata name/value pair to a
// correspondingly named XML element with the pair's value as character data content.
func (m metadataMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if err := e.EncodeToken(start); err != nil {
		return err
	}

	for k, v := range m {
		if err := e.EncodeElement(v, startLocalName(k)); err != nil {
			return err
		}
	}

	return e.EncodeToken(start.End())
}
Example #28
0
// MarshalXML is a custom XML marshaler for DataCenterInfo, writing either Metadata or AlternateMetadata
// depending on the type of data center indicated by the Name.
func (i DataCenterInfo) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if err := e.EncodeToken(start); err != nil {
		return err
	}

	if err := e.EncodeElement(i.Name, startName); err != nil {
		return err
	}
	if err := e.EncodeElement(metadataValue(i), startMetadata); err != nil {
		return err
	}

	return e.EncodeToken(start.End())
}
Example #29
0
func marshalMapToXML(c *map[string]string, e *xml.Encoder, start xml.StartElement, entryName string, keyName string, valueName string) error {
	if len(*c) == 0 {
		return nil
	}
	e.EncodeToken(start)

	// Sort the keys so we'll have a deterministic result.
	keys := []string{}
	for k, _ := range *c {
		keys = append(keys, k)
	}
	sort.Strings(keys)

	for _, k := range keys {
		v := (*c)[k]
		e.EncodeToken(xml.StartElement{
			Name: xml.Name{Local: entryName},
			Attr: []xml.Attr{
				xml.Attr{
					Name:  xml.Name{Local: keyName},
					Value: k,
				},
				xml.Attr{
					Name:  xml.Name{Local: valueName},
					Value: v,
				},
			},
		})
		e.EncodeToken(xml.EndElement{xml.Name{Local: entryName}})
	}
	e.EncodeToken(xml.EndElement{start.Name})
	return nil
}
func (nagc NegativeAdGroupCriterion) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	start.Attr = append(
		start.Attr,
		xml.Attr{
			xml.Name{"http://www.w3.org/2001/XMLSchema-instance", "type"},
			"NegativeAdGroupCriterion",
		},
	)
	e.EncodeToken(start)
	e.EncodeElement(&nagc.AdGroupId, xml.StartElement{Name: xml.Name{"", "adGroupId"}})
	criterionMarshalXML(nagc.Criterion, e)
	e.EncodeToken(start.End())
	return nil
}