jmdict/common.go

52 lines
1.1 KiB
Go
Raw Normal View History

2016-07-30 17:33:36 +00:00
package jmdict
import (
"encoding/xml"
2016-07-31 01:04:49 +00:00
"io"
2016-07-30 17:33:36 +00:00
"regexp"
)
2016-08-07 00:43:26 +00:00
func parseDict(reader io.Reader, container interface{}, transform bool) (map[string]string, error) {
2016-08-06 18:20:13 +00:00
decoder := xml.NewDecoder(reader)
var entities map[string]string
for {
token, _ := decoder.Token()
if token == nil {
break
}
switch startElement := token.(type) {
case xml.Directive:
directive := token.(xml.Directive)
entities = parseEntities(&directive)
if transform {
decoder.Entity = entities
} else {
decoder.Entity = make(map[string]string)
for k, _ := range entities {
decoder.Entity[k] = k
}
}
case xml.StartElement:
if err := decoder.DecodeElement(container, &startElement); err != nil {
return nil, err
}
}
}
return entities, nil
}
2016-08-03 15:52:14 +00:00
func parseEntities(d *xml.Directive) map[string]string {
2016-07-30 17:33:36 +00:00
re := regexp.MustCompile("<!ENTITY\\s([0-9\\-A-z]+)\\s\"(.+)\">")
matches := re.FindAllStringSubmatch(string(*d), -1)
entities := make(map[string]string)
for _, match := range matches {
2016-08-03 15:52:14 +00:00
entities[match[1]] = match[2]
2016-07-30 17:33:36 +00:00
}
2016-07-31 01:04:49 +00:00
return entities
2016-07-30 17:33:36 +00:00
}