github.com/go-enjin/golang-org-x-text@v0.12.1-enjin.2/encoding/internal/identifier/gen.go (about)

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