You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.5 KiB

  1. // Copyright 2013 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. package cldr
  5. import (
  6. "encoding/xml"
  7. "regexp"
  8. "strconv"
  9. )
  10. // Elem is implemented by every XML element.
  11. type Elem interface {
  12. setEnclosing(Elem)
  13. setName(string)
  14. enclosing() Elem
  15. GetCommon() *Common
  16. }
  17. type hidden struct {
  18. CharData string `xml:",chardata"`
  19. Alias *struct {
  20. Common
  21. Source string `xml:"source,attr"`
  22. Path string `xml:"path,attr"`
  23. } `xml:"alias"`
  24. Def *struct {
  25. Common
  26. Choice string `xml:"choice,attr,omitempty"`
  27. Type string `xml:"type,attr,omitempty"`
  28. } `xml:"default"`
  29. }
  30. // Common holds several of the most common attributes and sub elements
  31. // of an XML element.
  32. type Common struct {
  33. XMLName xml.Name
  34. name string
  35. enclElem Elem
  36. Type string `xml:"type,attr,omitempty"`
  37. Reference string `xml:"reference,attr,omitempty"`
  38. Alt string `xml:"alt,attr,omitempty"`
  39. ValidSubLocales string `xml:"validSubLocales,attr,omitempty"`
  40. Draft string `xml:"draft,attr,omitempty"`
  41. hidden
  42. }
  43. // Default returns the default type to select from the enclosed list
  44. // or "" if no default value is specified.
  45. func (e *Common) Default() string {
  46. if e.Def == nil {
  47. return ""
  48. }
  49. if e.Def.Choice != "" {
  50. return e.Def.Choice
  51. } else if e.Def.Type != "" {
  52. // Type is still used by the default element in collation.
  53. return e.Def.Type
  54. }
  55. return ""
  56. }
  57. // GetCommon returns e. It is provided such that Common implements Elem.
  58. func (e *Common) GetCommon() *Common {
  59. return e
  60. }
  61. // Data returns the character data accumulated for this element.
  62. func (e *Common) Data() string {
  63. e.CharData = charRe.ReplaceAllStringFunc(e.CharData, replaceUnicode)
  64. return e.CharData
  65. }
  66. func (e *Common) setName(s string) {
  67. e.name = s
  68. }
  69. func (e *Common) enclosing() Elem {
  70. return e.enclElem
  71. }
  72. func (e *Common) setEnclosing(en Elem) {
  73. e.enclElem = en
  74. }
  75. // Escape characters that can be escaped without further escaping the string.
  76. var charRe = regexp.MustCompile(`&#x[0-9a-fA-F]*;|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|\\x[0-9a-fA-F]{2}|\\[0-7]{3}|\\[abtnvfr]`)
  77. // replaceUnicode converts hexadecimal Unicode codepoint notations to a one-rune string.
  78. // It assumes the input string is correctly formatted.
  79. func replaceUnicode(s string) string {
  80. if s[1] == '#' {
  81. r, _ := strconv.ParseInt(s[3:len(s)-1], 16, 32)
  82. return string(r)
  83. }
  84. r, _, _, _ := strconv.UnquoteChar(s, 0)
  85. return string(r)
  86. }