github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/encoding/xml/xml.go (about) 1 // Copyright 2009 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 implements a simple XML 1.0 parser that 6 // understands XML name spaces. 7 package xml 8 9 // References: 10 // Annotated XML spec: http://www.xml.com/axml/testaxml.htm 11 // XML name spaces: http://www.w3.org/TR/REC-xml-names/ 12 13 // TODO(rsc): 14 // Test error handling. 15 16 import ( 17 "bufio" 18 "bytes" 19 "errors" 20 "fmt" 21 "io" 22 "strconv" 23 "strings" 24 "unicode" 25 "unicode/utf8" 26 ) 27 28 // A SyntaxError represents a syntax error in the XML input stream. 29 type SyntaxError struct { 30 Msg string 31 Line int 32 } 33 34 func (e *SyntaxError) Error() string { 35 return "XML syntax error on line " + strconv.Itoa(e.Line) + ": " + e.Msg 36 } 37 38 // A Name represents an XML name (Local) annotated 39 // with a name space identifier (Space). 40 // In tokens returned by Decoder.Token, the Space identifier 41 // is given as a canonical URL, not the short prefix used 42 // in the document being parsed. 43 type Name struct { 44 Space, Local string 45 } 46 47 // An Attr represents an attribute in an XML element (Name=Value). 48 type Attr struct { 49 Name Name 50 Value string 51 } 52 53 // A Token is an interface holding one of the token types: 54 // StartElement, EndElement, CharData, Comment, ProcInst, or Directive. 55 type Token interface{} 56 57 // A StartElement represents an XML start element. 58 type StartElement struct { 59 Name Name 60 Attr []Attr 61 } 62 63 func (e StartElement) Copy() StartElement { 64 attrs := make([]Attr, len(e.Attr)) 65 copy(attrs, e.Attr) 66 e.Attr = attrs 67 return e 68 } 69 70 // End returns the corresponding XML end element. 71 func (e StartElement) End() EndElement { 72 return EndElement{e.Name} 73 } 74 75 // An EndElement represents an XML end element. 76 type EndElement struct { 77 Name Name 78 } 79 80 // A CharData represents XML character data (raw text), 81 // in which XML escape sequences have been replaced by 82 // the characters they represent. 83 type CharData []byte 84 85 func makeCopy(b []byte) []byte { 86 b1 := make([]byte, len(b)) 87 copy(b1, b) 88 return b1 89 } 90 91 func (c CharData) Copy() CharData { return CharData(makeCopy(c)) } 92 93 // A Comment represents an XML comment of the form <!--comment-->. 94 // The bytes do not include the <!-- and --> comment markers. 95 type Comment []byte 96 97 func (c Comment) Copy() Comment { return Comment(makeCopy(c)) } 98 99 // A ProcInst represents an XML processing instruction of the form <?target inst?> 100 type ProcInst struct { 101 Target string 102 Inst []byte 103 } 104 105 func (p ProcInst) Copy() ProcInst { 106 p.Inst = makeCopy(p.Inst) 107 return p 108 } 109 110 // A Directive represents an XML directive of the form <!text>. 111 // The bytes do not include the <! and > markers. 112 type Directive []byte 113 114 func (d Directive) Copy() Directive { return Directive(makeCopy(d)) } 115 116 // CopyToken returns a copy of a Token. 117 func CopyToken(t Token) Token { 118 switch v := t.(type) { 119 case CharData: 120 return v.Copy() 121 case Comment: 122 return v.Copy() 123 case Directive: 124 return v.Copy() 125 case ProcInst: 126 return v.Copy() 127 case StartElement: 128 return v.Copy() 129 } 130 return t 131 } 132 133 // A Decoder represents an XML parser reading a particular input stream. 134 // The parser assumes that its input is encoded in UTF-8. 135 type Decoder struct { 136 // Strict defaults to true, enforcing the requirements 137 // of the XML specification. 138 // If set to false, the parser allows input containing common 139 // mistakes: 140 // * If an element is missing an end tag, the parser invents 141 // end tags as necessary to keep the return values from Token 142 // properly balanced. 143 // * In attribute values and character data, unknown or malformed 144 // character entities (sequences beginning with &) are left alone. 145 // 146 // Setting: 147 // 148 // d.Strict = false; 149 // d.AutoClose = HTMLAutoClose; 150 // d.Entity = HTMLEntity 151 // 152 // creates a parser that can handle typical HTML. 153 // 154 // Strict mode does not enforce the requirements of the XML name spaces TR. 155 // In particular it does not reject name space tags using undefined prefixes. 156 // Such tags are recorded with the unknown prefix as the name space URL. 157 Strict bool 158 159 // When Strict == false, AutoClose indicates a set of elements to 160 // consider closed immediately after they are opened, regardless 161 // of whether an end element is present. 162 AutoClose []string 163 164 // Entity can be used to map non-standard entity names to string replacements. 165 // The parser behaves as if these standard mappings are present in the map, 166 // regardless of the actual map content: 167 // 168 // "lt": "<", 169 // "gt": ">", 170 // "amp": "&", 171 // "apos": "'", 172 // "quot": `"`, 173 Entity map[string]string 174 175 // CharsetReader, if non-nil, defines a function to generate 176 // charset-conversion readers, converting from the provided 177 // non-UTF-8 charset into UTF-8. If CharsetReader is nil or 178 // returns an error, parsing stops with an error. One of the 179 // the CharsetReader's result values must be non-nil. 180 CharsetReader func(charset string, input io.Reader) (io.Reader, error) 181 182 // DefaultSpace sets the default name space used for unadorned tags, 183 // as if the entire XML stream were wrapped in an element containing 184 // the attribute xmlns="DefaultSpace". 185 DefaultSpace string 186 187 r io.ByteReader 188 buf bytes.Buffer 189 saved *bytes.Buffer 190 stk *stack 191 free *stack 192 needClose bool 193 toClose Name 194 nextToken Token 195 nextByte int 196 ns map[string]string 197 err error 198 line int 199 unmarshalDepth int 200 } 201 202 // NewDecoder creates a new XML parser reading from r. 203 func NewDecoder(r io.Reader) *Decoder { 204 d := &Decoder{ 205 ns: make(map[string]string), 206 nextByte: -1, 207 line: 1, 208 Strict: true, 209 } 210 d.switchToReader(r) 211 return d 212 } 213 214 // Token returns the next XML token in the input stream. 215 // At the end of the input stream, Token returns nil, io.EOF. 216 // 217 // Slices of bytes in the returned token data refer to the 218 // parser's internal buffer and remain valid only until the next 219 // call to Token. To acquire a copy of the bytes, call CopyToken 220 // or the token's Copy method. 221 // 222 // Token expands self-closing elements such as <br/> 223 // into separate start and end elements returned by successive calls. 224 // 225 // Token guarantees that the StartElement and EndElement 226 // tokens it returns are properly nested and matched: 227 // if Token encounters an unexpected end element, 228 // it will return an error. 229 // 230 // Token implements XML name spaces as described by 231 // http://www.w3.org/TR/REC-xml-names/. Each of the 232 // Name structures contained in the Token has the Space 233 // set to the URL identifying its name space when known. 234 // If Token encounters an unrecognized name space prefix, 235 // it uses the prefix as the Space rather than report an error. 236 func (d *Decoder) Token() (t Token, err error) { 237 if d.stk != nil && d.stk.kind == stkEOF { 238 err = io.EOF 239 return 240 } 241 if d.nextToken != nil { 242 t = d.nextToken 243 d.nextToken = nil 244 } else if t, err = d.rawToken(); err != nil { 245 return 246 } 247 248 if !d.Strict { 249 if t1, ok := d.autoClose(t); ok { 250 d.nextToken = t 251 t = t1 252 } 253 } 254 switch t1 := t.(type) { 255 case StartElement: 256 // In XML name spaces, the translations listed in the 257 // attributes apply to the element name and 258 // to the other attribute names, so process 259 // the translations first. 260 for _, a := range t1.Attr { 261 if a.Name.Space == "xmlns" { 262 v, ok := d.ns[a.Name.Local] 263 d.pushNs(a.Name.Local, v, ok) 264 d.ns[a.Name.Local] = a.Value 265 } 266 if a.Name.Space == "" && a.Name.Local == "xmlns" { 267 // Default space for untagged names 268 v, ok := d.ns[""] 269 d.pushNs("", v, ok) 270 d.ns[""] = a.Value 271 } 272 } 273 274 d.translate(&t1.Name, true) 275 for i := range t1.Attr { 276 d.translate(&t1.Attr[i].Name, false) 277 } 278 d.pushElement(t1.Name) 279 t = t1 280 281 case EndElement: 282 d.translate(&t1.Name, true) 283 if !d.popElement(&t1) { 284 return nil, d.err 285 } 286 t = t1 287 } 288 return 289 } 290 291 const xmlURL = "http://www.w3.org/XML/1998/namespace" 292 293 // Apply name space translation to name n. 294 // The default name space (for Space=="") 295 // applies only to element names, not to attribute names. 296 func (d *Decoder) translate(n *Name, isElementName bool) { 297 switch { 298 case n.Space == "xmlns": 299 return 300 case n.Space == "" && !isElementName: 301 return 302 case n.Space == "xml": 303 n.Space = xmlURL 304 case n.Space == "" && n.Local == "xmlns": 305 return 306 } 307 if v, ok := d.ns[n.Space]; ok { 308 n.Space = v 309 } else if n.Space == "" { 310 n.Space = d.DefaultSpace 311 } 312 } 313 314 func (d *Decoder) switchToReader(r io.Reader) { 315 // Get efficient byte at a time reader. 316 // Assume that if reader has its own 317 // ReadByte, it's efficient enough. 318 // Otherwise, use bufio. 319 if rb, ok := r.(io.ByteReader); ok { 320 d.r = rb 321 } else { 322 d.r = bufio.NewReader(r) 323 } 324 } 325 326 // Parsing state - stack holds old name space translations 327 // and the current set of open elements. The translations to pop when 328 // ending a given tag are *below* it on the stack, which is 329 // more work but forced on us by XML. 330 type stack struct { 331 next *stack 332 kind int 333 name Name 334 ok bool 335 } 336 337 const ( 338 stkStart = iota 339 stkNs 340 stkEOF 341 ) 342 343 func (d *Decoder) push(kind int) *stack { 344 s := d.free 345 if s != nil { 346 d.free = s.next 347 } else { 348 s = new(stack) 349 } 350 s.next = d.stk 351 s.kind = kind 352 d.stk = s 353 return s 354 } 355 356 func (d *Decoder) pop() *stack { 357 s := d.stk 358 if s != nil { 359 d.stk = s.next 360 s.next = d.free 361 d.free = s 362 } 363 return s 364 } 365 366 // Record that after the current element is finished 367 // (that element is already pushed on the stack) 368 // Token should return EOF until popEOF is called. 369 func (d *Decoder) pushEOF() { 370 // Walk down stack to find Start. 371 // It might not be the top, because there might be stkNs 372 // entries above it. 373 start := d.stk 374 for start.kind != stkStart { 375 start = start.next 376 } 377 // The stkNs entries below a start are associated with that 378 // element too; skip over them. 379 for start.next != nil && start.next.kind == stkNs { 380 start = start.next 381 } 382 s := d.free 383 if s != nil { 384 d.free = s.next 385 } else { 386 s = new(stack) 387 } 388 s.kind = stkEOF 389 s.next = start.next 390 start.next = s 391 } 392 393 // Undo a pushEOF. 394 // The element must have been finished, so the EOF should be at the top of the stack. 395 func (d *Decoder) popEOF() bool { 396 if d.stk == nil || d.stk.kind != stkEOF { 397 return false 398 } 399 d.pop() 400 return true 401 } 402 403 // Record that we are starting an element with the given name. 404 func (d *Decoder) pushElement(name Name) { 405 s := d.push(stkStart) 406 s.name = name 407 } 408 409 // Record that we are changing the value of ns[local]. 410 // The old value is url, ok. 411 func (d *Decoder) pushNs(local string, url string, ok bool) { 412 s := d.push(stkNs) 413 s.name.Local = local 414 s.name.Space = url 415 s.ok = ok 416 } 417 418 // Creates a SyntaxError with the current line number. 419 func (d *Decoder) syntaxError(msg string) error { 420 return &SyntaxError{Msg: msg, Line: d.line} 421 } 422 423 // Record that we are ending an element with the given name. 424 // The name must match the record at the top of the stack, 425 // which must be a pushElement record. 426 // After popping the element, apply any undo records from 427 // the stack to restore the name translations that existed 428 // before we saw this element. 429 func (d *Decoder) popElement(t *EndElement) bool { 430 s := d.pop() 431 name := t.Name 432 switch { 433 case s == nil || s.kind != stkStart: 434 d.err = d.syntaxError("unexpected end element </" + name.Local + ">") 435 return false 436 case s.name.Local != name.Local: 437 if !d.Strict { 438 d.needClose = true 439 d.toClose = t.Name 440 t.Name = s.name 441 return true 442 } 443 d.err = d.syntaxError("element <" + s.name.Local + "> closed by </" + name.Local + ">") 444 return false 445 case s.name.Space != name.Space: 446 d.err = d.syntaxError("element <" + s.name.Local + "> in space " + s.name.Space + 447 "closed by </" + name.Local + "> in space " + name.Space) 448 return false 449 } 450 451 // Pop stack until a Start or EOF is on the top, undoing the 452 // translations that were associated with the element we just closed. 453 for d.stk != nil && d.stk.kind != stkStart && d.stk.kind != stkEOF { 454 s := d.pop() 455 if s.ok { 456 d.ns[s.name.Local] = s.name.Space 457 } else { 458 delete(d.ns, s.name.Local) 459 } 460 } 461 462 return true 463 } 464 465 // If the top element on the stack is autoclosing and 466 // t is not the end tag, invent the end tag. 467 func (d *Decoder) autoClose(t Token) (Token, bool) { 468 if d.stk == nil || d.stk.kind != stkStart { 469 return nil, false 470 } 471 name := strings.ToLower(d.stk.name.Local) 472 for _, s := range d.AutoClose { 473 if strings.ToLower(s) == name { 474 // This one should be auto closed if t doesn't close it. 475 et, ok := t.(EndElement) 476 if !ok || et.Name.Local != name { 477 return EndElement{d.stk.name}, true 478 } 479 break 480 } 481 } 482 return nil, false 483 } 484 485 var errRawToken = errors.New("xml: cannot use RawToken from UnmarshalXML method") 486 487 // RawToken is like Token but does not verify that 488 // start and end elements match and does not translate 489 // name space prefixes to their corresponding URLs. 490 func (d *Decoder) RawToken() (Token, error) { 491 if d.unmarshalDepth > 0 { 492 return nil, errRawToken 493 } 494 return d.rawToken() 495 } 496 497 func (d *Decoder) rawToken() (Token, error) { 498 if d.err != nil { 499 return nil, d.err 500 } 501 if d.needClose { 502 // The last element we read was self-closing and 503 // we returned just the StartElement half. 504 // Return the EndElement half now. 505 d.needClose = false 506 return EndElement{d.toClose}, nil 507 } 508 509 b, ok := d.getc() 510 if !ok { 511 return nil, d.err 512 } 513 514 if b != '<' { 515 // Text section. 516 d.ungetc(b) 517 data := d.text(-1, false) 518 if data == nil { 519 return nil, d.err 520 } 521 return CharData(data), nil 522 } 523 524 if b, ok = d.mustgetc(); !ok { 525 return nil, d.err 526 } 527 switch b { 528 case '/': 529 // </: End element 530 var name Name 531 if name, ok = d.nsname(); !ok { 532 if d.err == nil { 533 d.err = d.syntaxError("expected element name after </") 534 } 535 return nil, d.err 536 } 537 d.space() 538 if b, ok = d.mustgetc(); !ok { 539 return nil, d.err 540 } 541 if b != '>' { 542 d.err = d.syntaxError("invalid characters between </" + name.Local + " and >") 543 return nil, d.err 544 } 545 return EndElement{name}, nil 546 547 case '?': 548 // <?: Processing instruction. 549 // TODO(rsc): Should parse the <?xml declaration to make sure the version is 1.0. 550 var target string 551 if target, ok = d.name(); !ok { 552 if d.err == nil { 553 d.err = d.syntaxError("expected target name after <?") 554 } 555 return nil, d.err 556 } 557 d.space() 558 d.buf.Reset() 559 var b0 byte 560 for { 561 if b, ok = d.mustgetc(); !ok { 562 return nil, d.err 563 } 564 d.buf.WriteByte(b) 565 if b0 == '?' && b == '>' { 566 break 567 } 568 b0 = b 569 } 570 data := d.buf.Bytes() 571 data = data[0 : len(data)-2] // chop ?> 572 573 if target == "xml" { 574 enc := procInstEncoding(string(data)) 575 if enc != "" && enc != "utf-8" && enc != "UTF-8" { 576 if d.CharsetReader == nil { 577 d.err = fmt.Errorf("xml: encoding %q declared but Decoder.CharsetReader is nil", enc) 578 return nil, d.err 579 } 580 newr, err := d.CharsetReader(enc, d.r.(io.Reader)) 581 if err != nil { 582 d.err = fmt.Errorf("xml: opening charset %q: %v", enc, err) 583 return nil, d.err 584 } 585 if newr == nil { 586 panic("CharsetReader returned a nil Reader for charset " + enc) 587 } 588 d.switchToReader(newr) 589 } 590 } 591 return ProcInst{target, data}, nil 592 593 case '!': 594 // <!: Maybe comment, maybe CDATA. 595 if b, ok = d.mustgetc(); !ok { 596 return nil, d.err 597 } 598 switch b { 599 case '-': // <!- 600 // Probably <!-- for a comment. 601 if b, ok = d.mustgetc(); !ok { 602 return nil, d.err 603 } 604 if b != '-' { 605 d.err = d.syntaxError("invalid sequence <!- not part of <!--") 606 return nil, d.err 607 } 608 // Look for terminator. 609 d.buf.Reset() 610 var b0, b1 byte 611 for { 612 if b, ok = d.mustgetc(); !ok { 613 return nil, d.err 614 } 615 d.buf.WriteByte(b) 616 if b0 == '-' && b1 == '-' && b == '>' { 617 break 618 } 619 b0, b1 = b1, b 620 } 621 data := d.buf.Bytes() 622 data = data[0 : len(data)-3] // chop --> 623 return Comment(data), nil 624 625 case '[': // <![ 626 // Probably <![CDATA[. 627 for i := 0; i < 6; i++ { 628 if b, ok = d.mustgetc(); !ok { 629 return nil, d.err 630 } 631 if b != "CDATA["[i] { 632 d.err = d.syntaxError("invalid <![ sequence") 633 return nil, d.err 634 } 635 } 636 // Have <![CDATA[. Read text until ]]>. 637 data := d.text(-1, true) 638 if data == nil { 639 return nil, d.err 640 } 641 return CharData(data), nil 642 } 643 644 // Probably a directive: <!DOCTYPE ...>, <!ENTITY ...>, etc. 645 // We don't care, but accumulate for caller. Quoted angle 646 // brackets do not count for nesting. 647 d.buf.Reset() 648 d.buf.WriteByte(b) 649 inquote := uint8(0) 650 depth := 0 651 for { 652 if b, ok = d.mustgetc(); !ok { 653 return nil, d.err 654 } 655 if inquote == 0 && b == '>' && depth == 0 { 656 break 657 } 658 HandleB: 659 d.buf.WriteByte(b) 660 switch { 661 case b == inquote: 662 inquote = 0 663 664 case inquote != 0: 665 // in quotes, no special action 666 667 case b == '\'' || b == '"': 668 inquote = b 669 670 case b == '>' && inquote == 0: 671 depth-- 672 673 case b == '<' && inquote == 0: 674 // Look for <!-- to begin comment. 675 s := "!--" 676 for i := 0; i < len(s); i++ { 677 if b, ok = d.mustgetc(); !ok { 678 return nil, d.err 679 } 680 if b != s[i] { 681 for j := 0; j < i; j++ { 682 d.buf.WriteByte(s[j]) 683 } 684 depth++ 685 goto HandleB 686 } 687 } 688 689 // Remove < that was written above. 690 d.buf.Truncate(d.buf.Len() - 1) 691 692 // Look for terminator. 693 var b0, b1 byte 694 for { 695 if b, ok = d.mustgetc(); !ok { 696 return nil, d.err 697 } 698 if b0 == '-' && b1 == '-' && b == '>' { 699 break 700 } 701 b0, b1 = b1, b 702 } 703 } 704 } 705 return Directive(d.buf.Bytes()), nil 706 } 707 708 // Must be an open element like <a href="foo"> 709 d.ungetc(b) 710 711 var ( 712 name Name 713 empty bool 714 attr []Attr 715 ) 716 if name, ok = d.nsname(); !ok { 717 if d.err == nil { 718 d.err = d.syntaxError("expected element name after <") 719 } 720 return nil, d.err 721 } 722 723 attr = make([]Attr, 0, 4) 724 for { 725 d.space() 726 if b, ok = d.mustgetc(); !ok { 727 return nil, d.err 728 } 729 if b == '/' { 730 empty = true 731 if b, ok = d.mustgetc(); !ok { 732 return nil, d.err 733 } 734 if b != '>' { 735 d.err = d.syntaxError("expected /> in element") 736 return nil, d.err 737 } 738 break 739 } 740 if b == '>' { 741 break 742 } 743 d.ungetc(b) 744 745 n := len(attr) 746 if n >= cap(attr) { 747 nattr := make([]Attr, n, 2*cap(attr)) 748 copy(nattr, attr) 749 attr = nattr 750 } 751 attr = attr[0 : n+1] 752 a := &attr[n] 753 if a.Name, ok = d.nsname(); !ok { 754 if d.err == nil { 755 d.err = d.syntaxError("expected attribute name in element") 756 } 757 return nil, d.err 758 } 759 d.space() 760 if b, ok = d.mustgetc(); !ok { 761 return nil, d.err 762 } 763 if b != '=' { 764 if d.Strict { 765 d.err = d.syntaxError("attribute name without = in element") 766 return nil, d.err 767 } else { 768 d.ungetc(b) 769 a.Value = a.Name.Local 770 } 771 } else { 772 d.space() 773 data := d.attrval() 774 if data == nil { 775 return nil, d.err 776 } 777 a.Value = string(data) 778 } 779 } 780 if empty { 781 d.needClose = true 782 d.toClose = name 783 } 784 return StartElement{name, attr}, nil 785 } 786 787 func (d *Decoder) attrval() []byte { 788 b, ok := d.mustgetc() 789 if !ok { 790 return nil 791 } 792 // Handle quoted attribute values 793 if b == '"' || b == '\'' { 794 return d.text(int(b), false) 795 } 796 // Handle unquoted attribute values for strict parsers 797 if d.Strict { 798 d.err = d.syntaxError("unquoted or missing attribute value in element") 799 return nil 800 } 801 // Handle unquoted attribute values for unstrict parsers 802 d.ungetc(b) 803 d.buf.Reset() 804 for { 805 b, ok = d.mustgetc() 806 if !ok { 807 return nil 808 } 809 // http://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.2 810 if 'a' <= b && b <= 'z' || 'A' <= b && b <= 'Z' || 811 '0' <= b && b <= '9' || b == '_' || b == ':' || b == '-' { 812 d.buf.WriteByte(b) 813 } else { 814 d.ungetc(b) 815 break 816 } 817 } 818 return d.buf.Bytes() 819 } 820 821 // Skip spaces if any 822 func (d *Decoder) space() { 823 for { 824 b, ok := d.getc() 825 if !ok { 826 return 827 } 828 switch b { 829 case ' ', '\r', '\n', '\t': 830 default: 831 d.ungetc(b) 832 return 833 } 834 } 835 } 836 837 // Read a single byte. 838 // If there is no byte to read, return ok==false 839 // and leave the error in d.err. 840 // Maintain line number. 841 func (d *Decoder) getc() (b byte, ok bool) { 842 if d.err != nil { 843 return 0, false 844 } 845 if d.nextByte >= 0 { 846 b = byte(d.nextByte) 847 d.nextByte = -1 848 } else { 849 b, d.err = d.r.ReadByte() 850 if d.err != nil { 851 return 0, false 852 } 853 if d.saved != nil { 854 d.saved.WriteByte(b) 855 } 856 } 857 if b == '\n' { 858 d.line++ 859 } 860 return b, true 861 } 862 863 // Return saved offset. 864 // If we did ungetc (nextByte >= 0), have to back up one. 865 func (d *Decoder) savedOffset() int { 866 n := d.saved.Len() 867 if d.nextByte >= 0 { 868 n-- 869 } 870 return n 871 } 872 873 // Must read a single byte. 874 // If there is no byte to read, 875 // set d.err to SyntaxError("unexpected EOF") 876 // and return ok==false 877 func (d *Decoder) mustgetc() (b byte, ok bool) { 878 if b, ok = d.getc(); !ok { 879 if d.err == io.EOF { 880 d.err = d.syntaxError("unexpected EOF") 881 } 882 } 883 return 884 } 885 886 // Unread a single byte. 887 func (d *Decoder) ungetc(b byte) { 888 if b == '\n' { 889 d.line-- 890 } 891 d.nextByte = int(b) 892 } 893 894 var entity = map[string]int{ 895 "lt": '<', 896 "gt": '>', 897 "amp": '&', 898 "apos": '\'', 899 "quot": '"', 900 } 901 902 // Read plain text section (XML calls it character data). 903 // If quote >= 0, we are in a quoted string and need to find the matching quote. 904 // If cdata == true, we are in a <![CDATA[ section and need to find ]]>. 905 // On failure return nil and leave the error in d.err. 906 func (d *Decoder) text(quote int, cdata bool) []byte { 907 var b0, b1 byte 908 var trunc int 909 d.buf.Reset() 910 Input: 911 for { 912 b, ok := d.getc() 913 if !ok { 914 if cdata { 915 if d.err == io.EOF { 916 d.err = d.syntaxError("unexpected EOF in CDATA section") 917 } 918 return nil 919 } 920 break Input 921 } 922 923 // <![CDATA[ section ends with ]]>. 924 // It is an error for ]]> to appear in ordinary text. 925 if b0 == ']' && b1 == ']' && b == '>' { 926 if cdata { 927 trunc = 2 928 break Input 929 } 930 d.err = d.syntaxError("unescaped ]]> not in CDATA section") 931 return nil 932 } 933 934 // Stop reading text if we see a <. 935 if b == '<' && !cdata { 936 if quote >= 0 { 937 d.err = d.syntaxError("unescaped < inside quoted string") 938 return nil 939 } 940 d.ungetc('<') 941 break Input 942 } 943 if quote >= 0 && b == byte(quote) { 944 break Input 945 } 946 if b == '&' && !cdata { 947 // Read escaped character expression up to semicolon. 948 // XML in all its glory allows a document to define and use 949 // its own character names with <!ENTITY ...> directives. 950 // Parsers are required to recognize lt, gt, amp, apos, and quot 951 // even if they have not been declared. 952 before := d.buf.Len() 953 d.buf.WriteByte('&') 954 var ok bool 955 var text string 956 var haveText bool 957 if b, ok = d.mustgetc(); !ok { 958 return nil 959 } 960 if b == '#' { 961 d.buf.WriteByte(b) 962 if b, ok = d.mustgetc(); !ok { 963 return nil 964 } 965 base := 10 966 if b == 'x' { 967 base = 16 968 d.buf.WriteByte(b) 969 if b, ok = d.mustgetc(); !ok { 970 return nil 971 } 972 } 973 start := d.buf.Len() 974 for '0' <= b && b <= '9' || 975 base == 16 && 'a' <= b && b <= 'f' || 976 base == 16 && 'A' <= b && b <= 'F' { 977 d.buf.WriteByte(b) 978 if b, ok = d.mustgetc(); !ok { 979 return nil 980 } 981 } 982 if b != ';' { 983 d.ungetc(b) 984 } else { 985 s := string(d.buf.Bytes()[start:]) 986 d.buf.WriteByte(';') 987 n, err := strconv.ParseUint(s, base, 64) 988 if err == nil && n <= unicode.MaxRune { 989 text = string(n) 990 haveText = true 991 } 992 } 993 } else { 994 d.ungetc(b) 995 if !d.readName() { 996 if d.err != nil { 997 return nil 998 } 999 ok = false 1000 } 1001 if b, ok = d.mustgetc(); !ok { 1002 return nil 1003 } 1004 if b != ';' { 1005 d.ungetc(b) 1006 } else { 1007 name := d.buf.Bytes()[before+1:] 1008 d.buf.WriteByte(';') 1009 if isName(name) { 1010 s := string(name) 1011 if r, ok := entity[s]; ok { 1012 text = string(r) 1013 haveText = true 1014 } else if d.Entity != nil { 1015 text, haveText = d.Entity[s] 1016 } 1017 } 1018 } 1019 } 1020 1021 if haveText { 1022 d.buf.Truncate(before) 1023 d.buf.Write([]byte(text)) 1024 b0, b1 = 0, 0 1025 continue Input 1026 } 1027 if !d.Strict { 1028 b0, b1 = 0, 0 1029 continue Input 1030 } 1031 ent := string(d.buf.Bytes()[before:]) 1032 if ent[len(ent)-1] != ';' { 1033 ent += " (no semicolon)" 1034 } 1035 d.err = d.syntaxError("invalid character entity " + ent) 1036 return nil 1037 } 1038 1039 // We must rewrite unescaped \r and \r\n into \n. 1040 if b == '\r' { 1041 d.buf.WriteByte('\n') 1042 } else if b1 == '\r' && b == '\n' { 1043 // Skip \r\n--we already wrote \n. 1044 } else { 1045 d.buf.WriteByte(b) 1046 } 1047 1048 b0, b1 = b1, b 1049 } 1050 data := d.buf.Bytes() 1051 data = data[0 : len(data)-trunc] 1052 1053 // Inspect each rune for being a disallowed character. 1054 buf := data 1055 for len(buf) > 0 { 1056 r, size := utf8.DecodeRune(buf) 1057 if r == utf8.RuneError && size == 1 { 1058 d.err = d.syntaxError("invalid UTF-8") 1059 return nil 1060 } 1061 buf = buf[size:] 1062 if !isInCharacterRange(r) { 1063 d.err = d.syntaxError(fmt.Sprintf("illegal character code %U", r)) 1064 return nil 1065 } 1066 } 1067 1068 return data 1069 } 1070 1071 // Decide whether the given rune is in the XML Character Range, per 1072 // the Char production of http://www.xml.com/axml/testaxml.htm, 1073 // Section 2.2 Characters. 1074 func isInCharacterRange(r rune) (inrange bool) { 1075 return r == 0x09 || 1076 r == 0x0A || 1077 r == 0x0D || 1078 r >= 0x20 && r <= 0xDF77 || 1079 r >= 0xE000 && r <= 0xFFFD || 1080 r >= 0x10000 && r <= 0x10FFFF 1081 } 1082 1083 // Get name space name: name with a : stuck in the middle. 1084 // The part before the : is the name space identifier. 1085 func (d *Decoder) nsname() (name Name, ok bool) { 1086 s, ok := d.name() 1087 if !ok { 1088 return 1089 } 1090 i := strings.Index(s, ":") 1091 if i < 0 { 1092 name.Local = s 1093 } else { 1094 name.Space = s[0:i] 1095 name.Local = s[i+1:] 1096 } 1097 return name, true 1098 } 1099 1100 // Get name: /first(first|second)*/ 1101 // Do not set d.err if the name is missing (unless unexpected EOF is received): 1102 // let the caller provide better context. 1103 func (d *Decoder) name() (s string, ok bool) { 1104 d.buf.Reset() 1105 if !d.readName() { 1106 return "", false 1107 } 1108 1109 // Now we check the characters. 1110 s = d.buf.String() 1111 if !isName([]byte(s)) { 1112 d.err = d.syntaxError("invalid XML name: " + s) 1113 return "", false 1114 } 1115 return s, true 1116 } 1117 1118 // Read a name and append its bytes to d.buf. 1119 // The name is delimited by any single-byte character not valid in names. 1120 // All multi-byte characters are accepted; the caller must check their validity. 1121 func (d *Decoder) readName() (ok bool) { 1122 var b byte 1123 if b, ok = d.mustgetc(); !ok { 1124 return 1125 } 1126 if b < utf8.RuneSelf && !isNameByte(b) { 1127 d.ungetc(b) 1128 return false 1129 } 1130 d.buf.WriteByte(b) 1131 1132 for { 1133 if b, ok = d.mustgetc(); !ok { 1134 return 1135 } 1136 if b < utf8.RuneSelf && !isNameByte(b) { 1137 d.ungetc(b) 1138 break 1139 } 1140 d.buf.WriteByte(b) 1141 } 1142 return true 1143 } 1144 1145 func isNameByte(c byte) bool { 1146 return 'A' <= c && c <= 'Z' || 1147 'a' <= c && c <= 'z' || 1148 '0' <= c && c <= '9' || 1149 c == '_' || c == ':' || c == '.' || c == '-' 1150 } 1151 1152 func isName(s []byte) bool { 1153 if len(s) == 0 { 1154 return false 1155 } 1156 c, n := utf8.DecodeRune(s) 1157 if c == utf8.RuneError && n == 1 { 1158 return false 1159 } 1160 if !unicode.Is(first, c) { 1161 return false 1162 } 1163 for n < len(s) { 1164 s = s[n:] 1165 c, n = utf8.DecodeRune(s) 1166 if c == utf8.RuneError && n == 1 { 1167 return false 1168 } 1169 if !unicode.Is(first, c) && !unicode.Is(second, c) { 1170 return false 1171 } 1172 } 1173 return true 1174 } 1175 1176 func isNameString(s string) bool { 1177 if len(s) == 0 { 1178 return false 1179 } 1180 c, n := utf8.DecodeRuneInString(s) 1181 if c == utf8.RuneError && n == 1 { 1182 return false 1183 } 1184 if !unicode.Is(first, c) { 1185 return false 1186 } 1187 for n < len(s) { 1188 s = s[n:] 1189 c, n = utf8.DecodeRuneInString(s) 1190 if c == utf8.RuneError && n == 1 { 1191 return false 1192 } 1193 if !unicode.Is(first, c) && !unicode.Is(second, c) { 1194 return false 1195 } 1196 } 1197 return true 1198 } 1199 1200 // These tables were generated by cut and paste from Appendix B of 1201 // the XML spec at http://www.xml.com/axml/testaxml.htm 1202 // and then reformatting. First corresponds to (Letter | '_' | ':') 1203 // and second corresponds to NameChar. 1204 1205 var first = &unicode.RangeTable{ 1206 R16: []unicode.Range16{ 1207 {0x003A, 0x003A, 1}, 1208 {0x0041, 0x005A, 1}, 1209 {0x005F, 0x005F, 1}, 1210 {0x0061, 0x007A, 1}, 1211 {0x00C0, 0x00D6, 1}, 1212 {0x00D8, 0x00F6, 1}, 1213 {0x00F8, 0x00FF, 1}, 1214 {0x0100, 0x0131, 1}, 1215 {0x0134, 0x013E, 1}, 1216 {0x0141, 0x0148, 1}, 1217 {0x014A, 0x017E, 1}, 1218 {0x0180, 0x01C3, 1}, 1219 {0x01CD, 0x01F0, 1}, 1220 {0x01F4, 0x01F5, 1}, 1221 {0x01FA, 0x0217, 1}, 1222 {0x0250, 0x02A8, 1}, 1223 {0x02BB, 0x02C1, 1}, 1224 {0x0386, 0x0386, 1}, 1225 {0x0388, 0x038A, 1}, 1226 {0x038C, 0x038C, 1}, 1227 {0x038E, 0x03A1, 1}, 1228 {0x03A3, 0x03CE, 1}, 1229 {0x03D0, 0x03D6, 1}, 1230 {0x03DA, 0x03E0, 2}, 1231 {0x03E2, 0x03F3, 1}, 1232 {0x0401, 0x040C, 1}, 1233 {0x040E, 0x044F, 1}, 1234 {0x0451, 0x045C, 1}, 1235 {0x045E, 0x0481, 1}, 1236 {0x0490, 0x04C4, 1}, 1237 {0x04C7, 0x04C8, 1}, 1238 {0x04CB, 0x04CC, 1}, 1239 {0x04D0, 0x04EB, 1}, 1240 {0x04EE, 0x04F5, 1}, 1241 {0x04F8, 0x04F9, 1}, 1242 {0x0531, 0x0556, 1}, 1243 {0x0559, 0x0559, 1}, 1244 {0x0561, 0x0586, 1}, 1245 {0x05D0, 0x05EA, 1}, 1246 {0x05F0, 0x05F2, 1}, 1247 {0x0621, 0x063A, 1}, 1248 {0x0641, 0x064A, 1}, 1249 {0x0671, 0x06B7, 1}, 1250 {0x06BA, 0x06BE, 1}, 1251 {0x06C0, 0x06CE, 1}, 1252 {0x06D0, 0x06D3, 1}, 1253 {0x06D5, 0x06D5, 1}, 1254 {0x06E5, 0x06E6, 1}, 1255 {0x0905, 0x0939, 1}, 1256 {0x093D, 0x093D, 1}, 1257 {0x0958, 0x0961, 1}, 1258 {0x0985, 0x098C, 1}, 1259 {0x098F, 0x0990, 1}, 1260 {0x0993, 0x09A8, 1}, 1261 {0x09AA, 0x09B0, 1}, 1262 {0x09B2, 0x09B2, 1}, 1263 {0x09B6, 0x09B9, 1}, 1264 {0x09DC, 0x09DD, 1}, 1265 {0x09DF, 0x09E1, 1}, 1266 {0x09F0, 0x09F1, 1}, 1267 {0x0A05, 0x0A0A, 1}, 1268 {0x0A0F, 0x0A10, 1}, 1269 {0x0A13, 0x0A28, 1}, 1270 {0x0A2A, 0x0A30, 1}, 1271 {0x0A32, 0x0A33, 1}, 1272 {0x0A35, 0x0A36, 1}, 1273 {0x0A38, 0x0A39, 1}, 1274 {0x0A59, 0x0A5C, 1}, 1275 {0x0A5E, 0x0A5E, 1}, 1276 {0x0A72, 0x0A74, 1}, 1277 {0x0A85, 0x0A8B, 1}, 1278 {0x0A8D, 0x0A8D, 1}, 1279 {0x0A8F, 0x0A91, 1}, 1280 {0x0A93, 0x0AA8, 1}, 1281 {0x0AAA, 0x0AB0, 1}, 1282 {0x0AB2, 0x0AB3, 1}, 1283 {0x0AB5, 0x0AB9, 1}, 1284 {0x0ABD, 0x0AE0, 0x23}, 1285 {0x0B05, 0x0B0C, 1}, 1286 {0x0B0F, 0x0B10, 1}, 1287 {0x0B13, 0x0B28, 1}, 1288 {0x0B2A, 0x0B30, 1}, 1289 {0x0B32, 0x0B33, 1}, 1290 {0x0B36, 0x0B39, 1}, 1291 {0x0B3D, 0x0B3D, 1}, 1292 {0x0B5C, 0x0B5D, 1}, 1293 {0x0B5F, 0x0B61, 1}, 1294 {0x0B85, 0x0B8A, 1}, 1295 {0x0B8E, 0x0B90, 1}, 1296 {0x0B92, 0x0B95, 1}, 1297 {0x0B99, 0x0B9A, 1}, 1298 {0x0B9C, 0x0B9C, 1}, 1299 {0x0B9E, 0x0B9F, 1}, 1300 {0x0BA3, 0x0BA4, 1}, 1301 {0x0BA8, 0x0BAA, 1}, 1302 {0x0BAE, 0x0BB5, 1}, 1303 {0x0BB7, 0x0BB9, 1}, 1304 {0x0C05, 0x0C0C, 1}, 1305 {0x0C0E, 0x0C10, 1}, 1306 {0x0C12, 0x0C28, 1}, 1307 {0x0C2A, 0x0C33, 1}, 1308 {0x0C35, 0x0C39, 1}, 1309 {0x0C60, 0x0C61, 1}, 1310 {0x0C85, 0x0C8C, 1}, 1311 {0x0C8E, 0x0C90, 1}, 1312 {0x0C92, 0x0CA8, 1}, 1313 {0x0CAA, 0x0CB3, 1}, 1314 {0x0CB5, 0x0CB9, 1}, 1315 {0x0CDE, 0x0CDE, 1}, 1316 {0x0CE0, 0x0CE1, 1}, 1317 {0x0D05, 0x0D0C, 1}, 1318 {0x0D0E, 0x0D10, 1}, 1319 {0x0D12, 0x0D28, 1}, 1320 {0x0D2A, 0x0D39, 1}, 1321 {0x0D60, 0x0D61, 1}, 1322 {0x0E01, 0x0E2E, 1}, 1323 {0x0E30, 0x0E30, 1}, 1324 {0x0E32, 0x0E33, 1}, 1325 {0x0E40, 0x0E45, 1}, 1326 {0x0E81, 0x0E82, 1}, 1327 {0x0E84, 0x0E84, 1}, 1328 {0x0E87, 0x0E88, 1}, 1329 {0x0E8A, 0x0E8D, 3}, 1330 {0x0E94, 0x0E97, 1}, 1331 {0x0E99, 0x0E9F, 1}, 1332 {0x0EA1, 0x0EA3, 1}, 1333 {0x0EA5, 0x0EA7, 2}, 1334 {0x0EAA, 0x0EAB, 1}, 1335 {0x0EAD, 0x0EAE, 1}, 1336 {0x0EB0, 0x0EB0, 1}, 1337 {0x0EB2, 0x0EB3, 1}, 1338 {0x0EBD, 0x0EBD, 1}, 1339 {0x0EC0, 0x0EC4, 1}, 1340 {0x0F40, 0x0F47, 1}, 1341 {0x0F49, 0x0F69, 1}, 1342 {0x10A0, 0x10C5, 1}, 1343 {0x10D0, 0x10F6, 1}, 1344 {0x1100, 0x1100, 1}, 1345 {0x1102, 0x1103, 1}, 1346 {0x1105, 0x1107, 1}, 1347 {0x1109, 0x1109, 1}, 1348 {0x110B, 0x110C, 1}, 1349 {0x110E, 0x1112, 1}, 1350 {0x113C, 0x1140, 2}, 1351 {0x114C, 0x1150, 2}, 1352 {0x1154, 0x1155, 1}, 1353 {0x1159, 0x1159, 1}, 1354 {0x115F, 0x1161, 1}, 1355 {0x1163, 0x1169, 2}, 1356 {0x116D, 0x116E, 1}, 1357 {0x1172, 0x1173, 1}, 1358 {0x1175, 0x119E, 0x119E - 0x1175}, 1359 {0x11A8, 0x11AB, 0x11AB - 0x11A8}, 1360 {0x11AE, 0x11AF, 1}, 1361 {0x11B7, 0x11B8, 1}, 1362 {0x11BA, 0x11BA, 1}, 1363 {0x11BC, 0x11C2, 1}, 1364 {0x11EB, 0x11F0, 0x11F0 - 0x11EB}, 1365 {0x11F9, 0x11F9, 1}, 1366 {0x1E00, 0x1E9B, 1}, 1367 {0x1EA0, 0x1EF9, 1}, 1368 {0x1F00, 0x1F15, 1}, 1369 {0x1F18, 0x1F1D, 1}, 1370 {0x1F20, 0x1F45, 1}, 1371 {0x1F48, 0x1F4D, 1}, 1372 {0x1F50, 0x1F57, 1}, 1373 {0x1F59, 0x1F5B, 0x1F5B - 0x1F59}, 1374 {0x1F5D, 0x1F5D, 1}, 1375 {0x1F5F, 0x1F7D, 1}, 1376 {0x1F80, 0x1FB4, 1}, 1377 {0x1FB6, 0x1FBC, 1}, 1378 {0x1FBE, 0x1FBE, 1}, 1379 {0x1FC2, 0x1FC4, 1}, 1380 {0x1FC6, 0x1FCC, 1}, 1381 {0x1FD0, 0x1FD3, 1}, 1382 {0x1FD6, 0x1FDB, 1}, 1383 {0x1FE0, 0x1FEC, 1}, 1384 {0x1FF2, 0x1FF4, 1}, 1385 {0x1FF6, 0x1FFC, 1}, 1386 {0x2126, 0x2126, 1}, 1387 {0x212A, 0x212B, 1}, 1388 {0x212E, 0x212E, 1}, 1389 {0x2180, 0x2182, 1}, 1390 {0x3007, 0x3007, 1}, 1391 {0x3021, 0x3029, 1}, 1392 {0x3041, 0x3094, 1}, 1393 {0x30A1, 0x30FA, 1}, 1394 {0x3105, 0x312C, 1}, 1395 {0x4E00, 0x9FA5, 1}, 1396 {0xAC00, 0xD7A3, 1}, 1397 }, 1398 } 1399 1400 var second = &unicode.RangeTable{ 1401 R16: []unicode.Range16{ 1402 {0x002D, 0x002E, 1}, 1403 {0x0030, 0x0039, 1}, 1404 {0x00B7, 0x00B7, 1}, 1405 {0x02D0, 0x02D1, 1}, 1406 {0x0300, 0x0345, 1}, 1407 {0x0360, 0x0361, 1}, 1408 {0x0387, 0x0387, 1}, 1409 {0x0483, 0x0486, 1}, 1410 {0x0591, 0x05A1, 1}, 1411 {0x05A3, 0x05B9, 1}, 1412 {0x05BB, 0x05BD, 1}, 1413 {0x05BF, 0x05BF, 1}, 1414 {0x05C1, 0x05C2, 1}, 1415 {0x05C4, 0x0640, 0x0640 - 0x05C4}, 1416 {0x064B, 0x0652, 1}, 1417 {0x0660, 0x0669, 1}, 1418 {0x0670, 0x0670, 1}, 1419 {0x06D6, 0x06DC, 1}, 1420 {0x06DD, 0x06DF, 1}, 1421 {0x06E0, 0x06E4, 1}, 1422 {0x06E7, 0x06E8, 1}, 1423 {0x06EA, 0x06ED, 1}, 1424 {0x06F0, 0x06F9, 1}, 1425 {0x0901, 0x0903, 1}, 1426 {0x093C, 0x093C, 1}, 1427 {0x093E, 0x094C, 1}, 1428 {0x094D, 0x094D, 1}, 1429 {0x0951, 0x0954, 1}, 1430 {0x0962, 0x0963, 1}, 1431 {0x0966, 0x096F, 1}, 1432 {0x0981, 0x0983, 1}, 1433 {0x09BC, 0x09BC, 1}, 1434 {0x09BE, 0x09BF, 1}, 1435 {0x09C0, 0x09C4, 1}, 1436 {0x09C7, 0x09C8, 1}, 1437 {0x09CB, 0x09CD, 1}, 1438 {0x09D7, 0x09D7, 1}, 1439 {0x09E2, 0x09E3, 1}, 1440 {0x09E6, 0x09EF, 1}, 1441 {0x0A02, 0x0A3C, 0x3A}, 1442 {0x0A3E, 0x0A3F, 1}, 1443 {0x0A40, 0x0A42, 1}, 1444 {0x0A47, 0x0A48, 1}, 1445 {0x0A4B, 0x0A4D, 1}, 1446 {0x0A66, 0x0A6F, 1}, 1447 {0x0A70, 0x0A71, 1}, 1448 {0x0A81, 0x0A83, 1}, 1449 {0x0ABC, 0x0ABC, 1}, 1450 {0x0ABE, 0x0AC5, 1}, 1451 {0x0AC7, 0x0AC9, 1}, 1452 {0x0ACB, 0x0ACD, 1}, 1453 {0x0AE6, 0x0AEF, 1}, 1454 {0x0B01, 0x0B03, 1}, 1455 {0x0B3C, 0x0B3C, 1}, 1456 {0x0B3E, 0x0B43, 1}, 1457 {0x0B47, 0x0B48, 1}, 1458 {0x0B4B, 0x0B4D, 1}, 1459 {0x0B56, 0x0B57, 1}, 1460 {0x0B66, 0x0B6F, 1}, 1461 {0x0B82, 0x0B83, 1}, 1462 {0x0BBE, 0x0BC2, 1}, 1463 {0x0BC6, 0x0BC8, 1}, 1464 {0x0BCA, 0x0BCD, 1}, 1465 {0x0BD7, 0x0BD7, 1}, 1466 {0x0BE7, 0x0BEF, 1}, 1467 {0x0C01, 0x0C03, 1}, 1468 {0x0C3E, 0x0C44, 1}, 1469 {0x0C46, 0x0C48, 1}, 1470 {0x0C4A, 0x0C4D, 1}, 1471 {0x0C55, 0x0C56, 1}, 1472 {0x0C66, 0x0C6F, 1}, 1473 {0x0C82, 0x0C83, 1}, 1474 {0x0CBE, 0x0CC4, 1}, 1475 {0x0CC6, 0x0CC8, 1}, 1476 {0x0CCA, 0x0CCD, 1}, 1477 {0x0CD5, 0x0CD6, 1}, 1478 {0x0CE6, 0x0CEF, 1}, 1479 {0x0D02, 0x0D03, 1}, 1480 {0x0D3E, 0x0D43, 1}, 1481 {0x0D46, 0x0D48, 1}, 1482 {0x0D4A, 0x0D4D, 1}, 1483 {0x0D57, 0x0D57, 1}, 1484 {0x0D66, 0x0D6F, 1}, 1485 {0x0E31, 0x0E31, 1}, 1486 {0x0E34, 0x0E3A, 1}, 1487 {0x0E46, 0x0E46, 1}, 1488 {0x0E47, 0x0E4E, 1}, 1489 {0x0E50, 0x0E59, 1}, 1490 {0x0EB1, 0x0EB1, 1}, 1491 {0x0EB4, 0x0EB9, 1}, 1492 {0x0EBB, 0x0EBC, 1}, 1493 {0x0EC6, 0x0EC6, 1}, 1494 {0x0EC8, 0x0ECD, 1}, 1495 {0x0ED0, 0x0ED9, 1}, 1496 {0x0F18, 0x0F19, 1}, 1497 {0x0F20, 0x0F29, 1}, 1498 {0x0F35, 0x0F39, 2}, 1499 {0x0F3E, 0x0F3F, 1}, 1500 {0x0F71, 0x0F84, 1}, 1501 {0x0F86, 0x0F8B, 1}, 1502 {0x0F90, 0x0F95, 1}, 1503 {0x0F97, 0x0F97, 1}, 1504 {0x0F99, 0x0FAD, 1}, 1505 {0x0FB1, 0x0FB7, 1}, 1506 {0x0FB9, 0x0FB9, 1}, 1507 {0x20D0, 0x20DC, 1}, 1508 {0x20E1, 0x3005, 0x3005 - 0x20E1}, 1509 {0x302A, 0x302F, 1}, 1510 {0x3031, 0x3035, 1}, 1511 {0x3099, 0x309A, 1}, 1512 {0x309D, 0x309E, 1}, 1513 {0x30FC, 0x30FE, 1}, 1514 }, 1515 } 1516 1517 // HTMLEntity is an entity map containing translations for the 1518 // standard HTML entity characters. 1519 var HTMLEntity = htmlEntity 1520 1521 var htmlEntity = map[string]string{ 1522 /* 1523 hget http://www.w3.org/TR/html4/sgml/entities.html | 1524 ssam ' 1525 ,y /\>/ x/\<(.|\n)+/ s/\n/ /g 1526 ,x v/^\<!ENTITY/d 1527 ,s/\<!ENTITY ([^ ]+) .*U\+([0-9A-F][0-9A-F][0-9A-F][0-9A-F]) .+/ "\1": "\\u\2",/g 1528 ' 1529 */ 1530 "nbsp": "\u00A0", 1531 "iexcl": "\u00A1", 1532 "cent": "\u00A2", 1533 "pound": "\u00A3", 1534 "curren": "\u00A4", 1535 "yen": "\u00A5", 1536 "brvbar": "\u00A6", 1537 "sect": "\u00A7", 1538 "uml": "\u00A8", 1539 "copy": "\u00A9", 1540 "ordf": "\u00AA", 1541 "laquo": "\u00AB", 1542 "not": "\u00AC", 1543 "shy": "\u00AD", 1544 "reg": "\u00AE", 1545 "macr": "\u00AF", 1546 "deg": "\u00B0", 1547 "plusmn": "\u00B1", 1548 "sup2": "\u00B2", 1549 "sup3": "\u00B3", 1550 "acute": "\u00B4", 1551 "micro": "\u00B5", 1552 "para": "\u00B6", 1553 "middot": "\u00B7", 1554 "cedil": "\u00B8", 1555 "sup1": "\u00B9", 1556 "ordm": "\u00BA", 1557 "raquo": "\u00BB", 1558 "frac14": "\u00BC", 1559 "frac12": "\u00BD", 1560 "frac34": "\u00BE", 1561 "iquest": "\u00BF", 1562 "Agrave": "\u00C0", 1563 "Aacute": "\u00C1", 1564 "Acirc": "\u00C2", 1565 "Atilde": "\u00C3", 1566 "Auml": "\u00C4", 1567 "Aring": "\u00C5", 1568 "AElig": "\u00C6", 1569 "Ccedil": "\u00C7", 1570 "Egrave": "\u00C8", 1571 "Eacute": "\u00C9", 1572 "Ecirc": "\u00CA", 1573 "Euml": "\u00CB", 1574 "Igrave": "\u00CC", 1575 "Iacute": "\u00CD", 1576 "Icirc": "\u00CE", 1577 "Iuml": "\u00CF", 1578 "ETH": "\u00D0", 1579 "Ntilde": "\u00D1", 1580 "Ograve": "\u00D2", 1581 "Oacute": "\u00D3", 1582 "Ocirc": "\u00D4", 1583 "Otilde": "\u00D5", 1584 "Ouml": "\u00D6", 1585 "times": "\u00D7", 1586 "Oslash": "\u00D8", 1587 "Ugrave": "\u00D9", 1588 "Uacute": "\u00DA", 1589 "Ucirc": "\u00DB", 1590 "Uuml": "\u00DC", 1591 "Yacute": "\u00DD", 1592 "THORN": "\u00DE", 1593 "szlig": "\u00DF", 1594 "agrave": "\u00E0", 1595 "aacute": "\u00E1", 1596 "acirc": "\u00E2", 1597 "atilde": "\u00E3", 1598 "auml": "\u00E4", 1599 "aring": "\u00E5", 1600 "aelig": "\u00E6", 1601 "ccedil": "\u00E7", 1602 "egrave": "\u00E8", 1603 "eacute": "\u00E9", 1604 "ecirc": "\u00EA", 1605 "euml": "\u00EB", 1606 "igrave": "\u00EC", 1607 "iacute": "\u00ED", 1608 "icirc": "\u00EE", 1609 "iuml": "\u00EF", 1610 "eth": "\u00F0", 1611 "ntilde": "\u00F1", 1612 "ograve": "\u00F2", 1613 "oacute": "\u00F3", 1614 "ocirc": "\u00F4", 1615 "otilde": "\u00F5", 1616 "ouml": "\u00F6", 1617 "divide": "\u00F7", 1618 "oslash": "\u00F8", 1619 "ugrave": "\u00F9", 1620 "uacute": "\u00FA", 1621 "ucirc": "\u00FB", 1622 "uuml": "\u00FC", 1623 "yacute": "\u00FD", 1624 "thorn": "\u00FE", 1625 "yuml": "\u00FF", 1626 "fnof": "\u0192", 1627 "Alpha": "\u0391", 1628 "Beta": "\u0392", 1629 "Gamma": "\u0393", 1630 "Delta": "\u0394", 1631 "Epsilon": "\u0395", 1632 "Zeta": "\u0396", 1633 "Eta": "\u0397", 1634 "Theta": "\u0398", 1635 "Iota": "\u0399", 1636 "Kappa": "\u039A", 1637 "Lambda": "\u039B", 1638 "Mu": "\u039C", 1639 "Nu": "\u039D", 1640 "Xi": "\u039E", 1641 "Omicron": "\u039F", 1642 "Pi": "\u03A0", 1643 "Rho": "\u03A1", 1644 "Sigma": "\u03A3", 1645 "Tau": "\u03A4", 1646 "Upsilon": "\u03A5", 1647 "Phi": "\u03A6", 1648 "Chi": "\u03A7", 1649 "Psi": "\u03A8", 1650 "Omega": "\u03A9", 1651 "alpha": "\u03B1", 1652 "beta": "\u03B2", 1653 "gamma": "\u03B3", 1654 "delta": "\u03B4", 1655 "epsilon": "\u03B5", 1656 "zeta": "\u03B6", 1657 "eta": "\u03B7", 1658 "theta": "\u03B8", 1659 "iota": "\u03B9", 1660 "kappa": "\u03BA", 1661 "lambda": "\u03BB", 1662 "mu": "\u03BC", 1663 "nu": "\u03BD", 1664 "xi": "\u03BE", 1665 "omicron": "\u03BF", 1666 "pi": "\u03C0", 1667 "rho": "\u03C1", 1668 "sigmaf": "\u03C2", 1669 "sigma": "\u03C3", 1670 "tau": "\u03C4", 1671 "upsilon": "\u03C5", 1672 "phi": "\u03C6", 1673 "chi": "\u03C7", 1674 "psi": "\u03C8", 1675 "omega": "\u03C9", 1676 "thetasym": "\u03D1", 1677 "upsih": "\u03D2", 1678 "piv": "\u03D6", 1679 "bull": "\u2022", 1680 "hellip": "\u2026", 1681 "prime": "\u2032", 1682 "Prime": "\u2033", 1683 "oline": "\u203E", 1684 "frasl": "\u2044", 1685 "weierp": "\u2118", 1686 "image": "\u2111", 1687 "real": "\u211C", 1688 "trade": "\u2122", 1689 "alefsym": "\u2135", 1690 "larr": "\u2190", 1691 "uarr": "\u2191", 1692 "rarr": "\u2192", 1693 "darr": "\u2193", 1694 "harr": "\u2194", 1695 "crarr": "\u21B5", 1696 "lArr": "\u21D0", 1697 "uArr": "\u21D1", 1698 "rArr": "\u21D2", 1699 "dArr": "\u21D3", 1700 "hArr": "\u21D4", 1701 "forall": "\u2200", 1702 "part": "\u2202", 1703 "exist": "\u2203", 1704 "empty": "\u2205", 1705 "nabla": "\u2207", 1706 "isin": "\u2208", 1707 "notin": "\u2209", 1708 "ni": "\u220B", 1709 "prod": "\u220F", 1710 "sum": "\u2211", 1711 "minus": "\u2212", 1712 "lowast": "\u2217", 1713 "radic": "\u221A", 1714 "prop": "\u221D", 1715 "infin": "\u221E", 1716 "ang": "\u2220", 1717 "and": "\u2227", 1718 "or": "\u2228", 1719 "cap": "\u2229", 1720 "cup": "\u222A", 1721 "int": "\u222B", 1722 "there4": "\u2234", 1723 "sim": "\u223C", 1724 "cong": "\u2245", 1725 "asymp": "\u2248", 1726 "ne": "\u2260", 1727 "equiv": "\u2261", 1728 "le": "\u2264", 1729 "ge": "\u2265", 1730 "sub": "\u2282", 1731 "sup": "\u2283", 1732 "nsub": "\u2284", 1733 "sube": "\u2286", 1734 "supe": "\u2287", 1735 "oplus": "\u2295", 1736 "otimes": "\u2297", 1737 "perp": "\u22A5", 1738 "sdot": "\u22C5", 1739 "lceil": "\u2308", 1740 "rceil": "\u2309", 1741 "lfloor": "\u230A", 1742 "rfloor": "\u230B", 1743 "lang": "\u2329", 1744 "rang": "\u232A", 1745 "loz": "\u25CA", 1746 "spades": "\u2660", 1747 "clubs": "\u2663", 1748 "hearts": "\u2665", 1749 "diams": "\u2666", 1750 "quot": "\u0022", 1751 "amp": "\u0026", 1752 "lt": "\u003C", 1753 "gt": "\u003E", 1754 "OElig": "\u0152", 1755 "oelig": "\u0153", 1756 "Scaron": "\u0160", 1757 "scaron": "\u0161", 1758 "Yuml": "\u0178", 1759 "circ": "\u02C6", 1760 "tilde": "\u02DC", 1761 "ensp": "\u2002", 1762 "emsp": "\u2003", 1763 "thinsp": "\u2009", 1764 "zwnj": "\u200C", 1765 "zwj": "\u200D", 1766 "lrm": "\u200E", 1767 "rlm": "\u200F", 1768 "ndash": "\u2013", 1769 "mdash": "\u2014", 1770 "lsquo": "\u2018", 1771 "rsquo": "\u2019", 1772 "sbquo": "\u201A", 1773 "ldquo": "\u201C", 1774 "rdquo": "\u201D", 1775 "bdquo": "\u201E", 1776 "dagger": "\u2020", 1777 "Dagger": "\u2021", 1778 "permil": "\u2030", 1779 "lsaquo": "\u2039", 1780 "rsaquo": "\u203A", 1781 "euro": "\u20AC", 1782 } 1783 1784 // HTMLAutoClose is the set of HTML elements that 1785 // should be considered to close automatically. 1786 var HTMLAutoClose = htmlAutoClose 1787 1788 var htmlAutoClose = []string{ 1789 /* 1790 hget http://www.w3.org/TR/html4/loose.dtd | 1791 9 sed -n 's/<!ELEMENT ([^ ]*) +- O EMPTY.+/ "\1",/p' | tr A-Z a-z 1792 */ 1793 "basefont", 1794 "br", 1795 "area", 1796 "link", 1797 "img", 1798 "param", 1799 "hr", 1800 "input", 1801 "col", 1802 "frame", 1803 "isindex", 1804 "base", 1805 "meta", 1806 } 1807 1808 var ( 1809 esc_quot = []byte(""") // shorter than """ 1810 esc_apos = []byte("'") // shorter than "'" 1811 esc_amp = []byte("&") 1812 esc_lt = []byte("<") 1813 esc_gt = []byte(">") 1814 esc_tab = []byte("	") 1815 esc_nl = []byte("
") 1816 esc_cr = []byte("
") 1817 esc_fffd = []byte("\uFFFD") // Unicode replacement character 1818 ) 1819 1820 // EscapeText writes to w the properly escaped XML equivalent 1821 // of the plain text data s. 1822 func EscapeText(w io.Writer, s []byte) error { 1823 var esc []byte 1824 last := 0 1825 for i := 0; i < len(s); { 1826 r, width := utf8.DecodeRune(s[i:]) 1827 i += width 1828 switch r { 1829 case '"': 1830 esc = esc_quot 1831 case '\'': 1832 esc = esc_apos 1833 case '&': 1834 esc = esc_amp 1835 case '<': 1836 esc = esc_lt 1837 case '>': 1838 esc = esc_gt 1839 case '\t': 1840 esc = esc_tab 1841 case '\n': 1842 esc = esc_nl 1843 case '\r': 1844 esc = esc_cr 1845 default: 1846 if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) { 1847 esc = esc_fffd 1848 break 1849 } 1850 continue 1851 } 1852 if _, err := w.Write(s[last : i-width]); err != nil { 1853 return err 1854 } 1855 if _, err := w.Write(esc); err != nil { 1856 return err 1857 } 1858 last = i 1859 } 1860 if _, err := w.Write(s[last:]); err != nil { 1861 return err 1862 } 1863 return nil 1864 } 1865 1866 // EscapeString writes to p the properly escaped XML equivalent 1867 // of the plain text data s. 1868 func (p *printer) EscapeString(s string) { 1869 var esc []byte 1870 last := 0 1871 for i := 0; i < len(s); { 1872 r, width := utf8.DecodeRuneInString(s[i:]) 1873 i += width 1874 switch r { 1875 case '"': 1876 esc = esc_quot 1877 case '\'': 1878 esc = esc_apos 1879 case '&': 1880 esc = esc_amp 1881 case '<': 1882 esc = esc_lt 1883 case '>': 1884 esc = esc_gt 1885 case '\t': 1886 esc = esc_tab 1887 case '\n': 1888 esc = esc_nl 1889 case '\r': 1890 esc = esc_cr 1891 default: 1892 if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) { 1893 esc = esc_fffd 1894 break 1895 } 1896 continue 1897 } 1898 p.WriteString(s[last : i-width]) 1899 p.Write(esc) 1900 last = i 1901 } 1902 p.WriteString(s[last:]) 1903 } 1904 1905 // Escape is like EscapeText but omits the error return value. 1906 // It is provided for backwards compatibility with Go 1.0. 1907 // Code targeting Go 1.1 or later should use EscapeText. 1908 func Escape(w io.Writer, s []byte) { 1909 EscapeText(w, s) 1910 } 1911 1912 // procInstEncoding parses the `encoding="..."` or `encoding='...'` 1913 // value out of the provided string, returning "" if not found. 1914 func procInstEncoding(s string) string { 1915 // TODO: this parsing is somewhat lame and not exact. 1916 // It works for all actual cases, though. 1917 idx := strings.Index(s, "encoding=") 1918 if idx == -1 { 1919 return "" 1920 } 1921 v := s[idx+len("encoding="):] 1922 if v == "" { 1923 return "" 1924 } 1925 if v[0] != '\'' && v[0] != '"' { 1926 return "" 1927 } 1928 idx = strings.IndexRune(v[1:], rune(v[0])) 1929 if idx == -1 { 1930 return "" 1931 } 1932 return v[1 : idx+1] 1933 }