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.
 
 
 

106 lines
2.6 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. // Element returns the XML element name.
  58. func (e *Common) Element() string {
  59. return e.name
  60. }
  61. // GetCommon returns e. It is provided such that Common implements Elem.
  62. func (e *Common) GetCommon() *Common {
  63. return e
  64. }
  65. // Data returns the character data accumulated for this element.
  66. func (e *Common) Data() string {
  67. e.CharData = charRe.ReplaceAllStringFunc(e.CharData, replaceUnicode)
  68. return e.CharData
  69. }
  70. func (e *Common) setName(s string) {
  71. e.name = s
  72. }
  73. func (e *Common) enclosing() Elem {
  74. return e.enclElem
  75. }
  76. func (e *Common) setEnclosing(en Elem) {
  77. e.enclElem = en
  78. }
  79. // Escape characters that can be escaped without further escaping the string.
  80. 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]`)
  81. // replaceUnicode converts hexadecimal Unicode codepoint notations to a one-rune string.
  82. // It assumes the input string is correctly formatted.
  83. func replaceUnicode(s string) string {
  84. if s[1] == '#' {
  85. r, _ := strconv.ParseInt(s[3:len(s)-1], 16, 32)
  86. return string(r)
  87. }
  88. r, _, _, _ := strconv.UnquoteChar(s, 0)
  89. return string(r)
  90. }