Debian Codegraph @

src/encoding/xml/marshal.go

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