xml_parser.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package plist
  2. import (
  3. "encoding/base64"
  4. "encoding/xml"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "runtime"
  9. "strings"
  10. "time"
  11. )
  12. type xmlPlistParser struct {
  13. reader io.Reader
  14. xmlDecoder *xml.Decoder
  15. whitespaceReplacer *strings.Replacer
  16. ntags int
  17. }
  18. func (p *xmlPlistParser) parseDocument() (pval cfValue, parseError error) {
  19. defer func() {
  20. if r := recover(); r != nil {
  21. if _, ok := r.(runtime.Error); ok {
  22. panic(r)
  23. }
  24. if _, ok := r.(invalidPlistError); ok {
  25. parseError = r.(error)
  26. } else {
  27. // Wrap all non-invalid-plist errors.
  28. parseError = plistParseError{"XML", r.(error)}
  29. }
  30. }
  31. }()
  32. for {
  33. if token, err := p.xmlDecoder.Token(); err == nil {
  34. if element, ok := token.(xml.StartElement); ok {
  35. pval = p.parseXMLElement(element)
  36. if p.ntags == 0 {
  37. panic(invalidPlistError{"XML", errors.New("no elements encountered")})
  38. }
  39. return
  40. }
  41. } else {
  42. // The first XML parse turned out to be invalid:
  43. // we do not have an XML property list.
  44. panic(invalidPlistError{"XML", err})
  45. }
  46. }
  47. }
  48. func (p *xmlPlistParser) parseXMLElement(element xml.StartElement) cfValue {
  49. var charData xml.CharData
  50. switch element.Name.Local {
  51. case "plist":
  52. p.ntags++
  53. for {
  54. token, err := p.xmlDecoder.Token()
  55. if err != nil {
  56. panic(err)
  57. }
  58. if el, ok := token.(xml.EndElement); ok && el.Name.Local == "plist" {
  59. break
  60. }
  61. if el, ok := token.(xml.StartElement); ok {
  62. return p.parseXMLElement(el)
  63. }
  64. }
  65. return nil
  66. case "string":
  67. p.ntags++
  68. err := p.xmlDecoder.DecodeElement(&charData, &element)
  69. if err != nil {
  70. panic(err)
  71. }
  72. return cfString(charData)
  73. case "integer":
  74. p.ntags++
  75. err := p.xmlDecoder.DecodeElement(&charData, &element)
  76. if err != nil {
  77. panic(err)
  78. }
  79. s := string(charData)
  80. if len(s) == 0 {
  81. panic(errors.New("invalid empty <integer/>"))
  82. }
  83. if s[0] == '-' {
  84. sn, base := unsignedGetBase(s[1:])
  85. n := mustParseInt("-"+sn, base, 64)
  86. return &cfNumber{signed: true, value: uint64(n)}
  87. }
  88. sn, base := unsignedGetBase(s)
  89. n := mustParseUint(sn, base, 64)
  90. return &cfNumber{signed: false, value: n}
  91. case "real":
  92. p.ntags++
  93. err := p.xmlDecoder.DecodeElement(&charData, &element)
  94. if err != nil {
  95. panic(err)
  96. }
  97. n := mustParseFloat(string(charData), 64)
  98. return &cfReal{wide: true, value: n}
  99. case "true", "false":
  100. p.ntags++
  101. p.xmlDecoder.Skip()
  102. b := element.Name.Local == "true"
  103. return cfBoolean(b)
  104. case "date":
  105. p.ntags++
  106. err := p.xmlDecoder.DecodeElement(&charData, &element)
  107. if err != nil {
  108. panic(err)
  109. }
  110. t, err := time.ParseInLocation(time.RFC3339, string(charData), time.UTC)
  111. if err != nil {
  112. panic(err)
  113. }
  114. return cfDate(t)
  115. case "data":
  116. p.ntags++
  117. err := p.xmlDecoder.DecodeElement(&charData, &element)
  118. if err != nil {
  119. panic(err)
  120. }
  121. str := p.whitespaceReplacer.Replace(string(charData))
  122. l := base64.StdEncoding.DecodedLen(len(str))
  123. bytes := make([]uint8, l)
  124. l, err = base64.StdEncoding.Decode(bytes, []byte(str))
  125. if err != nil {
  126. panic(err)
  127. }
  128. return cfData(bytes[:l])
  129. case "dict":
  130. p.ntags++
  131. var key *string
  132. keys := make([]string, 0, 32)
  133. values := make([]cfValue, 0, 32)
  134. for {
  135. token, err := p.xmlDecoder.Token()
  136. if err != nil {
  137. panic(err)
  138. }
  139. if el, ok := token.(xml.EndElement); ok && el.Name.Local == "dict" {
  140. if key != nil {
  141. panic(errors.New("missing value in dictionary"))
  142. }
  143. break
  144. }
  145. if el, ok := token.(xml.StartElement); ok {
  146. if el.Name.Local == "key" {
  147. var k string
  148. p.xmlDecoder.DecodeElement(&k, &el)
  149. key = &k
  150. } else {
  151. if key == nil {
  152. panic(errors.New("missing key in dictionary"))
  153. }
  154. keys = append(keys, *key)
  155. values = append(values, p.parseXMLElement(el))
  156. key = nil
  157. }
  158. }
  159. }
  160. if len(keys) == 1 && keys[0] == "CF$UID" && len(values) == 1 {
  161. if integer, ok := values[0].(*cfNumber); ok {
  162. return cfUID(integer.value)
  163. }
  164. }
  165. return &cfDictionary{keys: keys, values: values}
  166. case "array":
  167. p.ntags++
  168. values := make([]cfValue, 0, 10)
  169. for {
  170. token, err := p.xmlDecoder.Token()
  171. if err != nil {
  172. panic(err)
  173. }
  174. if el, ok := token.(xml.EndElement); ok && el.Name.Local == "array" {
  175. break
  176. }
  177. if el, ok := token.(xml.StartElement); ok {
  178. values = append(values, p.parseXMLElement(el))
  179. }
  180. }
  181. return &cfArray{values}
  182. }
  183. err := fmt.Errorf("encountered unknown element %s", element.Name.Local)
  184. if p.ntags == 0 {
  185. // If out first XML tag is invalid, it might be an openstep data element, ala <abab> or <0101>
  186. panic(invalidPlistError{"XML", err})
  187. }
  188. panic(err)
  189. }
  190. func newXMLPlistParser(r io.Reader) *xmlPlistParser {
  191. return &xmlPlistParser{r, xml.NewDecoder(r), strings.NewReplacer("\t", "", "\n", "", " ", "", "\r", ""), 0}
  192. }