/
marcdump.go
255 lines (215 loc) · 5.76 KB
/
marcdump.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2013-14 Thomas Emerson
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"flag"
"fmt"
"github.com/TreeRex/marc21"
"math"
"os"
"regexp"
"text/tabwriter"
)
type selectionSpec struct {
field string
subfield string
criterion *regexp.Regexp
}
// An actionFunc is called to display a record
type actionFunc func(record *marc21.MarcRecord, w *tabwriter.Writer) error
var (
errInvalidSelectorSpec = errors.New("marcdump: invalid selector specification")
)
var (
// Group 1: field
// Group 2: subfield, or ""
// Group 3: specification, or ""
// field subfield spec
selectionSpecRegexp = regexp.MustCompile("^([0-9A-Za-z]{3})(?:_([0-9a-z]))?(?:=(.+))?$")
)
// Command-line options
var (
maxRecords uint
makeIndex string
useIndex string
selectorOpt string
fieldsOpt string
)
// Select the record whose 020$a == 9780743264747, output field 650 value(s) only
// marcdump -selector 020_a=9780743264747 -fields 650 <marcfile>
//
// Select any record that has a value in 020_a and generate an index for the marcfile
// marcdump -selector 020_a -mkindex <indxfile> <marcfile>
// marcdump -selector 020_a=9780743264747 -fields 650 -index <indexfile> <marcfile>
func init() {
flag.UintVar(&maxRecords, "m", math.MaxUint32, "Maximum number of records to dump")
flag.StringVar(&fieldsOpt, "f", "", "Colon separated field tags to output")
flag.StringVar(&selectorOpt, "s", "", "Field selector(s)")
flag.StringVar(&makeIndex, "mkindex", "", "Name of index file to generate")
flag.StringVar(&useIndex, "index", "", "Name of index file")
}
func getSelectionSpec() (*selectionSpec, error) {
selectionSpec := new(selectionSpec)
if selectorOpt != "" {
spec := selectionSpecRegexp.FindStringSubmatch(selectorOpt)
if spec != nil {
if spec[3] != "" {
re,err := regexp.Compile(spec[3])
if err != nil {
return nil, err
}
selectionSpec.criterion = re
}
selectionSpec.field = spec[1]
selectionSpec.subfield = spec[2]
} else {
return nil, errInvalidSelectorSpec
}
}
return selectionSpec, nil
}
func (s *selectionSpec) match(r *marc21.MarcRecord) bool {
if s.field == "" {
return true
}
if marc21.IsControlFieldTag(s.field) {
field, err := r.GetControlField(s.field)
if err != nil {
return false
}
if s.criterion != nil {
return s.criterion.MatchString(field)
}
return true
} else { // Data Field
subfields := make([]string, 1)
field, _ := r.GetDataField(s.field)
for instance := 0; instance < field.ValueCount(); instance++ {
// if no subfield is specified in the spec then
// we want to search all of them. since these can
// vary per field instance we need to get the list
// each time.
if s.subfield != "" {
subfields[0] = s.subfield
} else {
subfields = field.GetSubfields(instance)
}
for _, subfield := range subfields {
sfv := field.GetNthSubfield(subfield, instance)
if sfv != "" {
// the subfield exists: need to check because the
// user supplied subfield may not exist in this
// instance
if s.criterion != nil {
// and there is a search criterion
if s.criterion.MatchString(sfv) {
// and it matches
return true;
}
} else {
// no search criterion, but the field exists
return true;
}
}
}
}
return false;
}
}
func getActionFunction() actionFunc {
if makeIndex != "" {
return nil
} else {
return printRecord
}
}
func main() {
flag.Parse()
if flag.NArg() != 1 {
usage()
}
file, err := os.Open(flag.Arg(0))
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 3, ' ', 0)
selector, err := getSelectionSpec()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
action := getActionFunction()
if action == nil {
fmt.Fprintln(os.Stderr, "Internal Error: could not get action function")
}
recordCount := uint(0)
reader := marc21.NewReader(file, false)
for {
rec,err := reader.Next()
if rec == nil && err == nil {
break
} else if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
break
}
if selector.match(rec) {
action(rec, w)
recordCount += 1
if recordCount == maxRecords {
break
}
}
}
}
//
// Record Printing Functions
//
func printRecord(record *marc21.MarcRecord, w *tabwriter.Writer) error {
fmt.Fprintf(w, "Leader\t%s\n", record.GetLeader())
fields := record.GetFieldList()
for _,f := range fields {
if marc21.IsControlFieldTag(f) {
v,_ := record.GetControlField(f)
fmt.Fprintf(w, "%s\t%s\n", f, v)
} else {
v,_ := record.GetDataField(f)
printDataField(w, v)
}
}
w.Flush()
return nil
}
func printDataField(w *tabwriter.Writer, field marc21.VariableField) {
for i := 0; i < field.ValueCount(); i++ {
value := field.GetIndicators(i)
for _,sf := range field.GetSubfields(i) {
value += fmt.Sprintf("$%s%s", sf, field.GetNthSubfield(sf, i))
}
fmt.Fprintf(w, "%s\t%s\n", field.Tag, value)
}
}
//
// Record Selection Functions
//
func selectAll(record *marc21.MarcRecord) bool {
return true
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: marcdump [-m max] marcfile\n")
os.Exit(1)
}
// ~/shrc/hlom/data/hlom/ab.bib.00.20131101.full.mrc