...
1
2
3
4
5 package cldr
6
7 import (
8 "encoding/xml"
9 "regexp"
10 "strconv"
11 )
12
13
14 type Elem interface {
15 setEnclosing(Elem)
16 setName(string)
17 enclosing() Elem
18
19 GetCommon() *Common
20 }
21
22 type hidden struct {
23 CharData string `xml:",chardata"`
24 Alias *struct {
25 Common
26 Source string `xml:"source,attr"`
27 Path string `xml:"path,attr"`
28 } `xml:"alias"`
29 Def *struct {
30 Common
31 Choice string `xml:"choice,attr,omitempty"`
32 Type string `xml:"type,attr,omitempty"`
33 } `xml:"default"`
34 }
35
36
37
38 type Common struct {
39 XMLName xml.Name
40 name string
41 enclElem Elem
42 Type string `xml:"type,attr,omitempty"`
43 Reference string `xml:"reference,attr,omitempty"`
44 Alt string `xml:"alt,attr,omitempty"`
45 ValidSubLocales string `xml:"validSubLocales,attr,omitempty"`
46 Draft string `xml:"draft,attr,omitempty"`
47 hidden
48 }
49
50
51
52 func (e *Common) Default() string {
53 if e.Def == nil {
54 return ""
55 }
56 if e.Def.Choice != "" {
57 return e.Def.Choice
58 } else if e.Def.Type != "" {
59
60 return e.Def.Type
61 }
62 return ""
63 }
64
65
66 func (e *Common) Element() string {
67 return e.name
68 }
69
70
71 func (e *Common) GetCommon() *Common {
72 return e
73 }
74
75
76 func (e *Common) Data() string {
77 e.CharData = charRe.ReplaceAllStringFunc(e.CharData, replaceUnicode)
78 return e.CharData
79 }
80
81 func (e *Common) setName(s string) {
82 e.name = s
83 }
84
85 func (e *Common) enclosing() Elem {
86 return e.enclElem
87 }
88
89 func (e *Common) setEnclosing(en Elem) {
90 e.enclElem = en
91 }
92
93
94 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]`)
95
96
97
98 func replaceUnicode(s string) string {
99 if s[1] == '#' {
100 r, _ := strconv.ParseInt(s[3:len(s)-1], 16, 32)
101 return string(rune(r))
102 }
103 r, _, _, _ := strconv.UnquoteChar(s, 0)
104 return string(r)
105 }
106
View as plain text