-
Notifications
You must be signed in to change notification settings - Fork 0
/
listx.go
216 lines (200 loc) · 4.52 KB
/
listx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package exif
import (
"context"
"encoding/xml"
"errors"
"fmt"
"io"
"os/exec"
)
type (
// TagFunc is used by WalkTags to iterate over all tags,
// users can return ErrStop error to indicate that they don't want
// to process further tags.
TagFunc func(Tag) error
// Tag contains details about an ExIF Tag
Tag struct {
Table TableInfo `xml:"-"`
Writable bool `xml:"writable,attr"`
Type string `xml:"type,attr"`
Name string `xml:"name,attr"`
Description []Description `xml:"desc"`
}
// Description of a given tag
Description struct {
Lang string `xml:"lang,attr"`
Text string `xml:",innerxml"`
}
// TableInfo contains information about an ExIF tag table
TableInfo struct {
// Name of this table
Name string `xml:"name,attr"`
}
)
var (
// ErrStop is used by TagFunc implementations to indicate that
// the Walk process is completed
ErrStop = errors.New("stop")
)
// WalkTags from -listx calling handler for each tag processed. See TagFunc
// for more information.
//
// The function will abort as soon as the first error is found, if the first
// error is ErrStop the error is silenced, otherwise it is passed as-is,
// to callers.
//
// The operation works as a stream, so it is possible that a mal-formed XML
// document is partially decoded, usually this is not a concern since the
// underlying tool will always generate valid XMLs, but if the Context is
// Done before the whole XML is streamed, partial data might have been processed
// by handler.
func (t *Tool) WalkTags(ctx context.Context, handler TagFunc) error {
var earlyStop context.CancelFunc
ctx, earlyStop = context.WithCancel(ctx)
defer earlyStop()
cmd := exec.CommandContext(ctx, t.binary, "-listx")
xmlBytes, err := cmd.StdoutPipe()
if err != nil {
return err
}
err = cmd.Start()
if err != nil {
return err
}
go func() {
cmd.Wait()
}()
dec := xml.NewDecoder(xmlBytes)
var streamCompleted bool
for !streamCompleted {
tk, err := dec.RawToken()
if err != nil {
streamCompleted = true
if errors.Is(err, io.EOF) {
// EOF is expected here
err = nil
}
}
switch tk := tk.(type) {
case xml.StartElement:
if tk.Name.Local == "taginfo" {
err = processTagInfo(dec, handler)
}
default:
// ignore any other tag
}
if err == ErrStop {
err = nil
streamCompleted = true
}
}
return err
}
func processTagInfo(dec *xml.Decoder, handle TagFunc) error {
for {
tk, err := dec.RawToken()
if err != nil {
return err
}
switch tk := tk.(type) {
case xml.EndElement:
if tk.Name.Local == "taginfo" {
return nil
}
case xml.StartElement:
if tk.Name.Local == "table" {
err = processTable(dec, tk, handle)
if err != nil {
return err
}
}
default:
// ignore any other tag
}
}
}
func processTable(dec *xml.Decoder, tableStart xml.StartElement, handle TagFunc) error {
var table TableInfo
for _, a := range tableStart.Attr {
if a.Name.Local == "name" {
table.Name = a.Value
break
}
}
for {
tk, err := dec.RawToken()
if err != nil {
return err
}
switch tk := tk.(type) {
case xml.StartElement:
if tk.Name.Local == "tag" {
err = processTag(dec, table, tk, handle)
if err != nil {
return err
}
}
case xml.EndElement:
if tk.Name.Local == "table" {
return nil
}
default:
}
}
}
func processTag(dec *xml.Decoder, table TableInfo, tagStart xml.StartElement, handle TagFunc) error {
var tag Tag
tag.Table = table
for _, a := range tagStart.Attr {
switch a.Name.Local {
case "name":
tag.Name = a.Value
case "type":
tag.Type = a.Value
case "writable":
tag.Writable = a.Value != "false"
}
}
for {
tk, err := dec.RawToken()
if err != nil {
return err
}
switch tk := tk.(type) {
case xml.StartElement:
if tk.Name.Local == "desc" {
err = processDescription(dec, tk, &tag)
}
case xml.EndElement:
if tk.Name.Local == "tag" {
return handle(tag)
}
default:
}
}
return fmt.Errorf("mal-formed XML missing table tag at offset %v", dec.InputOffset())
}
func processDescription(dec *xml.Decoder, description xml.StartElement, tag *Tag) error {
var desc Description
for _, a := range description.Attr {
if a.Name.Local == "lang" {
desc.Lang = a.Value
}
}
for {
tk, err := dec.RawToken()
if err != nil {
return err
}
switch tk := tk.(type) {
case xml.CharData:
desc.Text = string(tk)
case xml.EndElement:
if tk.Name.Local == "desc" {
tag.Description = append(tag.Description, desc)
return nil
}
default:
}
}
}