Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 1 | // Copyright 2011 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package xml |
| 6 | |
| 7 | import ( |
| 8 | "bufio" |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 9 | "bytes" |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 10 | "encoding" |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 11 | "fmt" |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 12 | "io" |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 13 | "reflect" |
| 14 | "strconv" |
| 15 | "strings" |
| 16 | ) |
| 17 | |
| 18 | const ( |
Karel Pazdera | 6e9e9df | 2017-08-24 00:36:28 +0200 | [diff] [blame] | 19 | // Header is a generic XML header suitable for use with the output of Marshal. |
Gustavo Niemeyer | fd9c995 | 2012-01-23 01:32:07 -0200 | [diff] [blame] | 20 | // This is not automatically added to any output of this package, |
| 21 | // it is provided as a convenience. |
Russ Cox | 894869e | 2017-11-14 14:01:11 -0500 | [diff] [blame] | 22 | Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n" |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 23 | ) |
| 24 | |
Gustavo Niemeyer | 0442087 | 2012-01-24 01:10:32 -0200 | [diff] [blame] | 25 | // Marshal returns the XML encoding of v. |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 26 | // |
Dmitri Shuralyov | d8264de | 2016-11-09 14:49:12 -0800 | [diff] [blame] | 27 | // Marshal handles an array or slice by marshaling each of the elements. |
| 28 | // Marshal handles a pointer by marshaling the value it points at or, if the |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 29 | // pointer is nil, by writing nothing. Marshal handles an interface value by |
Dmitri Shuralyov | d8264de | 2016-11-09 14:49:12 -0800 | [diff] [blame] | 30 | // marshaling the value it contains or, if the interface value is nil, by |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 31 | // writing nothing. Marshal handles all other data by writing one or more XML |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 32 | // elements containing the data. |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 33 | // |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 34 | // The name for the XML elements is taken from, in order of preference: |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 35 | // - the tag on the XMLName field, if the data is a struct |
Sam Whited | 820e30f | 2016-07-05 20:06:00 -0500 | [diff] [blame] | 36 | // - the value of the XMLName field of type Name |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 37 | // - the tag of the struct field used to obtain the data |
| 38 | // - the name of the struct field used to obtain the data |
Dmitri Shuralyov | d8264de | 2016-11-09 14:49:12 -0800 | [diff] [blame] | 39 | // - the name of the marshaled type |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 40 | // |
Dmitri Shuralyov | d8264de | 2016-11-09 14:49:12 -0800 | [diff] [blame] | 41 | // The XML element for a struct contains marshaled elements for each of the |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 42 | // exported fields of the struct, with these exceptions: |
| 43 | // - the XMLName field, described above, is omitted. |
Gustavo Niemeyer | e3ab30b | 2012-01-24 21:04:40 -0200 | [diff] [blame] | 44 | // - a field with tag "-" is omitted. |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 45 | // - a field with tag "name,attr" becomes an attribute with |
| 46 | // the given name in the XML element. |
| 47 | // - a field with tag ",attr" becomes an attribute with the |
Francisco Souza | 76de81d | 2012-12-10 10:59:15 -0500 | [diff] [blame] | 48 | // field name in the XML element. |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 49 | // - a field with tag ",chardata" is written as character data, |
| 50 | // not as an XML element. |
Russ Cox | 92b02e3 | 2015-11-25 12:06:06 -0500 | [diff] [blame] | 51 | // - a field with tag ",cdata" is written as character data |
| 52 | // wrapped in one or more <![CDATA[ ... ]]> tags, not as an XML element. |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 53 | // - a field with tag ",innerxml" is written verbatim, not subject |
Dmitri Shuralyov | d8264de | 2016-11-09 14:49:12 -0800 | [diff] [blame] | 54 | // to the usual marshaling procedure. |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 55 | // - a field with tag ",comment" is written as an XML comment, not |
Dmitri Shuralyov | d8264de | 2016-11-09 14:49:12 -0800 | [diff] [blame] | 56 | // subject to the usual marshaling procedure. It must not contain |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 57 | // the "--" string within it. |
Gustavo Niemeyer | 0a7ad32 | 2012-02-08 01:57:44 -0200 | [diff] [blame] | 58 | // - a field with a tag including the "omitempty" option is omitted |
| 59 | // if the field value is empty. The empty values are false, 0, any |
| 60 | // nil pointer or interface value, and any array, slice, map, or |
| 61 | // string of length zero. |
Gustavo Niemeyer | 9242a90 | 2012-05-16 23:21:31 -0300 | [diff] [blame] | 62 | // - an anonymous struct field is handled as if the fields of its |
| 63 | // value were part of the outer struct. |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 64 | // |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 65 | // If a field uses a tag "a>b>c", then the element c will be nested inside |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 66 | // parent elements a and b. Fields that appear next to each other that name |
Gustavo Niemeyer | aed20a6 | 2012-02-16 02:01:46 -0200 | [diff] [blame] | 67 | // the same parent will be enclosed in one XML element. |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 68 | // |
Leigh McCulloch | 65a864a | 2017-11-07 05:33:35 +0000 | [diff] [blame] | 69 | // If the XML name for a struct field is defined by both the field tag and the |
| 70 | // struct's XMLName field, the names must match. |
| 71 | // |
Gustavo Niemeyer | aed20a6 | 2012-02-16 02:01:46 -0200 | [diff] [blame] | 72 | // See MarshalIndent for an example. |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 73 | // |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 74 | // Marshal will return an error if asked to marshal a channel, function, or map. |
Gustavo Niemeyer | 0442087 | 2012-01-24 01:10:32 -0200 | [diff] [blame] | 75 | func Marshal(v interface{}) ([]byte, error) { |
| 76 | var b bytes.Buffer |
| 77 | if err := NewEncoder(&b).Encode(v); err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | return b.Bytes(), nil |
| 81 | } |
| 82 | |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 83 | // Marshaler is the interface implemented by objects that can marshal |
| 84 | // themselves into valid XML elements. |
| 85 | // |
| 86 | // MarshalXML encodes the receiver as zero or more XML elements. |
| 87 | // By convention, arrays or slices are typically encoded as a sequence |
| 88 | // of elements, one per entry. |
| 89 | // Using start as the element tag is not required, but doing so |
| 90 | // will enable Unmarshal to match the XML elements to the correct |
| 91 | // struct field. |
| 92 | // One common implementation strategy is to construct a separate |
| 93 | // value with a layout corresponding to the desired XML and then |
| 94 | // to encode it using e.EncodeElement. |
| 95 | // Another common strategy is to use repeated calls to e.EncodeToken |
| 96 | // to generate the XML output one token at a time. |
| 97 | // The sequence of encoded tokens must make up zero or more valid |
| 98 | // XML elements. |
| 99 | type Marshaler interface { |
| 100 | MarshalXML(e *Encoder, start StartElement) error |
| 101 | } |
| 102 | |
| 103 | // MarshalerAttr is the interface implemented by objects that can marshal |
| 104 | // themselves into valid XML attributes. |
| 105 | // |
| 106 | // MarshalXMLAttr returns an XML attribute with the encoded value of the receiver. |
| 107 | // Using name as the attribute name is not required, but doing so |
| 108 | // will enable Unmarshal to match the attribute to the correct |
| 109 | // struct field. |
| 110 | // If MarshalXMLAttr returns the zero attribute Attr{}, no attribute |
| 111 | // will be generated in the output. |
| 112 | // MarshalXMLAttr is used only for struct fields with the |
| 113 | // "attr" option in the field tag. |
| 114 | type MarshalerAttr interface { |
| 115 | MarshalXMLAttr(name Name) (Attr, error) |
| 116 | } |
| 117 | |
Gustavo Niemeyer | aed20a6 | 2012-02-16 02:01:46 -0200 | [diff] [blame] | 118 | // MarshalIndent works like Marshal, but each XML element begins on a new |
| 119 | // indented line that starts with prefix and is followed by one or more |
| 120 | // copies of indent according to the nesting depth. |
| 121 | func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { |
| 122 | var b bytes.Buffer |
| 123 | enc := NewEncoder(&b) |
Russ Cox | ee90874 | 2013-01-30 07:57:20 -0800 | [diff] [blame] | 124 | enc.Indent(prefix, indent) |
Jan Ziak | 32a0cbb | 2012-06-25 16:00:35 -0400 | [diff] [blame] | 125 | if err := enc.Encode(v); err != nil { |
Gustavo Niemeyer | aed20a6 | 2012-02-16 02:01:46 -0200 | [diff] [blame] | 126 | return nil, err |
| 127 | } |
| 128 | return b.Bytes(), nil |
| 129 | } |
| 130 | |
Gustavo Niemeyer | 0442087 | 2012-01-24 01:10:32 -0200 | [diff] [blame] | 131 | // An Encoder writes XML data to an output stream. |
| 132 | type Encoder struct { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 133 | p printer |
Gustavo Niemeyer | 0442087 | 2012-01-24 01:10:32 -0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // NewEncoder returns a new encoder that writes to w. |
| 137 | func NewEncoder(w io.Writer) *Encoder { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 138 | e := &Encoder{printer{Writer: bufio.NewWriter(w)}} |
| 139 | e.p.encoder = e |
| 140 | return e |
Gustavo Niemeyer | 0442087 | 2012-01-24 01:10:32 -0200 | [diff] [blame] | 141 | } |
| 142 | |
Russ Cox | ee90874 | 2013-01-30 07:57:20 -0800 | [diff] [blame] | 143 | // Indent sets the encoder to generate XML in which each element |
| 144 | // begins on a new indented line that starts with prefix and is followed by |
| 145 | // one or more copies of indent according to the nesting depth. |
| 146 | func (enc *Encoder) Indent(prefix, indent string) { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 147 | enc.p.prefix = prefix |
| 148 | enc.p.indent = indent |
Russ Cox | ee90874 | 2013-01-30 07:57:20 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Gustavo Niemeyer | 0442087 | 2012-01-24 01:10:32 -0200 | [diff] [blame] | 151 | // Encode writes the XML encoding of v to the stream. |
| 152 | // |
| 153 | // See the documentation for Marshal for details about the conversion |
| 154 | // of Go values to XML. |
Russ Cox | 3c11dd8 | 2013-09-12 16:54:01 -0400 | [diff] [blame] | 155 | // |
| 156 | // Encode calls Flush before returning. |
Gustavo Niemeyer | 0442087 | 2012-01-24 01:10:32 -0200 | [diff] [blame] | 157 | func (enc *Encoder) Encode(v interface{}) error { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 158 | err := enc.p.marshalValue(reflect.ValueOf(v), nil, nil) |
Jan Ziak | 32a0cbb | 2012-06-25 16:00:35 -0400 | [diff] [blame] | 159 | if err != nil { |
| 160 | return err |
| 161 | } |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 162 | return enc.p.Flush() |
| 163 | } |
| 164 | |
| 165 | // EncodeElement writes the XML encoding of v to the stream, |
| 166 | // using start as the outermost tag in the encoding. |
| 167 | // |
| 168 | // See the documentation for Marshal for details about the conversion |
| 169 | // of Go values to XML. |
Russ Cox | 3c11dd8 | 2013-09-12 16:54:01 -0400 | [diff] [blame] | 170 | // |
| 171 | // EncodeElement calls Flush before returning. |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 172 | func (enc *Encoder) EncodeElement(v interface{}, start StartElement) error { |
| 173 | err := enc.p.marshalValue(reflect.ValueOf(v), nil, &start) |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | return enc.p.Flush() |
| 178 | } |
| 179 | |
| 180 | var ( |
Dominik Honnef | fdba5a7 | 2016-03-21 00:12:18 +0100 | [diff] [blame] | 181 | begComment = []byte("<!--") |
| 182 | endComment = []byte("-->") |
| 183 | endProcInst = []byte("?>") |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 184 | ) |
| 185 | |
| 186 | // EncodeToken writes the given XML token to the stream. |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 187 | // It returns an error if StartElement and EndElement tokens are not properly matched. |
Russ Cox | 3c11dd8 | 2013-09-12 16:54:01 -0400 | [diff] [blame] | 188 | // |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 189 | // EncodeToken does not call Flush, because usually it is part of a larger operation |
| 190 | // such as Encode or EncodeElement (or a custom Marshaler's MarshalXML invoked |
| 191 | // during those), and those will call Flush when finished. |
| 192 | // Callers that create an Encoder and then invoke EncodeToken directly, without |
| 193 | // using Encode or EncodeElement, need to call Flush when finished to ensure |
| 194 | // that the XML is written to the underlying writer. |
Jason Del Ponte | 92440fb | 2014-05-12 23:35:56 -0400 | [diff] [blame] | 195 | // |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 196 | // EncodeToken allows writing a ProcInst with Target set to "xml" only as the first token |
| 197 | // in the stream. |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 198 | func (enc *Encoder) EncodeToken(t Token) error { |
Didier Spezia | ca1d6c4 | 2015-07-15 20:09:24 +0000 | [diff] [blame] | 199 | |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 200 | p := &enc.p |
| 201 | switch t := t.(type) { |
| 202 | case StartElement: |
| 203 | if err := p.writeStart(&t); err != nil { |
| 204 | return err |
| 205 | } |
| 206 | case EndElement: |
| 207 | if err := p.writeEnd(t.Name); err != nil { |
| 208 | return err |
| 209 | } |
| 210 | case CharData: |
Roger Peppe | 4a3e000 | 2015-04-24 17:20:21 +0100 | [diff] [blame] | 211 | escapeText(p, t, false) |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 212 | case Comment: |
| 213 | if bytes.Contains(t, endComment) { |
| 214 | return fmt.Errorf("xml: EncodeToken of Comment containing --> marker") |
| 215 | } |
| 216 | p.WriteString("<!--") |
| 217 | p.Write(t) |
| 218 | p.WriteString("-->") |
Rob Pike | 46f9607 | 2013-09-06 07:54:43 +1000 | [diff] [blame] | 219 | return p.cachedWriteError() |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 220 | case ProcInst: |
Jason Del Ponte | 92440fb | 2014-05-12 23:35:56 -0400 | [diff] [blame] | 221 | // First token to be encoded which is also a ProcInst with target of xml |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 222 | // is the xml declaration. The only ProcInst where target of xml is allowed. |
Jason Del Ponte | 92440fb | 2014-05-12 23:35:56 -0400 | [diff] [blame] | 223 | if t.Target == "xml" && p.Buffered() != 0 { |
| 224 | return fmt.Errorf("xml: EncodeToken of ProcInst xml target only valid for xml declaration, first token encoded") |
| 225 | } |
| 226 | if !isNameString(t.Target) { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 227 | return fmt.Errorf("xml: EncodeToken of ProcInst with invalid Target") |
| 228 | } |
| 229 | if bytes.Contains(t.Inst, endProcInst) { |
| 230 | return fmt.Errorf("xml: EncodeToken of ProcInst containing ?> marker") |
| 231 | } |
| 232 | p.WriteString("<?") |
| 233 | p.WriteString(t.Target) |
| 234 | if len(t.Inst) > 0 { |
| 235 | p.WriteByte(' ') |
| 236 | p.Write(t.Inst) |
| 237 | } |
| 238 | p.WriteString("?>") |
| 239 | case Directive: |
Didier Spezia | 8b6527b | 2015-06-27 13:07:22 +0000 | [diff] [blame] | 240 | if !isValidDirective(t) { |
| 241 | return fmt.Errorf("xml: EncodeToken of Directive containing wrong < or > markers") |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 242 | } |
| 243 | p.WriteString("<!") |
| 244 | p.Write(t) |
| 245 | p.WriteString(">") |
Didier Spezia | ca1d6c4 | 2015-07-15 20:09:24 +0000 | [diff] [blame] | 246 | default: |
| 247 | return fmt.Errorf("xml: EncodeToken of invalid token type") |
| 248 | |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 249 | } |
Rob Pike | 46f9607 | 2013-09-06 07:54:43 +1000 | [diff] [blame] | 250 | return p.cachedWriteError() |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Didier Spezia | 8b6527b | 2015-06-27 13:07:22 +0000 | [diff] [blame] | 253 | // isValidDirective reports whether dir is a valid directive text, |
| 254 | // meaning angle brackets are matched, ignoring comments and strings. |
| 255 | func isValidDirective(dir Directive) bool { |
| 256 | var ( |
| 257 | depth int |
| 258 | inquote uint8 |
| 259 | incomment bool |
| 260 | ) |
| 261 | for i, c := range dir { |
| 262 | switch { |
| 263 | case incomment: |
| 264 | if c == '>' { |
| 265 | if n := 1 + i - len(endComment); n >= 0 && bytes.Equal(dir[n:i+1], endComment) { |
| 266 | incomment = false |
| 267 | } |
| 268 | } |
| 269 | // Just ignore anything in comment |
| 270 | case inquote != 0: |
| 271 | if c == inquote { |
| 272 | inquote = 0 |
| 273 | } |
| 274 | // Just ignore anything within quotes |
| 275 | case c == '\'' || c == '"': |
| 276 | inquote = c |
| 277 | case c == '<': |
| 278 | if i+len(begComment) < len(dir) && bytes.Equal(dir[i:i+len(begComment)], begComment) { |
| 279 | incomment = true |
| 280 | } else { |
| 281 | depth++ |
| 282 | } |
| 283 | case c == '>': |
| 284 | if depth == 0 { |
| 285 | return false |
| 286 | } |
| 287 | depth-- |
| 288 | } |
| 289 | } |
| 290 | return depth == 0 && inquote == 0 && !incomment |
| 291 | } |
| 292 | |
Russ Cox | 3c11dd8 | 2013-09-12 16:54:01 -0400 | [diff] [blame] | 293 | // Flush flushes any buffered XML to the underlying writer. |
| 294 | // See the EncodeToken documentation for details about when it is necessary. |
| 295 | func (enc *Encoder) Flush() error { |
| 296 | return enc.p.Flush() |
| 297 | } |
| 298 | |
Gustavo Niemeyer | 0442087 | 2012-01-24 01:10:32 -0200 | [diff] [blame] | 299 | type printer struct { |
| 300 | *bufio.Writer |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 301 | encoder *Encoder |
Russ Cox | 4dd3e1e | 2013-03-12 11:46:12 -0400 | [diff] [blame] | 302 | seq int |
Gustavo Niemeyer | aed20a6 | 2012-02-16 02:01:46 -0200 | [diff] [blame] | 303 | indent string |
| 304 | prefix string |
| 305 | depth int |
| 306 | indentedIn bool |
Shivakumar GN | 848d10f | 2013-02-03 11:21:07 -0500 | [diff] [blame] | 307 | putNewline bool |
Russ Cox | bdf8bf6 | 2013-03-13 14:36:42 -0400 | [diff] [blame] | 308 | attrNS map[string]string // map prefix -> name space |
| 309 | attrPrefix map[string]string // map name space -> prefix |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 310 | prefixes []string |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 311 | tags []Name |
Russ Cox | bdf8bf6 | 2013-03-13 14:36:42 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 314 | // createAttrPrefix finds the name space prefix attribute to use for the given name space, |
| 315 | // defining a new prefix if necessary. It returns the prefix. |
| 316 | func (p *printer) createAttrPrefix(url string) string { |
| 317 | if prefix := p.attrPrefix[url]; prefix != "" { |
| 318 | return prefix |
| 319 | } |
Russ Cox | bdf8bf6 | 2013-03-13 14:36:42 -0400 | [diff] [blame] | 320 | |
| 321 | // The "http://www.w3.org/XML/1998/namespace" name space is predefined as "xml" |
| 322 | // and must be referred to that way. |
| 323 | // (The "http://www.w3.org/2000/xmlns/" name space is also predefined as "xmlns", |
| 324 | // but users should not be trying to use that one directly - that's our job.) |
| 325 | if url == xmlURL { |
Russ Cox | 894869e | 2017-11-14 14:01:11 -0500 | [diff] [blame] | 326 | return xmlPrefix |
Russ Cox | bdf8bf6 | 2013-03-13 14:36:42 -0400 | [diff] [blame] | 327 | } |
Roger Peppe | 3be158d | 2015-01-10 14:00:21 +0000 | [diff] [blame] | 328 | |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 329 | // Need to define a new name space. |
| 330 | if p.attrPrefix == nil { |
| 331 | p.attrPrefix = make(map[string]string) |
| 332 | p.attrNS = make(map[string]string) |
Roger Peppe | 3be158d | 2015-01-10 14:00:21 +0000 | [diff] [blame] | 333 | } |
Russ Cox | bdf8bf6 | 2013-03-13 14:36:42 -0400 | [diff] [blame] | 334 | |
| 335 | // Pick a name. We try to use the final element of the path |
| 336 | // but fall back to _. |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 337 | prefix := strings.TrimRight(url, "/") |
Marvin Stenger | d153df8 | 2017-10-05 15:50:11 +0200 | [diff] [blame] | 338 | if i := strings.LastIndex(prefix, "/"); i >= 0 { |
Russ Cox | bdf8bf6 | 2013-03-13 14:36:42 -0400 | [diff] [blame] | 339 | prefix = prefix[i+1:] |
| 340 | } |
| 341 | if prefix == "" || !isName([]byte(prefix)) || strings.Contains(prefix, ":") { |
| 342 | prefix = "_" |
| 343 | } |
| 344 | if strings.HasPrefix(prefix, "xml") { |
| 345 | // xmlanything is reserved. |
| 346 | prefix = "_" + prefix |
| 347 | } |
| 348 | if p.attrNS[prefix] != "" { |
| 349 | // Name is taken. Find a better one. |
| 350 | for p.seq++; ; p.seq++ { |
| 351 | if id := prefix + "_" + strconv.Itoa(p.seq); p.attrNS[id] == "" { |
| 352 | prefix = id |
| 353 | break |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 358 | p.attrPrefix[url] = prefix |
| 359 | p.attrNS[prefix] = url |
| 360 | |
| 361 | p.WriteString(`xmlns:`) |
| 362 | p.WriteString(prefix) |
| 363 | p.WriteString(`="`) |
| 364 | EscapeText(p, []byte(url)) |
| 365 | p.WriteString(`" `) |
| 366 | |
| 367 | p.prefixes = append(p.prefixes, prefix) |
| 368 | |
| 369 | return prefix |
Russ Cox | bdf8bf6 | 2013-03-13 14:36:42 -0400 | [diff] [blame] | 370 | } |
| 371 | |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 372 | // deleteAttrPrefix removes an attribute name space prefix. |
| 373 | func (p *printer) deleteAttrPrefix(prefix string) { |
| 374 | delete(p.attrPrefix, p.attrNS[prefix]) |
| 375 | delete(p.attrNS, prefix) |
Gustavo Niemeyer | 0442087 | 2012-01-24 01:10:32 -0200 | [diff] [blame] | 376 | } |
| 377 | |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 378 | func (p *printer) markPrefix() { |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 379 | p.prefixes = append(p.prefixes, "") |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | func (p *printer) popPrefix() { |
| 383 | for len(p.prefixes) > 0 { |
| 384 | prefix := p.prefixes[len(p.prefixes)-1] |
| 385 | p.prefixes = p.prefixes[:len(p.prefixes)-1] |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 386 | if prefix == "" { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 387 | break |
| 388 | } |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 389 | p.deleteAttrPrefix(prefix) |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | var ( |
| 394 | marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem() |
| 395 | marshalerAttrType = reflect.TypeOf((*MarshalerAttr)(nil)).Elem() |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 396 | textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem() |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 397 | ) |
| 398 | |
Gustavo Niemeyer | aed20a6 | 2012-02-16 02:01:46 -0200 | [diff] [blame] | 399 | // marshalValue writes one or more XML elements representing val. |
| 400 | // If val was obtained from a struct field, finfo must have its details. |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 401 | func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplate *StartElement) error { |
| 402 | if startTemplate != nil && startTemplate.Name.Local == "" { |
| 403 | return fmt.Errorf("xml: EncodeElement of StartElement with missing name") |
| 404 | } |
| 405 | |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 406 | if !val.IsValid() { |
| 407 | return nil |
| 408 | } |
Gustavo Niemeyer | 0a7ad32 | 2012-02-08 01:57:44 -0200 | [diff] [blame] | 409 | if finfo != nil && finfo.flags&fOmitEmpty != 0 && isEmptyValue(val) { |
| 410 | return nil |
| 411 | } |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 412 | |
Russ Cox | 4dce7f8 | 2013-10-17 12:13:33 -0400 | [diff] [blame] | 413 | // Drill into interfaces and pointers. |
| 414 | // This can turn into an infinite loop given a cyclic chain, |
| 415 | // but it matches the Go 1 behavior. |
| 416 | for val.Kind() == reflect.Interface || val.Kind() == reflect.Ptr { |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 417 | if val.IsNil() { |
| 418 | return nil |
| 419 | } |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 420 | val = val.Elem() |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 421 | } |
| 422 | |
Russ Cox | 4dce7f8 | 2013-10-17 12:13:33 -0400 | [diff] [blame] | 423 | kind := val.Kind() |
| 424 | typ := val.Type() |
| 425 | |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 426 | // Check for marshaler. |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 427 | if val.CanInterface() && typ.Implements(marshalerType) { |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 428 | return p.marshalInterface(val.Interface().(Marshaler), defaultStart(typ, finfo, startTemplate)) |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 429 | } |
| 430 | if val.CanAddr() { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 431 | pv := val.Addr() |
| 432 | if pv.CanInterface() && pv.Type().Implements(marshalerType) { |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 433 | return p.marshalInterface(pv.Interface().(Marshaler), defaultStart(pv.Type(), finfo, startTemplate)) |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 434 | } |
| 435 | } |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 436 | |
| 437 | // Check for text marshaler. |
| 438 | if val.CanInterface() && typ.Implements(textMarshalerType) { |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 439 | return p.marshalTextInterface(val.Interface().(encoding.TextMarshaler), defaultStart(typ, finfo, startTemplate)) |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 440 | } |
| 441 | if val.CanAddr() { |
| 442 | pv := val.Addr() |
| 443 | if pv.CanInterface() && pv.Type().Implements(textMarshalerType) { |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 444 | return p.marshalTextInterface(pv.Interface().(encoding.TextMarshaler), defaultStart(pv.Type(), finfo, startTemplate)) |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 445 | } |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // Slices and arrays iterate over the elements. They do not have an enclosing tag. |
| 449 | if (kind == reflect.Slice || kind == reflect.Array) && typ.Elem().Kind() != reflect.Uint8 { |
| 450 | for i, n := 0, val.Len(); i < n; i++ { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 451 | if err := p.marshalValue(val.Index(i), finfo, startTemplate); err != nil { |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 452 | return err |
| 453 | } |
| 454 | } |
| 455 | return nil |
| 456 | } |
| 457 | |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 458 | tinfo, err := getTypeInfo(typ) |
| 459 | if err != nil { |
| 460 | return err |
| 461 | } |
| 462 | |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 463 | // Create start element. |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 464 | // Precedence for the XML element name is: |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 465 | // 0. startTemplate |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 466 | // 1. XMLName field in underlying struct; |
| 467 | // 2. field name/tag in the struct field; and |
| 468 | // 3. type name |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 469 | var start StartElement |
| 470 | |
| 471 | if startTemplate != nil { |
| 472 | start.Name = startTemplate.Name |
| 473 | start.Attr = append(start.Attr, startTemplate.Attr...) |
| 474 | } else if tinfo.xmlname != nil { |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 475 | xmlname := tinfo.xmlname |
| 476 | if xmlname.name != "" { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 477 | start.Name.Space, start.Name.Local = xmlname.xmlns, xmlname.name |
Gustavo Niemeyer | 9242a90 | 2012-05-16 23:21:31 -0300 | [diff] [blame] | 478 | } else if v, ok := xmlname.value(val).Interface().(Name); ok && v.Local != "" { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 479 | start.Name = v |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 480 | } |
| 481 | } |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 482 | if start.Name.Local == "" && finfo != nil { |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 483 | start.Name.Space, start.Name.Local = finfo.xmlns, finfo.name |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 484 | } |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 485 | if start.Name.Local == "" { |
| 486 | name := typ.Name() |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 487 | if name == "" { |
| 488 | return &UnsupportedTypeError{typ} |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 489 | } |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 490 | start.Name.Local = name |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 491 | } |
Roger Peppe | bb7e665 | 2015-06-29 12:36:48 +0100 | [diff] [blame] | 492 | |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 493 | // Attributes |
| 494 | for i := range tinfo.fields { |
| 495 | finfo := &tinfo.fields[i] |
| 496 | if finfo.flags&fAttr == 0 { |
| 497 | continue |
| 498 | } |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 499 | fv := finfo.value(val) |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 500 | |
| 501 | if finfo.flags&fOmitEmpty != 0 && isEmptyValue(fv) { |
| 502 | continue |
| 503 | } |
| 504 | |
| 505 | if fv.Kind() == reflect.Interface && fv.IsNil() { |
| 506 | continue |
| 507 | } |
| 508 | |
Russ Cox | 2427123 | 2016-10-12 22:42:40 -0400 | [diff] [blame] | 509 | name := Name{Space: finfo.xmlns, Local: finfo.name} |
| 510 | if err := p.marshalAttr(&start, name, fv); err != nil { |
Gustavo Niemeyer | 57007fe | 2012-01-23 00:50:05 -0200 | [diff] [blame] | 511 | return err |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 512 | } |
| 513 | } |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 514 | |
| 515 | if err := p.writeStart(&start); err != nil { |
| 516 | return err |
| 517 | } |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 518 | |
Gustavo Niemeyer | 57007fe | 2012-01-23 00:50:05 -0200 | [diff] [blame] | 519 | if val.Kind() == reflect.Struct { |
| 520 | err = p.marshalStruct(tinfo, val) |
| 521 | } else { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 522 | s, b, err1 := p.marshalSimple(typ, val) |
| 523 | if err1 != nil { |
| 524 | err = err1 |
| 525 | } else if b != nil { |
| 526 | EscapeText(p, b) |
| 527 | } else { |
| 528 | p.EscapeString(s) |
| 529 | } |
Gustavo Niemeyer | 57007fe | 2012-01-23 00:50:05 -0200 | [diff] [blame] | 530 | } |
| 531 | if err != nil { |
| 532 | return err |
| 533 | } |
| 534 | |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 535 | if err := p.writeEnd(start.Name); err != nil { |
| 536 | return err |
| 537 | } |
Russ Cox | 56ce83f | 2013-08-14 00:20:55 -0400 | [diff] [blame] | 538 | |
| 539 | return p.cachedWriteError() |
Russ Cox | 85f3acd | 2013-08-14 00:17:42 -0400 | [diff] [blame] | 540 | } |
| 541 | |
Russ Cox | 2427123 | 2016-10-12 22:42:40 -0400 | [diff] [blame] | 542 | // marshalAttr marshals an attribute with the given name and value, adding to start.Attr. |
| 543 | func (p *printer) marshalAttr(start *StartElement, name Name, val reflect.Value) error { |
| 544 | if val.CanInterface() && val.Type().Implements(marshalerAttrType) { |
| 545 | attr, err := val.Interface().(MarshalerAttr).MarshalXMLAttr(name) |
| 546 | if err != nil { |
| 547 | return err |
| 548 | } |
| 549 | if attr.Name.Local != "" { |
| 550 | start.Attr = append(start.Attr, attr) |
| 551 | } |
| 552 | return nil |
| 553 | } |
| 554 | |
| 555 | if val.CanAddr() { |
| 556 | pv := val.Addr() |
| 557 | if pv.CanInterface() && pv.Type().Implements(marshalerAttrType) { |
| 558 | attr, err := pv.Interface().(MarshalerAttr).MarshalXMLAttr(name) |
| 559 | if err != nil { |
| 560 | return err |
| 561 | } |
| 562 | if attr.Name.Local != "" { |
| 563 | start.Attr = append(start.Attr, attr) |
| 564 | } |
| 565 | return nil |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if val.CanInterface() && val.Type().Implements(textMarshalerType) { |
| 570 | text, err := val.Interface().(encoding.TextMarshaler).MarshalText() |
| 571 | if err != nil { |
| 572 | return err |
| 573 | } |
| 574 | start.Attr = append(start.Attr, Attr{name, string(text)}) |
| 575 | return nil |
| 576 | } |
| 577 | |
| 578 | if val.CanAddr() { |
| 579 | pv := val.Addr() |
| 580 | if pv.CanInterface() && pv.Type().Implements(textMarshalerType) { |
| 581 | text, err := pv.Interface().(encoding.TextMarshaler).MarshalText() |
| 582 | if err != nil { |
| 583 | return err |
| 584 | } |
| 585 | start.Attr = append(start.Attr, Attr{name, string(text)}) |
| 586 | return nil |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | // Dereference or skip nil pointer, interface values. |
| 591 | switch val.Kind() { |
| 592 | case reflect.Ptr, reflect.Interface: |
| 593 | if val.IsNil() { |
| 594 | return nil |
| 595 | } |
| 596 | val = val.Elem() |
| 597 | } |
| 598 | |
Russ Cox | c1a1328c | 2016-10-12 22:58:47 -0400 | [diff] [blame] | 599 | // Walk slices. |
| 600 | if val.Kind() == reflect.Slice && val.Type().Elem().Kind() != reflect.Uint8 { |
| 601 | n := val.Len() |
| 602 | for i := 0; i < n; i++ { |
| 603 | if err := p.marshalAttr(start, name, val.Index(i)); err != nil { |
| 604 | return err |
| 605 | } |
| 606 | } |
| 607 | return nil |
| 608 | } |
| 609 | |
| 610 | if val.Type() == attrType { |
| 611 | start.Attr = append(start.Attr, val.Interface().(Attr)) |
| 612 | return nil |
| 613 | } |
| 614 | |
Russ Cox | 2427123 | 2016-10-12 22:42:40 -0400 | [diff] [blame] | 615 | s, b, err := p.marshalSimple(val.Type(), val) |
| 616 | if err != nil { |
| 617 | return err |
| 618 | } |
| 619 | if b != nil { |
| 620 | s = string(b) |
| 621 | } |
| 622 | start.Attr = append(start.Attr, Attr{name, s}) |
| 623 | return nil |
| 624 | } |
| 625 | |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 626 | // defaultStart returns the default start element to use, |
| 627 | // given the reflect type, field info, and start template. |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 628 | func defaultStart(typ reflect.Type, finfo *fieldInfo, startTemplate *StartElement) StartElement { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 629 | var start StartElement |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 630 | // Precedence for the XML element name is as above, |
| 631 | // except that we do not look inside structs for the first field. |
| 632 | if startTemplate != nil { |
| 633 | start.Name = startTemplate.Name |
| 634 | start.Attr = append(start.Attr, startTemplate.Attr...) |
| 635 | } else if finfo != nil && finfo.name != "" { |
| 636 | start.Name.Local = finfo.name |
| 637 | start.Name.Space = finfo.xmlns |
| 638 | } else if typ.Name() != "" { |
| 639 | start.Name.Local = typ.Name() |
| 640 | } else { |
| 641 | // Must be a pointer to a named type, |
| 642 | // since it has the Marshaler methods. |
| 643 | start.Name.Local = typ.Elem().Name() |
| 644 | } |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 645 | return start |
| 646 | } |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 647 | |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 648 | // marshalInterface marshals a Marshaler interface value. |
| 649 | func (p *printer) marshalInterface(val Marshaler, start StartElement) error { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 650 | // Push a marker onto the tag stack so that MarshalXML |
| 651 | // cannot close the XML tags that it did not open. |
| 652 | p.tags = append(p.tags, Name{}) |
| 653 | n := len(p.tags) |
| 654 | |
| 655 | err := val.MarshalXML(p.encoder, start) |
| 656 | if err != nil { |
| 657 | return err |
| 658 | } |
| 659 | |
| 660 | // Make sure MarshalXML closed all its tags. p.tags[n-1] is the mark. |
| 661 | if len(p.tags) > n { |
| 662 | return fmt.Errorf("xml: %s.MarshalXML wrote invalid XML: <%s> not closed", receiverType(val), p.tags[len(p.tags)-1].Local) |
| 663 | } |
| 664 | p.tags = p.tags[:n-1] |
| 665 | return nil |
| 666 | } |
| 667 | |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 668 | // marshalTextInterface marshals a TextMarshaler interface value. |
| 669 | func (p *printer) marshalTextInterface(val encoding.TextMarshaler, start StartElement) error { |
| 670 | if err := p.writeStart(&start); err != nil { |
| 671 | return err |
| 672 | } |
| 673 | text, err := val.MarshalText() |
| 674 | if err != nil { |
| 675 | return err |
| 676 | } |
| 677 | EscapeText(p, text) |
| 678 | return p.writeEnd(start.Name) |
| 679 | } |
| 680 | |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 681 | // writeStart writes the given start element. |
| 682 | func (p *printer) writeStart(start *StartElement) error { |
| 683 | if start.Name.Local == "" { |
| 684 | return fmt.Errorf("xml: start tag with no name") |
| 685 | } |
| 686 | |
| 687 | p.tags = append(p.tags, start.Name) |
| 688 | p.markPrefix() |
| 689 | |
| 690 | p.writeIndent(1) |
| 691 | p.WriteByte('<') |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 692 | p.WriteString(start.Name.Local) |
| 693 | |
| 694 | if start.Name.Space != "" { |
| 695 | p.WriteString(` xmlns="`) |
| 696 | p.EscapeString(start.Name.Space) |
| 697 | p.WriteByte('"') |
| 698 | } |
| 699 | |
| 700 | // Attributes |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 701 | for _, attr := range start.Attr { |
| 702 | name := attr.Name |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 703 | if name.Local == "" { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 704 | continue |
| 705 | } |
| 706 | p.WriteByte(' ') |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 707 | if name.Space != "" { |
| 708 | p.WriteString(p.createAttrPrefix(name.Space)) |
| 709 | p.WriteByte(':') |
| 710 | } |
| 711 | p.WriteString(name.Local) |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 712 | p.WriteString(`="`) |
| 713 | p.EscapeString(attr.Value) |
| 714 | p.WriteByte('"') |
| 715 | } |
| 716 | p.WriteByte('>') |
| 717 | return nil |
| 718 | } |
| 719 | |
| 720 | func (p *printer) writeEnd(name Name) error { |
| 721 | if name.Local == "" { |
| 722 | return fmt.Errorf("xml: end tag with no name") |
| 723 | } |
| 724 | if len(p.tags) == 0 || p.tags[len(p.tags)-1].Local == "" { |
| 725 | return fmt.Errorf("xml: end tag </%s> without start tag", name.Local) |
| 726 | } |
| 727 | if top := p.tags[len(p.tags)-1]; top != name { |
| 728 | if top.Local != name.Local { |
| 729 | return fmt.Errorf("xml: end tag </%s> does not match start tag <%s>", name.Local, top.Local) |
| 730 | } |
| 731 | return fmt.Errorf("xml: end tag </%s> in namespace %s does not match start tag <%s> in namespace %s", name.Local, name.Space, top.Local, top.Space) |
| 732 | } |
| 733 | p.tags = p.tags[:len(p.tags)-1] |
| 734 | |
| 735 | p.writeIndent(-1) |
| 736 | p.WriteByte('<') |
| 737 | p.WriteByte('/') |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 738 | p.WriteString(name.Local) |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 739 | p.WriteByte('>') |
| 740 | p.popPrefix() |
| 741 | return nil |
| 742 | } |
| 743 | |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 744 | func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []byte, error) { |
Gustavo Niemeyer | 57007fe | 2012-01-23 00:50:05 -0200 | [diff] [blame] | 745 | switch val.Kind() { |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 746 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 747 | return strconv.FormatInt(val.Int(), 10), nil, nil |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 748 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 749 | return strconv.FormatUint(val.Uint(), 10), nil, nil |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 750 | case reflect.Float32, reflect.Float64: |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 751 | return strconv.FormatFloat(val.Float(), 'g', -1, val.Type().Bits()), nil, nil |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 752 | case reflect.String: |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 753 | return val.String(), nil, nil |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 754 | case reflect.Bool: |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 755 | return strconv.FormatBool(val.Bool()), nil, nil |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 756 | case reflect.Array: |
Russ Cox | 10c36fb | 2013-09-09 16:42:07 -0400 | [diff] [blame] | 757 | if typ.Elem().Kind() != reflect.Uint8 { |
| 758 | break |
| 759 | } |
| 760 | // [...]byte |
Olivier Saingre | afde71c | 2013-02-20 14:41:23 -0800 | [diff] [blame] | 761 | var bytes []byte |
| 762 | if val.CanAddr() { |
| 763 | bytes = val.Slice(0, val.Len()).Bytes() |
| 764 | } else { |
| 765 | bytes = make([]byte, val.Len()) |
| 766 | reflect.Copy(reflect.ValueOf(bytes), val) |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 767 | } |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 768 | return "", bytes, nil |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 769 | case reflect.Slice: |
Russ Cox | 10c36fb | 2013-09-09 16:42:07 -0400 | [diff] [blame] | 770 | if typ.Elem().Kind() != reflect.Uint8 { |
| 771 | break |
| 772 | } |
| 773 | // []byte |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 774 | return "", val.Bytes(), nil |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 775 | } |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 776 | return "", nil, &UnsupportedTypeError{typ} |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 777 | } |
| 778 | |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 779 | var ddBytes = []byte("--") |
| 780 | |
Russ Cox | 72aa757 | 2017-02-14 00:17:50 -0500 | [diff] [blame] | 781 | // indirect drills into interfaces and pointers, returning the pointed-at value. |
| 782 | // If it encounters a nil interface or pointer, indirect returns that nil value. |
| 783 | // This can turn into an infinite loop given a cyclic chain, |
| 784 | // but it matches the Go 1 behavior. |
| 785 | func indirect(vf reflect.Value) reflect.Value { |
| 786 | for vf.Kind() == reflect.Interface || vf.Kind() == reflect.Ptr { |
| 787 | if vf.IsNil() { |
| 788 | return vf |
| 789 | } |
| 790 | vf = vf.Elem() |
| 791 | } |
| 792 | return vf |
| 793 | } |
| 794 | |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 795 | func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 796 | s := parentStack{p: p} |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 797 | for i := range tinfo.fields { |
| 798 | finfo := &tinfo.fields[i] |
Russ Cox | bfe80e2 | 2013-03-12 16:42:25 -0400 | [diff] [blame] | 799 | if finfo.flags&fAttr != 0 { |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 800 | continue |
| 801 | } |
Gustavo Niemeyer | 9242a90 | 2012-05-16 23:21:31 -0300 | [diff] [blame] | 802 | vf := finfo.value(val) |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 803 | |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 804 | switch finfo.flags & fMode { |
Charles Weill | 3f6b91b | 2015-10-23 16:08:20 -0400 | [diff] [blame] | 805 | case fCDATA, fCharData: |
| 806 | emit := EscapeText |
| 807 | if finfo.flags&fMode == fCDATA { |
| 808 | emit = emitCDATA |
| 809 | } |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 810 | if err := s.trim(finfo.parents); err != nil { |
Hajime Hoshi | 2db587c | 2015-05-10 04:22:11 +0900 | [diff] [blame] | 811 | return err |
| 812 | } |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 813 | if vf.CanInterface() && vf.Type().Implements(textMarshalerType) { |
| 814 | data, err := vf.Interface().(encoding.TextMarshaler).MarshalText() |
| 815 | if err != nil { |
| 816 | return err |
| 817 | } |
Charles Weill | 3f6b91b | 2015-10-23 16:08:20 -0400 | [diff] [blame] | 818 | if err := emit(p, data); err != nil { |
| 819 | return err |
| 820 | } |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 821 | continue |
| 822 | } |
| 823 | if vf.CanAddr() { |
| 824 | pv := vf.Addr() |
| 825 | if pv.CanInterface() && pv.Type().Implements(textMarshalerType) { |
| 826 | data, err := pv.Interface().(encoding.TextMarshaler).MarshalText() |
| 827 | if err != nil { |
| 828 | return err |
| 829 | } |
Charles Weill | 3f6b91b | 2015-10-23 16:08:20 -0400 | [diff] [blame] | 830 | if err := emit(p, data); err != nil { |
| 831 | return err |
| 832 | } |
Russ Cox | 904e11361 | 2013-08-14 18:52:09 -0400 | [diff] [blame] | 833 | continue |
| 834 | } |
| 835 | } |
Allan Simon | daa12116 | 2015-10-11 04:16:58 +0800 | [diff] [blame] | 836 | |
Vega Garcia Luis Alfonso | 14bd52d | 2013-01-22 22:13:40 -0500 | [diff] [blame] | 837 | var scratch [64]byte |
Russ Cox | 72aa757 | 2017-02-14 00:17:50 -0500 | [diff] [blame] | 838 | vf = indirect(vf) |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 839 | switch vf.Kind() { |
Vega Garcia Luis Alfonso | 14bd52d | 2013-01-22 22:13:40 -0500 | [diff] [blame] | 840 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
Charles Weill | 3f6b91b | 2015-10-23 16:08:20 -0400 | [diff] [blame] | 841 | if err := emit(p, strconv.AppendInt(scratch[:0], vf.Int(), 10)); err != nil { |
| 842 | return err |
| 843 | } |
Vega Garcia Luis Alfonso | 14bd52d | 2013-01-22 22:13:40 -0500 | [diff] [blame] | 844 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
Charles Weill | 3f6b91b | 2015-10-23 16:08:20 -0400 | [diff] [blame] | 845 | if err := emit(p, strconv.AppendUint(scratch[:0], vf.Uint(), 10)); err != nil { |
| 846 | return err |
| 847 | } |
Vega Garcia Luis Alfonso | 14bd52d | 2013-01-22 22:13:40 -0500 | [diff] [blame] | 848 | case reflect.Float32, reflect.Float64: |
Charles Weill | 3f6b91b | 2015-10-23 16:08:20 -0400 | [diff] [blame] | 849 | if err := emit(p, strconv.AppendFloat(scratch[:0], vf.Float(), 'g', -1, vf.Type().Bits())); err != nil { |
| 850 | return err |
| 851 | } |
Vega Garcia Luis Alfonso | 14bd52d | 2013-01-22 22:13:40 -0500 | [diff] [blame] | 852 | case reflect.Bool: |
Charles Weill | 3f6b91b | 2015-10-23 16:08:20 -0400 | [diff] [blame] | 853 | if err := emit(p, strconv.AppendBool(scratch[:0], vf.Bool())); err != nil { |
| 854 | return err |
| 855 | } |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 856 | case reflect.String: |
Charles Weill | 3f6b91b | 2015-10-23 16:08:20 -0400 | [diff] [blame] | 857 | if err := emit(p, []byte(vf.String())); err != nil { |
Olivier Saingre | afde71c | 2013-02-20 14:41:23 -0800 | [diff] [blame] | 858 | return err |
| 859 | } |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 860 | case reflect.Slice: |
| 861 | if elem, ok := vf.Interface().([]byte); ok { |
Charles Weill | 3f6b91b | 2015-10-23 16:08:20 -0400 | [diff] [blame] | 862 | if err := emit(p, elem); err != nil { |
Olivier Saingre | afde71c | 2013-02-20 14:41:23 -0800 | [diff] [blame] | 863 | return err |
| 864 | } |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | continue |
| 868 | |
| 869 | case fComment: |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 870 | if err := s.trim(finfo.parents); err != nil { |
Hajime Hoshi | 2db587c | 2015-05-10 04:22:11 +0900 | [diff] [blame] | 871 | return err |
| 872 | } |
Russ Cox | 72aa757 | 2017-02-14 00:17:50 -0500 | [diff] [blame] | 873 | vf = indirect(vf) |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 874 | k := vf.Kind() |
| 875 | if !(k == reflect.String || k == reflect.Slice && vf.Type().Elem().Kind() == reflect.Uint8) { |
| 876 | return fmt.Errorf("xml: bad type for comment field of %s", val.Type()) |
| 877 | } |
| 878 | if vf.Len() == 0 { |
| 879 | continue |
| 880 | } |
Gustavo Niemeyer | aed20a6 | 2012-02-16 02:01:46 -0200 | [diff] [blame] | 881 | p.writeIndent(0) |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 882 | p.WriteString("<!--") |
| 883 | dashDash := false |
| 884 | dashLast := false |
| 885 | switch k { |
| 886 | case reflect.String: |
| 887 | s := vf.String() |
Nathan VanBenschoten | b04f3b0 | 2015-12-22 02:40:47 -0500 | [diff] [blame] | 888 | dashDash = strings.Contains(s, "--") |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 889 | dashLast = s[len(s)-1] == '-' |
| 890 | if !dashDash { |
| 891 | p.WriteString(s) |
| 892 | } |
| 893 | case reflect.Slice: |
| 894 | b := vf.Bytes() |
Dominik Honnef | 1cb3044 | 2016-04-01 03:49:43 +0200 | [diff] [blame] | 895 | dashDash = bytes.Contains(b, ddBytes) |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 896 | dashLast = b[len(b)-1] == '-' |
| 897 | if !dashDash { |
| 898 | p.Write(b) |
| 899 | } |
| 900 | default: |
| 901 | panic("can't happen") |
| 902 | } |
| 903 | if dashDash { |
| 904 | return fmt.Errorf(`xml: comments must not contain "--"`) |
| 905 | } |
| 906 | if dashLast { |
| 907 | // "--->" is invalid grammar. Make it "- -->" |
| 908 | p.WriteByte(' ') |
| 909 | } |
| 910 | p.WriteString("-->") |
| 911 | continue |
| 912 | |
| 913 | case fInnerXml: |
Russ Cox | 72aa757 | 2017-02-14 00:17:50 -0500 | [diff] [blame] | 914 | vf = indirect(vf) |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 915 | iface := vf.Interface() |
| 916 | switch raw := iface.(type) { |
| 917 | case []byte: |
| 918 | p.Write(raw) |
| 919 | continue |
| 920 | case string: |
| 921 | p.WriteString(raw) |
| 922 | continue |
| 923 | } |
| 924 | |
Chris Jones | a9121a1 | 2012-12-22 10:00:36 -0500 | [diff] [blame] | 925 | case fElement, fElement | fAny: |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 926 | if err := s.trim(finfo.parents); err != nil { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 927 | return err |
| 928 | } |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 929 | if len(finfo.parents) > len(s.stack) { |
| 930 | if vf.Kind() != reflect.Ptr && vf.Kind() != reflect.Interface || !vf.IsNil() { |
| 931 | if err := s.push(finfo.parents[len(s.stack):]); err != nil { |
| 932 | return err |
| 933 | } |
| 934 | } |
| 935 | } |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 936 | } |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 937 | if err := p.marshalValue(vf, finfo, nil); err != nil { |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 938 | return err |
| 939 | } |
| 940 | } |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 941 | s.trim(nil) |
Jan Ziak | 32a0cbb | 2012-06-25 16:00:35 -0400 | [diff] [blame] | 942 | return p.cachedWriteError() |
| 943 | } |
| 944 | |
| 945 | // return the bufio Writer's cached write error |
| 946 | func (p *printer) cachedWriteError() error { |
| 947 | _, err := p.Write(nil) |
| 948 | return err |
Gustavo Niemeyer | 1627b46 | 2012-01-13 11:05:19 +0100 | [diff] [blame] | 949 | } |
| 950 | |
Gustavo Niemeyer | aed20a6 | 2012-02-16 02:01:46 -0200 | [diff] [blame] | 951 | func (p *printer) writeIndent(depthDelta int) { |
| 952 | if len(p.prefix) == 0 && len(p.indent) == 0 { |
| 953 | return |
| 954 | } |
| 955 | if depthDelta < 0 { |
| 956 | p.depth-- |
| 957 | if p.indentedIn { |
| 958 | p.indentedIn = false |
| 959 | return |
| 960 | } |
| 961 | p.indentedIn = false |
| 962 | } |
Shivakumar GN | 848d10f | 2013-02-03 11:21:07 -0500 | [diff] [blame] | 963 | if p.putNewline { |
| 964 | p.WriteByte('\n') |
| 965 | } else { |
| 966 | p.putNewline = true |
| 967 | } |
Gustavo Niemeyer | aed20a6 | 2012-02-16 02:01:46 -0200 | [diff] [blame] | 968 | if len(p.prefix) > 0 { |
| 969 | p.WriteString(p.prefix) |
| 970 | } |
| 971 | if len(p.indent) > 0 { |
| 972 | for i := 0; i < p.depth; i++ { |
| 973 | p.WriteString(p.indent) |
| 974 | } |
| 975 | } |
| 976 | if depthDelta > 0 { |
| 977 | p.depth++ |
| 978 | p.indentedIn = true |
| 979 | } |
| 980 | } |
| 981 | |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 982 | type parentStack struct { |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 983 | p *printer |
| 984 | stack []string |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 985 | } |
| 986 | |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 987 | // trim updates the XML context to match the longest common prefix of the stack |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 988 | // and the given parents. A closing tag will be written for every parent |
| 989 | // popped. Passing a zero slice or nil will close all the elements. |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 990 | func (s *parentStack) trim(parents []string) error { |
| 991 | split := 0 |
| 992 | for ; split < len(parents) && split < len(s.stack); split++ { |
| 993 | if parents[split] != s.stack[split] { |
| 994 | break |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 995 | } |
| 996 | } |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 997 | for i := len(s.stack) - 1; i >= split; i-- { |
| 998 | if err := s.p.writeEnd(Name{Local: s.stack[i]}); err != nil { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 999 | return err |
| 1000 | } |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 1001 | } |
Russ Cox | 765cea2 | 2015-07-27 13:52:04 -0400 | [diff] [blame] | 1002 | s.stack = s.stack[:split] |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 1003 | return nil |
| 1004 | } |
| 1005 | |
| 1006 | // push adds parent elements to the stack and writes open tags. |
| 1007 | func (s *parentStack) push(parents []string) error { |
| 1008 | for i := 0; i < len(parents); i++ { |
| 1009 | if err := s.p.writeStart(&StartElement{Name: Name{Local: parents[i]}}); err != nil { |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 1010 | return err |
| 1011 | } |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 1012 | } |
Russ Cox | c0d6d33 | 2015-07-23 10:28:27 -0400 | [diff] [blame] | 1013 | s.stack = append(s.stack, parents...) |
Russ Cox | 54bdfc0 | 2013-08-14 14:58:28 -0400 | [diff] [blame] | 1014 | return nil |
Ross Light | 4541fa9 | 2011-08-26 12:29:52 -0300 | [diff] [blame] | 1015 | } |
| 1016 | |
Karel Pazdera | 6e9e9df | 2017-08-24 00:36:28 +0200 | [diff] [blame] | 1017 | // UnsupportedTypeError is returned when Marshal encounters a type |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 1018 | // that cannot be converted into XML. |
| 1019 | type UnsupportedTypeError struct { |
| 1020 | Type reflect.Type |
| 1021 | } |
| 1022 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 1023 | func (e *UnsupportedTypeError) Error() string { |
Kyle Lemons | abd50de | 2011-06-27 19:07:28 -0400 | [diff] [blame] | 1024 | return "xml: unsupported type: " + e.Type.String() |
| 1025 | } |
Gustavo Niemeyer | 0a7ad32 | 2012-02-08 01:57:44 -0200 | [diff] [blame] | 1026 | |
| 1027 | func isEmptyValue(v reflect.Value) bool { |
| 1028 | switch v.Kind() { |
| 1029 | case reflect.Array, reflect.Map, reflect.Slice, reflect.String: |
| 1030 | return v.Len() == 0 |
| 1031 | case reflect.Bool: |
| 1032 | return !v.Bool() |
| 1033 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 1034 | return v.Int() == 0 |
| 1035 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 1036 | return v.Uint() == 0 |
| 1037 | case reflect.Float32, reflect.Float64: |
| 1038 | return v.Float() == 0 |
| 1039 | case reflect.Interface, reflect.Ptr: |
| 1040 | return v.IsNil() |
| 1041 | } |
| 1042 | return false |
| 1043 | } |