OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / text / encoding / internal / identifier / gen.go
1 // Copyright 2015 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
5 // +build ignore
6
7 package main
8
9 import (
10         "bytes"
11         "encoding/xml"
12         "fmt"
13         "io"
14         "log"
15         "strings"
16
17         "golang.org/x/text/internal/gen"
18 )
19
20 type registry struct {
21         XMLName  xml.Name `xml:"registry"`
22         Updated  string   `xml:"updated"`
23         Registry []struct {
24                 ID     string `xml:"id,attr"`
25                 Record []struct {
26                         Name string `xml:"name"`
27                         Xref []struct {
28                                 Type string `xml:"type,attr"`
29                                 Data string `xml:"data,attr"`
30                         } `xml:"xref"`
31                         Desc struct {
32                                 Data string `xml:",innerxml"`
33                                 // Any []struct {
34                                 //      Data string `xml:",chardata"`
35                                 // } `xml:",any"`
36                                 // Data string `xml:",chardata"`
37                         } `xml:"description,"`
38                         MIB   string   `xml:"value"`
39                         Alias []string `xml:"alias"`
40                         MIME  string   `xml:"preferred_alias"`
41                 } `xml:"record"`
42         } `xml:"registry"`
43 }
44
45 func main() {
46         r := gen.OpenIANAFile("assignments/character-sets/character-sets.xml")
47         reg := &registry{}
48         if err := xml.NewDecoder(r).Decode(&reg); err != nil && err != io.EOF {
49                 log.Fatalf("Error decoding charset registry: %v", err)
50         }
51         if len(reg.Registry) == 0 || reg.Registry[0].ID != "character-sets-1" {
52                 log.Fatalf("Unexpected ID %s", reg.Registry[0].ID)
53         }
54
55         w := &bytes.Buffer{}
56         fmt.Fprintf(w, "const (\n")
57         for _, rec := range reg.Registry[0].Record {
58                 constName := ""
59                 for _, a := range rec.Alias {
60                         if strings.HasPrefix(a, "cs") && strings.IndexByte(a, '-') == -1 {
61                                 // Some of the constant definitions have comments in them. Strip those.
62                                 constName = strings.Title(strings.SplitN(a[2:], "\n", 2)[0])
63                         }
64                 }
65                 if constName == "" {
66                         switch rec.MIB {
67                         case "2085":
68                                 constName = "HZGB2312" // Not listed as alias for some reason.
69                         default:
70                                 log.Fatalf("No cs alias defined for %s.", rec.MIB)
71                         }
72                 }
73                 if rec.MIME != "" {
74                         rec.MIME = fmt.Sprintf(" (MIME: %s)", rec.MIME)
75                 }
76                 fmt.Fprintf(w, "// %s is the MIB identifier with IANA name %s%s.\n//\n", constName, rec.Name, rec.MIME)
77                 if len(rec.Desc.Data) > 0 {
78                         fmt.Fprint(w, "// ")
79                         d := xml.NewDecoder(strings.NewReader(rec.Desc.Data))
80                         inElem := true
81                         attr := ""
82                         for {
83                                 t, err := d.Token()
84                                 if err != nil {
85                                         if err != io.EOF {
86                                                 log.Fatal(err)
87                                         }
88                                         break
89                                 }
90                                 switch x := t.(type) {
91                                 case xml.CharData:
92                                         attr = "" // Don't need attribute info.
93                                         a := bytes.Split([]byte(x), []byte("\n"))
94                                         for i, b := range a {
95                                                 if b = bytes.TrimSpace(b); len(b) != 0 {
96                                                         if !inElem && i > 0 {
97                                                                 fmt.Fprint(w, "\n// ")
98                                                         }
99                                                         inElem = false
100                                                         fmt.Fprintf(w, "%s ", string(b))
101                                                 }
102                                         }
103                                 case xml.StartElement:
104                                         if x.Name.Local == "xref" {
105                                                 inElem = true
106                                                 use := false
107                                                 for _, a := range x.Attr {
108                                                         if a.Name.Local == "type" {
109                                                                 use = use || a.Value != "person"
110                                                         }
111                                                         if a.Name.Local == "data" && use {
112                                                                 attr = a.Value + " "
113                                                         }
114                                                 }
115                                         }
116                                 case xml.EndElement:
117                                         inElem = false
118                                         fmt.Fprint(w, attr)
119                                 }
120                         }
121                         fmt.Fprint(w, "\n")
122                 }
123                 for _, x := range rec.Xref {
124                         switch x.Type {
125                         case "rfc":
126                                 fmt.Fprintf(w, "// Reference: %s\n", strings.ToUpper(x.Data))
127                         case "uri":
128                                 fmt.Fprintf(w, "// Reference: %s\n", x.Data)
129                         }
130                 }
131                 fmt.Fprintf(w, "%s MIB = %s\n", constName, rec.MIB)
132                 fmt.Fprintln(w)
133         }
134         fmt.Fprintln(w, ")")
135
136         gen.WriteGoFile("mib.go", "identifier", w.Bytes())
137 }