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.
20 // A Decoder loads an archive of CLDR data.
23 sectionFilter []string
29 // SetSectionFilter takes a list top-level LDML element names to which
30 // evaluation of LDML should be limited. It automatically calls SetDirFilter.
31 func (d *Decoder) SetSectionFilter(filter ...string) {
32 d.sectionFilter = filter
33 // TODO: automatically set dir filter
36 // SetDirFilter limits the loading of LDML XML files of the specied directories.
37 // Note that sections may be split across directories differently for different CLDR versions.
38 // For more robust code, use SetSectionFilter.
39 func (d *Decoder) SetDirFilter(dir ...string) {
43 // A Loader provides access to the files of a CLDR archive.
44 type Loader interface {
47 Reader(i int) (io.ReadCloser, error)
50 var fileRe = regexp.MustCompile(".*/(.*)/(.*)\\.xml")
52 // Decode loads and decodes the files represented by l.
53 func (d *Decoder) Decode(l Loader) (cldr *CLDR, err error) {
55 for i := 0; i < l.Len(); i++ {
57 if m := fileRe.FindStringSubmatch(fname); m != nil {
58 if len(d.dirFilter) > 0 && !in(d.dirFilter, m[1]) {
62 if r, err = l.Reader(i); err == nil {
63 err = d.decode(m[1], m[2], r)
70 d.cldr.finalize(d.sectionFilter)
74 func (d *Decoder) decode(dir, id string, r io.Reader) error {
79 case dir == "supplemental":
81 case dir == "transforms":
85 case dir == "validity":
89 if v, ok = cldr.locale[id]; !ok {
91 v, cldr.locale[id] = l, l
94 x := xml.NewDecoder(r)
95 if err := x.Decode(v); err != nil {
96 log.Printf("%s/%s: %v", dir, id, err)
100 if l.Identity == nil {
101 return fmt.Errorf("%s/%s: missing identity element", dir, id)
103 // TODO: verify when CLDR bug http://unicode.org/cldr/trac/ticket/8970
105 // path := strings.Split(id, "_")
106 // if lang := l.Identity.Language.Type; lang != path[0] {
107 // return fmt.Errorf("%s/%s: language was %s; want %s", dir, id, lang, path[0])
113 type pathLoader []string
115 func makePathLoader(path string) (pl pathLoader, err error) {
116 err = filepath.Walk(path, func(path string, _ os.FileInfo, err error) error {
117 pl = append(pl, path)
123 func (pl pathLoader) Len() int {
127 func (pl pathLoader) Path(i int) string {
131 func (pl pathLoader) Reader(i int) (io.ReadCloser, error) {
132 return os.Open(pl[i])
135 // DecodePath loads CLDR data from the given path.
136 func (d *Decoder) DecodePath(path string) (cldr *CLDR, err error) {
137 loader, err := makePathLoader(path)
141 return d.Decode(loader)
144 type zipLoader struct {
148 func (zl zipLoader) Len() int {
149 return len(zl.r.File)
152 func (zl zipLoader) Path(i int) string {
153 return zl.r.File[i].Name
156 func (zl zipLoader) Reader(i int) (io.ReadCloser, error) {
157 return zl.r.File[i].Open()
160 // DecodeZip loads CLDR data from the zip archive for which r is the source.
161 func (d *Decoder) DecodeZip(r io.Reader) (cldr *CLDR, err error) {
162 buffer, err := ioutil.ReadAll(r)
166 archive, err := zip.NewReader(bytes.NewReader(buffer), int64(len(buffer)))
170 return d.Decode(zipLoader{archive})