File: node.go

package info (click to toggle)
golang-github-joshdk-go-junit 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 196 kB
  • sloc: xml: 369; makefile: 36; sh: 13
file content (47 lines) | stat: -rw-r--r-- 1,009 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright Josh Komoroske. All rights reserved.
// Use of this source code is governed by the MIT license,
// a copy of which can be found in the LICENSE.txt file.

package junit

import "encoding/xml"

type xmlNode struct {
	XMLName xml.Name
	Attrs   map[string]string `xml:"-"`
	Content []byte            `xml:",innerxml"`
	Nodes   []xmlNode         `xml:",any"`
}

func (n *xmlNode) Attr(name string) string {
	return n.Attrs[name]
}

func (n *xmlNode) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	type nodeAlias xmlNode
	if err := d.DecodeElement((*nodeAlias)(n), &start); err != nil {
		return err
	}

	content, err := extractContent(n.Content)
	if err != nil {
		return err
	}

	n.Content = content

	n.Attrs = attrMap(start.Attr)
	return nil
}

func attrMap(attrs []xml.Attr) map[string]string {
	if len(attrs) == 0 {
		return nil
	}

	attributes := make(map[string]string, len(attrs))
	for _, attr := range attrs {
		attributes[attr.Name.Local] = attr.Value
	}
	return attributes
}