|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/hectorcorrea/marcli/pkg/marc" |
| 10 | +) |
| 11 | + |
| 12 | +type controlField struct { |
| 13 | + Tag string `xml:"tag,attr"` |
| 14 | + Value string `xml:",chardata"` |
| 15 | +} |
| 16 | + |
| 17 | +type subfield struct { |
| 18 | + Code string `xml:"code,attr"` |
| 19 | + Value string `xml:",chardata"` |
| 20 | +} |
| 21 | +type dataField struct { |
| 22 | + Tag string `xml:"tag,attr"` |
| 23 | + Ind1 string `xml:"ind1,attr"` |
| 24 | + Ind2 string `xml:"ind2,attr"` |
| 25 | + Subfields []subfield `xml:"subfield"` |
| 26 | +} |
| 27 | + |
| 28 | +type xmlRecord struct { |
| 29 | + XMLName xml.Name `xml:"record"` |
| 30 | + Leader string `xml:"leader"` |
| 31 | + ControlFields []controlField `xml:"controlfield"` |
| 32 | + DataFields []dataField `xml:"datafield"` |
| 33 | +} |
| 34 | + |
| 35 | +const xmlProlog = `<?xml version="1.0" encoding="UTF-8"?>` |
| 36 | +const xmlRootBegin = `<collection xmlns="http://www.loc.gov/MARC21/slim" xmlns:marc="http://www.loc.gov/MARC21/slim">` |
| 37 | +const xmlRootEnd = `</collection>` |
| 38 | + |
| 39 | +func toXML(params ProcessFileParams) error { |
| 40 | + if count == 0 { |
| 41 | + return nil |
| 42 | + } |
| 43 | + |
| 44 | + file, err := os.Open(params.filename) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + defer file.Close() |
| 49 | + |
| 50 | + fmt.Printf("%s\n%s\n", xmlProlog, xmlRootBegin) |
| 51 | + |
| 52 | + var i, out int |
| 53 | + marc := marc.NewMarcFile(file) |
| 54 | + for marc.Scan() { |
| 55 | + |
| 56 | + r, err := marc.Record() |
| 57 | + if err == io.EOF { |
| 58 | + break |
| 59 | + } |
| 60 | + |
| 61 | + if err != nil { |
| 62 | + printError(r, "PARSE ERROR", err) |
| 63 | + if params.debug { |
| 64 | + continue |
| 65 | + } |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + if i++; i < start { |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + if r.Contains(params.searchValue) && r.HasFields(params.hasFields) { |
| 74 | + str, err := recordToXML(r, params.debug) |
| 75 | + if err != nil { |
| 76 | + if params.debug { |
| 77 | + printError(r, "XML PARSE ERROR", err) |
| 78 | + continue |
| 79 | + } |
| 80 | + panic(err) |
| 81 | + } |
| 82 | + fmt.Printf("%s\r\n", str) |
| 83 | + if out++; out == count { |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + fmt.Printf("%s\n", xmlRootEnd) |
| 89 | + |
| 90 | + return marc.Err() |
| 91 | +} |
| 92 | + |
| 93 | +func recordToXML(r marc.Record, debug bool) (string, error) { |
| 94 | + x := xmlRecord{ |
| 95 | + Leader: r.Leader.Raw(), |
| 96 | + } |
| 97 | + |
| 98 | + for _, f := range r.Fields { |
| 99 | + if f.IsControlField() { |
| 100 | + x.ControlFields = append(x.ControlFields, controlField{Tag: f.Tag, Value: f.Value}) |
| 101 | + } else { |
| 102 | + df := dataField{Tag: f.Tag, Ind1: f.Indicator1, Ind2: f.Indicator2} |
| 103 | + for _, s := range f.SubFields { |
| 104 | + df.Subfields = append(df.Subfields, subfield{Code: s.Code, Value: s.Value}) |
| 105 | + } |
| 106 | + x.DataFields = append(x.DataFields, df) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + indent := "" |
| 111 | + if debug { |
| 112 | + indent = " " |
| 113 | + } |
| 114 | + b, err := xml.MarshalIndent(x, indent, indent) |
| 115 | + return string(b), err |
| 116 | +} |
| 117 | + |
| 118 | +func printError(r marc.Record, errType string, err error) { |
| 119 | + str := "== RECORD WITH ERROR STARTS HERE\n" |
| 120 | + str += fmt.Sprintf("%s:\n%s\n", errType, err.Error()) |
| 121 | + str += r.DebugString() + "\n" |
| 122 | + str += "== RECORD WITH ERROR ENDS HERE\n\n" |
| 123 | + fmt.Print(str) |
| 124 | +} |
0 commit comments