1
2
3
4
5
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
34
35
36
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 := ®istry{}
48 if err := xml.NewDecoder(r).Decode(®); 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
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"
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 = ""
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
113
114 s := a.Value
115 s = strings.Replace(s, "http://", "https://", -1)
116 s = strings.Replace(s, "/unicode/", "/", -1)
117 attr = s + " "
118 }
119 }
120 }
121 case xml.EndElement:
122 inElem = false
123 fmt.Fprint(w, attr)
124 }
125 }
126 fmt.Fprint(w, "\n")
127 }
128 for _, x := range rec.Xref {
129 switch x.Type {
130 case "rfc":
131 fmt.Fprintf(w, "// Reference: %s\n", strings.ToUpper(x.Data))
132 case "uri":
133 fmt.Fprintf(w, "// Reference: %s\n", x.Data)
134 }
135 }
136 fmt.Fprintf(w, "%s MIB = %s\n", constName, rec.MIB)
137 fmt.Fprintln(w)
138 }
139 fmt.Fprintln(w, ")")
140
141 gen.WriteGoFile("mib.go", "identifier", w.Bytes())
142 }
143
View as plain text