diff options
Diffstat (limited to 'vendor/github.com/aws/aws-sdk-go/private/protocol/xml')
3 files changed, 703 insertions, 0 deletions
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/build.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/build.go new file mode 100644 index 0000000..7091b45 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/build.go | |||
@@ -0,0 +1,296 @@ | |||
1 | // Package xmlutil provides XML serialization of AWS requests and responses. | ||
2 | package xmlutil | ||
3 | |||
4 | import ( | ||
5 | "encoding/base64" | ||
6 | "encoding/xml" | ||
7 | "fmt" | ||
8 | "reflect" | ||
9 | "sort" | ||
10 | "strconv" | ||
11 | "time" | ||
12 | |||
13 | "github.com/aws/aws-sdk-go/private/protocol" | ||
14 | ) | ||
15 | |||
16 | // BuildXML will serialize params into an xml.Encoder. | ||
17 | // Error will be returned if the serialization of any of the params or nested values fails. | ||
18 | func BuildXML(params interface{}, e *xml.Encoder) error { | ||
19 | b := xmlBuilder{encoder: e, namespaces: map[string]string{}} | ||
20 | root := NewXMLElement(xml.Name{}) | ||
21 | if err := b.buildValue(reflect.ValueOf(params), root, ""); err != nil { | ||
22 | return err | ||
23 | } | ||
24 | for _, c := range root.Children { | ||
25 | for _, v := range c { | ||
26 | return StructToXML(e, v, false) | ||
27 | } | ||
28 | } | ||
29 | return nil | ||
30 | } | ||
31 | |||
32 | // Returns the reflection element of a value, if it is a pointer. | ||
33 | func elemOf(value reflect.Value) reflect.Value { | ||
34 | for value.Kind() == reflect.Ptr { | ||
35 | value = value.Elem() | ||
36 | } | ||
37 | return value | ||
38 | } | ||
39 | |||
40 | // A xmlBuilder serializes values from Go code to XML | ||
41 | type xmlBuilder struct { | ||
42 | encoder *xml.Encoder | ||
43 | namespaces map[string]string | ||
44 | } | ||
45 | |||
46 | // buildValue generic XMLNode builder for any type. Will build value for their specific type | ||
47 | // struct, list, map, scalar. | ||
48 | // | ||
49 | // Also takes a "type" tag value to set what type a value should be converted to XMLNode as. If | ||
50 | // type is not provided reflect will be used to determine the value's type. | ||
51 | func (b *xmlBuilder) buildValue(value reflect.Value, current *XMLNode, tag reflect.StructTag) error { | ||
52 | value = elemOf(value) | ||
53 | if !value.IsValid() { // no need to handle zero values | ||
54 | return nil | ||
55 | } else if tag.Get("location") != "" { // don't handle non-body location values | ||
56 | return nil | ||
57 | } | ||
58 | |||
59 | t := tag.Get("type") | ||
60 | if t == "" { | ||
61 | switch value.Kind() { | ||
62 | case reflect.Struct: | ||
63 | t = "structure" | ||
64 | case reflect.Slice: | ||
65 | t = "list" | ||
66 | case reflect.Map: | ||
67 | t = "map" | ||
68 | } | ||
69 | } | ||
70 | |||
71 | switch t { | ||
72 | case "structure": | ||
73 | if field, ok := value.Type().FieldByName("_"); ok { | ||
74 | tag = tag + reflect.StructTag(" ") + field.Tag | ||
75 | } | ||
76 | return b.buildStruct(value, current, tag) | ||
77 | case "list": | ||
78 | return b.buildList(value, current, tag) | ||
79 | case "map": | ||
80 | return b.buildMap(value, current, tag) | ||
81 | default: | ||
82 | return b.buildScalar(value, current, tag) | ||
83 | } | ||
84 | } | ||
85 | |||
86 | // buildStruct adds a struct and its fields to the current XMLNode. All fields any any nested | ||
87 | // types are converted to XMLNodes also. | ||
88 | func (b *xmlBuilder) buildStruct(value reflect.Value, current *XMLNode, tag reflect.StructTag) error { | ||
89 | if !value.IsValid() { | ||
90 | return nil | ||
91 | } | ||
92 | |||
93 | fieldAdded := false | ||
94 | |||
95 | // unwrap payloads | ||
96 | if payload := tag.Get("payload"); payload != "" { | ||
97 | field, _ := value.Type().FieldByName(payload) | ||
98 | tag = field.Tag | ||
99 | value = elemOf(value.FieldByName(payload)) | ||
100 | |||
101 | if !value.IsValid() { | ||
102 | return nil | ||
103 | } | ||
104 | } | ||
105 | |||
106 | child := NewXMLElement(xml.Name{Local: tag.Get("locationName")}) | ||
107 | |||
108 | // there is an xmlNamespace associated with this struct | ||
109 | if prefix, uri := tag.Get("xmlPrefix"), tag.Get("xmlURI"); uri != "" { | ||
110 | ns := xml.Attr{ | ||
111 | Name: xml.Name{Local: "xmlns"}, | ||
112 | Value: uri, | ||
113 | } | ||
114 | if prefix != "" { | ||
115 | b.namespaces[prefix] = uri // register the namespace | ||
116 | ns.Name.Local = "xmlns:" + prefix | ||
117 | } | ||
118 | |||
119 | child.Attr = append(child.Attr, ns) | ||
120 | } | ||
121 | |||
122 | t := value.Type() | ||
123 | for i := 0; i < value.NumField(); i++ { | ||
124 | member := elemOf(value.Field(i)) | ||
125 | field := t.Field(i) | ||
126 | |||
127 | if field.PkgPath != "" { | ||
128 | continue // ignore unexported fields | ||
129 | } | ||
130 | if field.Tag.Get("ignore") != "" { | ||
131 | continue | ||
132 | } | ||
133 | |||
134 | mTag := field.Tag | ||
135 | if mTag.Get("location") != "" { // skip non-body members | ||
136 | continue | ||
137 | } | ||
138 | |||
139 | if protocol.CanSetIdempotencyToken(value.Field(i), field) { | ||
140 | token := protocol.GetIdempotencyToken() | ||
141 | member = reflect.ValueOf(token) | ||
142 | } | ||
143 | |||
144 | memberName := mTag.Get("locationName") | ||
145 | if memberName == "" { | ||
146 | memberName = field.Name | ||
147 | mTag = reflect.StructTag(string(mTag) + ` locationName:"` + memberName + `"`) | ||
148 | } | ||
149 | if err := b.buildValue(member, child, mTag); err != nil { | ||
150 | return err | ||
151 | } | ||
152 | |||
153 | fieldAdded = true | ||
154 | } | ||
155 | |||
156 | if fieldAdded { // only append this child if we have one ore more valid members | ||
157 | current.AddChild(child) | ||
158 | } | ||
159 | |||
160 | return nil | ||
161 | } | ||
162 | |||
163 | // buildList adds the value's list items to the current XMLNode as children nodes. All | ||
164 | // nested values in the list are converted to XMLNodes also. | ||
165 | func (b *xmlBuilder) buildList(value reflect.Value, current *XMLNode, tag reflect.StructTag) error { | ||
166 | if value.IsNil() { // don't build omitted lists | ||
167 | return nil | ||
168 | } | ||
169 | |||
170 | // check for unflattened list member | ||
171 | flattened := tag.Get("flattened") != "" | ||
172 | |||
173 | xname := xml.Name{Local: tag.Get("locationName")} | ||
174 | if flattened { | ||
175 | for i := 0; i < value.Len(); i++ { | ||
176 | child := NewXMLElement(xname) | ||
177 | current.AddChild(child) | ||
178 | if err := b.buildValue(value.Index(i), child, ""); err != nil { | ||
179 | return err | ||
180 | } | ||
181 | } | ||
182 | } else { | ||
183 | list := NewXMLElement(xname) | ||
184 | current.AddChild(list) | ||
185 | |||
186 | for i := 0; i < value.Len(); i++ { | ||
187 | iname := tag.Get("locationNameList") | ||
188 | if iname == "" { | ||
189 | iname = "member" | ||
190 | } | ||
191 | |||
192 | child := NewXMLElement(xml.Name{Local: iname}) | ||
193 | list.AddChild(child) | ||
194 | if err := b.buildValue(value.Index(i), child, ""); err != nil { | ||
195 | return err | ||
196 | } | ||
197 | } | ||
198 | } | ||
199 | |||
200 | return nil | ||
201 | } | ||
202 | |||
203 | // buildMap adds the value's key/value pairs to the current XMLNode as children nodes. All | ||
204 | // nested values in the map are converted to XMLNodes also. | ||
205 | // | ||
206 | // Error will be returned if it is unable to build the map's values into XMLNodes | ||
207 | func (b *xmlBuilder) buildMap(value reflect.Value, current *XMLNode, tag reflect.StructTag) error { | ||
208 | if value.IsNil() { // don't build omitted maps | ||
209 | return nil | ||
210 | } | ||
211 | |||
212 | maproot := NewXMLElement(xml.Name{Local: tag.Get("locationName")}) | ||
213 | current.AddChild(maproot) | ||
214 | current = maproot | ||
215 | |||
216 | kname, vname := "key", "value" | ||
217 | if n := tag.Get("locationNameKey"); n != "" { | ||
218 | kname = n | ||
219 | } | ||
220 | if n := tag.Get("locationNameValue"); n != "" { | ||
221 | vname = n | ||
222 | } | ||
223 | |||
224 | // sorting is not required for compliance, but it makes testing easier | ||
225 | keys := make([]string, value.Len()) | ||
226 | for i, k := range value.MapKeys() { | ||
227 | keys[i] = k.String() | ||
228 | } | ||
229 | sort.Strings(keys) | ||
230 | |||
231 | for _, k := range keys { | ||
232 | v := value.MapIndex(reflect.ValueOf(k)) | ||
233 | |||
234 | mapcur := current | ||
235 | if tag.Get("flattened") == "" { // add "entry" tag to non-flat maps | ||
236 | child := NewXMLElement(xml.Name{Local: "entry"}) | ||
237 | mapcur.AddChild(child) | ||
238 | mapcur = child | ||
239 | } | ||
240 | |||
241 | kchild := NewXMLElement(xml.Name{Local: kname}) | ||
242 | kchild.Text = k | ||
243 | vchild := NewXMLElement(xml.Name{Local: vname}) | ||
244 | mapcur.AddChild(kchild) | ||
245 | mapcur.AddChild(vchild) | ||
246 | |||
247 | if err := b.buildValue(v, vchild, ""); err != nil { | ||
248 | return err | ||
249 | } | ||
250 | } | ||
251 | |||
252 | return nil | ||
253 | } | ||
254 | |||
255 | // buildScalar will convert the value into a string and append it as a attribute or child | ||
256 | // of the current XMLNode. | ||
257 | // | ||
258 | // The value will be added as an attribute if tag contains a "xmlAttribute" attribute value. | ||
259 | // | ||
260 | // Error will be returned if the value type is unsupported. | ||
261 | func (b *xmlBuilder) buildScalar(value reflect.Value, current *XMLNode, tag reflect.StructTag) error { | ||
262 | var str string | ||
263 | switch converted := value.Interface().(type) { | ||
264 | case string: | ||
265 | str = converted | ||
266 | case []byte: | ||
267 | if !value.IsNil() { | ||
268 | str = base64.StdEncoding.EncodeToString(converted) | ||
269 | } | ||
270 | case bool: | ||
271 | str = strconv.FormatBool(converted) | ||
272 | case int64: | ||
273 | str = strconv.FormatInt(converted, 10) | ||
274 | case int: | ||
275 | str = strconv.Itoa(converted) | ||
276 | case float64: | ||
277 | str = strconv.FormatFloat(converted, 'f', -1, 64) | ||
278 | case float32: | ||
279 | str = strconv.FormatFloat(float64(converted), 'f', -1, 32) | ||
280 | case time.Time: | ||
281 | const ISO8601UTC = "2006-01-02T15:04:05Z" | ||
282 | str = converted.UTC().Format(ISO8601UTC) | ||
283 | default: | ||
284 | return fmt.Errorf("unsupported value for param %s: %v (%s)", | ||
285 | tag.Get("locationName"), value.Interface(), value.Type().Name()) | ||
286 | } | ||
287 | |||
288 | xname := xml.Name{Local: tag.Get("locationName")} | ||
289 | if tag.Get("xmlAttribute") != "" { // put into current node's attribute list | ||
290 | attr := xml.Attr{Name: xname, Value: str} | ||
291 | current.Attr = append(current.Attr, attr) | ||
292 | } else { // regular text node | ||
293 | current.AddChild(&XMLNode{Name: xname, Text: str}) | ||
294 | } | ||
295 | return nil | ||
296 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/unmarshal.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/unmarshal.go new file mode 100644 index 0000000..8758462 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/unmarshal.go | |||
@@ -0,0 +1,260 @@ | |||
1 | package xmlutil | ||
2 | |||
3 | import ( | ||
4 | "encoding/base64" | ||
5 | "encoding/xml" | ||
6 | "fmt" | ||
7 | "io" | ||
8 | "reflect" | ||
9 | "strconv" | ||
10 | "strings" | ||
11 | "time" | ||
12 | ) | ||
13 | |||
14 | // UnmarshalXML deserializes an xml.Decoder into the container v. V | ||
15 | // needs to match the shape of the XML expected to be decoded. | ||
16 | // If the shape doesn't match unmarshaling will fail. | ||
17 | func UnmarshalXML(v interface{}, d *xml.Decoder, wrapper string) error { | ||
18 | n, err := XMLToStruct(d, nil) | ||
19 | if err != nil { | ||
20 | return err | ||
21 | } | ||
22 | if n.Children != nil { | ||
23 | for _, root := range n.Children { | ||
24 | for _, c := range root { | ||
25 | if wrappedChild, ok := c.Children[wrapper]; ok { | ||
26 | c = wrappedChild[0] // pull out wrapped element | ||
27 | } | ||
28 | |||
29 | err = parse(reflect.ValueOf(v), c, "") | ||
30 | if err != nil { | ||
31 | if err == io.EOF { | ||
32 | return nil | ||
33 | } | ||
34 | return err | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | return nil | ||
39 | } | ||
40 | return nil | ||
41 | } | ||
42 | |||
43 | // parse deserializes any value from the XMLNode. The type tag is used to infer the type, or reflect | ||
44 | // will be used to determine the type from r. | ||
45 | func parse(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
46 | rtype := r.Type() | ||
47 | if rtype.Kind() == reflect.Ptr { | ||
48 | rtype = rtype.Elem() // check kind of actual element type | ||
49 | } | ||
50 | |||
51 | t := tag.Get("type") | ||
52 | if t == "" { | ||
53 | switch rtype.Kind() { | ||
54 | case reflect.Struct: | ||
55 | t = "structure" | ||
56 | case reflect.Slice: | ||
57 | t = "list" | ||
58 | case reflect.Map: | ||
59 | t = "map" | ||
60 | } | ||
61 | } | ||
62 | |||
63 | switch t { | ||
64 | case "structure": | ||
65 | if field, ok := rtype.FieldByName("_"); ok { | ||
66 | tag = field.Tag | ||
67 | } | ||
68 | return parseStruct(r, node, tag) | ||
69 | case "list": | ||
70 | return parseList(r, node, tag) | ||
71 | case "map": | ||
72 | return parseMap(r, node, tag) | ||
73 | default: | ||
74 | return parseScalar(r, node, tag) | ||
75 | } | ||
76 | } | ||
77 | |||
78 | // parseStruct deserializes a structure and its fields from an XMLNode. Any nested | ||
79 | // types in the structure will also be deserialized. | ||
80 | func parseStruct(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
81 | t := r.Type() | ||
82 | if r.Kind() == reflect.Ptr { | ||
83 | if r.IsNil() { // create the structure if it's nil | ||
84 | s := reflect.New(r.Type().Elem()) | ||
85 | r.Set(s) | ||
86 | r = s | ||
87 | } | ||
88 | |||
89 | r = r.Elem() | ||
90 | t = t.Elem() | ||
91 | } | ||
92 | |||
93 | // unwrap any payloads | ||
94 | if payload := tag.Get("payload"); payload != "" { | ||
95 | field, _ := t.FieldByName(payload) | ||
96 | return parseStruct(r.FieldByName(payload), node, field.Tag) | ||
97 | } | ||
98 | |||
99 | for i := 0; i < t.NumField(); i++ { | ||
100 | field := t.Field(i) | ||
101 | if c := field.Name[0:1]; strings.ToLower(c) == c { | ||
102 | continue // ignore unexported fields | ||
103 | } | ||
104 | |||
105 | // figure out what this field is called | ||
106 | name := field.Name | ||
107 | if field.Tag.Get("flattened") != "" && field.Tag.Get("locationNameList") != "" { | ||
108 | name = field.Tag.Get("locationNameList") | ||
109 | } else if locName := field.Tag.Get("locationName"); locName != "" { | ||
110 | name = locName | ||
111 | } | ||
112 | |||
113 | // try to find the field by name in elements | ||
114 | elems := node.Children[name] | ||
115 | |||
116 | if elems == nil { // try to find the field in attributes | ||
117 | if val, ok := node.findElem(name); ok { | ||
118 | elems = []*XMLNode{{Text: val}} | ||
119 | } | ||
120 | } | ||
121 | |||
122 | member := r.FieldByName(field.Name) | ||
123 | for _, elem := range elems { | ||
124 | err := parse(member, elem, field.Tag) | ||
125 | if err != nil { | ||
126 | return err | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | return nil | ||
131 | } | ||
132 | |||
133 | // parseList deserializes a list of values from an XML node. Each list entry | ||
134 | // will also be deserialized. | ||
135 | func parseList(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
136 | t := r.Type() | ||
137 | |||
138 | if tag.Get("flattened") == "" { // look at all item entries | ||
139 | mname := "member" | ||
140 | if name := tag.Get("locationNameList"); name != "" { | ||
141 | mname = name | ||
142 | } | ||
143 | |||
144 | if Children, ok := node.Children[mname]; ok { | ||
145 | if r.IsNil() { | ||
146 | r.Set(reflect.MakeSlice(t, len(Children), len(Children))) | ||
147 | } | ||
148 | |||
149 | for i, c := range Children { | ||
150 | err := parse(r.Index(i), c, "") | ||
151 | if err != nil { | ||
152 | return err | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | } else { // flattened list means this is a single element | ||
157 | if r.IsNil() { | ||
158 | r.Set(reflect.MakeSlice(t, 0, 0)) | ||
159 | } | ||
160 | |||
161 | childR := reflect.Zero(t.Elem()) | ||
162 | r.Set(reflect.Append(r, childR)) | ||
163 | err := parse(r.Index(r.Len()-1), node, "") | ||
164 | if err != nil { | ||
165 | return err | ||
166 | } | ||
167 | } | ||
168 | |||
169 | return nil | ||
170 | } | ||
171 | |||
172 | // parseMap deserializes a map from an XMLNode. The direct children of the XMLNode | ||
173 | // will also be deserialized as map entries. | ||
174 | func parseMap(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
175 | if r.IsNil() { | ||
176 | r.Set(reflect.MakeMap(r.Type())) | ||
177 | } | ||
178 | |||
179 | if tag.Get("flattened") == "" { // look at all child entries | ||
180 | for _, entry := range node.Children["entry"] { | ||
181 | parseMapEntry(r, entry, tag) | ||
182 | } | ||
183 | } else { // this element is itself an entry | ||
184 | parseMapEntry(r, node, tag) | ||
185 | } | ||
186 | |||
187 | return nil | ||
188 | } | ||
189 | |||
190 | // parseMapEntry deserializes a map entry from a XML node. | ||
191 | func parseMapEntry(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
192 | kname, vname := "key", "value" | ||
193 | if n := tag.Get("locationNameKey"); n != "" { | ||
194 | kname = n | ||
195 | } | ||
196 | if n := tag.Get("locationNameValue"); n != "" { | ||
197 | vname = n | ||
198 | } | ||
199 | |||
200 | keys, ok := node.Children[kname] | ||
201 | values := node.Children[vname] | ||
202 | if ok { | ||
203 | for i, key := range keys { | ||
204 | keyR := reflect.ValueOf(key.Text) | ||
205 | value := values[i] | ||
206 | valueR := reflect.New(r.Type().Elem()).Elem() | ||
207 | |||
208 | parse(valueR, value, "") | ||
209 | r.SetMapIndex(keyR, valueR) | ||
210 | } | ||
211 | } | ||
212 | return nil | ||
213 | } | ||
214 | |||
215 | // parseScaller deserializes an XMLNode value into a concrete type based on the | ||
216 | // interface type of r. | ||
217 | // | ||
218 | // Error is returned if the deserialization fails due to invalid type conversion, | ||
219 | // or unsupported interface type. | ||
220 | func parseScalar(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
221 | switch r.Interface().(type) { | ||
222 | case *string: | ||
223 | r.Set(reflect.ValueOf(&node.Text)) | ||
224 | return nil | ||
225 | case []byte: | ||
226 | b, err := base64.StdEncoding.DecodeString(node.Text) | ||
227 | if err != nil { | ||
228 | return err | ||
229 | } | ||
230 | r.Set(reflect.ValueOf(b)) | ||
231 | case *bool: | ||
232 | v, err := strconv.ParseBool(node.Text) | ||
233 | if err != nil { | ||
234 | return err | ||
235 | } | ||
236 | r.Set(reflect.ValueOf(&v)) | ||
237 | case *int64: | ||
238 | v, err := strconv.ParseInt(node.Text, 10, 64) | ||
239 | if err != nil { | ||
240 | return err | ||
241 | } | ||
242 | r.Set(reflect.ValueOf(&v)) | ||
243 | case *float64: | ||
244 | v, err := strconv.ParseFloat(node.Text, 64) | ||
245 | if err != nil { | ||
246 | return err | ||
247 | } | ||
248 | r.Set(reflect.ValueOf(&v)) | ||
249 | case *time.Time: | ||
250 | const ISO8601UTC = "2006-01-02T15:04:05Z" | ||
251 | t, err := time.Parse(ISO8601UTC, node.Text) | ||
252 | if err != nil { | ||
253 | return err | ||
254 | } | ||
255 | r.Set(reflect.ValueOf(&t)) | ||
256 | default: | ||
257 | return fmt.Errorf("unsupported value: %v (%s)", r.Interface(), r.Type()) | ||
258 | } | ||
259 | return nil | ||
260 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/xml_to_struct.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/xml_to_struct.go new file mode 100644 index 0000000..3e970b6 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/xml_to_struct.go | |||
@@ -0,0 +1,147 @@ | |||
1 | package xmlutil | ||
2 | |||
3 | import ( | ||
4 | "encoding/xml" | ||
5 | "fmt" | ||
6 | "io" | ||
7 | "sort" | ||
8 | ) | ||
9 | |||
10 | // A XMLNode contains the values to be encoded or decoded. | ||
11 | type XMLNode struct { | ||
12 | Name xml.Name `json:",omitempty"` | ||
13 | Children map[string][]*XMLNode `json:",omitempty"` | ||
14 | Text string `json:",omitempty"` | ||
15 | Attr []xml.Attr `json:",omitempty"` | ||
16 | |||
17 | namespaces map[string]string | ||
18 | parent *XMLNode | ||
19 | } | ||
20 | |||
21 | // NewXMLElement returns a pointer to a new XMLNode initialized to default values. | ||
22 | func NewXMLElement(name xml.Name) *XMLNode { | ||
23 | return &XMLNode{ | ||
24 | Name: name, | ||
25 | Children: map[string][]*XMLNode{}, | ||
26 | Attr: []xml.Attr{}, | ||
27 | } | ||
28 | } | ||
29 | |||
30 | // AddChild adds child to the XMLNode. | ||
31 | func (n *XMLNode) AddChild(child *XMLNode) { | ||
32 | if _, ok := n.Children[child.Name.Local]; !ok { | ||
33 | n.Children[child.Name.Local] = []*XMLNode{} | ||
34 | } | ||
35 | n.Children[child.Name.Local] = append(n.Children[child.Name.Local], child) | ||
36 | } | ||
37 | |||
38 | // XMLToStruct converts a xml.Decoder stream to XMLNode with nested values. | ||
39 | func XMLToStruct(d *xml.Decoder, s *xml.StartElement) (*XMLNode, error) { | ||
40 | out := &XMLNode{} | ||
41 | for { | ||
42 | tok, err := d.Token() | ||
43 | if err != nil { | ||
44 | if err == io.EOF { | ||
45 | break | ||
46 | } else { | ||
47 | return out, err | ||
48 | } | ||
49 | } | ||
50 | |||
51 | if tok == nil { | ||
52 | break | ||
53 | } | ||
54 | |||
55 | switch typed := tok.(type) { | ||
56 | case xml.CharData: | ||
57 | out.Text = string(typed.Copy()) | ||
58 | case xml.StartElement: | ||
59 | el := typed.Copy() | ||
60 | out.Attr = el.Attr | ||
61 | if out.Children == nil { | ||
62 | out.Children = map[string][]*XMLNode{} | ||
63 | } | ||
64 | |||
65 | name := typed.Name.Local | ||
66 | slice := out.Children[name] | ||
67 | if slice == nil { | ||
68 | slice = []*XMLNode{} | ||
69 | } | ||
70 | node, e := XMLToStruct(d, &el) | ||
71 | out.findNamespaces() | ||
72 | if e != nil { | ||
73 | return out, e | ||
74 | } | ||
75 | node.Name = typed.Name | ||
76 | node.findNamespaces() | ||
77 | tempOut := *out | ||
78 | // Save into a temp variable, simply because out gets squashed during | ||
79 | // loop iterations | ||
80 | node.parent = &tempOut | ||
81 | slice = append(slice, node) | ||
82 | out.Children[name] = slice | ||
83 | case xml.EndElement: | ||
84 | if s != nil && s.Name.Local == typed.Name.Local { // matching end token | ||
85 | return out, nil | ||
86 | } | ||
87 | out = &XMLNode{} | ||
88 | } | ||
89 | } | ||
90 | return out, nil | ||
91 | } | ||
92 | |||
93 | func (n *XMLNode) findNamespaces() { | ||
94 | ns := map[string]string{} | ||
95 | for _, a := range n.Attr { | ||
96 | if a.Name.Space == "xmlns" { | ||
97 | ns[a.Value] = a.Name.Local | ||
98 | } | ||
99 | } | ||
100 | |||
101 | n.namespaces = ns | ||
102 | } | ||
103 | |||
104 | func (n *XMLNode) findElem(name string) (string, bool) { | ||
105 | for node := n; node != nil; node = node.parent { | ||
106 | for _, a := range node.Attr { | ||
107 | namespace := a.Name.Space | ||
108 | if v, ok := node.namespaces[namespace]; ok { | ||
109 | namespace = v | ||
110 | } | ||
111 | if name == fmt.Sprintf("%s:%s", namespace, a.Name.Local) { | ||
112 | return a.Value, true | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | return "", false | ||
117 | } | ||
118 | |||
119 | // StructToXML writes an XMLNode to a xml.Encoder as tokens. | ||
120 | func StructToXML(e *xml.Encoder, node *XMLNode, sorted bool) error { | ||
121 | e.EncodeToken(xml.StartElement{Name: node.Name, Attr: node.Attr}) | ||
122 | |||
123 | if node.Text != "" { | ||
124 | e.EncodeToken(xml.CharData([]byte(node.Text))) | ||
125 | } else if sorted { | ||
126 | sortedNames := []string{} | ||
127 | for k := range node.Children { | ||
128 | sortedNames = append(sortedNames, k) | ||
129 | } | ||
130 | sort.Strings(sortedNames) | ||
131 | |||
132 | for _, k := range sortedNames { | ||
133 | for _, v := range node.Children[k] { | ||
134 | StructToXML(e, v, sorted) | ||
135 | } | ||
136 | } | ||
137 | } else { | ||
138 | for _, c := range node.Children { | ||
139 | for _, v := range c { | ||
140 | StructToXML(e, v, sorted) | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | |||
145 | e.EncodeToken(xml.EndElement{Name: node.Name}) | ||
146 | return e.Flush() | ||
147 | } | ||