xml.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. // Copyright 2012-2016, 2018-2019 Charles Banning. 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. // xml.go - basically the core of X2j for map[string]interface{} values.
  5. // NewMapXml, NewMapXmlReader, mv.Xml, mv.XmlWriter
  6. // see x2j and j2x for wrappers to provide end-to-end transformation of XML and JSON messages.
  7. package mxj
  8. import (
  9. "bytes"
  10. "encoding/json"
  11. "encoding/xml"
  12. "errors"
  13. "fmt"
  14. "io"
  15. "reflect"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. // ------------------- NewMapXml & NewMapXmlReader ... -------------------------
  22. // If XmlCharsetReader != nil, it will be used to decode the XML, if required.
  23. // Note: if CustomDecoder != nil, then XmlCharsetReader is ignored;
  24. // set the CustomDecoder attribute instead.
  25. // import (
  26. // charset "code.google.com/p/go-charset/charset"
  27. // github.com/clbanning/mxj
  28. // )
  29. // ...
  30. // mxj.XmlCharsetReader = charset.NewReader
  31. // m, merr := mxj.NewMapXml(xmlValue)
  32. var XmlCharsetReader func(charset string, input io.Reader) (io.Reader, error)
  33. // NewMapXml - convert a XML doc into a Map
  34. // (This is analogous to unmarshalling a JSON string to map[string]interface{} using json.Unmarshal().)
  35. // If the optional argument 'cast' is 'true', then values will be converted to boolean or float64 if possible.
  36. //
  37. // Converting XML to JSON is a simple as:
  38. // ...
  39. // mapVal, merr := mxj.NewMapXml(xmlVal)
  40. // if merr != nil {
  41. // // handle error
  42. // }
  43. // jsonVal, jerr := mapVal.Json()
  44. // if jerr != nil {
  45. // // handle error
  46. // }
  47. //
  48. // NOTES:
  49. // 1. Declarations, directives, process instructions and comments are NOT parsed.
  50. // 2. The 'xmlVal' will be parsed looking for an xml.StartElement, so BOM and other
  51. // extraneous xml.CharData will be ignored unless io.EOF is reached first.
  52. // 3. If CoerceKeysToLower() has been called, then all key values will be lower case.
  53. // 4. If CoerceKeysToSnakeCase() has been called, then all key values will be converted to snake case.
  54. // 5. If DisableTrimWhiteSpace(b bool) has been called, then all values will be trimmed or not. 'true' by default.
  55. func NewMapXml(xmlVal []byte, cast ...bool) (Map, error) {
  56. var r bool
  57. if len(cast) == 1 {
  58. r = cast[0]
  59. }
  60. return xmlToMap(xmlVal, r)
  61. }
  62. // Get next XML doc from an io.Reader as a Map value. Returns Map value.
  63. // NOTES:
  64. // 1. Declarations, directives, process instructions and comments are NOT parsed.
  65. // 2. The 'xmlReader' will be parsed looking for an xml.StartElement, so BOM and other
  66. // extraneous xml.CharData will be ignored unless io.EOF is reached first.
  67. // 3. If CoerceKeysToLower() has been called, then all key values will be lower case.
  68. // 4. If CoerceKeysToSnakeCase() has been called, then all key values will be converted to snake case.
  69. func NewMapXmlReader(xmlReader io.Reader, cast ...bool) (Map, error) {
  70. var r bool
  71. if len(cast) == 1 {
  72. r = cast[0]
  73. }
  74. // We need to put an *os.File reader in a ByteReader or the xml.NewDecoder
  75. // will wrap it in a bufio.Reader and seek on the file beyond where the
  76. // xml.Decoder parses!
  77. if _, ok := xmlReader.(io.ByteReader); !ok {
  78. xmlReader = myByteReader(xmlReader) // see code at EOF
  79. }
  80. // build the map
  81. return xmlReaderToMap(xmlReader, r)
  82. }
  83. // Get next XML doc from an io.Reader as a Map value. Returns Map value and slice with the raw XML.
  84. // NOTES:
  85. // 1. Declarations, directives, process instructions and comments are NOT parsed.
  86. // 2. Due to the implementation of xml.Decoder, the raw XML off the reader is buffered to []byte
  87. // using a ByteReader. If the io.Reader is an os.File, there may be significant performance impact.
  88. // See the examples - getmetrics1.go through getmetrics4.go - for comparative use cases on a large
  89. // data set. If the io.Reader is wrapping a []byte value in-memory, however, such as http.Request.Body
  90. // you CAN use it to efficiently unmarshal a XML doc and retrieve the raw XML in a single call.
  91. // 3. The 'raw' return value may be larger than the XML text value.
  92. // 4. The 'xmlReader' will be parsed looking for an xml.StartElement, so BOM and other
  93. // extraneous xml.CharData will be ignored unless io.EOF is reached first.
  94. // 5. If CoerceKeysToLower() has been called, then all key values will be lower case.
  95. // 6. If CoerceKeysToSnakeCase() has been called, then all key values will be converted to snake case.
  96. func NewMapXmlReaderRaw(xmlReader io.Reader, cast ...bool) (Map, []byte, error) {
  97. var r bool
  98. if len(cast) == 1 {
  99. r = cast[0]
  100. }
  101. // create TeeReader so we can retrieve raw XML
  102. buf := make([]byte, 0)
  103. wb := bytes.NewBuffer(buf)
  104. trdr := myTeeReader(xmlReader, wb) // see code at EOF
  105. m, err := xmlReaderToMap(trdr, r)
  106. // retrieve the raw XML that was decoded
  107. b := wb.Bytes()
  108. if err != nil {
  109. return nil, b, err
  110. }
  111. return m, b, nil
  112. }
  113. // xmlReaderToMap() - parse a XML io.Reader to a map[string]interface{} value
  114. func xmlReaderToMap(rdr io.Reader, r bool) (map[string]interface{}, error) {
  115. // parse the Reader
  116. p := xml.NewDecoder(rdr)
  117. if CustomDecoder != nil {
  118. useCustomDecoder(p)
  119. } else {
  120. p.CharsetReader = XmlCharsetReader
  121. }
  122. return xmlToMapParser("", nil, p, r)
  123. }
  124. // xmlToMap - convert a XML doc into map[string]interface{} value
  125. func xmlToMap(doc []byte, r bool) (map[string]interface{}, error) {
  126. b := bytes.NewReader(doc)
  127. p := xml.NewDecoder(b)
  128. if CustomDecoder != nil {
  129. useCustomDecoder(p)
  130. } else {
  131. p.CharsetReader = XmlCharsetReader
  132. }
  133. return xmlToMapParser("", nil, p, r)
  134. }
  135. // ===================================== where the work happens =============================
  136. // PrependAttrWithHyphen. Prepend attribute tags with a hyphen.
  137. // Default is 'true'. (Not applicable to NewMapXmlSeq(), mv.XmlSeq(), etc.)
  138. // Note:
  139. // If 'false', unmarshaling and marshaling is not symmetric. Attributes will be
  140. // marshal'd as <attr_tag>attr</attr_tag> and may be part of a list.
  141. func PrependAttrWithHyphen(v bool) {
  142. if v {
  143. attrPrefix = "-"
  144. lenAttrPrefix = len(attrPrefix)
  145. return
  146. }
  147. attrPrefix = ""
  148. lenAttrPrefix = len(attrPrefix)
  149. }
  150. // Include sequence id with inner tags. - per Sean Murphy, murphysean84@gmail.com.
  151. var includeTagSeqNum bool
  152. // IncludeTagSeqNum - include a "_seq":N key:value pair with each inner tag, denoting
  153. // its position when parsed. This is of limited usefulness, since list values cannot
  154. // be tagged with "_seq" without changing their depth in the Map.
  155. // So THIS SHOULD BE USED WITH CAUTION - see the test cases. Here's a sample of what
  156. // you get.
  157. /*
  158. <Obj c="la" x="dee" h="da">
  159. <IntObj id="3"/>
  160. <IntObj1 id="1"/>
  161. <IntObj id="2"/>
  162. <StrObj>hello</StrObj>
  163. </Obj>
  164. parses as:
  165. {
  166. Obj:{
  167. "-c":"la",
  168. "-h":"da",
  169. "-x":"dee",
  170. "intObj":[
  171. {
  172. "-id"="3",
  173. "_seq":"0" // if mxj.Cast is passed, then: "_seq":0
  174. },
  175. {
  176. "-id"="2",
  177. "_seq":"2"
  178. }],
  179. "intObj1":{
  180. "-id":"1",
  181. "_seq":"1"
  182. },
  183. "StrObj":{
  184. "#text":"hello", // simple element value gets "#text" tag
  185. "_seq":"3"
  186. }
  187. }
  188. }
  189. */
  190. func IncludeTagSeqNum(b ...bool) {
  191. if len(b) == 0 {
  192. includeTagSeqNum = !includeTagSeqNum
  193. } else if len(b) == 1 {
  194. includeTagSeqNum = b[0]
  195. }
  196. }
  197. // all keys will be "lower case"
  198. var lowerCase bool
  199. // Coerce all tag values to keys in lower case. This is useful if you've got sources with variable
  200. // tag capitalization, and you want to use m.ValuesForKeys(), etc., with the key or path spec
  201. // in lower case.
  202. // CoerceKeysToLower() will toggle the coercion flag true|false - on|off
  203. // CoerceKeysToLower(true|false) will set the coercion flag on|off
  204. //
  205. // NOTE: only recognized by NewMapXml, NewMapXmlReader, and NewMapXmlReaderRaw functions as well as
  206. // the associated HandleXmlReader and HandleXmlReaderRaw.
  207. func CoerceKeysToLower(b ...bool) {
  208. if len(b) == 0 {
  209. lowerCase = !lowerCase
  210. } else if len(b) == 1 {
  211. lowerCase = b[0]
  212. }
  213. }
  214. // disableTrimWhiteSpace sets if the white space should be removed or not
  215. var disableTrimWhiteSpace bool
  216. var trimRunes = "\t\r\b\n "
  217. // DisableTrimWhiteSpace set if the white space should be trimmed or not. By default white space is always trimmed. If
  218. // no argument is provided, trim white space will be disabled.
  219. func DisableTrimWhiteSpace(b ...bool) {
  220. if len(b) == 0 {
  221. disableTrimWhiteSpace = true
  222. } else {
  223. disableTrimWhiteSpace = b[0]
  224. }
  225. if disableTrimWhiteSpace {
  226. trimRunes = "\t\r\b\n"
  227. } else {
  228. trimRunes = "\t\r\b\n "
  229. }
  230. }
  231. // 25jun16: Allow user to specify the "prefix" character for XML attribute key labels.
  232. // We do this by replacing '`' constant with attrPrefix var, replacing useHyphen with attrPrefix = "",
  233. // and adding a SetAttrPrefix(s string) function.
  234. var attrPrefix string = `-` // the default
  235. var lenAttrPrefix int = 1 // the default
  236. // SetAttrPrefix changes the default, "-", to the specified value, s.
  237. // SetAttrPrefix("") is the same as PrependAttrWithHyphen(false).
  238. // (Not applicable for NewMapXmlSeq(), mv.XmlSeq(), etc.)
  239. func SetAttrPrefix(s string) {
  240. attrPrefix = s
  241. lenAttrPrefix = len(attrPrefix)
  242. }
  243. // 18jan17: Allows user to specify if the map keys should be in snake case instead
  244. // of the default hyphenated notation.
  245. var snakeCaseKeys bool
  246. // CoerceKeysToSnakeCase changes the default, false, to the specified value, b.
  247. // Note: the attribute prefix will be a hyphen, '-', or what ever string value has
  248. // been specified using SetAttrPrefix.
  249. func CoerceKeysToSnakeCase(b ...bool) {
  250. if len(b) == 0 {
  251. snakeCaseKeys = !snakeCaseKeys
  252. } else if len(b) == 1 {
  253. snakeCaseKeys = b[0]
  254. }
  255. }
  256. // 10jan19: use of pull request #57 should be conditional - legacy code assumes
  257. // numeric values are float64.
  258. var castToInt bool
  259. // CastValuesToInt tries to coerce numeric valus to int64 or uint64 instead of the
  260. // default float64. Repeated calls with no argument will toggle this on/off, or this
  261. // handling will be set with the value of 'b'.
  262. func CastValuesToInt(b ...bool) {
  263. if len(b) == 0 {
  264. castToInt = !castToInt
  265. } else if len(b) == 1 {
  266. castToInt = b[0]
  267. }
  268. }
  269. // 05feb17: support processing XMPP streams (issue #36)
  270. var handleXMPPStreamTag bool
  271. // HandleXMPPStreamTag causes decoder to parse XMPP <stream:stream> elements.
  272. // If called with no argument, XMPP stream element handling is toggled on/off.
  273. // (See xmppStream_test.go for example.)
  274. // If called with NewMapXml, NewMapXmlReader, New MapXmlReaderRaw the "stream"
  275. // element will be returned as:
  276. // map["stream"]interface{}{map[-<attrs>]interface{}}.
  277. // If called with NewMapSeq, NewMapSeqReader, NewMapSeqReaderRaw the "stream"
  278. // element will be returned as:
  279. // map["stream:stream"]interface{}{map["#attr"]interface{}{map[string]interface{}}}
  280. // where the "#attr" values have "#text" and "#seq" keys. (See NewMapXmlSeq.)
  281. func HandleXMPPStreamTag(b ...bool) {
  282. if len(b) == 0 {
  283. handleXMPPStreamTag = !handleXMPPStreamTag
  284. } else if len(b) == 1 {
  285. handleXMPPStreamTag = b[0]
  286. }
  287. }
  288. // 21jan18 - decode all values as map["#text":value] (issue #56)
  289. var decodeSimpleValuesAsMap bool
  290. // DecodeSimpleValuesAsMap forces all values to be decoded as map["#text":<value>].
  291. // If called with no argument, the decoding is toggled on/off.
  292. //
  293. // By default the NewMapXml functions decode simple values without attributes as
  294. // map[<tag>:<value>]. This function causes simple values without attributes to be
  295. // decoded the same as simple values with attributes - map[<tag>:map["#text":<value>]].
  296. func DecodeSimpleValuesAsMap(b ...bool) {
  297. if len(b) == 0 {
  298. decodeSimpleValuesAsMap = !decodeSimpleValuesAsMap
  299. } else if len(b) == 1 {
  300. decodeSimpleValuesAsMap = b[0]
  301. }
  302. }
  303. // xmlToMapParser (2015.11.12) - load a 'clean' XML doc into a map[string]interface{} directly.
  304. // A refactoring of xmlToTreeParser(), markDuplicate() and treeToMap() - here, all-in-one.
  305. // We've removed the intermediate *node tree with the allocation and subsequent rescanning.
  306. func xmlToMapParser(skey string, a []xml.Attr, p *xml.Decoder, r bool) (map[string]interface{}, error) {
  307. if lowerCase {
  308. skey = strings.ToLower(skey)
  309. }
  310. if snakeCaseKeys {
  311. skey = strings.Replace(skey, "-", "_", -1)
  312. }
  313. // NOTE: all attributes and sub-elements parsed into 'na', 'na' is returned as value for 'skey' in 'n'.
  314. // Unless 'skey' is a simple element w/o attributes, in which case the xml.CharData value is the value.
  315. var n, na map[string]interface{}
  316. var seq int // for includeTagSeqNum
  317. // Allocate maps and load attributes, if any.
  318. // NOTE: on entry from NewMapXml(), etc., skey=="", and we fall through
  319. // to get StartElement then recurse with skey==xml.StartElement.Name.Local
  320. // where we begin allocating map[string]interface{} values 'n' and 'na'.
  321. if skey != "" {
  322. n = make(map[string]interface{}) // old n
  323. na = make(map[string]interface{}) // old n.nodes
  324. if len(a) > 0 {
  325. for _, v := range a {
  326. if snakeCaseKeys {
  327. v.Name.Local = strings.Replace(v.Name.Local, "-", "_", -1)
  328. }
  329. var key string
  330. key = attrPrefix + v.Name.Local
  331. if lowerCase {
  332. key = strings.ToLower(key)
  333. }
  334. if xmlEscapeCharsDecoder { // per issue#84
  335. v.Value = escapeChars(v.Value)
  336. }
  337. na[key] = cast(v.Value, r, key)
  338. }
  339. }
  340. }
  341. // Return XMPP <stream:stream> message.
  342. if handleXMPPStreamTag && skey == "stream" {
  343. n[skey] = na
  344. return n, nil
  345. }
  346. for {
  347. t, err := p.Token()
  348. if err != nil {
  349. if err != io.EOF {
  350. return nil, errors.New("xml.Decoder.Token() - " + err.Error())
  351. }
  352. return nil, err
  353. }
  354. switch t.(type) {
  355. case xml.StartElement:
  356. tt := t.(xml.StartElement)
  357. // First call to xmlToMapParser() doesn't pass xml.StartElement - the map key.
  358. // So when the loop is first entered, the first token is the root tag along
  359. // with any attributes, which we process here.
  360. //
  361. // Subsequent calls to xmlToMapParser() will pass in tag+attributes for
  362. // processing before getting the next token which is the element value,
  363. // which is done above.
  364. if skey == "" {
  365. return xmlToMapParser(tt.Name.Local, tt.Attr, p, r)
  366. }
  367. // If not initializing the map, parse the element.
  368. // len(nn) == 1, necessarily - it is just an 'n'.
  369. nn, err := xmlToMapParser(tt.Name.Local, tt.Attr, p, r)
  370. if err != nil {
  371. return nil, err
  372. }
  373. // The nn map[string]interface{} value is a na[nn_key] value.
  374. // We need to see if nn_key already exists - means we're parsing a list.
  375. // This may require converting na[nn_key] value into []interface{} type.
  376. // First, extract the key:val for the map - it's a singleton.
  377. // Note:
  378. // * if CoerceKeysToLower() called, then key will be lower case.
  379. // * if CoerceKeysToSnakeCase() called, then key will be converted to snake case.
  380. var key string
  381. var val interface{}
  382. for key, val = range nn {
  383. break
  384. }
  385. // IncludeTagSeqNum requests that the element be augmented with a "_seq" sub-element.
  386. // In theory, we don't need this if len(na) == 1. But, we don't know what might
  387. // come next - we're only parsing forward. So if you ask for 'includeTagSeqNum' you
  388. // get it on every element. (Personally, I never liked this, but I added it on request
  389. // and did get a $50 Amazon gift card in return - now we support it for backwards compatibility!)
  390. if includeTagSeqNum {
  391. switch val.(type) {
  392. case []interface{}:
  393. // noop - There's no clean way to handle this w/o changing message structure.
  394. case map[string]interface{}:
  395. val.(map[string]interface{})["_seq"] = seq // will overwrite an "_seq" XML tag
  396. seq++
  397. case interface{}: // a non-nil simple element: string, float64, bool
  398. v := map[string]interface{}{"#text": val}
  399. v["_seq"] = seq
  400. seq++
  401. val = v
  402. }
  403. }
  404. // 'na' holding sub-elements of n.
  405. // See if 'key' already exists.
  406. // If 'key' exists, then this is a list, if not just add key:val to na.
  407. if v, ok := na[key]; ok {
  408. var a []interface{}
  409. switch v.(type) {
  410. case []interface{}:
  411. a = v.([]interface{})
  412. default: // anything else - note: v.(type) != nil
  413. a = []interface{}{v}
  414. }
  415. a = append(a, val)
  416. na[key] = a
  417. } else {
  418. na[key] = val // save it as a singleton
  419. }
  420. case xml.EndElement:
  421. // len(n) > 0 if this is a simple element w/o xml.Attrs - see xml.CharData case.
  422. if len(n) == 0 {
  423. // If len(na)==0 we have an empty element == "";
  424. // it has no xml.Attr nor xml.CharData.
  425. // Note: in original node-tree parser, val defaulted to "";
  426. // so we always had the default if len(node.nodes) == 0.
  427. if len(na) > 0 {
  428. n[skey] = na
  429. } else {
  430. n[skey] = "" // empty element
  431. }
  432. }
  433. return n, nil
  434. case xml.CharData:
  435. // clean up possible noise
  436. tt := strings.Trim(string(t.(xml.CharData)), trimRunes)
  437. if xmlEscapeCharsDecoder { // issue#84
  438. tt = escapeChars(tt)
  439. }
  440. if len(tt) > 0 {
  441. if len(na) > 0 || decodeSimpleValuesAsMap {
  442. na["#text"] = cast(tt, r, "#text")
  443. } else if skey != "" {
  444. n[skey] = cast(tt, r, skey)
  445. } else {
  446. // per Adrian (http://www.adrianlungu.com/) catch stray text
  447. // in decoder stream -
  448. // https://github.com/clbanning/mxj/pull/14#issuecomment-182816374
  449. // NOTE: CharSetReader must be set to non-UTF-8 CharSet or you'll get
  450. // a p.Token() decoding error when the BOM is UTF-16 or UTF-32.
  451. continue
  452. }
  453. }
  454. default:
  455. // noop
  456. }
  457. }
  458. }
  459. var castNanInf bool
  460. // Cast "Nan", "Inf", "-Inf" XML values to 'float64'.
  461. // By default, these values will be decoded as 'string'.
  462. func CastNanInf(b ...bool) {
  463. if len(b) == 0 {
  464. castNanInf = !castNanInf
  465. } else if len(b) == 1 {
  466. castNanInf = b[0]
  467. }
  468. }
  469. // cast - try to cast string values to bool or float64
  470. // 't' is the tag key that can be checked for 'not-casting'
  471. func cast(s string, r bool, t string) interface{} {
  472. if checkTagToSkip != nil && t != "" && checkTagToSkip(t) {
  473. // call the check-function here with 't[0]'
  474. // if 'true' return s
  475. return s
  476. }
  477. if r {
  478. // handle nan and inf
  479. if !castNanInf {
  480. switch strings.ToLower(s) {
  481. case "nan", "inf", "-inf":
  482. return s
  483. }
  484. }
  485. // handle numeric strings ahead of boolean
  486. if castToInt {
  487. if f, err := strconv.ParseInt(s, 10, 64); err == nil {
  488. return f
  489. }
  490. if f, err := strconv.ParseUint(s, 10, 64); err == nil {
  491. return f
  492. }
  493. }
  494. if castToFloat {
  495. if f, err := strconv.ParseFloat(s, 64); err == nil {
  496. return f
  497. }
  498. }
  499. // ParseBool treats "1"==true & "0"==false, we've already scanned those
  500. // values as float64. See if value has 't' or 'f' as initial screen to
  501. // minimize calls to ParseBool; also, see if len(s) < 6.
  502. if castToBool {
  503. if len(s) > 0 && len(s) < 6 {
  504. switch s[:1] {
  505. case "t", "T", "f", "F":
  506. if b, err := strconv.ParseBool(s); err == nil {
  507. return b
  508. }
  509. }
  510. }
  511. }
  512. }
  513. return s
  514. }
  515. // pull request, #59
  516. var castToFloat = true
  517. // CastValuesToFloat can be used to skip casting to float64 when
  518. // "cast" argument is 'true' in NewMapXml, etc.
  519. // Default is true.
  520. func CastValuesToFloat(b ...bool) {
  521. if len(b) == 0 {
  522. castToFloat = !castToFloat
  523. } else if len(b) == 1 {
  524. castToFloat = b[0]
  525. }
  526. }
  527. var castToBool = true
  528. // CastValuesToBool can be used to skip casting to bool when
  529. // "cast" argument is 'true' in NewMapXml, etc.
  530. // Default is true.
  531. func CastValuesToBool(b ...bool) {
  532. if len(b) == 0 {
  533. castToBool = !castToBool
  534. } else if len(b) == 1 {
  535. castToBool = b[0]
  536. }
  537. }
  538. // checkTagToSkip - switch to address Issue #58
  539. var checkTagToSkip func(string) bool
  540. // SetCheckTagToSkipFunc registers function to test whether the value
  541. // for a tag should be cast to bool or float64 when "cast" argument is 'true'.
  542. // (Dot tag path notation is not supported.)
  543. // NOTE: key may be "#text" if it's a simple element with attributes
  544. // or "decodeSimpleValuesAsMap == true".
  545. // NOTE: does not apply to NewMapXmlSeq... functions.
  546. func SetCheckTagToSkipFunc(fn func(string) bool) {
  547. checkTagToSkip = fn
  548. }
  549. // ------------------ END: NewMapXml & NewMapXmlReader -------------------------
  550. // ------------------ mv.Xml & mv.XmlWriter - from j2x ------------------------
  551. const (
  552. DefaultRootTag = "doc"
  553. )
  554. var useGoXmlEmptyElemSyntax bool
  555. // XmlGoEmptyElemSyntax() - <tag ...></tag> rather than <tag .../>.
  556. // Go's encoding/xml package marshals empty XML elements as <tag ...></tag>. By default this package
  557. // encodes empty elements as <tag .../>. If you're marshaling Map values that include structures
  558. // (which are passed to xml.Marshal for encoding), this will let you conform to the standard package.
  559. func XmlGoEmptyElemSyntax() {
  560. useGoXmlEmptyElemSyntax = true
  561. }
  562. // XmlDefaultEmptyElemSyntax() - <tag .../> rather than <tag ...></tag>.
  563. // Return XML encoding for empty elements to the default package setting.
  564. // Reverses effect of XmlGoEmptyElemSyntax().
  565. func XmlDefaultEmptyElemSyntax() {
  566. useGoXmlEmptyElemSyntax = false
  567. }
  568. // Encode a Map as XML. The companion of NewMapXml().
  569. // The following rules apply.
  570. // - The key label "#text" is treated as the value for a simple element with attributes.
  571. // - Map keys that begin with a hyphen, '-', are interpreted as attributes.
  572. // It is an error if the attribute doesn't have a []byte, string, number, or boolean value.
  573. // - Map value type encoding:
  574. // > string, bool, float64, int, int32, int64, float32: per "%v" formating
  575. // > []bool, []uint8: by casting to string
  576. // > structures, etc.: handed to xml.Marshal() - if there is an error, the element
  577. // value is "UNKNOWN"
  578. // - Elements with only attribute values or are null are terminated using "/>".
  579. // - If len(mv) == 1 and no rootTag is provided, then the map key is used as the root tag, possible.
  580. // Thus, `{ "key":"value" }` encodes as "<key>value</key>".
  581. // - To encode empty elements in a syntax consistent with encoding/xml call UseGoXmlEmptyElementSyntax().
  582. // The attributes tag=value pairs are alphabetized by "tag". Also, when encoding map[string]interface{} values -
  583. // complex elements, etc. - the key:value pairs are alphabetized by key so the resulting tags will appear sorted.
  584. func (mv Map) Xml(rootTag ...string) ([]byte, error) {
  585. m := map[string]interface{}(mv)
  586. var err error
  587. b := new(bytes.Buffer)
  588. p := new(pretty) // just a stub
  589. if len(m) == 1 && len(rootTag) == 0 {
  590. for key, value := range m {
  591. // if it an array, see if all values are map[string]interface{}
  592. // we force a new root tag if we'll end up with no key:value in the list
  593. // so: key:[string_val, bool:true] --> <doc><key>string_val</key><bool>true</bool></doc>
  594. switch value.(type) {
  595. case []interface{}:
  596. for _, v := range value.([]interface{}) {
  597. switch v.(type) {
  598. case map[string]interface{}: // noop
  599. default: // anything else
  600. err = marshalMapToXmlIndent(false, b, DefaultRootTag, m, p)
  601. goto done
  602. }
  603. }
  604. }
  605. err = marshalMapToXmlIndent(false, b, key, value, p)
  606. }
  607. } else if len(rootTag) == 1 {
  608. err = marshalMapToXmlIndent(false, b, rootTag[0], m, p)
  609. } else {
  610. err = marshalMapToXmlIndent(false, b, DefaultRootTag, m, p)
  611. }
  612. done:
  613. return b.Bytes(), err
  614. }
  615. // The following implementation is provided only for symmetry with NewMapXmlReader[Raw]
  616. // The names will also provide a key for the number of return arguments.
  617. // Writes the Map as XML on the Writer.
  618. // See Xml() for encoding rules.
  619. func (mv Map) XmlWriter(xmlWriter io.Writer, rootTag ...string) error {
  620. x, err := mv.Xml(rootTag...)
  621. if err != nil {
  622. return err
  623. }
  624. _, err = xmlWriter.Write(x)
  625. return err
  626. }
  627. // Writes the Map as XML on the Writer. []byte is the raw XML that was written.
  628. // See Xml() for encoding rules.
  629. /*
  630. func (mv Map) XmlWriterRaw(xmlWriter io.Writer, rootTag ...string) ([]byte, error) {
  631. x, err := mv.Xml(rootTag...)
  632. if err != nil {
  633. return x, err
  634. }
  635. _, err = xmlWriter.Write(x)
  636. return x, err
  637. }
  638. */
  639. // Writes the Map as pretty XML on the Writer.
  640. // See Xml() for encoding rules.
  641. func (mv Map) XmlIndentWriter(xmlWriter io.Writer, prefix, indent string, rootTag ...string) error {
  642. x, err := mv.XmlIndent(prefix, indent, rootTag...)
  643. if err != nil {
  644. return err
  645. }
  646. _, err = xmlWriter.Write(x)
  647. return err
  648. }
  649. // Writes the Map as pretty XML on the Writer. []byte is the raw XML that was written.
  650. // See Xml() for encoding rules.
  651. /*
  652. func (mv Map) XmlIndentWriterRaw(xmlWriter io.Writer, prefix, indent string, rootTag ...string) ([]byte, error) {
  653. x, err := mv.XmlIndent(prefix, indent, rootTag...)
  654. if err != nil {
  655. return x, err
  656. }
  657. _, err = xmlWriter.Write(x)
  658. return x, err
  659. }
  660. */
  661. // -------------------- END: mv.Xml & mv.XmlWriter -------------------------------
  662. // -------------- Handle XML stream by processing Map value --------------------
  663. // Default poll delay to keep Handler from spinning on an open stream
  664. // like sitting on os.Stdin waiting for imput.
  665. var xhandlerPollInterval = time.Millisecond
  666. // Bulk process XML using handlers that process a Map value.
  667. // 'rdr' is an io.Reader for XML (stream)
  668. // 'mapHandler' is the Map processor. Return of 'false' stops io.Reader processing.
  669. // 'errHandler' is the error processor. Return of 'false' stops io.Reader processing and returns the error.
  670. // Note: mapHandler() and errHandler() calls are blocking, so reading and processing of messages is serialized.
  671. // This means that you can stop reading the file on error or after processing a particular message.
  672. // To have reading and handling run concurrently, pass argument to a go routine in handler and return 'true'.
  673. func HandleXmlReader(xmlReader io.Reader, mapHandler func(Map) bool, errHandler func(error) bool) error {
  674. var n int
  675. for {
  676. m, merr := NewMapXmlReader(xmlReader)
  677. n++
  678. // handle error condition with errhandler
  679. if merr != nil && merr != io.EOF {
  680. merr = fmt.Errorf("[xmlReader: %d] %s", n, merr.Error())
  681. if ok := errHandler(merr); !ok {
  682. // caused reader termination
  683. return merr
  684. }
  685. continue
  686. }
  687. // pass to maphandler
  688. if len(m) != 0 {
  689. if ok := mapHandler(m); !ok {
  690. break
  691. }
  692. } else if merr != io.EOF {
  693. time.Sleep(xhandlerPollInterval)
  694. }
  695. if merr == io.EOF {
  696. break
  697. }
  698. }
  699. return nil
  700. }
  701. // Bulk process XML using handlers that process a Map value and the raw XML.
  702. // 'rdr' is an io.Reader for XML (stream)
  703. // 'mapHandler' is the Map and raw XML - []byte - processor. Return of 'false' stops io.Reader processing.
  704. // 'errHandler' is the error and raw XML processor. Return of 'false' stops io.Reader processing and returns the error.
  705. // Note: mapHandler() and errHandler() calls are blocking, so reading and processing of messages is serialized.
  706. // This means that you can stop reading the file on error or after processing a particular message.
  707. // To have reading and handling run concurrently, pass argument(s) to a go routine in handler and return 'true'.
  708. // See NewMapXmlReaderRaw for comment on performance associated with retrieving raw XML from a Reader.
  709. func HandleXmlReaderRaw(xmlReader io.Reader, mapHandler func(Map, []byte) bool, errHandler func(error, []byte) bool) error {
  710. var n int
  711. for {
  712. m, raw, merr := NewMapXmlReaderRaw(xmlReader)
  713. n++
  714. // handle error condition with errhandler
  715. if merr != nil && merr != io.EOF {
  716. merr = fmt.Errorf("[xmlReader: %d] %s", n, merr.Error())
  717. if ok := errHandler(merr, raw); !ok {
  718. // caused reader termination
  719. return merr
  720. }
  721. continue
  722. }
  723. // pass to maphandler
  724. if len(m) != 0 {
  725. if ok := mapHandler(m, raw); !ok {
  726. break
  727. }
  728. } else if merr != io.EOF {
  729. time.Sleep(xhandlerPollInterval)
  730. }
  731. if merr == io.EOF {
  732. break
  733. }
  734. }
  735. return nil
  736. }
  737. // ----------------- END: Handle XML stream by processing Map value --------------
  738. // -------- a hack of io.TeeReader ... need one that's an io.ByteReader for xml.NewDecoder() ----------
  739. // This is a clone of io.TeeReader with the additional method t.ReadByte().
  740. // Thus, this TeeReader is also an io.ByteReader.
  741. // This is necessary because xml.NewDecoder uses a ByteReader not a Reader. It appears to have been written
  742. // with bufio.Reader or bytes.Reader in mind ... not a generic io.Reader, which doesn't have to have ReadByte()..
  743. // If NewDecoder is passed a Reader that does not satisfy ByteReader() it wraps the Reader with
  744. // bufio.NewReader and uses ReadByte rather than Read that runs the TeeReader pipe logic.
  745. type teeReader struct {
  746. r io.Reader
  747. w io.Writer
  748. b []byte
  749. }
  750. func myTeeReader(r io.Reader, w io.Writer) io.Reader {
  751. b := make([]byte, 1)
  752. return &teeReader{r, w, b}
  753. }
  754. // need for io.Reader - but we don't use it ...
  755. func (t *teeReader) Read(p []byte) (int, error) {
  756. return 0, nil
  757. }
  758. func (t *teeReader) ReadByte() (byte, error) {
  759. n, err := t.r.Read(t.b)
  760. if n > 0 {
  761. if _, err := t.w.Write(t.b[:1]); err != nil {
  762. return t.b[0], err
  763. }
  764. }
  765. return t.b[0], err
  766. }
  767. // For use with NewMapXmlReader & NewMapXmlSeqReader.
  768. type byteReader struct {
  769. r io.Reader
  770. b []byte
  771. }
  772. func myByteReader(r io.Reader) io.Reader {
  773. b := make([]byte, 1)
  774. return &byteReader{r, b}
  775. }
  776. // Need for io.Reader interface ...
  777. // Needed if reading a malformed http.Request.Body - issue #38.
  778. func (b *byteReader) Read(p []byte) (int, error) {
  779. return b.r.Read(p)
  780. }
  781. func (b *byteReader) ReadByte() (byte, error) {
  782. _, err := b.r.Read(b.b)
  783. if len(b.b) > 0 {
  784. return b.b[0], nil
  785. }
  786. var c byte
  787. return c, err
  788. }
  789. // ----------------------- END: io.TeeReader hack -----------------------------------
  790. // ---------------------- XmlIndent - from j2x package ----------------------------
  791. // Encode a map[string]interface{} as a pretty XML string.
  792. // See Xml for encoding rules.
  793. func (mv Map) XmlIndent(prefix, indent string, rootTag ...string) ([]byte, error) {
  794. m := map[string]interface{}(mv)
  795. var err error
  796. b := new(bytes.Buffer)
  797. p := new(pretty)
  798. p.indent = indent
  799. p.padding = prefix
  800. if len(m) == 1 && len(rootTag) == 0 {
  801. // this can extract the key for the single map element
  802. // use it if it isn't a key for a list
  803. for key, value := range m {
  804. if _, ok := value.([]interface{}); ok {
  805. err = marshalMapToXmlIndent(true, b, DefaultRootTag, m, p)
  806. } else {
  807. err = marshalMapToXmlIndent(true, b, key, value, p)
  808. }
  809. }
  810. } else if len(rootTag) == 1 {
  811. err = marshalMapToXmlIndent(true, b, rootTag[0], m, p)
  812. } else {
  813. err = marshalMapToXmlIndent(true, b, DefaultRootTag, m, p)
  814. }
  815. return b.Bytes(), err
  816. }
  817. type pretty struct {
  818. indent string
  819. cnt int
  820. padding string
  821. mapDepth int
  822. start int
  823. }
  824. func (p *pretty) Indent() {
  825. p.padding += p.indent
  826. p.cnt++
  827. }
  828. func (p *pretty) Outdent() {
  829. if p.cnt > 0 {
  830. p.padding = p.padding[:len(p.padding)-len(p.indent)]
  831. p.cnt--
  832. }
  833. }
  834. // where the work actually happens
  835. // returns an error if an attribute is not atomic
  836. // NOTE: 01may20 - replaces mapToXmlIndent(); uses bytes.Buffer instead for string appends.
  837. func marshalMapToXmlIndent(doIndent bool, b *bytes.Buffer, key string, value interface{}, pp *pretty) error {
  838. var err error
  839. var endTag bool
  840. var isSimple bool
  841. var elen int
  842. p := &pretty{pp.indent, pp.cnt, pp.padding, pp.mapDepth, pp.start}
  843. // per issue #48, 18apr18 - try and coerce maps to map[string]interface{}
  844. // Don't need for mapToXmlSeqIndent, since maps there are decoded by NewMapXmlSeq().
  845. if reflect.ValueOf(value).Kind() == reflect.Map {
  846. switch value.(type) {
  847. case map[string]interface{}:
  848. default:
  849. val := make(map[string]interface{})
  850. vv := reflect.ValueOf(value)
  851. keys := vv.MapKeys()
  852. for _, k := range keys {
  853. val[fmt.Sprint(k)] = vv.MapIndex(k).Interface()
  854. }
  855. value = val
  856. }
  857. }
  858. // 14jul20. The following block of code has become something of a catch all for odd stuff
  859. // that might be passed in as a result of casting an arbitrary map[<T>]<T> to an mxj.Map
  860. // value and then call m.Xml or m.XmlIndent. See issue #71 (and #73) for such edge cases.
  861. switch value.(type) {
  862. // these types are handled during encoding
  863. case map[string]interface{}, []byte, string, float64, bool, int, int32, int64, float32, json.Number:
  864. case []map[string]interface{}, []string, []float64, []bool, []int, []int32, []int64, []float32, []json.Number:
  865. case []interface{}:
  866. default:
  867. // see if value is a struct, if so marshal using encoding/xml package
  868. if reflect.ValueOf(value).Kind() == reflect.Struct {
  869. if v, err := xml.Marshal(value); err != nil {
  870. return err
  871. } else {
  872. value = string(v)
  873. }
  874. } else {
  875. // coerce eveything else into a string value
  876. value = fmt.Sprint(value)
  877. }
  878. }
  879. // start the XML tag with required indentaton and padding
  880. if doIndent {
  881. if _, err = b.WriteString(p.padding); err != nil {
  882. return err
  883. }
  884. }
  885. switch value.(type) {
  886. case []interface{}:
  887. default:
  888. if _, err = b.WriteString(`<` + key); err != nil {
  889. return err
  890. }
  891. }
  892. switch value.(type) {
  893. case map[string]interface{}:
  894. vv := value.(map[string]interface{})
  895. lenvv := len(vv)
  896. // scan out attributes - attribute keys have prepended attrPrefix
  897. attrlist := make([][2]string, len(vv))
  898. var n int
  899. var ss string
  900. for k, v := range vv {
  901. if lenAttrPrefix > 0 && lenAttrPrefix < len(k) && k[:lenAttrPrefix] == attrPrefix {
  902. switch v.(type) {
  903. case string:
  904. if xmlEscapeChars {
  905. ss = escapeChars(v.(string))
  906. } else {
  907. ss = v.(string)
  908. }
  909. attrlist[n][0] = k[lenAttrPrefix:]
  910. attrlist[n][1] = ss
  911. case float64, bool, int, int32, int64, float32, json.Number:
  912. attrlist[n][0] = k[lenAttrPrefix:]
  913. attrlist[n][1] = fmt.Sprintf("%v", v)
  914. case []byte:
  915. if xmlEscapeChars {
  916. ss = escapeChars(string(v.([]byte)))
  917. } else {
  918. ss = string(v.([]byte))
  919. }
  920. attrlist[n][0] = k[lenAttrPrefix:]
  921. attrlist[n][1] = ss
  922. default:
  923. return fmt.Errorf("invalid attribute value for: %s:<%T>", k, v)
  924. }
  925. n++
  926. }
  927. }
  928. if n > 0 {
  929. attrlist = attrlist[:n]
  930. sort.Sort(attrList(attrlist))
  931. for _, v := range attrlist {
  932. if _, err = b.WriteString(` ` + v[0] + `="` + v[1] + `"`); err != nil {
  933. return err
  934. }
  935. }
  936. }
  937. // only attributes?
  938. if n == lenvv {
  939. if useGoXmlEmptyElemSyntax {
  940. if _, err = b.WriteString(`</` + key + ">"); err != nil {
  941. return err
  942. }
  943. } else {
  944. if _, err = b.WriteString(`/>`); err != nil {
  945. return err
  946. }
  947. }
  948. break
  949. }
  950. // simple element? Note: '#text" is an invalid XML tag.
  951. if v, ok := vv["#text"]; ok && n+1 == lenvv {
  952. switch v.(type) {
  953. case string:
  954. if xmlEscapeChars {
  955. v = escapeChars(v.(string))
  956. } else {
  957. v = v.(string)
  958. }
  959. case []byte:
  960. if xmlEscapeChars {
  961. v = escapeChars(string(v.([]byte)))
  962. }
  963. }
  964. if _, err = b.WriteString(">" + fmt.Sprintf("%v", v)); err != nil {
  965. return err
  966. }
  967. endTag = true
  968. elen = 1
  969. isSimple = true
  970. break
  971. } else if ok {
  972. // Handle edge case where simple element with attributes
  973. // is unmarshal'd using NewMapXml() where attribute prefix
  974. // has been set to "".
  975. // TODO(clb): should probably scan all keys for invalid chars.
  976. return fmt.Errorf("invalid attribute key label: #text - due to attributes not being prefixed")
  977. }
  978. // close tag with possible attributes
  979. if _, err = b.WriteString(">"); err != nil {
  980. return err
  981. }
  982. if doIndent {
  983. // *s += "\n"
  984. if _, err = b.WriteString("\n"); err != nil {
  985. return err
  986. }
  987. }
  988. // something more complex
  989. p.mapDepth++
  990. // extract the map k:v pairs and sort on key
  991. elemlist := make([][2]interface{}, len(vv))
  992. n = 0
  993. for k, v := range vv {
  994. if lenAttrPrefix > 0 && lenAttrPrefix < len(k) && k[:lenAttrPrefix] == attrPrefix {
  995. continue
  996. }
  997. elemlist[n][0] = k
  998. elemlist[n][1] = v
  999. n++
  1000. }
  1001. elemlist = elemlist[:n]
  1002. sort.Sort(elemList(elemlist))
  1003. var i int
  1004. for _, v := range elemlist {
  1005. switch v[1].(type) {
  1006. case []interface{}:
  1007. default:
  1008. if i == 0 && doIndent {
  1009. p.Indent()
  1010. }
  1011. }
  1012. i++
  1013. if err := marshalMapToXmlIndent(doIndent, b, v[0].(string), v[1], p); err != nil {
  1014. return err
  1015. }
  1016. switch v[1].(type) {
  1017. case []interface{}: // handled in []interface{} case
  1018. default:
  1019. if doIndent {
  1020. p.Outdent()
  1021. }
  1022. }
  1023. i--
  1024. }
  1025. p.mapDepth--
  1026. endTag = true
  1027. elen = 1 // we do have some content ...
  1028. case []interface{}:
  1029. // special case - found during implementing Issue #23
  1030. if len(value.([]interface{})) == 0 {
  1031. if doIndent {
  1032. if _, err = b.WriteString(p.padding + p.indent); err != nil {
  1033. return err
  1034. }
  1035. }
  1036. if _, err = b.WriteString("<" + key); err != nil {
  1037. return err
  1038. }
  1039. elen = 0
  1040. endTag = true
  1041. break
  1042. }
  1043. for _, v := range value.([]interface{}) {
  1044. if doIndent {
  1045. p.Indent()
  1046. }
  1047. if err := marshalMapToXmlIndent(doIndent, b, key, v, p); err != nil {
  1048. return err
  1049. }
  1050. if doIndent {
  1051. p.Outdent()
  1052. }
  1053. }
  1054. return nil
  1055. case []string:
  1056. // This was added by https://github.com/slotix ... not a type that
  1057. // would be encountered if mv generated from NewMapXml, NewMapJson.
  1058. // Could be encountered in AnyXml(), so we'll let it stay, though
  1059. // it should be merged with case []interface{}, above.
  1060. //quick fix for []string type
  1061. //[]string should be treated exaclty as []interface{}
  1062. if len(value.([]string)) == 0 {
  1063. if doIndent {
  1064. if _, err = b.WriteString(p.padding + p.indent); err != nil {
  1065. return err
  1066. }
  1067. }
  1068. if _, err = b.WriteString("<" + key); err != nil {
  1069. return err
  1070. }
  1071. elen = 0
  1072. endTag = true
  1073. break
  1074. }
  1075. for _, v := range value.([]string) {
  1076. if doIndent {
  1077. p.Indent()
  1078. }
  1079. if err := marshalMapToXmlIndent(doIndent, b, key, v, p); err != nil {
  1080. return err
  1081. }
  1082. if doIndent {
  1083. p.Outdent()
  1084. }
  1085. }
  1086. return nil
  1087. case nil:
  1088. // terminate the tag
  1089. if doIndent {
  1090. // *s += p.padding
  1091. if _, err = b.WriteString(p.padding); err != nil {
  1092. return err
  1093. }
  1094. }
  1095. if _, err = b.WriteString("<" + key); err != nil {
  1096. return err
  1097. }
  1098. endTag, isSimple = true, true
  1099. break
  1100. default: // handle anything - even goofy stuff
  1101. elen = 0
  1102. switch value.(type) {
  1103. case string:
  1104. v := value.(string)
  1105. if xmlEscapeChars {
  1106. v = escapeChars(v)
  1107. }
  1108. elen = len(v)
  1109. if elen > 0 {
  1110. // *s += ">" + v
  1111. if _, err = b.WriteString(">" + v); err != nil {
  1112. return err
  1113. }
  1114. }
  1115. case float64, bool, int, int32, int64, float32, json.Number:
  1116. v := fmt.Sprintf("%v", value)
  1117. elen = len(v) // always > 0
  1118. if _, err = b.WriteString(">" + v); err != nil {
  1119. return err
  1120. }
  1121. case []byte: // NOTE: byte is just an alias for uint8
  1122. // similar to how xml.Marshal handles []byte structure members
  1123. v := string(value.([]byte))
  1124. if xmlEscapeChars {
  1125. v = escapeChars(v)
  1126. }
  1127. elen = len(v)
  1128. if elen > 0 {
  1129. // *s += ">" + v
  1130. if _, err = b.WriteString(">" + v); err != nil {
  1131. return err
  1132. }
  1133. }
  1134. default:
  1135. if _, err = b.WriteString(">"); err != nil {
  1136. return err
  1137. }
  1138. var v []byte
  1139. var err error
  1140. if doIndent {
  1141. v, err = xml.MarshalIndent(value, p.padding, p.indent)
  1142. } else {
  1143. v, err = xml.Marshal(value)
  1144. }
  1145. if err != nil {
  1146. if _, err = b.WriteString(">UNKNOWN"); err != nil {
  1147. return err
  1148. }
  1149. } else {
  1150. elen = len(v)
  1151. if elen > 0 {
  1152. if _, err = b.Write(v); err != nil {
  1153. return err
  1154. }
  1155. }
  1156. }
  1157. }
  1158. isSimple = true
  1159. endTag = true
  1160. }
  1161. if endTag {
  1162. if doIndent {
  1163. if !isSimple {
  1164. if _, err = b.WriteString(p.padding); err != nil {
  1165. return err
  1166. }
  1167. }
  1168. }
  1169. if elen > 0 || useGoXmlEmptyElemSyntax {
  1170. if elen == 0 {
  1171. if _, err = b.WriteString(">"); err != nil {
  1172. return err
  1173. }
  1174. }
  1175. if _, err = b.WriteString(`</` + key + ">"); err != nil {
  1176. return err
  1177. }
  1178. } else {
  1179. if _, err = b.WriteString(`/>`); err != nil {
  1180. return err
  1181. }
  1182. }
  1183. }
  1184. if doIndent {
  1185. if p.cnt > p.start {
  1186. if _, err = b.WriteString("\n"); err != nil {
  1187. return err
  1188. }
  1189. }
  1190. p.Outdent()
  1191. }
  1192. return nil
  1193. }
  1194. // ============================ sort interface implementation =================
  1195. type attrList [][2]string
  1196. func (a attrList) Len() int {
  1197. return len(a)
  1198. }
  1199. func (a attrList) Swap(i, j int) {
  1200. a[i], a[j] = a[j], a[i]
  1201. }
  1202. func (a attrList) Less(i, j int) bool {
  1203. return a[i][0] <= a[j][0]
  1204. }
  1205. type elemList [][2]interface{}
  1206. func (e elemList) Len() int {
  1207. return len(e)
  1208. }
  1209. func (e elemList) Swap(i, j int) {
  1210. e[i], e[j] = e[j], e[i]
  1211. }
  1212. func (e elemList) Less(i, j int) bool {
  1213. return e[i][0].(string) <= e[j][0].(string)
  1214. }